appwrite-utils-cli 0.0.286 → 0.9.0

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.
Files changed (109) hide show
  1. package/README.md +122 -96
  2. package/dist/collections/attributes.d.ts +4 -0
  3. package/dist/collections/attributes.js +224 -0
  4. package/dist/collections/indexes.d.ts +4 -0
  5. package/dist/collections/indexes.js +27 -0
  6. package/dist/collections/methods.d.ts +16 -0
  7. package/dist/collections/methods.js +216 -0
  8. package/dist/databases/methods.d.ts +6 -0
  9. package/dist/databases/methods.js +33 -0
  10. package/dist/interactiveCLI.d.ts +19 -0
  11. package/dist/interactiveCLI.js +555 -0
  12. package/dist/main.js +227 -62
  13. package/dist/migrations/afterImportActions.js +37 -40
  14. package/dist/migrations/appwriteToX.d.ts +26 -25
  15. package/dist/migrations/appwriteToX.js +42 -6
  16. package/dist/migrations/attributes.js +21 -20
  17. package/dist/migrations/backup.d.ts +93 -87
  18. package/dist/migrations/collections.d.ts +6 -0
  19. package/dist/migrations/collections.js +149 -20
  20. package/dist/migrations/converters.d.ts +2 -18
  21. package/dist/migrations/converters.js +13 -2
  22. package/dist/migrations/dataLoader.d.ts +276 -161
  23. package/dist/migrations/dataLoader.js +535 -292
  24. package/dist/migrations/databases.js +8 -2
  25. package/dist/migrations/helper.d.ts +3 -0
  26. package/dist/migrations/helper.js +21 -0
  27. package/dist/migrations/importController.d.ts +5 -2
  28. package/dist/migrations/importController.js +125 -88
  29. package/dist/migrations/importDataActions.d.ts +9 -1
  30. package/dist/migrations/importDataActions.js +15 -3
  31. package/dist/migrations/indexes.js +3 -2
  32. package/dist/migrations/logging.js +20 -8
  33. package/dist/migrations/migrationHelper.d.ts +9 -4
  34. package/dist/migrations/migrationHelper.js +6 -5
  35. package/dist/migrations/openapi.d.ts +1 -1
  36. package/dist/migrations/openapi.js +33 -18
  37. package/dist/migrations/queue.js +3 -2
  38. package/dist/migrations/relationships.d.ts +2 -2
  39. package/dist/migrations/schemaStrings.js +53 -41
  40. package/dist/migrations/setupDatabase.d.ts +2 -4
  41. package/dist/migrations/setupDatabase.js +24 -105
  42. package/dist/migrations/storage.d.ts +3 -1
  43. package/dist/migrations/storage.js +110 -16
  44. package/dist/migrations/transfer.d.ts +30 -0
  45. package/dist/migrations/transfer.js +337 -0
  46. package/dist/migrations/users.d.ts +2 -1
  47. package/dist/migrations/users.js +78 -43
  48. package/dist/schemas/authUser.d.ts +2 -2
  49. package/dist/storage/methods.d.ts +15 -0
  50. package/dist/storage/methods.js +207 -0
  51. package/dist/storage/schemas.d.ts +687 -0
  52. package/dist/storage/schemas.js +175 -0
  53. package/dist/utils/getClientFromConfig.d.ts +4 -0
  54. package/dist/utils/getClientFromConfig.js +16 -0
  55. package/dist/utils/helperFunctions.d.ts +11 -1
  56. package/dist/utils/helperFunctions.js +38 -0
  57. package/dist/utils/retryFailedPromises.d.ts +2 -0
  58. package/dist/utils/retryFailedPromises.js +21 -0
  59. package/dist/utils/schemaStrings.d.ts +13 -0
  60. package/dist/utils/schemaStrings.js +403 -0
  61. package/dist/utils/setupFiles.js +110 -61
  62. package/dist/utilsController.d.ts +40 -22
  63. package/dist/utilsController.js +164 -84
  64. package/package.json +13 -15
  65. package/src/collections/attributes.ts +483 -0
  66. package/src/collections/indexes.ts +53 -0
  67. package/src/collections/methods.ts +331 -0
  68. package/src/databases/methods.ts +47 -0
  69. package/src/init.ts +64 -64
  70. package/src/interactiveCLI.ts +767 -0
  71. package/src/main.ts +292 -83
  72. package/src/migrations/afterImportActions.ts +553 -490
  73. package/src/migrations/appwriteToX.ts +237 -174
  74. package/src/migrations/attributes.ts +483 -422
  75. package/src/migrations/backup.ts +205 -205
  76. package/src/migrations/collections.ts +545 -300
  77. package/src/migrations/converters.ts +161 -150
  78. package/src/migrations/dataLoader.ts +1615 -1304
  79. package/src/migrations/databases.ts +44 -25
  80. package/src/migrations/dbHelpers.ts +92 -92
  81. package/src/migrations/helper.ts +40 -0
  82. package/src/migrations/importController.ts +448 -384
  83. package/src/migrations/importDataActions.ts +315 -307
  84. package/src/migrations/indexes.ts +40 -37
  85. package/src/migrations/logging.ts +29 -16
  86. package/src/migrations/migrationHelper.ts +207 -201
  87. package/src/migrations/openapi.ts +83 -70
  88. package/src/migrations/queue.ts +118 -119
  89. package/src/migrations/relationships.ts +324 -324
  90. package/src/migrations/schemaStrings.ts +472 -460
  91. package/src/migrations/setupDatabase.ts +118 -219
  92. package/src/migrations/storage.ts +538 -358
  93. package/src/migrations/transfer.ts +608 -0
  94. package/src/migrations/users.ts +362 -285
  95. package/src/migrations/validationRules.ts +63 -63
  96. package/src/schemas/authUser.ts +23 -23
  97. package/src/setup.ts +8 -8
  98. package/src/storage/methods.ts +371 -0
  99. package/src/storage/schemas.ts +205 -0
  100. package/src/types.ts +9 -9
  101. package/src/utils/getClientFromConfig.ts +17 -0
  102. package/src/utils/helperFunctions.ts +181 -127
  103. package/src/utils/index.ts +2 -2
  104. package/src/utils/loadConfigs.ts +59 -59
  105. package/src/utils/retryFailedPromises.ts +27 -0
  106. package/src/utils/schemaStrings.ts +473 -0
  107. package/src/utils/setupFiles.ts +228 -182
  108. package/src/utilsController.ts +325 -194
  109. package/tsconfig.json +37 -37
@@ -1,1304 +1,1615 @@
1
- import type { ImportDataActions } from "./importDataActions.js";
2
- import {
3
- AttributeMappingsSchema,
4
- CollectionCreateSchema,
5
- importDefSchema,
6
- type AppwriteConfig,
7
- type AttributeMappings,
8
- type CollectionCreate,
9
- type ConfigDatabase,
10
- type IdMapping,
11
- type ImportDef,
12
- type ImportDefs,
13
- type RelationshipAttribute,
14
- } from "appwrite-utils";
15
- import path from "path";
16
- import fs from "fs";
17
- import { convertObjectByAttributeMappings } from "./converters.js";
18
- import { z } from "zod";
19
- import { checkForCollection } from "./collections.js";
20
- import { ID, Users, type Databases } from "node-appwrite";
21
- import { logger } from "./logging.js";
22
- import { findOrCreateOperation, updateOperation } from "./migrationHelper.js";
23
- import { AuthUserCreateSchema } from "../schemas/authUser.js";
24
- import _ from "lodash";
25
- import { UsersController } from "./users.js";
26
- import { finalizeByAttributeMap } from "../utils/helperFunctions.js";
27
- // Define a schema for the structure of collection import data using Zod for validation
28
- export const CollectionImportDataSchema = z.object({
29
- // Optional collection creation schema
30
- collection: CollectionCreateSchema.optional(),
31
- // Array of data objects each containing rawData, finalData, context, and an import definition
32
- data: z.array(
33
- z.object({
34
- rawData: z.any(), // The initial raw data
35
- finalData: z.any(), // The transformed data ready for import
36
- context: z.any(), // Additional context for the data transformation
37
- importDef: importDefSchema.optional(), // The import definition schema
38
- })
39
- ),
40
- });
41
-
42
- // Infer the TypeScript type from the Zod schema
43
- export type CollectionImportData = z.infer<typeof CollectionImportDataSchema>;
44
-
45
- // DataLoader class to handle the loading of data into collections
46
- export class DataLoader {
47
- // Private member variables to hold configuration and state
48
- private appwriteFolderPath: string;
49
- private importDataActions: ImportDataActions;
50
- private database: Databases;
51
- private usersController: UsersController;
52
- private config: AppwriteConfig;
53
- // Map to hold the import data for each collection by name
54
- importMap = new Map<string, CollectionImportData>();
55
- // Map to track old to new ID mappings for each collection, if applicable
56
- private oldIdToNewIdPerCollectionMap = new Map<string, Map<string, string>>();
57
- // Map to hold the import operation ID for each collection
58
- collectionImportOperations = new Map<string, string>();
59
- // Map to hold the merged user map for relationship resolution
60
- // Will hold an array of the old user ID's that are mapped to the same new user ID
61
- // For example, if there are two users with the same email, they will both be mapped to the same new user ID
62
- // Prevents duplicate users with the other two maps below it and allows me to keep the old ID's
63
- private mergedUserMap = new Map<string, string[]>();
64
- // Maps to hold email and phone to user ID mappings for unique-ness in User Accounts
65
- private emailToUserIdMap = new Map<string, string>();
66
- private phoneToUserIdMap = new Map<string, string>();
67
- userExistsMap = new Map<string, boolean>();
68
- private shouldWriteFile = false;
69
-
70
- // Constructor to initialize the DataLoader with necessary configurations
71
- constructor(
72
- appwriteFolderPath: string,
73
- importDataActions: ImportDataActions,
74
- database: Databases,
75
- config: AppwriteConfig,
76
- shouldWriteFile?: boolean
77
- ) {
78
- this.appwriteFolderPath = appwriteFolderPath;
79
- this.importDataActions = importDataActions;
80
- this.database = database;
81
- this.usersController = new UsersController(config, database);
82
- this.config = config;
83
- this.shouldWriteFile = shouldWriteFile || false;
84
- }
85
-
86
- // Helper method to generate a consistent key for collections
87
- getCollectionKey(name: string) {
88
- return name.toLowerCase().replace(" ", "");
89
- }
90
-
91
- /**
92
- * Merges two objects by updating the source object with the target object's values.
93
- * It iterates through the target object's keys and updates the source object if:
94
- * - The source object has the key.
95
- * - The target object's value for that key is not null, undefined, or an empty string.
96
- *
97
- * @param source - The source object to be updated.
98
- * @param target - The target object with values to update the source object.
99
- * @returns The updated source object.
100
- */
101
- mergeObjects(source: any, update: any): any {
102
- // Create a new object to hold the merged result
103
- const result = { ...source };
104
-
105
- Object.keys(update).forEach((key) => {
106
- const sourceValue = source[key];
107
- const updateValue = update[key];
108
-
109
- // If the update value is an array, concatenate and remove duplicates
110
- if (Array.isArray(updateValue)) {
111
- const sourceArray = Array.isArray(sourceValue) ? sourceValue : [];
112
- result[key] = [...new Set([...sourceArray, ...updateValue])];
113
- }
114
- // If the update value is an object, recursively merge
115
- else if (
116
- updateValue !== null &&
117
- typeof updateValue === "object" &&
118
- !(updateValue instanceof Date)
119
- ) {
120
- result[key] = this.mergeObjects(sourceValue, updateValue);
121
- }
122
- // If the update value is not nullish, overwrite the source value
123
- else if (updateValue !== null && updateValue !== undefined) {
124
- result[key] = updateValue;
125
- }
126
- // If the update value is nullish, keep the original value unless it doesn't exist
127
- else if (sourceValue === undefined) {
128
- result[key] = updateValue;
129
- }
130
- });
131
-
132
- return result;
133
- }
134
-
135
- // Method to load data from a file specified in the import definition
136
- loadData(importDef: ImportDef): any[] {
137
- // Resolve the file path and check if the file exists
138
- const filePath = path.resolve(this.appwriteFolderPath, importDef.filePath);
139
- if (!fs.existsSync(filePath)) {
140
- console.error(`File not found: ${filePath}`);
141
- return [];
142
- }
143
-
144
- // Read the file and parse the JSON data
145
- const rawData = fs.readFileSync(filePath, "utf8");
146
- return importDef.basePath
147
- ? JSON.parse(rawData)[importDef.basePath]
148
- : JSON.parse(rawData);
149
- }
150
-
151
- // Helper method to check if a new ID already exists in the old-to-new ID map
152
- checkMapValuesForId(newId: string, collectionName: string) {
153
- const oldIdMap = this.oldIdToNewIdPerCollectionMap.get(collectionName);
154
- for (const [key, value] of oldIdMap?.entries() || []) {
155
- if (value === newId) {
156
- return key;
157
- }
158
- }
159
- return false;
160
- }
161
-
162
- // Method to generate a unique ID that doesn't conflict with existing IDs
163
- getTrueUniqueId(collectionName: string) {
164
- let newId = ID.unique();
165
- while (this.checkMapValuesForId(newId, collectionName)) {
166
- newId = ID.unique();
167
- }
168
- return newId;
169
- }
170
-
171
- // Method to create a context object for data transformation
172
- createContext(
173
- db: ConfigDatabase,
174
- collection: CollectionCreate,
175
- item: any,
176
- docId: string
177
- ) {
178
- return {
179
- ...item, // Spread the item data for easy access to its properties
180
- dbId: db.$id,
181
- dbName: db.name,
182
- collId: collection.$id,
183
- collName: collection.name,
184
- docId: docId,
185
- createdDoc: {}, // Initially null, to be updated when the document is created
186
- };
187
- }
188
-
189
- /**
190
- * Transforms the given item based on the provided attribute mappings.
191
- * This method applies conversion rules to the item's attributes as defined in the attribute mappings.
192
- *
193
- * @param item - The item to be transformed.
194
- * @param attributeMappings - The mappings that define how each attribute should be transformed.
195
- * @returns The transformed item.
196
- */
197
- transformData(item: any, attributeMappings: AttributeMappings): any {
198
- // Convert the item using the attribute mappings provided
199
- const convertedItem = convertObjectByAttributeMappings(
200
- item,
201
- attributeMappings
202
- );
203
- // Run additional converter functions on the converted item, if any
204
- return this.importDataActions.runConverterFunctions(
205
- convertedItem,
206
- attributeMappings
207
- );
208
- }
209
-
210
- async setupMaps(dbId: string) {
211
- // Initialize the users collection in the import map
212
- this.importMap.set(this.getCollectionKey("users"), {
213
- data: [],
214
- });
215
- for (const db of this.config.databases) {
216
- if (db.$id !== dbId) {
217
- continue;
218
- }
219
- if (!this.config.collections) {
220
- continue;
221
- }
222
- for (let index = 0; index < this.config.collections.length; index++) {
223
- const collectionConfig = this.config.collections[index];
224
- let collection = CollectionCreateSchema.parse(collectionConfig);
225
- // Check if the collection exists in the database
226
- const collectionExists = await checkForCollection(
227
- this.database,
228
- db.$id,
229
- collection
230
- );
231
- if (!collectionExists) {
232
- logger.error(`No collection found for ${collection.name}`);
233
- continue;
234
- } else if (!collection.name) {
235
- logger.error(`Collection ${collection.name} has no name`);
236
- continue;
237
- }
238
- // Update the collection ID with the existing one
239
- collectionConfig.$id = collectionExists.$id;
240
- collection.$id = collectionExists.$id;
241
- this.config.collections[index] = collectionConfig;
242
- // Find or create an import operation for the collection
243
- const collectionImportOperation = await findOrCreateOperation(
244
- this.database,
245
- collection.$id,
246
- "importData"
247
- );
248
- // Store the operation ID in the map
249
- this.collectionImportOperations.set(
250
- this.getCollectionKey(collection.name),
251
- collectionImportOperation.$id
252
- );
253
- // Initialize the collection in the import map
254
- this.importMap.set(this.getCollectionKey(collection.name), {
255
- collection: collection,
256
- data: [],
257
- });
258
- }
259
- }
260
- }
261
-
262
- async getAllUsers() {
263
- const users = new UsersController(this.config, this.database);
264
- const allUsers = await users.getAllUsers();
265
- // Iterate over the users and setup our maps ahead of time for email and phone
266
- for (const user of allUsers) {
267
- if (user.email) {
268
- this.emailToUserIdMap.set(user.email, user.$id);
269
- }
270
- if (user.phone) {
271
- this.phoneToUserIdMap.set(user.phone, user.$id);
272
- }
273
- this.userExistsMap.set(user.$id, true);
274
- }
275
- return allUsers;
276
- }
277
-
278
- // Main method to start the data loading process for a given database ID
279
- async start(dbId: string) {
280
- console.log("---------------------------------");
281
- console.log(`Starting data setup for database: ${dbId}`);
282
- console.log("---------------------------------");
283
- await this.setupMaps(dbId);
284
- const allUsers = await this.getAllUsers();
285
- console.log(`Fetched ${allUsers.length} users`);
286
- // Iterate over the configured databases to find the matching one
287
- for (const db of this.config.databases) {
288
- if (db.$id !== dbId) {
289
- continue;
290
- }
291
- if (!this.config.collections) {
292
- continue;
293
- }
294
- // Iterate over the configured collections to process each
295
- for (const collectionConfig of this.config.collections) {
296
- const collection = collectionConfig;
297
- // Determine if this is the users collection
298
- let isUsersCollection =
299
- this.getCollectionKey(this.config.usersCollectionName) ===
300
- this.getCollectionKey(collection.name);
301
- // Process create and update definitions for the collection
302
- const createDefs = collection.importDefs.filter(
303
- (def: ImportDef) => def.type === "create" || !def.type
304
- );
305
- const updateDefs = collection.importDefs.filter(
306
- (def: ImportDef) => def.type === "update"
307
- );
308
- for (const createDef of createDefs) {
309
- if (!isUsersCollection) {
310
- console.log(`${collection.name} is not users collection`);
311
- await this.prepareCreateData(db, collection, createDef);
312
- } else {
313
- // Special handling for users collection if needed
314
- console.log(`${collection.name} is users collection`);
315
- await this.prepareUserCollectionCreateData(
316
- db,
317
- collection,
318
- createDef
319
- );
320
- }
321
- }
322
- for (const updateDef of updateDefs) {
323
- if (!this.importMap.has(this.getCollectionKey(collection.name))) {
324
- logger.error(
325
- `No data found for collection ${collection.name} for updateDef but it says it's supposed to have one...`
326
- );
327
- continue;
328
- }
329
- // Prepare the update data for the collection
330
- await this.prepareUpdateData(db, collection, updateDef);
331
- }
332
- }
333
- console.log("Running update references");
334
- await this.dealWithMergedUsers();
335
- await this.updateOldReferencesForNew();
336
- console.log("Done running update references");
337
- }
338
- // for (const collection of this.config.collections) {
339
- // this.resolveDataItemRelationships(collection);
340
- // }
341
- console.log("---------------------------------");
342
- console.log(`Data setup for database: ${dbId} completed`);
343
- console.log("---------------------------------");
344
- if (this.shouldWriteFile) {
345
- this.writeMapsToJsonFile();
346
- }
347
- }
348
-
349
- async dealWithMergedUsers() {
350
- const usersCollectionKey = this.getCollectionKey(
351
- this.config.usersCollectionName
352
- );
353
- const usersCollectionPrimaryKeyFields = new Set();
354
- if (!this.config.collections) {
355
- return;
356
- }
357
- // Collect primary key fields from the users collection definitions
358
- this.config.collections.forEach((collection) => {
359
- if (this.getCollectionKey(collection.name) === usersCollectionKey) {
360
- collection.importDefs.forEach((importDef) => {
361
- if (importDef.primaryKeyField) {
362
- usersCollectionPrimaryKeyFields.add(importDef.primaryKeyField);
363
- }
364
- });
365
- }
366
- });
367
-
368
- // Iterate over all collections to update references based on merged users
369
- this.config.collections.forEach((collection) => {
370
- const collectionData = this.importMap.get(
371
- this.getCollectionKey(collection.name)
372
- );
373
- if (!collectionData || !collectionData.data) return;
374
-
375
- collection.importDefs.forEach((importDef) => {
376
- importDef.idMappings?.forEach((idMapping) => {
377
- if (
378
- this.getCollectionKey(idMapping.targetCollection) ===
379
- usersCollectionKey
380
- ) {
381
- if (usersCollectionPrimaryKeyFields.has(idMapping.targetField)) {
382
- // Process each item in the collection
383
- collectionData.data.forEach((item) => {
384
- const oldId = item.context[idMapping.sourceField];
385
- const newId = this.mergedUserMap.get(oldId);
386
-
387
- if (newId) {
388
- // Update context to use new user ID
389
- item.context[idMapping.fieldToSet || idMapping.targetField] =
390
- newId;
391
- }
392
- });
393
- }
394
- }
395
- });
396
- });
397
- });
398
- }
399
-
400
- async updateOldReferencesForNew() {
401
- if (!this.config.collections) {
402
- return;
403
- }
404
- for (const collectionConfig of this.config.collections) {
405
- const collectionKey = this.getCollectionKey(collectionConfig.name);
406
- const collectionData = this.importMap.get(collectionKey);
407
-
408
- if (!collectionData || !collectionData.data) continue;
409
-
410
- console.log(
411
- `Updating references for collection: ${collectionConfig.name}`
412
- );
413
-
414
- let needsUpdate = false;
415
-
416
- // Iterate over each data item in the current collection
417
- for (let i = 0; i < collectionData.data.length; i++) {
418
- if (collectionConfig.importDefs) {
419
- for (const importDef of collectionConfig.importDefs) {
420
- if (importDef.idMappings) {
421
- for (const idMapping of importDef.idMappings) {
422
- const targetCollectionKey = this.getCollectionKey(
423
- idMapping.targetCollection
424
- );
425
- const fieldToSetKey =
426
- idMapping.fieldToSet || idMapping.sourceField;
427
- const valueToMatch =
428
- collectionData.data[i].context[idMapping.sourceField];
429
-
430
- if (!valueToMatch || _.isEmpty(valueToMatch)) continue;
431
-
432
- const targetCollectionData =
433
- this.importMap.get(targetCollectionKey);
434
- if (!targetCollectionData || !targetCollectionData.data)
435
- continue;
436
-
437
- const foundData = targetCollectionData.data.filter((data) => {
438
- const targetValue = data.context[idMapping.targetField];
439
- const isMatch = `${targetValue}` === `${valueToMatch}`;
440
- // Debugging output to understand what's being compared
441
- logger.warn(
442
- `Comparing target: ${targetValue} with match: ${valueToMatch} - Result: ${isMatch}`
443
- );
444
- return isMatch;
445
- });
446
-
447
- if (!foundData.length) {
448
- console.log(
449
- `No data found for collection: ${targetCollectionKey} with value: ${valueToMatch} for field: ${fieldToSetKey}`
450
- );
451
- logger.error(
452
- `No data found for collection: ${targetCollectionKey} with value: ${valueToMatch} for field: ${fieldToSetKey} -- idMapping: ${JSON.stringify(
453
- idMapping,
454
- null,
455
- 2
456
- )}`
457
- );
458
- continue;
459
- }
460
-
461
- needsUpdate = true;
462
-
463
- // Properly handle arrays and non-arrays
464
- if (
465
- Array.isArray(collectionData.data[i].finalData[fieldToSetKey])
466
- ) {
467
- collectionData.data[i].finalData[fieldToSetKey] =
468
- foundData.map((data) => data.finalData);
469
- } else {
470
- collectionData.data[i].finalData[fieldToSetKey] =
471
- foundData[0].finalData;
472
- }
473
- }
474
- }
475
- }
476
- }
477
- }
478
-
479
- if (needsUpdate) {
480
- this.importMap.set(collectionKey, collectionData);
481
- }
482
- }
483
- }
484
-
485
- async updateReferencesInRelatedCollections() {
486
- if (!this.config.collections) {
487
- return;
488
- }
489
- // Iterate over each collection configuration
490
- for (const collectionConfig of this.config.collections) {
491
- const collectionKey = this.getCollectionKey(collectionConfig.name);
492
- const collectionData = this.importMap.get(collectionKey);
493
-
494
- if (!collectionData || !collectionData.data) continue;
495
-
496
- console.log(
497
- `Updating references for collection: ${collectionConfig.name}`
498
- );
499
-
500
- // Iterate over each data item in the current collection
501
- for (const item of collectionData.data) {
502
- let needsUpdate = false;
503
-
504
- // Check if the current collection has import definitions with idMappings
505
- if (collectionConfig.importDefs) {
506
- for (const importDef of collectionConfig.importDefs) {
507
- if (importDef.idMappings) {
508
- // Iterate over each idMapping defined for the current import definition
509
- for (const idMapping of importDef.idMappings) {
510
- const oldIds = Array.isArray(
511
- item.context[idMapping.sourceField]
512
- )
513
- ? item.context[idMapping.sourceField]
514
- : [item.context[idMapping.sourceField]];
515
- const resolvedNewIds: string[] = [];
516
-
517
- oldIds.forEach((oldId: any) => {
518
- // Attempt to find a new ID for the old ID
519
- let newIdForOldId = this.findNewIdForOldId(
520
- oldId,
521
- idMapping,
522
- importDef
523
- );
524
-
525
- if (
526
- newIdForOldId &&
527
- !resolvedNewIds.includes(newIdForOldId)
528
- ) {
529
- resolvedNewIds.push(newIdForOldId);
530
- } else {
531
- logger.error(
532
- `No new ID found for old ID ${oldId} in collection ${collectionConfig.name}`
533
- );
534
- }
535
- });
536
-
537
- if (resolvedNewIds.length) {
538
- const targetField =
539
- idMapping.fieldToSet || idMapping.targetField;
540
- const isArray = collectionConfig.attributes.some(
541
- (attribute) =>
542
- attribute.key === targetField && attribute.array
543
- );
544
-
545
- // Set the target field based on whether it's an array or single value
546
- item.finalData[targetField] = isArray
547
- ? resolvedNewIds
548
- : resolvedNewIds[0];
549
- needsUpdate = true;
550
- }
551
- }
552
- }
553
- }
554
- }
555
-
556
- // Update the importMap if changes were made to the item
557
- if (needsUpdate) {
558
- this.importMap.set(collectionKey, collectionData);
559
- logger.info(
560
- `Updated item: ${JSON.stringify(item.finalData, undefined, 2)}`
561
- );
562
- }
563
- }
564
- }
565
- }
566
-
567
- findNewIdForOldId(oldId: string, idMapping: IdMapping, importDef: ImportDef) {
568
- // First, check if this ID mapping is related to the users collection.
569
- const targetCollectionKey = this.getCollectionKey(
570
- idMapping.targetCollection
571
- );
572
- const isUsersCollection =
573
- targetCollectionKey ===
574
- this.getCollectionKey(this.config.usersCollectionName);
575
-
576
- // If handling users, check the mergedUserMap for any existing new ID.
577
- if (isUsersCollection) {
578
- for (const [newUserId, oldIds] of this.mergedUserMap.entries()) {
579
- if (oldIds.includes(oldId)) {
580
- return newUserId;
581
- }
582
- }
583
- }
584
-
585
- // If not a user or no merged ID found, check the regular ID mapping from old to new.
586
- const targetCollectionData = this.importMap.get(targetCollectionKey);
587
- if (targetCollectionData) {
588
- const foundEntry = targetCollectionData.data.find(
589
- (entry) => entry.context[importDef.primaryKeyField] === oldId
590
- );
591
- if (foundEntry) {
592
- return foundEntry.context.docId; // Assuming `docId` stores the new ID after import
593
- }
594
- }
595
-
596
- logger.error(
597
- `No corresponding new ID found for ${oldId} in ${targetCollectionKey}`
598
- );
599
- return null; // Return null if no new ID is found
600
- }
601
-
602
- private writeMapsToJsonFile() {
603
- const outputDir = path.resolve(process.cwd());
604
- const outputFile = path.join(outputDir, "dataLoaderOutput.json");
605
-
606
- const dataToWrite = {
607
- // Convert Maps to arrays of entries for serialization
608
- oldIdToNewIdPerCollectionMap: Array.from(
609
- this.oldIdToNewIdPerCollectionMap.entries()
610
- ).map(([key, value]) => {
611
- return {
612
- collection: key,
613
- data: Array.from(value.entries()),
614
- };
615
- }),
616
- mergedUserMap: Array.from(this.mergedUserMap.entries()),
617
- dataFromCollections: Array.from(this.importMap.entries()).map(
618
- ([key, value]) => {
619
- return {
620
- collection: key,
621
- data: value.data.map((item: any) => item.finalData),
622
- };
623
- }
624
- ),
625
- // emailToUserIdMap: Array.from(this.emailToUserIdMap.entries()),
626
- // phoneToUserIdMap: Array.from(this.phoneToUserIdMap.entries()),
627
- };
628
-
629
- // Use JSON.stringify with a replacer function to handle Maps
630
- const replacer = (key: any, value: any) => {
631
- if (value instanceof Map) {
632
- return Array.from(value.entries());
633
- }
634
- return value;
635
- };
636
-
637
- fs.writeFile(
638
- outputFile,
639
- JSON.stringify(dataToWrite, replacer, 2),
640
- "utf8",
641
- (err) => {
642
- if (err) {
643
- console.error("Error writing data to JSON file:", err);
644
- return;
645
- }
646
- console.log(`Data successfully written to ${outputFile}`);
647
- }
648
- );
649
- }
650
-
651
- /**
652
- * Prepares user data by checking for duplicates based on email or phone, adding to a duplicate map if found,
653
- * and then returning the transformed item without user-specific keys.
654
- *
655
- * @param item - The raw item to be processed.
656
- * @param attributeMappings - The attribute mappings for the item.
657
- * @returns The transformed item with user-specific keys removed.
658
- */
659
- async prepareUserData(
660
- item: any,
661
- attributeMappings: AttributeMappings,
662
- primaryKeyField: string,
663
- newId: string
664
- ): Promise<any> {
665
- // Transform the item data based on the attribute mappings
666
- let transformedItem = this.transformData(item, attributeMappings);
667
- const userData = AuthUserCreateSchema.safeParse(transformedItem);
668
- if (!userData.success) {
669
- logger.error(
670
- `Invalid user data: ${JSON.stringify(
671
- userData.error.errors,
672
- undefined,
673
- 2
674
- )}`
675
- );
676
- return transformedItem;
677
- }
678
- const email = userData.data.email;
679
- const phone = userData.data.phone;
680
- let existingId: string | undefined;
681
-
682
- // Check for duplicate email and add to emailToUserIdMap if not found
683
- if (email && email.length > 0) {
684
- if (this.emailToUserIdMap.has(email)) {
685
- existingId = this.emailToUserIdMap.get(email);
686
- } else {
687
- this.emailToUserIdMap.set(email, newId);
688
- }
689
- }
690
-
691
- // Check for duplicate phone and add to phoneToUserIdMap if not found
692
- if (phone && phone.length > 0) {
693
- if (this.phoneToUserIdMap.has(phone)) {
694
- existingId = this.phoneToUserIdMap.get(phone);
695
- } else {
696
- this.phoneToUserIdMap.set(phone, newId);
697
- }
698
- }
699
- if (!existingId) {
700
- existingId = newId;
701
- }
702
-
703
- // If existingId is found, add to mergedUserMap
704
- if (existingId) {
705
- userData.data.userId = existingId;
706
- const mergedUsers = this.mergedUserMap.get(existingId) || [];
707
- mergedUsers.push(`${item[primaryKeyField]}`);
708
- this.mergedUserMap.set(existingId, mergedUsers);
709
- }
710
-
711
- // Remove user-specific keys from the transformed item
712
- const userKeys = ["email", "phone", "name", "labels", "prefs"];
713
- userKeys.forEach((key) => {
714
- if (transformedItem.hasOwnProperty(key)) {
715
- delete transformedItem[key];
716
- }
717
- });
718
- const usersMap = this.importMap.get(this.getCollectionKey("users"));
719
- const userDataToAdd = {
720
- rawData: item,
721
- finalData: userData.data,
722
- };
723
- // Directly update the importMap with the new user data, without pushing to usersMap.data first
724
- this.importMap.set(this.getCollectionKey("users"), {
725
- data: [...(usersMap?.data || []), userDataToAdd],
726
- });
727
-
728
- return [transformedItem, existingId, userDataToAdd];
729
- }
730
-
731
- /**
732
- * Prepares the data for creating user collection documents.
733
- * This involves loading the data, transforming it according to the import definition,
734
- * and handling the creation of new unique IDs for each item.
735
- *
736
- * @param db - The database configuration.
737
- * @param collection - The collection configuration.
738
- * @param importDef - The import definition containing the attribute mappings and other relevant info.
739
- */
740
- async prepareUserCollectionCreateData(
741
- db: ConfigDatabase,
742
- collection: CollectionCreate,
743
- importDef: ImportDef
744
- ): Promise<void> {
745
- // Load the raw data based on the import definition
746
- const rawData = this.loadData(importDef);
747
- const operationId = this.collectionImportOperations.get(
748
- this.getCollectionKey(collection.name)
749
- );
750
- // Initialize a new map for old ID to new ID mappings
751
- const oldIdToNewIdMap = new Map<string, string>();
752
- // Retrieve or initialize the collection-specific old ID to new ID map
753
- const collectionOldIdToNewIdMap =
754
- this.oldIdToNewIdPerCollectionMap.get(
755
- this.getCollectionKey(collection.name)
756
- ) ||
757
- this.oldIdToNewIdPerCollectionMap
758
- .set(this.getCollectionKey(collection.name), oldIdToNewIdMap)
759
- .get(this.getCollectionKey(collection.name));
760
- console.log(
761
- `${collection.name} -- collectionOldIdToNewIdMap: ${collectionOldIdToNewIdMap}`
762
- );
763
- if (!operationId) {
764
- throw new Error(
765
- `No import operation found for collection ${collection.name}`
766
- );
767
- }
768
- await updateOperation(this.database, operationId, {
769
- status: "ready",
770
- total: rawData.length,
771
- });
772
- // Retrieve the current user data and the current collection data from the import map
773
- const currentUserData = this.importMap.get(this.getCollectionKey("users"));
774
- const currentData = this.importMap.get(
775
- this.getCollectionKey(collection.name)
776
- );
777
- // Log errors if the necessary data is not found in the import map
778
- if (!currentUserData) {
779
- logger.error(
780
- `No data found for collection ${"users"} for createDef but it says it's supposed to have one...`
781
- );
782
- return;
783
- } else if (!currentData) {
784
- logger.error(
785
- `No data found for collection ${collection.name} for createDef but it says it's supposed to have one...`
786
- );
787
- return;
788
- }
789
- // Iterate through each item in the raw data
790
- for (const item of rawData) {
791
- // Prepare user data, check for duplicates, and remove user-specific keys
792
- let [transformedItem, existingId, userData] = await this.prepareUserData(
793
- item,
794
- importDef.attributeMappings,
795
- importDef.primaryKeyField,
796
- this.getTrueUniqueId(this.getCollectionKey("users"))
797
- );
798
-
799
- logger.info(
800
- `In create user -- transformedItem: ${JSON.stringify(
801
- transformedItem,
802
- null,
803
- 2
804
- )}`
805
- );
806
-
807
- // Generate a new unique ID for the item or use existing ID
808
- if (!existingId) {
809
- // No existing user ID, generate a new unique ID
810
- existingId = this.getTrueUniqueId(this.getCollectionKey("users"));
811
- transformedItem.userId = existingId; // Assign the new ID to the transformed data's userId field
812
- }
813
-
814
- // Create a context object for the item, including the new ID
815
- let context = this.createContext(db, collection, item, existingId);
816
-
817
- // Merge the transformed data into the context
818
- context = { ...context, ...transformedItem };
819
-
820
- // If a primary key field is defined, handle the ID mapping and check for duplicates
821
- if (importDef.primaryKeyField) {
822
- const oldId = item[importDef.primaryKeyField];
823
-
824
- // Check if the oldId already exists to handle potential duplicates
825
- if (
826
- this.oldIdToNewIdPerCollectionMap
827
- .get(this.getCollectionKey(collection.name))
828
- ?.has(`${oldId}`)
829
- ) {
830
- // Found a duplicate oldId, now decide how to merge or handle these duplicates
831
- for (const data of currentData.data) {
832
- if (
833
- data.finalData.docId === oldId ||
834
- data.finalData.userId === oldId
835
- ) {
836
- transformedItem = this.mergeObjects(
837
- data.finalData,
838
- transformedItem
839
- );
840
- }
841
- }
842
- } else {
843
- // No duplicate found, simply map the oldId to the new itemId
844
- collectionOldIdToNewIdMap?.set(`${oldId}`, `${existingId}`);
845
- }
846
- }
847
- // Merge the final user data into the context
848
- context = { ...context, ...userData.finalData };
849
-
850
- // Handle merging for currentUserData
851
- for (let i = 0; i < currentUserData.data.length; i++) {
852
- if (
853
- (currentUserData.data[i].finalData.docId === existingId ||
854
- currentUserData.data[i].finalData.userId === existingId) &&
855
- !_.isEqual(currentUserData.data[i], userData)
856
- ) {
857
- this.mergeObjects(
858
- currentUserData.data[i].finalData,
859
- userData.finalData
860
- );
861
- console.log("Merging user data", currentUserData.data[i].finalData);
862
- this.importMap.set(this.getCollectionKey("users"), currentUserData);
863
- }
864
- }
865
- // Update the attribute mappings with any actions that need to be performed post-import
866
- const mappingsWithActions = this.getAttributeMappingsWithActions(
867
- importDef.attributeMappings,
868
- context,
869
- transformedItem
870
- );
871
- // Update the import definition with the new attribute mappings
872
- const newImportDef = {
873
- ...importDef,
874
- attributeMappings: mappingsWithActions,
875
- };
876
-
877
- let foundData = false;
878
- for (let i = 0; i < currentData.data.length; i++) {
879
- if (
880
- currentData.data[i].finalData.docId === existingId ||
881
- currentData.data[i].finalData.userId === existingId
882
- ) {
883
- currentData.data[i].finalData = this.mergeObjects(
884
- currentData.data[i].finalData,
885
- transformedItem
886
- );
887
- currentData.data[i].context = context;
888
- currentData.data[i].importDef = newImportDef;
889
- this.importMap.set(
890
- this.getCollectionKey(collection.name),
891
- currentData
892
- );
893
- this.oldIdToNewIdPerCollectionMap.set(
894
- this.getCollectionKey(collection.name),
895
- collectionOldIdToNewIdMap!
896
- );
897
- foundData = true;
898
- }
899
- }
900
- if (!foundData) {
901
- // Add new data to the associated collection
902
- currentData.data.push({
903
- rawData: item,
904
- context: context,
905
- importDef: newImportDef,
906
- finalData: transformedItem,
907
- });
908
- this.importMap.set(this.getCollectionKey(collection.name), currentData);
909
- this.oldIdToNewIdPerCollectionMap.set(
910
- this.getCollectionKey(collection.name),
911
- collectionOldIdToNewIdMap!
912
- );
913
- }
914
- }
915
- }
916
-
917
- /**
918
- * Prepares the data for creating documents in a collection.
919
- * This involves loading the data, transforming it, and handling ID mappings.
920
- *
921
- * @param db - The database configuration.
922
- * @param collection - The collection configuration.
923
- * @param importDef - The import definition containing the attribute mappings and other relevant info.
924
- */
925
- async prepareCreateData(
926
- db: ConfigDatabase,
927
- collection: CollectionCreate,
928
- importDef: ImportDef
929
- ): Promise<void> {
930
- // Load the raw data based on the import definition
931
- const rawData = this.loadData(importDef);
932
- const operationId = this.collectionImportOperations.get(
933
- this.getCollectionKey(collection.name)
934
- );
935
- if (!operationId) {
936
- throw new Error(
937
- `No import operation found for collection ${collection.name}`
938
- );
939
- }
940
- await updateOperation(this.database, operationId, {
941
- status: "ready",
942
- total: rawData.length,
943
- });
944
- // Initialize a new map for old ID to new ID mappings
945
- const oldIdToNewIdMapNew = new Map<string, string>();
946
- // Retrieve or initialize the collection-specific old ID to new ID map
947
- const collectionOldIdToNewIdMap =
948
- this.oldIdToNewIdPerCollectionMap.get(
949
- this.getCollectionKey(collection.name)
950
- ) ||
951
- this.oldIdToNewIdPerCollectionMap
952
- .set(this.getCollectionKey(collection.name), oldIdToNewIdMapNew)
953
- .get(this.getCollectionKey(collection.name));
954
- console.log(
955
- `${collection.name} -- collectionOldIdToNewIdMap: ${collectionOldIdToNewIdMap}`
956
- );
957
- // Iterate through each item in the raw data
958
- for (const item of rawData) {
959
- // Generate a new unique ID for the item
960
- const itemIdNew = this.getTrueUniqueId(
961
- this.getCollectionKey(collection.name)
962
- );
963
- // Retrieve the current collection data from the import map
964
- const currentData = this.importMap.get(
965
- this.getCollectionKey(collection.name)
966
- );
967
- // Create a context object for the item, including the new ID
968
- let context = this.createContext(db, collection, item, itemIdNew);
969
- // Transform the item data based on the attribute mappings
970
- const transformedData = this.transformData(
971
- item,
972
- importDef.attributeMappings
973
- );
974
- // If a primary key field is defined, handle the ID mapping and check for duplicates
975
- if (importDef.primaryKeyField) {
976
- const oldId = item[importDef.primaryKeyField];
977
- if (collectionOldIdToNewIdMap?.has(`${oldId}`)) {
978
- logger.error(
979
- `Collection ${collection.name} has multiple documents with the same primary key ${oldId}`
980
- );
981
- continue;
982
- }
983
- collectionOldIdToNewIdMap?.set(`${oldId}`, `${itemIdNew}`);
984
- }
985
- // Merge the transformed data into the context
986
- context = { ...context, ...transformedData };
987
- // Validate the item before proceeding
988
- const isValid = await this.importDataActions.validateItem(
989
- transformedData,
990
- importDef.attributeMappings,
991
- context
992
- );
993
- if (!isValid) {
994
- continue;
995
- }
996
- // Update the attribute mappings with any actions that need to be performed post-import
997
- const mappingsWithActions = this.getAttributeMappingsWithActions(
998
- importDef.attributeMappings,
999
- context,
1000
- transformedData
1001
- );
1002
- // Update the import definition with the new attribute mappings
1003
- const newImportDef = {
1004
- ...importDef,
1005
- attributeMappings: mappingsWithActions,
1006
- };
1007
- // If the current collection data exists, add the item with its context and final data
1008
- if (currentData && currentData.data) {
1009
- currentData.data.push({
1010
- rawData: item,
1011
- context: context,
1012
- importDef: newImportDef,
1013
- finalData: transformedData,
1014
- });
1015
- this.importMap.set(this.getCollectionKey(collection.name), currentData);
1016
- this.oldIdToNewIdPerCollectionMap.set(
1017
- this.getCollectionKey(collection.name),
1018
- collectionOldIdToNewIdMap!
1019
- );
1020
- } else {
1021
- logger.error(
1022
- `No data found for collection ${collection.name} for createDef but it says it's supposed to have one...`
1023
- );
1024
- continue;
1025
- }
1026
- }
1027
- }
1028
- /**
1029
- * Prepares the data for updating documents within a collection.
1030
- * This method loads the raw data based on the import definition, transforms it according to the attribute mappings,
1031
- * finds the new ID for each item based on the primary key or update mapping, and then validates the transformed data.
1032
- * If the data is valid, it updates the import definition with any post-import actions and adds the item to the current collection data.
1033
- *
1034
- * @param db - The database configuration.
1035
- * @param collection - The collection configuration.
1036
- * @param importDef - The import definition containing the attribute mappings and other relevant info.
1037
- */
1038
- async prepareUpdateData(
1039
- db: ConfigDatabase,
1040
- collection: CollectionCreate,
1041
- importDef: ImportDef
1042
- ) {
1043
- // Retrieve the current collection data and old-to-new ID map from the import map
1044
- const currentData = this.importMap.get(
1045
- this.getCollectionKey(collection.name)
1046
- );
1047
- const oldIdToNewIdMap = this.oldIdToNewIdPerCollectionMap.get(
1048
- this.getCollectionKey(collection.name)
1049
- );
1050
- // Log an error and return if no current data is found for the collection
1051
- if (
1052
- !(currentData?.data && currentData?.data.length > 0) &&
1053
- !oldIdToNewIdMap
1054
- ) {
1055
- logger.error(
1056
- `No data found for collection ${collection.name} for updateDef but it says it's supposed to have one...`
1057
- );
1058
- return;
1059
- }
1060
- // Load the raw data based on the import definition
1061
- const rawData = this.loadData(importDef);
1062
- const operationId = this.collectionImportOperations.get(
1063
- this.getCollectionKey(collection.name)
1064
- );
1065
- if (!operationId) {
1066
- throw new Error(
1067
- `No import operation found for collection ${collection.name}`
1068
- );
1069
- }
1070
- for (const item of rawData) {
1071
- // Transform the item data based on the attribute mappings
1072
- let transformedData = this.transformData(
1073
- item,
1074
- importDef.attributeMappings
1075
- );
1076
- let newId: string | undefined;
1077
- let oldId: string | undefined;
1078
- // Determine the new ID for the item based on the primary key field or update mapping
1079
- if (importDef.primaryKeyField) {
1080
- oldId = item[importDef.primaryKeyField];
1081
- } else if (importDef.updateMapping) {
1082
- oldId = item[importDef.updateMapping.originalIdField];
1083
- }
1084
- if (oldId) {
1085
- newId = oldIdToNewIdMap?.get(`${oldId}`);
1086
- if (
1087
- !newId &&
1088
- this.getCollectionKey(this.config.usersCollectionName) ===
1089
- this.getCollectionKey(collection.name)
1090
- ) {
1091
- for (const [key, value] of this.mergedUserMap.entries()) {
1092
- if (value.includes(`${oldId}`)) {
1093
- newId = key;
1094
- break;
1095
- }
1096
- }
1097
- }
1098
- } else {
1099
- logger.error(
1100
- `No old ID found (to update another document with) in prepareUpdateData for ${
1101
- collection.name
1102
- }, ${JSON.stringify(item, null, 2)}`
1103
- );
1104
- continue;
1105
- }
1106
- // Log an error and continue to the next item if no new ID is found
1107
- if (!newId) {
1108
- logger.error(
1109
- `No new id found for collection ${
1110
- collection.name
1111
- } for updateDef ${JSON.stringify(
1112
- item,
1113
- null,
1114
- 2
1115
- )} but it says it's supposed to have one...`
1116
- );
1117
- continue;
1118
- }
1119
- const itemDataToUpdate = this.importMap
1120
- .get(this.getCollectionKey(collection.name))
1121
- ?.data.find(
1122
- (data) => data.rawData[importDef.primaryKeyField] === oldId
1123
- );
1124
- if (!itemDataToUpdate) {
1125
- logger.error(
1126
- `No data found for collection ${
1127
- collection.name
1128
- } for updateDef ${JSON.stringify(
1129
- item,
1130
- null,
1131
- 2
1132
- )} but it says it's supposed to have one...`
1133
- );
1134
- continue;
1135
- }
1136
- transformedData = this.mergeObjects(
1137
- itemDataToUpdate.finalData,
1138
- transformedData
1139
- );
1140
- // Create a context object for the item, including the new ID and transformed data
1141
- let context = this.createContext(db, collection, item, newId);
1142
- context = this.mergeObjects(context, transformedData);
1143
- // Validate the item before proceeding
1144
- const isValid = await this.importDataActions.validateItem(
1145
- item,
1146
- importDef.attributeMappings,
1147
- context
1148
- );
1149
- // Log info and continue to the next item if it's invalid
1150
- if (!isValid) {
1151
- logger.info(
1152
- `Skipping item: ${JSON.stringify(item, null, 2)} because it's invalid`
1153
- );
1154
- continue;
1155
- }
1156
- // Update the attribute mappings with any actions that need to be performed post-import
1157
- const mappingsWithActions = this.getAttributeMappingsWithActions(
1158
- importDef.attributeMappings,
1159
- context,
1160
- transformedData
1161
- );
1162
- // Update the import definition with the new attribute mappings
1163
- const newImportDef = {
1164
- ...importDef,
1165
- attributeMappings: mappingsWithActions,
1166
- };
1167
- // Add the item with its context and final data to the current collection data
1168
- if (itemDataToUpdate) {
1169
- // Update the existing item's finalData and context in place
1170
- itemDataToUpdate.finalData = this.mergeObjects(
1171
- itemDataToUpdate.finalData,
1172
- transformedData
1173
- );
1174
- itemDataToUpdate.context = context;
1175
- itemDataToUpdate.importDef = newImportDef;
1176
- } else {
1177
- // If no existing item matches, then add the new item
1178
- currentData!.data.push({
1179
- rawData: item,
1180
- context: context,
1181
- importDef: newImportDef,
1182
- finalData: transformedData,
1183
- });
1184
- }
1185
- // Since we're modifying currentData in place, we ensure no duplicates are added
1186
- this.importMap.set(this.getCollectionKey(collection.name), currentData!);
1187
- }
1188
- }
1189
-
1190
- private updateReferencesBasedOnAttributeMappings() {
1191
- if (!this.config.collections) {
1192
- return;
1193
- }
1194
- this.config.collections.forEach((collectionConfig) => {
1195
- const collectionName = collectionConfig.name;
1196
- const collectionData = this.importMap.get(
1197
- this.getCollectionKey(collectionName)
1198
- );
1199
-
1200
- if (!collectionData) {
1201
- logger.error(`No data found for collection ${collectionName}`);
1202
- return;
1203
- }
1204
-
1205
- collectionData.data.forEach((dataItem) => {
1206
- collectionConfig.importDefs.forEach((importDef) => {
1207
- if (!importDef.idMappings) return; // Skip collections without idMappings
1208
- importDef.idMappings.forEach((mapping) => {
1209
- if (mapping && mapping.targetField) {
1210
- const idsToUpdate = Array.isArray(
1211
- dataItem[mapping.targetField as keyof typeof dataItem]
1212
- )
1213
- ? dataItem[mapping.targetField as keyof typeof dataItem]
1214
- : [dataItem[mapping.targetField as keyof typeof dataItem]];
1215
- const updatedIds = idsToUpdate.map((id: string) =>
1216
- this.getMergedId(id, mapping.targetCollection)
1217
- );
1218
-
1219
- // Update the dataItem with the new IDs
1220
- dataItem[mapping.targetField as keyof typeof dataItem] =
1221
- Array.isArray(
1222
- dataItem[mapping.targetField as keyof typeof dataItem]
1223
- )
1224
- ? updatedIds
1225
- : updatedIds[0];
1226
- }
1227
- });
1228
- });
1229
- });
1230
- });
1231
- }
1232
-
1233
- private getMergedId(oldId: string, relatedCollectionName: string): string {
1234
- // Retrieve the old to new ID map for the related collection
1235
- const oldToNewIdMap = this.oldIdToNewIdPerCollectionMap.get(
1236
- this.getCollectionKey(relatedCollectionName)
1237
- );
1238
-
1239
- // If there's a mapping for the old ID, return the new ID
1240
- if (oldToNewIdMap && oldToNewIdMap.has(`${oldId}`)) {
1241
- return oldToNewIdMap.get(`${oldId}`)!; // The non-null assertion (!) is used because we checked if the map has the key
1242
- }
1243
-
1244
- // If no mapping is found, return the old ID as a fallback
1245
- return oldId;
1246
- }
1247
-
1248
- /**
1249
- * Generates attribute mappings with post-import actions based on the provided attribute mappings.
1250
- * This method checks each mapping for a fileData attribute and adds a post-import action to create a file
1251
- * and update the field with the file's ID if necessary.
1252
- *
1253
- * @param attributeMappings - The attribute mappings from the import definition.
1254
- * @param context - The context object containing information about the database, collection, and document.
1255
- * @param item - The item being imported, used for resolving template paths in fileData mappings.
1256
- * @returns The attribute mappings updated with any necessary post-import actions.
1257
- */
1258
- getAttributeMappingsWithActions(
1259
- attributeMappings: AttributeMappings,
1260
- context: any,
1261
- item: any
1262
- ) {
1263
- // Iterate over each attribute mapping to check for fileData attributes
1264
- return attributeMappings.map((mapping) => {
1265
- if (mapping.fileData) {
1266
- // Resolve the file path using the provided template, context, and item
1267
- let mappingFilePath = this.importDataActions.resolveTemplate(
1268
- mapping.fileData.path,
1269
- context,
1270
- item
1271
- );
1272
- // Ensure the file path is absolute if it doesn't start with "http"
1273
- if (!mappingFilePath.toLowerCase().startsWith("http")) {
1274
- mappingFilePath = path.resolve(
1275
- this.appwriteFolderPath,
1276
- mappingFilePath
1277
- );
1278
- }
1279
- // Define the after-import action to create a file and update the field
1280
- const afterImportAction = {
1281
- action: "createFileAndUpdateField",
1282
- params: [
1283
- "{dbId}",
1284
- "{collId}",
1285
- "{docId}",
1286
- mapping.targetKey,
1287
- `${this.config!.documentBucketId}_${context.dbName
1288
- .toLowerCase()
1289
- .replace(" ", "")}`, // Assuming 'images' is your bucket ID
1290
- mappingFilePath,
1291
- mapping.fileData.name,
1292
- ],
1293
- };
1294
- // Add the after-import action to the mapping's postImportActions array
1295
- const postImportActions = mapping.postImportActions
1296
- ? [...mapping.postImportActions, afterImportAction]
1297
- : [afterImportAction];
1298
- return { ...mapping, postImportActions };
1299
- }
1300
- // Return the mapping unchanged if no fileData attribute is found
1301
- return mapping;
1302
- });
1303
- }
1304
- }
1
+ import type { ImportDataActions } from "./importDataActions.js";
2
+ import {
3
+ AttributeMappingsSchema,
4
+ CollectionCreateSchema,
5
+ importDefSchema,
6
+ type AppwriteConfig,
7
+ type AttributeMappings,
8
+ type CollectionCreate,
9
+ type ConfigDatabase,
10
+ type IdMapping,
11
+ type ImportDef,
12
+ type ImportDefs,
13
+ type RelationshipAttribute,
14
+ } from "appwrite-utils";
15
+ import path from "path";
16
+ import fs from "fs";
17
+ import { convertObjectByAttributeMappings } from "./converters.js";
18
+ import { z } from "zod";
19
+ import { checkForCollection } from "./collections.js";
20
+ import { ID, Users, type Databases } from "node-appwrite";
21
+ import { logger } from "./logging.js";
22
+ import { findOrCreateOperation, updateOperation } from "./migrationHelper.js";
23
+ import { AuthUserCreateSchema } from "../schemas/authUser.js";
24
+ import _ from "lodash";
25
+ import { UsersController } from "./users.js";
26
+ import { finalizeByAttributeMap } from "../utils/helperFunctions.js";
27
+ // Define a schema for the structure of collection import data using Zod for validation
28
+ export const CollectionImportDataSchema = z.object({
29
+ // Optional collection creation schema
30
+ collection: CollectionCreateSchema.optional(),
31
+ // Array of data objects each containing rawData, finalData, context, and an import definition
32
+ data: z.array(
33
+ z.object({
34
+ rawData: z.any(), // The initial raw data
35
+ finalData: z.any(), // The transformed data ready for import
36
+ context: z.any(), // Additional context for the data transformation
37
+ importDef: importDefSchema.optional(), // The import definition schema
38
+ })
39
+ ),
40
+ });
41
+
42
+ // Infer the TypeScript type from the Zod schema
43
+ export type CollectionImportData = z.infer<typeof CollectionImportDataSchema>;
44
+
45
+ // DataLoader class to handle the loading of data into collections
46
+ export class DataLoader {
47
+ // Private member variables to hold configuration and state
48
+ private appwriteFolderPath: string;
49
+ private importDataActions: ImportDataActions;
50
+ private database: Databases;
51
+ private usersController: UsersController;
52
+ private config: AppwriteConfig;
53
+ // Map to hold the import data for each collection by name
54
+ importMap = new Map<string, CollectionImportData>();
55
+ // Map to track old to new ID mappings for each collection, if applicable
56
+ private oldIdToNewIdPerCollectionMap = new Map<string, Map<string, string>>();
57
+ // Map to hold the import operation ID for each collection
58
+ collectionImportOperations = new Map<string, string>();
59
+ // Map to hold the merged user map for relationship resolution
60
+ // Will hold an array of the old user ID's that are mapped to the same new user ID
61
+ // For example, if there are two users with the same email, they will both be mapped to the same new user ID
62
+ // Prevents duplicate users with the other two maps below it and allows me to keep the old ID's
63
+ private mergedUserMap = new Map<string, string[]>();
64
+ // Maps to hold email and phone to user ID mappings for unique-ness in User Accounts
65
+ private emailToUserIdMap = new Map<string, string>();
66
+ private phoneToUserIdMap = new Map<string, string>();
67
+ private userIdSet = new Set<string>();
68
+ userExistsMap = new Map<string, boolean>();
69
+ private shouldWriteFile = false;
70
+
71
+ // Constructor to initialize the DataLoader with necessary configurations
72
+ constructor(
73
+ appwriteFolderPath: string,
74
+ importDataActions: ImportDataActions,
75
+ database: Databases,
76
+ config: AppwriteConfig,
77
+ shouldWriteFile?: boolean
78
+ ) {
79
+ this.appwriteFolderPath = appwriteFolderPath;
80
+ this.importDataActions = importDataActions;
81
+ this.database = database;
82
+ this.usersController = new UsersController(config, database);
83
+ this.config = config;
84
+ this.shouldWriteFile = shouldWriteFile || false;
85
+ }
86
+
87
+ // Helper method to generate a consistent key for collections
88
+ getCollectionKey(name: string) {
89
+ return name.toLowerCase().replace(" ", "");
90
+ }
91
+
92
+ /**
93
+ * Merges two objects by updating the source object with the target object's values.
94
+ * It iterates through the target object's keys and updates the source object if:
95
+ * - The source object has the key.
96
+ * - The target object's value for that key is not null, undefined, or an empty string.
97
+ * - If the target object has an array value, it concatenates the values and removes duplicates.
98
+ *
99
+ * @param source - The source object to be updated.
100
+ * @param target - The target object with values to update the source object.
101
+ * @returns The updated source object.
102
+ */
103
+ mergeObjects(source: any, update: any): any {
104
+ // Create a new object to hold the merged result
105
+ const result = { ...source };
106
+
107
+ // Loop through the keys of the object we care about
108
+ for (const [key, value] of Object.entries(source)) {
109
+ // Check if the key exists in the target object
110
+ if (!Object.hasOwn(update, key)) {
111
+ // If the key doesn't exist, we can just skip it like bad cheese
112
+ continue;
113
+ }
114
+ if (update[key] === value) {
115
+ continue;
116
+ }
117
+ // If the value ain't here, we can just do whatever man
118
+ if (value === undefined || value === null || value === "") {
119
+ // If the update key is defined
120
+ if (
121
+ update[key] !== undefined &&
122
+ update[key] !== null &&
123
+ update[key] !== ""
124
+ ) {
125
+ // might as well use it eh?
126
+ result[key] = update[key];
127
+ }
128
+ // ELSE if the value is an array, because it would then not be === to those things above
129
+ } else if (Array.isArray(value)) {
130
+ // Get the update value
131
+ const updateValue = update[key];
132
+ // If the update value is an array, concatenate and remove duplicates
133
+ // and poopy data
134
+ if (Array.isArray(updateValue)) {
135
+ result[key] = [...new Set([...value, ...updateValue])].filter(
136
+ (item) => item !== null && item !== undefined && item !== ""
137
+ );
138
+ } else {
139
+ // If the update value is not an array, just use it
140
+ result[key] = [...value, updateValue].filter(
141
+ (item) => item !== null && item !== undefined && item !== ""
142
+ );
143
+ }
144
+ } else if (typeof value === "object" && !Array.isArray(value)) {
145
+ // If the value is an object, we need to merge it
146
+ if (typeof update[key] === "object" && !Array.isArray(update[key])) {
147
+ result[key] = this.mergeObjects(value, update[key]);
148
+ }
149
+ } else {
150
+ // Finally, the source value is defined, and not an array, so we don't care about the update value
151
+ continue;
152
+ }
153
+ }
154
+ // Because the objects should technically always be validated FIRST, we can assume the update keys are also defined on the source object
155
+ for (const [key, value] of Object.entries(update)) {
156
+ if (value === undefined || value === null || value === "") {
157
+ continue;
158
+ } else if (!Object.hasOwn(source, key)) {
159
+ result[key] = value;
160
+ } else if (
161
+ typeof source[key] === "object" &&
162
+ typeof value === "object" &&
163
+ !Array.isArray(source[key]) &&
164
+ !Array.isArray(value)
165
+ ) {
166
+ result[key] = this.mergeObjects(source[key], value);
167
+ } else if (Array.isArray(source[key]) && Array.isArray(value)) {
168
+ result[key] = [...new Set([...source[key], ...value])].filter(
169
+ (item) => item !== null && item !== undefined && item !== ""
170
+ );
171
+ } else if (
172
+ source[key] === undefined ||
173
+ source[key] === null ||
174
+ source[key] === ""
175
+ ) {
176
+ result[key] = value;
177
+ }
178
+ }
179
+
180
+ return result;
181
+ }
182
+
183
+ // Method to load data from a file specified in the import definition
184
+ loadData(importDef: ImportDef): any[] {
185
+ // Resolve the file path and check if the file exists
186
+ const filePath = path.resolve(this.appwriteFolderPath, importDef.filePath);
187
+ if (!fs.existsSync(filePath)) {
188
+ console.error(`File not found: ${filePath}`);
189
+ return [];
190
+ }
191
+
192
+ // Read the file and parse the JSON data
193
+ const rawData = fs.readFileSync(filePath, "utf8");
194
+ return importDef.basePath
195
+ ? JSON.parse(rawData)[importDef.basePath]
196
+ : JSON.parse(rawData);
197
+ }
198
+
199
+ // Helper method to check if a new ID already exists in the old-to-new ID map
200
+ checkMapValuesForId(newId: string, collectionName: string) {
201
+ const oldIdMap = this.oldIdToNewIdPerCollectionMap.get(collectionName);
202
+ for (const [key, value] of oldIdMap?.entries() || []) {
203
+ if (value === newId) {
204
+ return key;
205
+ }
206
+ }
207
+ return false;
208
+ }
209
+
210
+ // Method to generate a unique ID that doesn't conflict with existing IDs
211
+ getTrueUniqueId(collectionName: string) {
212
+ let newId = ID.unique();
213
+ let condition =
214
+ this.checkMapValuesForId(newId, collectionName) ||
215
+ this.userExistsMap.has(newId) ||
216
+ this.userIdSet.has(newId) ||
217
+ this.importMap
218
+ .get(this.getCollectionKey("users"))
219
+ ?.data.some(
220
+ (user) =>
221
+ user.finalData.docId === newId || user.finalData.userId === newId
222
+ );
223
+ while (condition) {
224
+ newId = ID.unique();
225
+ condition =
226
+ this.checkMapValuesForId(newId, collectionName) ||
227
+ this.userExistsMap.has(newId) ||
228
+ this.userIdSet.has(newId) ||
229
+ this.importMap
230
+ .get(this.getCollectionKey("users"))
231
+ ?.data.some(
232
+ (user) =>
233
+ user.finalData.docId === newId || user.finalData.userId === newId
234
+ );
235
+ }
236
+ return newId;
237
+ }
238
+
239
+ // Method to create a context object for data transformation
240
+ createContext(
241
+ db: ConfigDatabase,
242
+ collection: CollectionCreate,
243
+ item: any,
244
+ docId: string
245
+ ) {
246
+ return {
247
+ ...item, // Spread the item data for easy access to its properties
248
+ dbId: db.$id,
249
+ dbName: db.name,
250
+ collId: collection.$id,
251
+ collName: collection.name,
252
+ docId: docId,
253
+ createdDoc: {}, // Initially null, to be updated when the document is created
254
+ };
255
+ }
256
+
257
+ /**
258
+ * Transforms the given item based on the provided attribute mappings.
259
+ * This method applies conversion rules to the item's attributes as defined in the attribute mappings.
260
+ *
261
+ * @param item - The item to be transformed.
262
+ * @param attributeMappings - The mappings that define how each attribute should be transformed.
263
+ * @returns The transformed item.
264
+ */
265
+ transformData(item: any, attributeMappings: AttributeMappings): any {
266
+ // Convert the item using the attribute mappings provided
267
+ const convertedItem = convertObjectByAttributeMappings(
268
+ item,
269
+ attributeMappings
270
+ );
271
+ // Run additional converter functions on the converted item, if any
272
+ return this.importDataActions.runConverterFunctions(
273
+ convertedItem,
274
+ attributeMappings
275
+ );
276
+ }
277
+
278
+ async setupMaps(dbId: string) {
279
+ // Initialize the users collection in the import map
280
+ this.importMap.set(this.getCollectionKey("users"), {
281
+ data: [],
282
+ });
283
+ for (const db of this.config.databases) {
284
+ if (db.$id !== dbId) {
285
+ continue;
286
+ }
287
+ if (!this.config.collections) {
288
+ continue;
289
+ }
290
+ for (let index = 0; index < this.config.collections.length; index++) {
291
+ const collectionConfig = this.config.collections[index];
292
+ let collection = CollectionCreateSchema.parse(collectionConfig);
293
+ // Check if the collection exists in the database
294
+ const collectionExists = await checkForCollection(
295
+ this.database,
296
+ db.$id,
297
+ collection
298
+ );
299
+ if (!collectionExists) {
300
+ logger.error(`No collection found for ${collection.name}`);
301
+ continue;
302
+ } else if (!collection.name) {
303
+ logger.error(`Collection ${collection.name} has no name`);
304
+ continue;
305
+ }
306
+ // Update the collection ID with the existing one
307
+ collectionConfig.$id = collectionExists.$id;
308
+ collection.$id = collectionExists.$id;
309
+ this.config.collections[index] = collectionConfig;
310
+ // Find or create an import operation for the collection
311
+ const collectionImportOperation = await findOrCreateOperation(
312
+ this.database,
313
+ collection.$id,
314
+ "importData"
315
+ );
316
+ // Store the operation ID in the map
317
+ this.collectionImportOperations.set(
318
+ this.getCollectionKey(collection.name),
319
+ collectionImportOperation.$id
320
+ );
321
+ // Initialize the collection in the import map
322
+ this.importMap.set(this.getCollectionKey(collection.name), {
323
+ collection: collection,
324
+ data: [],
325
+ });
326
+ }
327
+ }
328
+ }
329
+
330
+ async getAllUsers() {
331
+ const users = new UsersController(this.config, this.database);
332
+ const allUsers = await users.getAllUsers();
333
+ // Iterate over the users and setup our maps ahead of time for email and phone
334
+ for (const user of allUsers) {
335
+ if (user.email) {
336
+ this.emailToUserIdMap.set(user.email.toLowerCase(), user.$id);
337
+ }
338
+ if (user.phone) {
339
+ this.phoneToUserIdMap.set(user.phone, user.$id);
340
+ }
341
+ this.userExistsMap.set(user.$id, true);
342
+ this.userIdSet.add(user.$id);
343
+ let importData = this.importMap.get(this.getCollectionKey("users"));
344
+ if (!importData) {
345
+ importData = {
346
+ data: [],
347
+ };
348
+ }
349
+ importData.data.push({
350
+ finalData: {
351
+ ...user,
352
+ email: user.email?.toLowerCase(),
353
+ userId: user.$id,
354
+ docId: user.$id,
355
+ },
356
+ context: {
357
+ ...user,
358
+ email: user.email?.toLowerCase(),
359
+ userId: user.$id,
360
+ docId: user.$id,
361
+ },
362
+ rawData: user,
363
+ });
364
+ this.importMap.set(this.getCollectionKey("users"), importData);
365
+ }
366
+ return allUsers;
367
+ }
368
+
369
+ // Main method to start the data loading process for a given database ID
370
+ async start(dbId: string) {
371
+ console.log("---------------------------------");
372
+ console.log(`Starting data setup for database: ${dbId}`);
373
+ console.log("---------------------------------");
374
+ await this.setupMaps(dbId);
375
+ const allUsers = await this.getAllUsers();
376
+ console.log(
377
+ `Fetched ${allUsers.length} users, waiting a few seconds to let the program catch up...`
378
+ );
379
+ await new Promise((resolve) => setTimeout(resolve, 5000));
380
+ // Iterate over the configured databases to find the matching one
381
+ for (const db of this.config.databases) {
382
+ if (db.$id !== dbId) {
383
+ continue;
384
+ }
385
+ if (!this.config.collections) {
386
+ continue;
387
+ }
388
+ // Iterate over the configured collections to process each
389
+ for (const collectionConfig of this.config.collections) {
390
+ const collection = collectionConfig;
391
+ // Determine if this is the users collection
392
+ let isUsersCollection =
393
+ this.getCollectionKey(this.config.usersCollectionName) ===
394
+ this.getCollectionKey(collection.name);
395
+ const collectionDefs = collection.importDefs;
396
+ if (!collectionDefs || !collectionDefs.length) {
397
+ continue;
398
+ }
399
+ // Process create and update definitions for the collection
400
+ const createDefs = collection.importDefs.filter(
401
+ (def: ImportDef) => def.type === "create" || !def.type
402
+ );
403
+ const updateDefs = collection.importDefs.filter(
404
+ (def: ImportDef) => def.type === "update"
405
+ );
406
+ for (const createDef of createDefs) {
407
+ if (!isUsersCollection || !createDef.createUsers) {
408
+ await this.prepareCreateData(db, collection, createDef);
409
+ } else {
410
+ // Special handling for users collection if needed
411
+ await this.prepareUserCollectionCreateData(
412
+ db,
413
+ collection,
414
+ createDef
415
+ );
416
+ }
417
+ }
418
+ for (const updateDef of updateDefs) {
419
+ if (!this.importMap.has(this.getCollectionKey(collection.name))) {
420
+ logger.error(
421
+ `No data found for collection ${collection.name} for updateDef but it says it's supposed to have one...`
422
+ );
423
+ continue;
424
+ }
425
+ // Prepare the update data for the collection
426
+ this.prepareUpdateData(db, collection, updateDef);
427
+ }
428
+ }
429
+ console.log("Running update references");
430
+ // this.dealWithMergedUsers();
431
+ this.updateOldReferencesForNew();
432
+ console.log("Done running update references");
433
+ }
434
+ // for (const collection of this.config.collections) {
435
+ // this.resolveDataItemRelationships(collection);
436
+ // }
437
+ console.log("---------------------------------");
438
+ console.log(`Data setup for database: ${dbId} completed`);
439
+ console.log("---------------------------------");
440
+ if (this.shouldWriteFile) {
441
+ this.writeMapsToJsonFile();
442
+ }
443
+ }
444
+
445
+ /**
446
+ * Deals with merged users by iterating through all collections in the configuration.
447
+ * We have merged users if there are duplicate emails or phones in the import data.
448
+ * This function will iterate through all collections that are the same name as the
449
+ * users collection and pull out their primaryKeyField's. It will then loop through
450
+ * each collection and find any documents that have a
451
+ *
452
+ * @return {void} This function does not return anything.
453
+ */
454
+ // dealWithMergedUsers() {
455
+ // const usersCollectionKey = this.getCollectionKey(
456
+ // this.config.usersCollectionName
457
+ // );
458
+ // const usersCollectionData = this.importMap.get(usersCollectionKey);
459
+
460
+ // if (!this.config.collections) {
461
+ // console.log("No collections found in configuration.");
462
+ // return;
463
+ // }
464
+
465
+ // let needsUpdate = false;
466
+ // let numUpdates = 0;
467
+
468
+ // for (const collectionConfig of this.config.collections) {
469
+ // const collectionKey = this.getCollectionKey(collectionConfig.name);
470
+ // const collectionData = this.importMap.get(collectionKey);
471
+ // const collectionImportDefs = collectionConfig.importDefs;
472
+ // const collectionIdMappings = collectionImportDefs
473
+ // .map((importDef) => importDef.idMappings)
474
+ // .flat()
475
+ // .filter((idMapping) => idMapping !== undefined && idMapping !== null);
476
+ // if (!collectionData || !collectionData.data) continue;
477
+ // for (const dataItem of collectionData.data) {
478
+ // for (const idMapping of collectionIdMappings) {
479
+ // // We know it's the users collection here
480
+ // if (this.getCollectionKey(idMapping.targetCollection) === usersCollectionKey) {
481
+ // const targetFieldKey = idMapping.targetFieldToMatch || idMapping.targetField;
482
+ // if (targetFieldKey === )
483
+ // const targetValue = dataItem.finalData[targetFieldKey];
484
+ // const targetCollectionData = this.importMap.get(this.getCollectionKey(idMapping.targetCollection));
485
+ // if (!targetCollectionData || !targetCollectionData.data) continue;
486
+ // const foundData = targetCollectionData.data.filter(({ context }) => {
487
+ // const targetValue = context[targetFieldKey];
488
+ // const isMatch = `${targetValue}` === `${valueToMatch}`;
489
+ // return isMatch && targetValue !== undefined && targetValue !== null;
490
+ // });
491
+ // }
492
+ // }
493
+ // }
494
+ // }
495
+ // }
496
+
497
+ /**
498
+ * Gets the value to match for a given key in the final data or context.
499
+ * @param finalData - The final data object.
500
+ * @param context - The context object.
501
+ * @param key - The key to get the value for.
502
+ * @returns The value to match for from finalData or Context
503
+ */
504
+ getValueFromData(finalData: any, context: any, key: string) {
505
+ if (
506
+ context[key] !== undefined &&
507
+ context[key] !== null &&
508
+ context[key] !== ""
509
+ ) {
510
+ return context[key];
511
+ }
512
+ return finalData[key];
513
+ }
514
+
515
+ updateOldReferencesForNew() {
516
+ if (!this.config.collections) {
517
+ return;
518
+ }
519
+
520
+ for (const collectionConfig of this.config.collections) {
521
+ const collectionKey = this.getCollectionKey(collectionConfig.name);
522
+ const collectionData = this.importMap.get(collectionKey);
523
+
524
+ if (!collectionData || !collectionData.data) continue;
525
+
526
+ console.log(
527
+ `Updating references for collection: ${collectionConfig.name}`
528
+ );
529
+
530
+ let needsUpdate = false;
531
+
532
+ // Iterate over each data item in the current collection
533
+ for (let i = 0; i < collectionData.data.length; i++) {
534
+ if (collectionConfig.importDefs) {
535
+ for (const importDef of collectionConfig.importDefs) {
536
+ if (importDef.idMappings) {
537
+ for (const idMapping of importDef.idMappings) {
538
+ const targetCollectionKey = this.getCollectionKey(
539
+ idMapping.targetCollection
540
+ );
541
+ const fieldToSetKey =
542
+ idMapping.fieldToSet || idMapping.sourceField;
543
+ const targetFieldKey =
544
+ idMapping.targetFieldToMatch || idMapping.targetField;
545
+ const sourceValue = this.getValueFromData(
546
+ collectionData.data[i].finalData,
547
+ collectionData.data[i].context,
548
+ idMapping.sourceField
549
+ );
550
+
551
+ // Skip if value to match is missing or empty
552
+ if (
553
+ !sourceValue ||
554
+ _.isEmpty(sourceValue) ||
555
+ sourceValue === null
556
+ )
557
+ continue;
558
+
559
+ const isFieldToSetArray = collectionConfig.attributes.find(
560
+ (attribute) => attribute.key === fieldToSetKey
561
+ )?.array;
562
+
563
+ const targetCollectionData =
564
+ this.importMap.get(targetCollectionKey);
565
+ if (!targetCollectionData || !targetCollectionData.data)
566
+ continue;
567
+
568
+ // Handle cases where sourceValue is an array
569
+ const sourceValues = Array.isArray(sourceValue)
570
+ ? sourceValue.map((sourceValue) => `${sourceValue}`)
571
+ : [`${sourceValue}`];
572
+ let newData = [];
573
+
574
+ for (const valueToMatch of sourceValues) {
575
+ // Find matching data in the target collection
576
+ const foundData = targetCollectionData.data.filter(
577
+ ({ context, finalData }) => {
578
+ const targetValue = this.getValueFromData(
579
+ finalData,
580
+ context,
581
+ targetFieldKey
582
+ );
583
+ const isMatch = `${targetValue}` === `${valueToMatch}`;
584
+ // Ensure the targetValue is defined and not null
585
+ return (
586
+ isMatch &&
587
+ targetValue !== undefined &&
588
+ targetValue !== null
589
+ );
590
+ }
591
+ );
592
+
593
+ if (foundData.length) {
594
+ newData.push(
595
+ ...foundData.map((data) => {
596
+ const newValue = this.getValueFromData(
597
+ data.finalData,
598
+ data.context,
599
+ idMapping.targetField
600
+ );
601
+ return newValue;
602
+ })
603
+ );
604
+ } else {
605
+ logger.info(
606
+ `No data found for collection: ${targetCollectionKey} with value: ${valueToMatch} for field: ${fieldToSetKey} -- idMapping: ${JSON.stringify(
607
+ idMapping,
608
+ null,
609
+ 2
610
+ )}`
611
+ );
612
+ }
613
+ continue;
614
+ }
615
+
616
+ const getCurrentDataFiltered = (currentData: any) => {
617
+ if (Array.isArray(currentData.finalData[fieldToSetKey])) {
618
+ return currentData.finalData[fieldToSetKey].filter(
619
+ (data: any) => !sourceValues.includes(`${data}`)
620
+ );
621
+ }
622
+ return currentData.finalData[fieldToSetKey];
623
+ };
624
+
625
+ // Get the current data to be updated
626
+ const currentDataFiltered = getCurrentDataFiltered(
627
+ collectionData.data[i]
628
+ );
629
+
630
+ if (newData.length) {
631
+ needsUpdate = true;
632
+
633
+ // Handle cases where current data is an array
634
+ if (isFieldToSetArray) {
635
+ if (!currentDataFiltered) {
636
+ // Set new data if current data is undefined
637
+ collectionData.data[i].finalData[fieldToSetKey] =
638
+ Array.isArray(newData) ? newData : [newData];
639
+ } else {
640
+ if (Array.isArray(currentDataFiltered)) {
641
+ // Convert current data to array and merge if new data is non-empty array
642
+ collectionData.data[i].finalData[fieldToSetKey] = [
643
+ ...new Set(
644
+ [...currentDataFiltered, ...newData].filter(
645
+ (value: any) =>
646
+ value !== null &&
647
+ value !== undefined &&
648
+ value !== ""
649
+ )
650
+ ),
651
+ ];
652
+ } else {
653
+ // Merge arrays if new data is non-empty array and filter for uniqueness
654
+ collectionData.data[i].finalData[fieldToSetKey] = [
655
+ ...new Set(
656
+ [
657
+ ...(Array.isArray(currentDataFiltered)
658
+ ? currentDataFiltered
659
+ : [currentDataFiltered]),
660
+ ...newData,
661
+ ].filter(
662
+ (value: any) =>
663
+ value !== null &&
664
+ value !== undefined &&
665
+ value !== "" &&
666
+ !sourceValues.includes(`${value}`)
667
+ )
668
+ ),
669
+ ];
670
+ }
671
+ }
672
+ } else {
673
+ if (!currentDataFiltered) {
674
+ // Set new data if current data is undefined
675
+ collectionData.data[i].finalData[fieldToSetKey] =
676
+ Array.isArray(newData) ? newData[0] : newData;
677
+ } else if (Array.isArray(newData) && newData.length > 0) {
678
+ // Convert current data to array and merge if new data is non-empty array, then filter for uniqueness
679
+ // and take the first value, because it's an array and the attribute is not an array
680
+ collectionData.data[i].finalData[fieldToSetKey] = [
681
+ ...new Set(
682
+ [currentDataFiltered, ...newData].filter(
683
+ (value: any) =>
684
+ value !== null &&
685
+ value !== undefined &&
686
+ value !== "" &&
687
+ !sourceValues.includes(`${value}`)
688
+ )
689
+ ),
690
+ ].slice(0, 1)[0];
691
+ } else if (
692
+ !Array.isArray(newData) &&
693
+ newData !== undefined
694
+ ) {
695
+ // Simply update the field if new data is not an array and defined
696
+ collectionData.data[i].finalData[fieldToSetKey] = newData;
697
+ }
698
+ }
699
+ }
700
+ }
701
+ }
702
+ }
703
+ }
704
+ }
705
+
706
+ // Update the import map if any changes were made
707
+ if (needsUpdate) {
708
+ this.importMap.set(collectionKey, collectionData);
709
+ }
710
+ }
711
+ }
712
+
713
+ private writeMapsToJsonFile() {
714
+ const outputDir = path.resolve(process.cwd(), "zlogs");
715
+
716
+ // Ensure the logs directory exists
717
+ if (!fs.existsSync(outputDir)) {
718
+ fs.mkdirSync(outputDir);
719
+ }
720
+
721
+ // Helper function to write data to a file
722
+ const writeToFile = (fileName: string, data: any) => {
723
+ const outputFile = path.join(outputDir, fileName);
724
+ fs.writeFile(outputFile, JSON.stringify(data, null, 2), "utf8", (err) => {
725
+ if (err) {
726
+ console.error(`Error writing data to ${fileName}:`, err);
727
+ return;
728
+ }
729
+ console.log(`Data successfully written to ${fileName}`);
730
+ });
731
+ };
732
+
733
+ // Convert Maps to arrays of entries for serialization
734
+ const oldIdToNewIdPerCollectionMap = Array.from(
735
+ this.oldIdToNewIdPerCollectionMap.entries()
736
+ ).map(([key, value]) => {
737
+ return {
738
+ collection: key,
739
+ data: Array.from(value.entries()),
740
+ };
741
+ });
742
+
743
+ const mergedUserMap = Array.from(this.mergedUserMap.entries());
744
+
745
+ // Write each part to a separate file
746
+ writeToFile(
747
+ "oldIdToNewIdPerCollectionMap.json",
748
+ oldIdToNewIdPerCollectionMap
749
+ );
750
+ writeToFile("mergedUserMap.json", mergedUserMap);
751
+
752
+ // Write each collection's data to a separate file
753
+ this.importMap.forEach((value, key) => {
754
+ const data = {
755
+ collection: key,
756
+ data: value.data.map((item: any) => {
757
+ return {
758
+ finalData: item.finalData,
759
+ context: item.context,
760
+ };
761
+ }),
762
+ };
763
+ writeToFile(`${key}.json`, data);
764
+ });
765
+ }
766
+
767
+ /**
768
+ * Prepares user data by checking for duplicates based on email or phone, adding to a duplicate map if found,
769
+ * and then returning the transformed item without user-specific keys.
770
+ *
771
+ * @param item - The raw item to be processed.
772
+ * @param attributeMappings - The attribute mappings for the item.
773
+ * @returns The transformed item with user-specific keys removed.
774
+ */
775
+ prepareUserData(
776
+ item: any,
777
+ attributeMappings: AttributeMappings,
778
+ primaryKeyField: string,
779
+ newId: string
780
+ ): {
781
+ transformedItem: any;
782
+ existingId: string | undefined;
783
+ userData: {
784
+ rawData: any;
785
+ finalData: z.infer<typeof AuthUserCreateSchema>;
786
+ };
787
+ } {
788
+ if (
789
+ this.userIdSet.has(newId) ||
790
+ this.userExistsMap.has(newId) ||
791
+ Array.from(this.emailToUserIdMap.values()).includes(newId) ||
792
+ Array.from(this.phoneToUserIdMap.values()).includes(newId)
793
+ ) {
794
+ newId = this.getTrueUniqueId(this.getCollectionKey("users"));
795
+ }
796
+ let transformedItem = this.transformData(item, attributeMappings);
797
+ let userData = AuthUserCreateSchema.safeParse(transformedItem);
798
+ if (userData.data?.email) {
799
+ userData.data.email = userData.data.email.toLowerCase();
800
+ }
801
+ if (!userData.success || !(userData.data.email || userData.data.phone)) {
802
+ logger.error(
803
+ `Invalid user data: ${JSON.stringify(
804
+ userData.error?.errors,
805
+ undefined,
806
+ 2
807
+ )} or missing email/phone`
808
+ );
809
+
810
+ const userKeys = ["email", "phone", "name", "labels", "prefs"];
811
+ userKeys.forEach((key) => {
812
+ if (transformedItem.hasOwnProperty(key)) {
813
+ delete transformedItem[key];
814
+ }
815
+ });
816
+ return {
817
+ transformedItem,
818
+ existingId: undefined,
819
+ userData: {
820
+ rawData: item,
821
+ finalData: transformedItem,
822
+ },
823
+ };
824
+ }
825
+ const email = userData.data.email?.toLowerCase();
826
+ const phone = userData.data.phone;
827
+ let existingId: string | undefined;
828
+
829
+ // Check for duplicate email and phone
830
+ if (email && this.emailToUserIdMap.has(email)) {
831
+ existingId = this.emailToUserIdMap.get(email);
832
+ if (phone && !this.phoneToUserIdMap.has(phone)) {
833
+ this.phoneToUserIdMap.set(phone, newId);
834
+ }
835
+ } else if (phone && this.phoneToUserIdMap.has(phone)) {
836
+ existingId = this.phoneToUserIdMap.get(phone);
837
+ if (email && !this.emailToUserIdMap.has(email)) {
838
+ this.emailToUserIdMap.set(email, newId);
839
+ }
840
+ } else {
841
+ if (email) this.emailToUserIdMap.set(email, newId);
842
+ if (phone) this.phoneToUserIdMap.set(phone, newId);
843
+ }
844
+
845
+ if (existingId) {
846
+ userData.data.userId = existingId;
847
+ const mergedUsers = this.mergedUserMap.get(existingId) || [];
848
+ mergedUsers.push(`${item[primaryKeyField]}`);
849
+ this.mergedUserMap.set(existingId, mergedUsers);
850
+ const userFound = this.importMap
851
+ .get(this.getCollectionKey("users"))
852
+ ?.data.find((userDataExisting) => {
853
+ let userIdToMatch: string | undefined;
854
+ if (userDataExisting?.finalData?.userId) {
855
+ userIdToMatch = userDataExisting?.finalData?.userId;
856
+ } else if (userDataExisting?.finalData?.docId) {
857
+ userIdToMatch = userDataExisting?.finalData?.docId;
858
+ } else if (userDataExisting?.context?.userId) {
859
+ userIdToMatch = userDataExisting.context.userId;
860
+ } else if (userDataExisting?.context?.docId) {
861
+ userIdToMatch = userDataExisting.context.docId;
862
+ }
863
+ return userIdToMatch === existingId;
864
+ });
865
+ if (userFound) {
866
+ userFound.finalData.userId = existingId;
867
+ userFound.finalData.docId = existingId;
868
+ this.userIdSet.add(existingId);
869
+ transformedItem = {
870
+ ...transformedItem,
871
+ userId: existingId,
872
+ docId: existingId,
873
+ };
874
+ }
875
+
876
+ const userKeys = ["email", "phone", "name", "labels", "prefs"];
877
+ userKeys.forEach((key) => {
878
+ if (transformedItem.hasOwnProperty(key)) {
879
+ delete transformedItem[key];
880
+ }
881
+ });
882
+ return {
883
+ transformedItem,
884
+ existingId,
885
+ userData: {
886
+ rawData: userFound?.rawData,
887
+ finalData: userFound?.finalData,
888
+ },
889
+ };
890
+ } else {
891
+ existingId = newId;
892
+ userData.data.userId = existingId;
893
+ }
894
+
895
+ const userKeys = ["email", "phone", "name", "labels", "prefs"];
896
+ userKeys.forEach((key) => {
897
+ if (transformedItem.hasOwnProperty(key)) {
898
+ delete transformedItem[key];
899
+ }
900
+ });
901
+
902
+ const usersMap = this.importMap.get(this.getCollectionKey("users"));
903
+ const userDataToAdd = {
904
+ rawData: item,
905
+ finalData: userData.data,
906
+ };
907
+ this.importMap.set(this.getCollectionKey("users"), {
908
+ data: [...(usersMap?.data || []), userDataToAdd],
909
+ });
910
+ this.userIdSet.add(existingId);
911
+
912
+ return {
913
+ transformedItem,
914
+ existingId,
915
+ userData: userDataToAdd,
916
+ };
917
+ }
918
+
919
+ /**
920
+ * Prepares the data for creating user collection documents.
921
+ * This involves loading the data, transforming it according to the import definition,
922
+ * and handling the creation of new unique IDs for each item.
923
+ *
924
+ * @param db - The database configuration.
925
+ * @param collection - The collection configuration.
926
+ * @param importDef - The import definition containing the attribute mappings and other relevant info.
927
+ */
928
+ async prepareUserCollectionCreateData(
929
+ db: ConfigDatabase,
930
+ collection: CollectionCreate,
931
+ importDef: ImportDef
932
+ ): Promise<void> {
933
+ // Load the raw data based on the import definition
934
+ const rawData = this.loadData(importDef);
935
+ const operationId = this.collectionImportOperations.get(
936
+ this.getCollectionKey(collection.name)
937
+ );
938
+ // Initialize a new map for old ID to new ID mappings
939
+ const oldIdToNewIdMap = new Map<string, string>();
940
+ // Retrieve or initialize the collection-specific old ID to new ID map
941
+ const collectionOldIdToNewIdMap =
942
+ this.oldIdToNewIdPerCollectionMap.get(
943
+ this.getCollectionKey(collection.name)
944
+ ) ||
945
+ this.oldIdToNewIdPerCollectionMap
946
+ .set(this.getCollectionKey(collection.name), oldIdToNewIdMap)
947
+ .get(this.getCollectionKey(collection.name));
948
+ if (!operationId) {
949
+ throw new Error(
950
+ `No import operation found for collection ${collection.name}`
951
+ );
952
+ }
953
+ await updateOperation(this.database, operationId, {
954
+ status: "ready",
955
+ total: rawData.length,
956
+ });
957
+ // Retrieve the current user data and the current collection data from the import map
958
+ const currentUserData = this.importMap.get(this.getCollectionKey("users"));
959
+ const currentData = this.importMap.get(
960
+ this.getCollectionKey(collection.name)
961
+ );
962
+ // Log errors if the necessary data is not found in the import map
963
+ if (!currentUserData) {
964
+ logger.error(
965
+ `No data found for collection ${"users"} for createDef but it says it's supposed to have one...`
966
+ );
967
+ return;
968
+ } else if (!currentData) {
969
+ logger.error(
970
+ `No data found for collection ${collection.name} for createDef but it says it's supposed to have one...`
971
+ );
972
+ return;
973
+ }
974
+ // Iterate through each item in the raw data
975
+ for (const item of rawData) {
976
+ // Prepare user data, check for duplicates, and remove user-specific keys
977
+ let { transformedItem, existingId, userData } = this.prepareUserData(
978
+ item,
979
+ importDef.attributeMappings,
980
+ importDef.primaryKeyField,
981
+ this.getTrueUniqueId(this.getCollectionKey("users"))
982
+ );
983
+
984
+ logger.info(
985
+ `In create user -- transformedItem: ${JSON.stringify(
986
+ transformedItem,
987
+ null,
988
+ 2
989
+ )}`
990
+ );
991
+
992
+ // Generate a new unique ID for the item or use existing ID
993
+ if (!existingId && !userData.finalData?.userId) {
994
+ // No existing user ID, generate a new unique ID
995
+ existingId = this.getTrueUniqueId(
996
+ this.getCollectionKey(collection.name)
997
+ );
998
+ transformedItem = {
999
+ ...transformedItem,
1000
+ userId: existingId,
1001
+ docId: existingId,
1002
+ };
1003
+ } else if (!existingId && userData.finalData?.userId) {
1004
+ // Existing user ID, use it as the new ID
1005
+ existingId = userData.finalData.userId;
1006
+ transformedItem = {
1007
+ ...transformedItem,
1008
+ userId: existingId,
1009
+ docId: existingId,
1010
+ };
1011
+ }
1012
+
1013
+ // Create a context object for the item, including the new ID
1014
+ let context = this.createContext(db, collection, item, existingId!);
1015
+
1016
+ // Merge the transformed data into the context
1017
+ context = { ...context, ...transformedItem, ...userData.finalData };
1018
+
1019
+ // If a primary key field is defined, handle the ID mapping and check for duplicates
1020
+ if (importDef.primaryKeyField) {
1021
+ const oldId = item[importDef.primaryKeyField];
1022
+
1023
+ // Check if the oldId already exists to handle potential duplicates
1024
+ if (
1025
+ this.oldIdToNewIdPerCollectionMap
1026
+ .get(this.getCollectionKey(collection.name))
1027
+ ?.has(`${oldId}`)
1028
+ ) {
1029
+ // Found a duplicate oldId, now decide how to merge or handle these duplicates
1030
+ for (const data of currentData.data) {
1031
+ if (
1032
+ data.finalData.docId === oldId ||
1033
+ data.finalData.userId === oldId ||
1034
+ data.context.docId === oldId ||
1035
+ data.context.userId === oldId
1036
+ ) {
1037
+ transformedItem = this.mergeObjects(
1038
+ data.finalData,
1039
+ transformedItem
1040
+ );
1041
+ }
1042
+ }
1043
+ } else {
1044
+ // No duplicate found, simply map the oldId to the new itemId
1045
+ collectionOldIdToNewIdMap?.set(`${oldId}`, `${existingId}`);
1046
+ }
1047
+ }
1048
+
1049
+ // Handle merging for currentUserData
1050
+ for (let i = 0; i < currentUserData.data.length; i++) {
1051
+ const currentUserDataItem = currentUserData.data[i];
1052
+ const samePhones =
1053
+ currentUserDataItem.finalData.phone &&
1054
+ transformedItem.phone &&
1055
+ currentUserDataItem.finalData.phone === transformedItem.phone;
1056
+ const sameEmails =
1057
+ currentUserDataItem.finalData.email &&
1058
+ transformedItem.email &&
1059
+ currentUserDataItem.finalData.email === transformedItem.email;
1060
+ if (
1061
+ (currentUserDataItem.finalData.docId === existingId ||
1062
+ currentUserDataItem.finalData.userId === existingId) &&
1063
+ (samePhones || sameEmails) &&
1064
+ currentUserDataItem.finalData &&
1065
+ userData.finalData
1066
+ ) {
1067
+ const userDataMerged = this.mergeObjects(
1068
+ currentUserData.data[i].finalData,
1069
+ userData.finalData
1070
+ );
1071
+ currentUserData.data[i].finalData = userDataMerged;
1072
+ this.importMap.set(this.getCollectionKey("users"), currentUserData);
1073
+ }
1074
+ }
1075
+ // Update the attribute mappings with any actions that need to be performed post-import
1076
+ const mappingsWithActions = this.getAttributeMappingsWithActions(
1077
+ importDef.attributeMappings,
1078
+ context,
1079
+ transformedItem
1080
+ );
1081
+ // Update the import definition with the new attribute mappings
1082
+ const newImportDef = {
1083
+ ...importDef,
1084
+ attributeMappings: mappingsWithActions,
1085
+ };
1086
+
1087
+ const updatedData = this.importMap.get(
1088
+ this.getCollectionKey(collection.name)
1089
+ )!;
1090
+
1091
+ let foundData = false;
1092
+ for (let i = 0; i < updatedData.data.length; i++) {
1093
+ if (
1094
+ updatedData.data[i].finalData.docId === existingId ||
1095
+ updatedData.data[i].finalData.userId === existingId ||
1096
+ updatedData.data[i].context.docId === existingId ||
1097
+ updatedData.data[i].context.userId === existingId
1098
+ ) {
1099
+ updatedData.data[i].finalData = this.mergeObjects(
1100
+ updatedData.data[i].finalData,
1101
+ transformedItem
1102
+ );
1103
+ updatedData.data[i].context = this.mergeObjects(
1104
+ updatedData.data[i].context,
1105
+ context
1106
+ );
1107
+ const mergedImportDef = {
1108
+ ...updatedData.data[i].importDef,
1109
+ idMappings: [
1110
+ ...(updatedData.data[i].importDef?.idMappings || []),
1111
+ ...(newImportDef.idMappings || []),
1112
+ ],
1113
+ attributeMappings: [
1114
+ ...(updatedData.data[i].importDef?.attributeMappings || []),
1115
+ ...(newImportDef.attributeMappings || []),
1116
+ ],
1117
+ };
1118
+ updatedData.data[i].importDef = mergedImportDef as ImportDef;
1119
+ this.importMap.set(
1120
+ this.getCollectionKey(collection.name),
1121
+ updatedData
1122
+ );
1123
+ this.oldIdToNewIdPerCollectionMap.set(
1124
+ this.getCollectionKey(collection.name),
1125
+ collectionOldIdToNewIdMap!
1126
+ );
1127
+
1128
+ foundData = true;
1129
+ }
1130
+ }
1131
+ if (!foundData) {
1132
+ // Add new data to the associated collection
1133
+ updatedData.data.push({
1134
+ rawData: item,
1135
+ context: context,
1136
+ importDef: newImportDef,
1137
+ finalData: transformedItem,
1138
+ });
1139
+ this.importMap.set(this.getCollectionKey(collection.name), updatedData);
1140
+ this.oldIdToNewIdPerCollectionMap.set(
1141
+ this.getCollectionKey(collection.name),
1142
+ collectionOldIdToNewIdMap!
1143
+ );
1144
+ }
1145
+ }
1146
+ }
1147
+
1148
+ /**
1149
+ * Prepares the data for creating documents in a collection.
1150
+ * This involves loading the data, transforming it, and handling ID mappings.
1151
+ *
1152
+ * @param db - The database configuration.
1153
+ * @param collection - The collection configuration.
1154
+ * @param importDef - The import definition containing the attribute mappings and other relevant info.
1155
+ */
1156
+ async prepareCreateData(
1157
+ db: ConfigDatabase,
1158
+ collection: CollectionCreate,
1159
+ importDef: ImportDef
1160
+ ): Promise<void> {
1161
+ // Load the raw data based on the import definition
1162
+ const rawData = this.loadData(importDef);
1163
+ const operationId = this.collectionImportOperations.get(
1164
+ this.getCollectionKey(collection.name)
1165
+ );
1166
+ if (!operationId) {
1167
+ throw new Error(
1168
+ `No import operation found for collection ${collection.name}`
1169
+ );
1170
+ }
1171
+ await updateOperation(this.database, operationId, {
1172
+ status: "ready",
1173
+ total: rawData.length,
1174
+ });
1175
+ // Initialize a new map for old ID to new ID mappings
1176
+ const oldIdToNewIdMapNew = new Map<string, string>();
1177
+ // Retrieve or initialize the collection-specific old ID to new ID map
1178
+ const collectionOldIdToNewIdMap =
1179
+ this.oldIdToNewIdPerCollectionMap.get(
1180
+ this.getCollectionKey(collection.name)
1181
+ ) ||
1182
+ this.oldIdToNewIdPerCollectionMap
1183
+ .set(this.getCollectionKey(collection.name), oldIdToNewIdMapNew)
1184
+ .get(this.getCollectionKey(collection.name));
1185
+ const isRegions = collection.name.toLowerCase() === "regions";
1186
+ // Iterate through each item in the raw data
1187
+ for (const item of rawData) {
1188
+ // Generate a new unique ID for the item
1189
+ const itemIdNew = this.getTrueUniqueId(
1190
+ this.getCollectionKey(collection.name)
1191
+ );
1192
+ if (isRegions) {
1193
+ logger.info(`Creating region: ${JSON.stringify(item, null, 2)}`);
1194
+ }
1195
+ // Retrieve the current collection data from the import map
1196
+ const currentData = this.importMap.get(
1197
+ this.getCollectionKey(collection.name)
1198
+ );
1199
+ // Create a context object for the item, including the new ID
1200
+ let context = this.createContext(db, collection, item, itemIdNew);
1201
+ // Transform the item data based on the attribute mappings
1202
+ let transformedData = this.transformData(
1203
+ item,
1204
+ importDef.attributeMappings
1205
+ );
1206
+ // If a primary key field is defined, handle the ID mapping and check for duplicates
1207
+ if (importDef.primaryKeyField) {
1208
+ const oldId = item[importDef.primaryKeyField];
1209
+ if (collectionOldIdToNewIdMap?.has(`${oldId}`)) {
1210
+ logger.error(
1211
+ `Collection ${collection.name} has multiple documents with the same primary key ${oldId}`
1212
+ );
1213
+ continue;
1214
+ }
1215
+ collectionOldIdToNewIdMap?.set(`${oldId}`, `${itemIdNew}`);
1216
+ }
1217
+ // Merge the transformed data into the context
1218
+ context = { ...context, ...transformedData };
1219
+ // Validate the item before proceeding
1220
+ const isValid = this.importDataActions.validateItem(
1221
+ transformedData,
1222
+ importDef.attributeMappings,
1223
+ context
1224
+ );
1225
+ if (!isValid) {
1226
+ continue;
1227
+ }
1228
+ // Update the attribute mappings with any actions that need to be performed post-import
1229
+ const mappingsWithActions = this.getAttributeMappingsWithActions(
1230
+ importDef.attributeMappings,
1231
+ context,
1232
+ transformedData
1233
+ );
1234
+ // Update the import definition with the new attribute mappings
1235
+ const newImportDef = {
1236
+ ...importDef,
1237
+ attributeMappings: mappingsWithActions,
1238
+ };
1239
+ // If the current collection data exists, add the item with its context and final data
1240
+ if (currentData && currentData.data) {
1241
+ currentData.data.push({
1242
+ rawData: item,
1243
+ context: context,
1244
+ importDef: newImportDef,
1245
+ finalData: transformedData,
1246
+ });
1247
+ this.importMap.set(this.getCollectionKey(collection.name), currentData);
1248
+ this.oldIdToNewIdPerCollectionMap.set(
1249
+ this.getCollectionKey(collection.name),
1250
+ collectionOldIdToNewIdMap!
1251
+ );
1252
+ } else {
1253
+ logger.error(
1254
+ `No data found for collection ${collection.name} for createDef but it says it's supposed to have one...`
1255
+ );
1256
+ continue;
1257
+ }
1258
+ }
1259
+ }
1260
+
1261
+ /**
1262
+ * Prepares the data for updating documents within a collection.
1263
+ * This method loads the raw data based on the import definition, transforms it according to the attribute mappings,
1264
+ * finds the new ID for each item based on the primary key or update mapping, and then validates the transformed data.
1265
+ * If the data is valid, it updates the import definition with any post-import actions and adds the item to the current collection data.
1266
+ *
1267
+ * @param db - The database configuration.
1268
+ * @param collection - The collection configuration.
1269
+ * @param importDef - The import definition containing the attribute mappings and other relevant info.
1270
+ */
1271
+ async prepareUpdateData(
1272
+ db: ConfigDatabase,
1273
+ collection: CollectionCreate,
1274
+ importDef: ImportDef
1275
+ ) {
1276
+ const currentData = this.importMap.get(
1277
+ this.getCollectionKey(collection.name)
1278
+ );
1279
+ const oldIdToNewIdMap = this.oldIdToNewIdPerCollectionMap.get(
1280
+ this.getCollectionKey(collection.name)
1281
+ );
1282
+
1283
+ if (
1284
+ !(currentData?.data && currentData?.data.length > 0) &&
1285
+ !oldIdToNewIdMap
1286
+ ) {
1287
+ logger.error(
1288
+ `No data found for collection ${collection.name} for updateDef but it says it's supposed to have one...`
1289
+ );
1290
+ return;
1291
+ }
1292
+
1293
+ const rawData = this.loadData(importDef);
1294
+ const operationId = this.collectionImportOperations.get(
1295
+ this.getCollectionKey(collection.name)
1296
+ );
1297
+ if (!operationId) {
1298
+ throw new Error(
1299
+ `No import operation found for collection ${collection.name}`
1300
+ );
1301
+ }
1302
+
1303
+ for (const item of rawData) {
1304
+ let transformedData = this.transformData(
1305
+ item,
1306
+ importDef.attributeMappings
1307
+ );
1308
+ let newId: string | undefined;
1309
+ let oldId: string | undefined;
1310
+ let itemDataToUpdate: CollectionImportData["data"][number] | undefined;
1311
+
1312
+ // Try to find itemDataToUpdate using updateMapping
1313
+ if (importDef.updateMapping) {
1314
+ oldId =
1315
+ item[importDef.updateMapping.originalIdField] ||
1316
+ transformedData[importDef.updateMapping.originalIdField];
1317
+ if (oldId) {
1318
+ itemDataToUpdate = currentData?.data.find(
1319
+ ({ context, finalData }) => {
1320
+ const targetField =
1321
+ importDef.updateMapping!.targetField ??
1322
+ importDef.updateMapping!.originalIdField;
1323
+
1324
+ return (
1325
+ `${context[targetField]}` === `${oldId}` ||
1326
+ `${finalData[targetField]}` === `${oldId}`
1327
+ );
1328
+ }
1329
+ );
1330
+
1331
+ if (itemDataToUpdate) {
1332
+ newId = itemDataToUpdate.context.docId;
1333
+ }
1334
+ }
1335
+ }
1336
+
1337
+ // If updateMapping is not defined or did not find the item, use primaryKeyField
1338
+ if (!itemDataToUpdate && importDef.primaryKeyField) {
1339
+ oldId =
1340
+ item[importDef.primaryKeyField] ||
1341
+ transformedData[importDef.primaryKeyField];
1342
+ if (oldId && oldId.length > 0) {
1343
+ newId = oldIdToNewIdMap?.get(`${oldId}`);
1344
+ if (
1345
+ !newId &&
1346
+ this.getCollectionKey(this.config.usersCollectionName) ===
1347
+ this.getCollectionKey(collection.name)
1348
+ ) {
1349
+ for (const [key, value] of this.mergedUserMap.entries()) {
1350
+ if (value.includes(`${oldId}`)) {
1351
+ newId = key;
1352
+ break;
1353
+ }
1354
+ }
1355
+ }
1356
+ }
1357
+
1358
+ if (oldId && !itemDataToUpdate) {
1359
+ itemDataToUpdate = currentData?.data.find(
1360
+ (data) =>
1361
+ `${data.context[importDef.primaryKeyField]}` === `${oldId}`
1362
+ );
1363
+ }
1364
+ }
1365
+
1366
+ if (!oldId) {
1367
+ logger.error(
1368
+ `No old ID found (to update another document with) in prepareUpdateData for ${
1369
+ collection.name
1370
+ }, ${JSON.stringify(item, null, 2)}`
1371
+ );
1372
+ continue;
1373
+ }
1374
+
1375
+ if (!newId && !itemDataToUpdate) {
1376
+ logger.error(
1377
+ `No new id && no data found for collection ${
1378
+ collection.name
1379
+ } for updateDef ${JSON.stringify(
1380
+ item,
1381
+ null,
1382
+ 2
1383
+ )} but it says it's supposed to have one...`
1384
+ );
1385
+ continue;
1386
+ } else if (itemDataToUpdate) {
1387
+ newId =
1388
+ itemDataToUpdate.finalData.docId || itemDataToUpdate.context.docId;
1389
+ if (!newId) {
1390
+ logger.error(
1391
+ `No new id found for collection ${
1392
+ collection.name
1393
+ } for updateDef ${JSON.stringify(
1394
+ item,
1395
+ null,
1396
+ 2
1397
+ )} but has itemDataToUpdate ${JSON.stringify(
1398
+ itemDataToUpdate,
1399
+ null,
1400
+ 2
1401
+ )} but it says it's supposed to have one...`
1402
+ );
1403
+ continue;
1404
+ }
1405
+ }
1406
+
1407
+ if (!itemDataToUpdate || !newId) {
1408
+ logger.error(
1409
+ `No data or ID (docId) found for collection ${
1410
+ collection.name
1411
+ } for updateDef ${JSON.stringify(
1412
+ item,
1413
+ null,
1414
+ 2
1415
+ )} but it says it's supposed to have one...`
1416
+ );
1417
+ continue;
1418
+ }
1419
+
1420
+ transformedData = this.mergeObjects(
1421
+ itemDataToUpdate.finalData,
1422
+ transformedData
1423
+ );
1424
+
1425
+ // Create a context object for the item, including the new ID and transformed data
1426
+ let context = itemDataToUpdate.context;
1427
+ context = this.mergeObjects(context, transformedData);
1428
+
1429
+ // Validate the item before proceeding
1430
+ const isValid = this.importDataActions.validateItem(
1431
+ item,
1432
+ importDef.attributeMappings,
1433
+ context
1434
+ );
1435
+
1436
+ if (!isValid) {
1437
+ logger.info(
1438
+ `Skipping item: ${JSON.stringify(item, null, 2)} because it's invalid`
1439
+ );
1440
+ continue;
1441
+ }
1442
+
1443
+ // Update the attribute mappings with any actions that need to be performed post-import
1444
+ const mappingsWithActions = this.getAttributeMappingsWithActions(
1445
+ importDef.attributeMappings,
1446
+ context,
1447
+ transformedData
1448
+ );
1449
+
1450
+ // Update the import definition with the new attribute mappings
1451
+ const newImportDef = {
1452
+ ...importDef,
1453
+ attributeMappings: mappingsWithActions,
1454
+ };
1455
+
1456
+ if (itemDataToUpdate) {
1457
+ itemDataToUpdate.finalData = this.mergeObjects(
1458
+ itemDataToUpdate.finalData,
1459
+ transformedData
1460
+ );
1461
+ itemDataToUpdate.context = context;
1462
+ // Merge existing importDef with new importDef, focusing only on postImportActions
1463
+ itemDataToUpdate.importDef = {
1464
+ ...itemDataToUpdate.importDef,
1465
+ attributeMappings:
1466
+ itemDataToUpdate.importDef?.attributeMappings.map(
1467
+ (attrMapping, index) => ({
1468
+ ...attrMapping,
1469
+ postImportActions: [
1470
+ ...(attrMapping.postImportActions || []),
1471
+ ...(newImportDef.attributeMappings[index]
1472
+ ?.postImportActions || []),
1473
+ ],
1474
+ })
1475
+ ) || [],
1476
+ } as ImportDef;
1477
+
1478
+ if (collection.name.toLowerCase() === "councils") {
1479
+ console.log(
1480
+ `Mappings in update councils: ${JSON.stringify(
1481
+ itemDataToUpdate.importDef.attributeMappings,
1482
+ null,
1483
+ 2
1484
+ )}`
1485
+ );
1486
+ }
1487
+ } else {
1488
+ currentData!.data.push({
1489
+ rawData: item,
1490
+ context: context,
1491
+ importDef: newImportDef,
1492
+ finalData: transformedData,
1493
+ });
1494
+ }
1495
+
1496
+ // Since we're modifying currentData in place, we ensure no duplicates are added
1497
+ this.importMap.set(this.getCollectionKey(collection.name), currentData!);
1498
+ }
1499
+ }
1500
+
1501
+ private updateReferencesBasedOnAttributeMappings() {
1502
+ if (!this.config.collections) {
1503
+ return;
1504
+ }
1505
+ this.config.collections.forEach((collectionConfig) => {
1506
+ const collectionName = collectionConfig.name;
1507
+ const collectionData = this.importMap.get(
1508
+ this.getCollectionKey(collectionName)
1509
+ );
1510
+
1511
+ if (!collectionData) {
1512
+ logger.error(`No data found for collection ${collectionName}`);
1513
+ return;
1514
+ }
1515
+
1516
+ collectionData.data.forEach((dataItem) => {
1517
+ collectionConfig.importDefs.forEach((importDef) => {
1518
+ if (!importDef.idMappings) return; // Skip collections without idMappings
1519
+ importDef.idMappings.forEach((mapping) => {
1520
+ if (mapping && mapping.targetField) {
1521
+ const idsToUpdate = Array.isArray(
1522
+ dataItem[mapping.targetField as keyof typeof dataItem]
1523
+ )
1524
+ ? dataItem[mapping.targetField as keyof typeof dataItem]
1525
+ : [dataItem[mapping.targetField as keyof typeof dataItem]];
1526
+ const updatedIds = idsToUpdate.map((id: string) =>
1527
+ this.getMergedId(id, mapping.targetCollection)
1528
+ );
1529
+
1530
+ // Update the dataItem with the new IDs
1531
+ dataItem[mapping.targetField as keyof typeof dataItem] =
1532
+ Array.isArray(
1533
+ dataItem[mapping.targetField as keyof typeof dataItem]
1534
+ )
1535
+ ? updatedIds
1536
+ : updatedIds[0];
1537
+ }
1538
+ });
1539
+ });
1540
+ });
1541
+ });
1542
+ }
1543
+
1544
+ private getMergedId(oldId: string, relatedCollectionName: string): string {
1545
+ // Retrieve the old to new ID map for the related collection
1546
+ const oldToNewIdMap = this.oldIdToNewIdPerCollectionMap.get(
1547
+ this.getCollectionKey(relatedCollectionName)
1548
+ );
1549
+
1550
+ // If there's a mapping for the old ID, return the new ID
1551
+ if (oldToNewIdMap && oldToNewIdMap.has(`${oldId}`)) {
1552
+ return oldToNewIdMap.get(`${oldId}`)!; // The non-null assertion (!) is used because we checked if the map has the key
1553
+ }
1554
+
1555
+ // If no mapping is found, return the old ID as a fallback
1556
+ return oldId;
1557
+ }
1558
+
1559
+ /**
1560
+ * Generates attribute mappings with post-import actions based on the provided attribute mappings.
1561
+ * This method checks each mapping for a fileData attribute and adds a post-import action to create a file
1562
+ * and update the field with the file's ID if necessary.
1563
+ *
1564
+ * @param attributeMappings - The attribute mappings from the import definition.
1565
+ * @param context - The context object containing information about the database, collection, and document.
1566
+ * @param item - The item being imported, used for resolving template paths in fileData mappings.
1567
+ * @returns The attribute mappings updated with any necessary post-import actions.
1568
+ */
1569
+ getAttributeMappingsWithActions(
1570
+ attributeMappings: AttributeMappings,
1571
+ context: any,
1572
+ item: any
1573
+ ) {
1574
+ // Iterate over each attribute mapping to check for fileData attributes
1575
+ return attributeMappings.map((mapping) => {
1576
+ if (mapping.fileData) {
1577
+ // Resolve the file path using the provided template, context, and item
1578
+ let mappingFilePath = this.importDataActions.resolveTemplate(
1579
+ mapping.fileData.path,
1580
+ context,
1581
+ item
1582
+ );
1583
+ // Ensure the file path is absolute if it doesn't start with "http"
1584
+ if (!mappingFilePath.toLowerCase().startsWith("http")) {
1585
+ mappingFilePath = path.resolve(
1586
+ this.appwriteFolderPath,
1587
+ mappingFilePath
1588
+ );
1589
+ }
1590
+ // Define the after-import action to create a file and update the field
1591
+ const afterImportAction = {
1592
+ action: "createFileAndUpdateField",
1593
+ params: [
1594
+ "{dbId}",
1595
+ "{collId}",
1596
+ "{docId}",
1597
+ mapping.targetKey,
1598
+ `${this.config!.documentBucketId}_${context.dbName
1599
+ .toLowerCase()
1600
+ .replace(" ", "")}`, // Assuming 'images' is your bucket ID
1601
+ mappingFilePath,
1602
+ mapping.fileData.name,
1603
+ ],
1604
+ };
1605
+ // Add the after-import action to the mapping's postImportActions array
1606
+ const postImportActions = mapping.postImportActions
1607
+ ? [...mapping.postImportActions, afterImportAction]
1608
+ : [afterImportAction];
1609
+ return { ...mapping, postImportActions };
1610
+ }
1611
+ // Return the mapping unchanged if no fileData attribute is found
1612
+ return mapping;
1613
+ });
1614
+ }
1615
+ }