appwrite-utils-cli 0.0.52 → 0.0.53
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -192,14 +192,7 @@ export declare const CollectionImportDataSchema: z.ZodObject<{
|
|
|
192
192
|
type: z.ZodLiteral<"ip">;
|
|
193
193
|
error: z.ZodOptional<z.ZodDefault<z.ZodString>>;
|
|
194
194
|
required: z.ZodOptional<z.ZodDefault<z.ZodBoolean>>;
|
|
195
|
-
array: z.ZodOptional<z.ZodDefault<z.ZodBoolean>>;
|
|
196
|
-
* Prepares the data for creating documents in a collection.
|
|
197
|
-
* This involves loading the data, transforming it, and handling ID mappings.
|
|
198
|
-
*
|
|
199
|
-
* @param db - The database configuration.
|
|
200
|
-
* @param collection - The collection configuration.
|
|
201
|
-
* @param importDef - The import definition containing the attribute mappings and other relevant info.
|
|
202
|
-
*/
|
|
195
|
+
array: z.ZodOptional<z.ZodDefault<z.ZodBoolean>>;
|
|
203
196
|
xdefault: z.ZodOptional<z.ZodNullable<z.ZodString>>;
|
|
204
197
|
description: z.ZodOptional<z.ZodNullable<z.ZodUnion<[z.ZodString, z.ZodRecord<z.ZodString, z.ZodString>]>>>;
|
|
205
198
|
}, "strip", z.ZodTypeAny, {
|
|
@@ -357,73 +357,6 @@ export class DataLoader {
|
|
|
357
357
|
}
|
|
358
358
|
let needsUpdate = false;
|
|
359
359
|
let numUpdates = 0;
|
|
360
|
-
// Collect primary key fields from the users collection definitions
|
|
361
|
-
this.config.collections.forEach((collection) => {
|
|
362
|
-
if (this.getCollectionKey(collection.name) === usersCollectionKey) {
|
|
363
|
-
const collectionImportDefs = collection.importDefs;
|
|
364
|
-
if (!collectionImportDefs || !collectionImportDefs.length) {
|
|
365
|
-
return;
|
|
366
|
-
}
|
|
367
|
-
collectionImportDefs.forEach((importDef) => {
|
|
368
|
-
if (importDef.primaryKeyField) {
|
|
369
|
-
usersCollectionPrimaryKeyFields.add(importDef.primaryKeyField);
|
|
370
|
-
}
|
|
371
|
-
});
|
|
372
|
-
}
|
|
373
|
-
});
|
|
374
|
-
console.log(`Primary key fields collected for users collection: ${[
|
|
375
|
-
...usersCollectionPrimaryKeyFields,
|
|
376
|
-
]}`);
|
|
377
|
-
// Iterate over all collections to update references based on merged users
|
|
378
|
-
this.config.collections.forEach((collection) => {
|
|
379
|
-
const collectionData = this.importMap.get(this.getCollectionKey(collection.name));
|
|
380
|
-
if (!collectionData || !collectionData.data) {
|
|
381
|
-
console.log(`No data found for collection ${collection.name}`);
|
|
382
|
-
return;
|
|
383
|
-
}
|
|
384
|
-
const collectionImportDefs = collection.importDefs;
|
|
385
|
-
if (!collectionImportDefs || !collectionImportDefs.length) {
|
|
386
|
-
console.log(`No import definitions found for collection ${collection.name}`);
|
|
387
|
-
return;
|
|
388
|
-
}
|
|
389
|
-
collectionImportDefs.forEach((importDef) => {
|
|
390
|
-
importDef.idMappings?.forEach((idMapping) => {
|
|
391
|
-
if (this.getCollectionKey(idMapping.targetCollection) ===
|
|
392
|
-
usersCollectionKey) {
|
|
393
|
-
const fieldToSetKey = idMapping.fieldToSet || idMapping.sourceField;
|
|
394
|
-
const targetFieldKey = idMapping.targetFieldToMatch || idMapping.targetField;
|
|
395
|
-
if (usersCollectionPrimaryKeyFields.has(targetFieldKey)) {
|
|
396
|
-
console.log(`Processing collection ${collection.name} with target field ${targetFieldKey}`);
|
|
397
|
-
// Process each item in the collection
|
|
398
|
-
collectionData.data.forEach((item) => {
|
|
399
|
-
const oldId = item.finalData[idMapping.sourceField] ||
|
|
400
|
-
item.context[idMapping.sourceField];
|
|
401
|
-
if (oldId === undefined || oldId === null) {
|
|
402
|
-
console.log(`Skipping item with undefined or null oldId in collection ${collection.name}`);
|
|
403
|
-
return;
|
|
404
|
-
}
|
|
405
|
-
const newId = this.mergedUserMap.get(`${oldId}`);
|
|
406
|
-
if (newId) {
|
|
407
|
-
needsUpdate = true;
|
|
408
|
-
numUpdates++;
|
|
409
|
-
console.log(`Updating old ID ${oldId} to new ID ${newId} in collection ${collection.name}`);
|
|
410
|
-
// Update context to use new user ID
|
|
411
|
-
item.finalData[fieldToSetKey] = newId;
|
|
412
|
-
item.context[fieldToSetKey] = newId;
|
|
413
|
-
}
|
|
414
|
-
else {
|
|
415
|
-
console.log(`No new ID found for old ID ${oldId} in mergedUserMap.`);
|
|
416
|
-
}
|
|
417
|
-
});
|
|
418
|
-
}
|
|
419
|
-
}
|
|
420
|
-
});
|
|
421
|
-
});
|
|
422
|
-
if (needsUpdate) {
|
|
423
|
-
console.log(`Updated ${numUpdates} references for collection ${collection.name}`);
|
|
424
|
-
this.importMap.set(this.getCollectionKey(collection.name), collectionData);
|
|
425
|
-
}
|
|
426
|
-
});
|
|
427
360
|
}
|
|
428
361
|
updateOldReferencesForNew() {
|
|
429
362
|
if (!this.config.collections) {
|
|
@@ -491,7 +424,12 @@ export class DataLoader {
|
|
|
491
424
|
else {
|
|
492
425
|
// Merge arrays if new data is non-empty array and filter for uniqueness
|
|
493
426
|
collectionData.data[i].finalData[fieldToSetKey] = [
|
|
494
|
-
...new Set([
|
|
427
|
+
...new Set([
|
|
428
|
+
...(Array.isArray(currentDataFiltered)
|
|
429
|
+
? currentDataFiltered
|
|
430
|
+
: [currentDataFiltered]),
|
|
431
|
+
...newData,
|
|
432
|
+
].filter((value) => `${value}` !== `${valueToMatch}`)),
|
|
495
433
|
];
|
|
496
434
|
}
|
|
497
435
|
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "appwrite-utils-cli",
|
|
3
3
|
"description": "Appwrite Utility Functions to help with database management, data conversion, data import, migrations, and much more. Meant to be used as a CLI tool, I do not recommend installing this in frontend environments.",
|
|
4
|
-
"version": "0.0.
|
|
4
|
+
"version": "0.0.53",
|
|
5
5
|
"main": "src/main.ts",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"repository": {
|
|
@@ -441,108 +441,6 @@ export class DataLoader {
|
|
|
441
441
|
|
|
442
442
|
let needsUpdate = false;
|
|
443
443
|
let numUpdates = 0;
|
|
444
|
-
|
|
445
|
-
// Collect primary key fields from the users collection definitions
|
|
446
|
-
this.config.collections.forEach((collection) => {
|
|
447
|
-
if (this.getCollectionKey(collection.name) === usersCollectionKey) {
|
|
448
|
-
const collectionImportDefs = collection.importDefs;
|
|
449
|
-
if (!collectionImportDefs || !collectionImportDefs.length) {
|
|
450
|
-
return;
|
|
451
|
-
}
|
|
452
|
-
collectionImportDefs.forEach((importDef) => {
|
|
453
|
-
if (importDef.primaryKeyField) {
|
|
454
|
-
usersCollectionPrimaryKeyFields.add(importDef.primaryKeyField);
|
|
455
|
-
}
|
|
456
|
-
});
|
|
457
|
-
}
|
|
458
|
-
});
|
|
459
|
-
|
|
460
|
-
console.log(
|
|
461
|
-
`Primary key fields collected for users collection: ${[
|
|
462
|
-
...usersCollectionPrimaryKeyFields,
|
|
463
|
-
]}`
|
|
464
|
-
);
|
|
465
|
-
|
|
466
|
-
// Iterate over all collections to update references based on merged users
|
|
467
|
-
this.config.collections.forEach((collection) => {
|
|
468
|
-
const collectionData = this.importMap.get(
|
|
469
|
-
this.getCollectionKey(collection.name)
|
|
470
|
-
);
|
|
471
|
-
|
|
472
|
-
if (!collectionData || !collectionData.data) {
|
|
473
|
-
console.log(`No data found for collection ${collection.name}`);
|
|
474
|
-
return;
|
|
475
|
-
}
|
|
476
|
-
|
|
477
|
-
const collectionImportDefs = collection.importDefs;
|
|
478
|
-
if (!collectionImportDefs || !collectionImportDefs.length) {
|
|
479
|
-
console.log(
|
|
480
|
-
`No import definitions found for collection ${collection.name}`
|
|
481
|
-
);
|
|
482
|
-
return;
|
|
483
|
-
}
|
|
484
|
-
|
|
485
|
-
collectionImportDefs.forEach((importDef) => {
|
|
486
|
-
importDef.idMappings?.forEach((idMapping) => {
|
|
487
|
-
if (
|
|
488
|
-
this.getCollectionKey(idMapping.targetCollection) ===
|
|
489
|
-
usersCollectionKey
|
|
490
|
-
) {
|
|
491
|
-
const fieldToSetKey = idMapping.fieldToSet || idMapping.sourceField;
|
|
492
|
-
const targetFieldKey =
|
|
493
|
-
idMapping.targetFieldToMatch || idMapping.targetField;
|
|
494
|
-
|
|
495
|
-
if (usersCollectionPrimaryKeyFields.has(targetFieldKey)) {
|
|
496
|
-
console.log(
|
|
497
|
-
`Processing collection ${collection.name} with target field ${targetFieldKey}`
|
|
498
|
-
);
|
|
499
|
-
|
|
500
|
-
// Process each item in the collection
|
|
501
|
-
collectionData.data.forEach((item) => {
|
|
502
|
-
const oldId =
|
|
503
|
-
item.finalData[idMapping.sourceField] ||
|
|
504
|
-
item.context[idMapping.sourceField];
|
|
505
|
-
|
|
506
|
-
if (oldId === undefined || oldId === null) {
|
|
507
|
-
console.log(
|
|
508
|
-
`Skipping item with undefined or null oldId in collection ${collection.name}`
|
|
509
|
-
);
|
|
510
|
-
return;
|
|
511
|
-
}
|
|
512
|
-
|
|
513
|
-
const newId = this.mergedUserMap.get(`${oldId}`);
|
|
514
|
-
|
|
515
|
-
if (newId) {
|
|
516
|
-
needsUpdate = true;
|
|
517
|
-
numUpdates++;
|
|
518
|
-
console.log(
|
|
519
|
-
`Updating old ID ${oldId} to new ID ${newId} in collection ${collection.name}`
|
|
520
|
-
);
|
|
521
|
-
|
|
522
|
-
// Update context to use new user ID
|
|
523
|
-
item.finalData[fieldToSetKey] = newId;
|
|
524
|
-
item.context[fieldToSetKey] = newId;
|
|
525
|
-
} else {
|
|
526
|
-
console.log(
|
|
527
|
-
`No new ID found for old ID ${oldId} in mergedUserMap.`
|
|
528
|
-
);
|
|
529
|
-
}
|
|
530
|
-
});
|
|
531
|
-
}
|
|
532
|
-
}
|
|
533
|
-
});
|
|
534
|
-
});
|
|
535
|
-
|
|
536
|
-
if (needsUpdate) {
|
|
537
|
-
console.log(
|
|
538
|
-
`Updated ${numUpdates} references for collection ${collection.name}`
|
|
539
|
-
);
|
|
540
|
-
this.importMap.set(
|
|
541
|
-
this.getCollectionKey(collection.name),
|
|
542
|
-
collectionData
|
|
543
|
-
);
|
|
544
|
-
}
|
|
545
|
-
});
|
|
546
444
|
}
|
|
547
445
|
|
|
548
446
|
updateOldReferencesForNew() {
|
|
@@ -655,7 +553,12 @@ export class DataLoader {
|
|
|
655
553
|
// Merge arrays if new data is non-empty array and filter for uniqueness
|
|
656
554
|
collectionData.data[i].finalData[fieldToSetKey] = [
|
|
657
555
|
...new Set(
|
|
658
|
-
[
|
|
556
|
+
[
|
|
557
|
+
...(Array.isArray(currentDataFiltered)
|
|
558
|
+
? currentDataFiltered
|
|
559
|
+
: [currentDataFiltered]),
|
|
560
|
+
...newData,
|
|
561
|
+
].filter(
|
|
659
562
|
(value: any) => `${value}` !== `${valueToMatch}`
|
|
660
563
|
)
|
|
661
564
|
),
|