appwrite-utils-cli 0.0.258 → 0.0.259

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.
@@ -1562,6 +1562,17 @@ export declare class DataLoader {
1562
1562
  private shouldWriteFile;
1563
1563
  constructor(appwriteFolderPath: string, importDataActions: ImportDataActions, database: Databases, config: AppwriteConfig, shouldWriteFile?: boolean);
1564
1564
  getCollectionKey(name: string): string;
1565
+ /**
1566
+ * Merges two objects by updating the source object with the target object's values.
1567
+ * It iterates through the target object's keys and updates the source object if:
1568
+ * - The source object has the key.
1569
+ * - The target object's value for that key is not null, undefined, or an empty string.
1570
+ *
1571
+ * @param source - The source object to be updated.
1572
+ * @param target - The target object with values to update the source object.
1573
+ * @returns The updated source object.
1574
+ */
1575
+ mergeObjects(source: Record<string, any>, target: Record<string, any>): Record<string, any>;
1565
1576
  loadData(importDef: ImportDef): Promise<any[]>;
1566
1577
  checkMapValuesForId(newId: string, collectionName: string): string | false;
1567
1578
  getTrueUniqueId(collectionName: string): string;
@@ -59,6 +59,28 @@ export class DataLoader {
59
59
  getCollectionKey(name) {
60
60
  return name.toLowerCase().replace(" ", "");
61
61
  }
62
+ /**
63
+ * Merges two objects by updating the source object with the target object's values.
64
+ * It iterates through the target object's keys and updates the source object if:
65
+ * - The source object has the key.
66
+ * - The target object's value for that key is not null, undefined, or an empty string.
67
+ *
68
+ * @param source - The source object to be updated.
69
+ * @param target - The target object with values to update the source object.
70
+ * @returns The updated source object.
71
+ */
72
+ mergeObjects(source, target) {
73
+ Object.keys(target).forEach((key) => {
74
+ const targetValue = target[key];
75
+ if (source.hasOwnProperty(key) &&
76
+ targetValue !== null &&
77
+ targetValue !== undefined &&
78
+ targetValue !== "") {
79
+ source[key] = targetValue;
80
+ }
81
+ });
82
+ return source;
83
+ }
62
84
  // Method to load data from a file specified in the import definition
63
85
  async loadData(importDef) {
64
86
  // Resolve the file path and check if the file exists
@@ -656,7 +678,7 @@ export class DataLoader {
656
678
  }
657
679
  for (const item of rawData) {
658
680
  // Transform the item data based on the attribute mappings
659
- const transformedData = await this.transformData(item, importDef.attributeMappings);
681
+ let transformedData = await this.transformData(item, importDef.attributeMappings);
660
682
  let newId;
661
683
  let oldId;
662
684
  // Determine the new ID for the item based on the primary key field or update mapping
@@ -688,9 +710,17 @@ export class DataLoader {
688
710
  logger.error(`No new id found for collection ${collection.name} for updateDef ${JSON.stringify(item, null, 2)} but it says it's supposed to have one...`);
689
711
  continue;
690
712
  }
713
+ const itemDataToUpdate = this.importMap
714
+ .get(this.getCollectionKey(collection.name))
715
+ ?.data.find((data) => data.rawData[importDef.primaryKeyField] === oldId);
716
+ if (!itemDataToUpdate) {
717
+ logger.error(`No data found for collection ${collection.name} for updateDef ${JSON.stringify(item, null, 2)} but it says it's supposed to have one...`);
718
+ continue;
719
+ }
720
+ transformedData = this.mergeObjects(itemDataToUpdate.finalData, transformedData);
691
721
  // Create a context object for the item, including the new ID and transformed data
692
722
  let context = this.createContext(db, collection, item, newId);
693
- context = { ...context, ...transformedData };
723
+ context = this.mergeObjects(context, transformedData);
694
724
  // Validate the item before proceeding
695
725
  const isValid = await this.importDataActions.validateItem(item, importDef.attributeMappings, context);
696
726
  // Log info and continue to the next item if it's invalid
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.258",
4
+ "version": "0.0.259",
5
5
  "main": "src/main.ts",
6
6
  "type": "module",
7
7
  "repository": {
@@ -86,6 +86,34 @@ export class DataLoader {
86
86
  return name.toLowerCase().replace(" ", "");
87
87
  }
88
88
 
89
+ /**
90
+ * Merges two objects by updating the source object with the target object's values.
91
+ * It iterates through the target object's keys and updates the source object if:
92
+ * - The source object has the key.
93
+ * - The target object's value for that key is not null, undefined, or an empty string.
94
+ *
95
+ * @param source - The source object to be updated.
96
+ * @param target - The target object with values to update the source object.
97
+ * @returns The updated source object.
98
+ */
99
+ mergeObjects(
100
+ source: Record<string, any>,
101
+ target: Record<string, any>
102
+ ): Record<string, any> {
103
+ Object.keys(target).forEach((key) => {
104
+ const targetValue = target[key];
105
+ if (
106
+ source.hasOwnProperty(key) &&
107
+ targetValue !== null &&
108
+ targetValue !== undefined &&
109
+ targetValue !== ""
110
+ ) {
111
+ source[key] = targetValue;
112
+ }
113
+ });
114
+ return source;
115
+ }
116
+
89
117
  // Method to load data from a file specified in the import definition
90
118
  async loadData(importDef: ImportDef): Promise<any[]> {
91
119
  // Resolve the file path and check if the file exists
@@ -874,7 +902,7 @@ export class DataLoader {
874
902
  }
875
903
  for (const item of rawData) {
876
904
  // Transform the item data based on the attribute mappings
877
- const transformedData = await this.transformData(
905
+ let transformedData = await this.transformData(
878
906
  item,
879
907
  importDef.attributeMappings
880
908
  );
@@ -921,9 +949,30 @@ export class DataLoader {
921
949
  );
922
950
  continue;
923
951
  }
952
+ const itemDataToUpdate = this.importMap
953
+ .get(this.getCollectionKey(collection.name))
954
+ ?.data.find(
955
+ (data) => data.rawData[importDef.primaryKeyField] === oldId
956
+ );
957
+ if (!itemDataToUpdate) {
958
+ logger.error(
959
+ `No data found for collection ${
960
+ collection.name
961
+ } for updateDef ${JSON.stringify(
962
+ item,
963
+ null,
964
+ 2
965
+ )} but it says it's supposed to have one...`
966
+ );
967
+ continue;
968
+ }
969
+ transformedData = this.mergeObjects(
970
+ itemDataToUpdate.finalData,
971
+ transformedData
972
+ );
924
973
  // Create a context object for the item, including the new ID and transformed data
925
974
  let context = this.createContext(db, collection, item, newId);
926
- context = { ...context, ...transformedData };
975
+ context = this.mergeObjects(context, transformedData);
927
976
  // Validate the item before proceeding
928
977
  const isValid = await this.importDataActions.validateItem(
929
978
  item,