appwrite-utils-cli 0.0.66 → 0.0.67

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.
package/README.md CHANGED
@@ -132,6 +132,7 @@ This setup ensures that developers have robust tools at their fingertips to mana
132
132
 
133
133
  ### Changelog
134
134
 
135
+ - 0.0.67: Fixed `updates` in `importDef`'s update mappings overwriting postImportActions from the original
135
136
  - 0.0.57: Fixed `dataLoader`'s `idMapping`'s giving me issues
136
137
  - 0.0.55: Added `documentExists` check to batch creation functionality to try to prevent duplicates
137
138
  - 0.0.54: Various fixes in here
@@ -366,16 +366,7 @@ export declare const CollectionImportDataSchema: z.ZodObject<{
366
366
  fieldToSet: z.ZodOptional<z.ZodString>;
367
367
  targetFieldToMatch: z.ZodOptional<z.ZodString>;
368
368
  targetField: z.ZodString;
369
- targetCollection: z.ZodString; /**
370
- * Prepares the data for updating documents within a collection.
371
- * This method loads the raw data based on the import definition, transforms it according to the attribute mappings,
372
- * finds the new ID for each item based on the primary key or update mapping, and then validates the transformed data.
373
- * If the data is valid, it updates the import definition with any post-import actions and adds the item to the current collection data.
374
- *
375
- * @param db - The database configuration.
376
- * @param collection - The collection configuration.
377
- * @param importDef - The import definition containing the attribute mappings and other relevant info.
378
- */
369
+ targetCollection: z.ZodString;
379
370
  }, "strip", z.ZodTypeAny, {
380
371
  sourceField: string;
381
372
  targetField: string;
@@ -470,7 +470,9 @@ export class DataLoader {
470
470
  const currentDataFiltered = getCurrentDataFiltered(collectionData.data[i]);
471
471
  // Log and skip if no matching data found
472
472
  if (!foundData.length) {
473
- console.log(`No data found for collection ${collectionConfig.name}:\nTarget collection: ${targetCollectionKey}\nValue to match: ${valueToMatch}\nField to set: ${fieldToSetKey}\nTarget field to match: ${targetFieldKey}\nTarget field value: ${idMapping.targetField}`);
473
+ // console.log(
474
+ // `No data found for collection ${collectionConfig.name}: - Target collection: ${targetCollectionKey} - Value to match: ${valueToMatch} - Field to set: ${fieldToSetKey} - Target field to match: ${targetFieldKey} - Target field value: ${idMapping.targetField}`
475
+ // );
474
476
  logger.info(`No data found for collection: ${targetCollectionKey} with value: ${valueToMatch} for field: ${fieldToSetKey} -- idMapping: ${JSON.stringify(idMapping, null, 2)}`);
475
477
  continue;
476
478
  }
@@ -978,17 +980,14 @@ export class DataLoader {
978
980
  item[importDef.updateMapping.originalIdField] ||
979
981
  transformedData[importDef.updateMapping.originalIdField];
980
982
  if (oldId) {
981
- itemDataToUpdate = currentData?.data.find(({ context, rawData, finalData }) => {
983
+ itemDataToUpdate = currentData?.data.find(({ context, finalData }) => {
982
984
  const targetField = importDef.updateMapping.targetField ??
983
985
  importDef.updateMapping.originalIdField;
984
986
  return (`${context[targetField]}` === `${oldId}` ||
985
- `${rawData[targetField]}` === `${oldId}` ||
986
987
  `${finalData[targetField]}` === `${oldId}`);
987
988
  });
988
989
  if (itemDataToUpdate) {
989
- newId =
990
- itemDataToUpdate.finalData.docId ||
991
- itemDataToUpdate.context.docId;
990
+ newId = itemDataToUpdate.context.docId;
992
991
  }
993
992
  }
994
993
  }
@@ -1036,10 +1035,10 @@ export class DataLoader {
1036
1035
  }
1037
1036
  transformedData = this.mergeObjects(itemDataToUpdate.finalData, transformedData);
1038
1037
  // Create a context object for the item, including the new ID and transformed data
1039
- let context = this.createContext(db, collection, item, newId);
1038
+ let context = itemDataToUpdate.context;
1040
1039
  context = this.mergeObjects(context, transformedData);
1041
1040
  // Validate the item before proceeding
1042
- const isValid = await this.importDataActions.validateItem(item, importDef.attributeMappings, context);
1041
+ const isValid = this.importDataActions.validateItem(item, importDef.attributeMappings, context);
1043
1042
  if (!isValid) {
1044
1043
  logger.info(`Skipping item: ${JSON.stringify(item, null, 2)} because it's invalid`);
1045
1044
  continue;
@@ -1049,15 +1048,26 @@ export class DataLoader {
1049
1048
  // Update the import definition with the new attribute mappings
1050
1049
  const newImportDef = {
1051
1050
  ...importDef,
1052
- attributeMappings: [
1053
- ...importDef.attributeMappings,
1054
- ...mappingsWithActions,
1055
- ],
1051
+ attributeMappings: mappingsWithActions,
1056
1052
  };
1057
1053
  if (itemDataToUpdate) {
1058
1054
  itemDataToUpdate.finalData = this.mergeObjects(itemDataToUpdate.finalData, transformedData);
1059
1055
  itemDataToUpdate.context = context;
1060
- itemDataToUpdate.importDef = newImportDef;
1056
+ // Merge existing importDef with new importDef, focusing only on postImportActions
1057
+ itemDataToUpdate.importDef = {
1058
+ ...itemDataToUpdate.importDef,
1059
+ attributeMappings: itemDataToUpdate.importDef?.attributeMappings.map((attrMapping, index) => ({
1060
+ ...attrMapping,
1061
+ postImportActions: [
1062
+ ...(attrMapping.postImportActions || []),
1063
+ ...(newImportDef.attributeMappings[index]
1064
+ ?.postImportActions || []),
1065
+ ],
1066
+ })) || [],
1067
+ };
1068
+ if (collection.name.toLowerCase() === "councils") {
1069
+ console.log(`Mappings in update councils: ${JSON.stringify(itemDataToUpdate.importDef.attributeMappings, null, 2)}`);
1070
+ }
1061
1071
  }
1062
1072
  else {
1063
1073
  currentData.data.push({
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.66",
4
+ "version": "0.0.67",
5
5
  "main": "src/main.ts",
6
6
  "type": "module",
7
7
  "repository": {
@@ -598,9 +598,9 @@ export class DataLoader {
598
598
  );
599
599
  // Log and skip if no matching data found
600
600
  if (!foundData.length) {
601
- console.log(
602
- `No data found for collection ${collectionConfig.name}:\nTarget collection: ${targetCollectionKey}\nValue to match: ${valueToMatch}\nField to set: ${fieldToSetKey}\nTarget field to match: ${targetFieldKey}\nTarget field value: ${idMapping.targetField}`
603
- );
601
+ // console.log(
602
+ // `No data found for collection ${collectionConfig.name}: - Target collection: ${targetCollectionKey} - Value to match: ${valueToMatch} - Field to set: ${fieldToSetKey} - Target field to match: ${targetFieldKey} - Target field value: ${idMapping.targetField}`
603
+ // );
604
604
  logger.info(
605
605
  `No data found for collection: ${targetCollectionKey} with value: ${valueToMatch} for field: ${fieldToSetKey} -- idMapping: ${JSON.stringify(
606
606
  idMapping,
@@ -1297,23 +1297,20 @@ export class DataLoader {
1297
1297
  transformedData[importDef.updateMapping.originalIdField];
1298
1298
  if (oldId) {
1299
1299
  itemDataToUpdate = currentData?.data.find(
1300
- ({ context, rawData, finalData }) => {
1300
+ ({ context, finalData }) => {
1301
1301
  const targetField =
1302
1302
  importDef.updateMapping!.targetField ??
1303
1303
  importDef.updateMapping!.originalIdField;
1304
1304
 
1305
1305
  return (
1306
1306
  `${context[targetField]}` === `${oldId}` ||
1307
- `${rawData[targetField]}` === `${oldId}` ||
1308
1307
  `${finalData[targetField]}` === `${oldId}`
1309
1308
  );
1310
1309
  }
1311
1310
  );
1312
1311
 
1313
1312
  if (itemDataToUpdate) {
1314
- newId =
1315
- itemDataToUpdate.finalData.docId ||
1316
- itemDataToUpdate.context.docId;
1313
+ newId = itemDataToUpdate.context.docId;
1317
1314
  }
1318
1315
  }
1319
1316
  }
@@ -1407,11 +1404,11 @@ export class DataLoader {
1407
1404
  );
1408
1405
 
1409
1406
  // Create a context object for the item, including the new ID and transformed data
1410
- let context = this.createContext(db, collection, item, newId);
1407
+ let context = itemDataToUpdate.context;
1411
1408
  context = this.mergeObjects(context, transformedData);
1412
1409
 
1413
1410
  // Validate the item before proceeding
1414
- const isValid = await this.importDataActions.validateItem(
1411
+ const isValid = this.importDataActions.validateItem(
1415
1412
  item,
1416
1413
  importDef.attributeMappings,
1417
1414
  context
@@ -1434,10 +1431,7 @@ export class DataLoader {
1434
1431
  // Update the import definition with the new attribute mappings
1435
1432
  const newImportDef = {
1436
1433
  ...importDef,
1437
- attributeMappings: [
1438
- ...importDef.attributeMappings,
1439
- ...mappingsWithActions,
1440
- ],
1434
+ attributeMappings: mappingsWithActions,
1441
1435
  };
1442
1436
 
1443
1437
  if (itemDataToUpdate) {
@@ -1446,7 +1440,31 @@ export class DataLoader {
1446
1440
  transformedData
1447
1441
  );
1448
1442
  itemDataToUpdate.context = context;
1449
- itemDataToUpdate.importDef = newImportDef;
1443
+ // Merge existing importDef with new importDef, focusing only on postImportActions
1444
+ itemDataToUpdate.importDef = {
1445
+ ...itemDataToUpdate.importDef,
1446
+ attributeMappings:
1447
+ itemDataToUpdate.importDef?.attributeMappings.map(
1448
+ (attrMapping, index) => ({
1449
+ ...attrMapping,
1450
+ postImportActions: [
1451
+ ...(attrMapping.postImportActions || []),
1452
+ ...(newImportDef.attributeMappings[index]
1453
+ ?.postImportActions || []),
1454
+ ],
1455
+ })
1456
+ ) || [],
1457
+ } as ImportDef;
1458
+
1459
+ if (collection.name.toLowerCase() === "councils") {
1460
+ console.log(
1461
+ `Mappings in update councils: ${JSON.stringify(
1462
+ itemDataToUpdate.importDef.attributeMappings,
1463
+ null,
1464
+ 2
1465
+ )}`
1466
+ );
1467
+ }
1450
1468
  } else {
1451
1469
  currentData!.data.push({
1452
1470
  rawData: item,