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.
- package/README.md +122 -96
- package/dist/collections/attributes.d.ts +4 -0
- package/dist/collections/attributes.js +224 -0
- package/dist/collections/indexes.d.ts +4 -0
- package/dist/collections/indexes.js +27 -0
- package/dist/collections/methods.d.ts +16 -0
- package/dist/collections/methods.js +216 -0
- package/dist/databases/methods.d.ts +6 -0
- package/dist/databases/methods.js +33 -0
- package/dist/interactiveCLI.d.ts +19 -0
- package/dist/interactiveCLI.js +555 -0
- package/dist/main.js +227 -62
- package/dist/migrations/afterImportActions.js +37 -40
- package/dist/migrations/appwriteToX.d.ts +26 -25
- package/dist/migrations/appwriteToX.js +42 -6
- package/dist/migrations/attributes.js +21 -20
- package/dist/migrations/backup.d.ts +93 -87
- package/dist/migrations/collections.d.ts +6 -0
- package/dist/migrations/collections.js +149 -20
- package/dist/migrations/converters.d.ts +2 -18
- package/dist/migrations/converters.js +13 -2
- package/dist/migrations/dataLoader.d.ts +276 -161
- package/dist/migrations/dataLoader.js +535 -292
- package/dist/migrations/databases.js +8 -2
- package/dist/migrations/helper.d.ts +3 -0
- package/dist/migrations/helper.js +21 -0
- package/dist/migrations/importController.d.ts +5 -2
- package/dist/migrations/importController.js +125 -88
- package/dist/migrations/importDataActions.d.ts +9 -1
- package/dist/migrations/importDataActions.js +15 -3
- package/dist/migrations/indexes.js +3 -2
- package/dist/migrations/logging.js +20 -8
- package/dist/migrations/migrationHelper.d.ts +9 -4
- package/dist/migrations/migrationHelper.js +6 -5
- package/dist/migrations/openapi.d.ts +1 -1
- package/dist/migrations/openapi.js +33 -18
- package/dist/migrations/queue.js +3 -2
- package/dist/migrations/relationships.d.ts +2 -2
- package/dist/migrations/schemaStrings.js +53 -41
- package/dist/migrations/setupDatabase.d.ts +2 -4
- package/dist/migrations/setupDatabase.js +24 -105
- package/dist/migrations/storage.d.ts +3 -1
- package/dist/migrations/storage.js +110 -16
- package/dist/migrations/transfer.d.ts +30 -0
- package/dist/migrations/transfer.js +337 -0
- package/dist/migrations/users.d.ts +2 -1
- package/dist/migrations/users.js +78 -43
- package/dist/schemas/authUser.d.ts +2 -2
- package/dist/storage/methods.d.ts +15 -0
- package/dist/storage/methods.js +207 -0
- package/dist/storage/schemas.d.ts +687 -0
- package/dist/storage/schemas.js +175 -0
- package/dist/utils/getClientFromConfig.d.ts +4 -0
- package/dist/utils/getClientFromConfig.js +16 -0
- package/dist/utils/helperFunctions.d.ts +11 -1
- package/dist/utils/helperFunctions.js +38 -0
- package/dist/utils/retryFailedPromises.d.ts +2 -0
- package/dist/utils/retryFailedPromises.js +21 -0
- package/dist/utils/schemaStrings.d.ts +13 -0
- package/dist/utils/schemaStrings.js +403 -0
- package/dist/utils/setupFiles.js +110 -61
- package/dist/utilsController.d.ts +40 -22
- package/dist/utilsController.js +164 -84
- package/package.json +13 -15
- package/src/collections/attributes.ts +483 -0
- package/src/collections/indexes.ts +53 -0
- package/src/collections/methods.ts +331 -0
- package/src/databases/methods.ts +47 -0
- package/src/init.ts +64 -64
- package/src/interactiveCLI.ts +767 -0
- package/src/main.ts +292 -83
- package/src/migrations/afterImportActions.ts +553 -490
- package/src/migrations/appwriteToX.ts +237 -174
- package/src/migrations/attributes.ts +483 -422
- package/src/migrations/backup.ts +205 -205
- package/src/migrations/collections.ts +545 -300
- package/src/migrations/converters.ts +161 -150
- package/src/migrations/dataLoader.ts +1615 -1304
- package/src/migrations/databases.ts +44 -25
- package/src/migrations/dbHelpers.ts +92 -92
- package/src/migrations/helper.ts +40 -0
- package/src/migrations/importController.ts +448 -384
- package/src/migrations/importDataActions.ts +315 -307
- package/src/migrations/indexes.ts +40 -37
- package/src/migrations/logging.ts +29 -16
- package/src/migrations/migrationHelper.ts +207 -201
- package/src/migrations/openapi.ts +83 -70
- package/src/migrations/queue.ts +118 -119
- package/src/migrations/relationships.ts +324 -324
- package/src/migrations/schemaStrings.ts +472 -460
- package/src/migrations/setupDatabase.ts +118 -219
- package/src/migrations/storage.ts +538 -358
- package/src/migrations/transfer.ts +608 -0
- package/src/migrations/users.ts +362 -285
- package/src/migrations/validationRules.ts +63 -63
- package/src/schemas/authUser.ts +23 -23
- package/src/setup.ts +8 -8
- package/src/storage/methods.ts +371 -0
- package/src/storage/schemas.ts +205 -0
- package/src/types.ts +9 -9
- package/src/utils/getClientFromConfig.ts +17 -0
- package/src/utils/helperFunctions.ts +181 -127
- package/src/utils/index.ts +2 -2
- package/src/utils/loadConfigs.ts +59 -59
- package/src/utils/retryFailedPromises.ts +27 -0
- package/src/utils/schemaStrings.ts +473 -0
- package/src/utils/setupFiles.ts +228 -182
- package/src/utilsController.ts +325 -194
- 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
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
this.
|
80
|
-
this.
|
81
|
-
this.
|
82
|
-
this.
|
83
|
-
this.
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
*
|
94
|
-
*
|
95
|
-
* - The
|
96
|
-
*
|
97
|
-
*
|
98
|
-
*
|
99
|
-
* @
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
//
|
110
|
-
if (
|
111
|
-
|
112
|
-
|
113
|
-
}
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
}
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
)
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
}
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
}
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
(
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
|
336
|
-
|
337
|
-
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
|
350
|
-
|
351
|
-
|
352
|
-
|
353
|
-
|
354
|
-
|
355
|
-
|
356
|
-
|
357
|
-
|
358
|
-
|
359
|
-
|
360
|
-
|
361
|
-
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
|
366
|
-
|
367
|
-
|
368
|
-
|
369
|
-
|
370
|
-
|
371
|
-
|
372
|
-
|
373
|
-
|
374
|
-
|
375
|
-
|
376
|
-
|
377
|
-
|
378
|
-
|
379
|
-
|
380
|
-
|
381
|
-
|
382
|
-
|
383
|
-
|
384
|
-
|
385
|
-
|
386
|
-
|
387
|
-
|
388
|
-
|
389
|
-
|
390
|
-
|
391
|
-
|
392
|
-
|
393
|
-
|
394
|
-
|
395
|
-
|
396
|
-
|
397
|
-
|
398
|
-
|
399
|
-
|
400
|
-
|
401
|
-
|
402
|
-
|
403
|
-
|
404
|
-
|
405
|
-
|
406
|
-
|
407
|
-
|
408
|
-
|
409
|
-
|
410
|
-
|
411
|
-
|
412
|
-
|
413
|
-
|
414
|
-
|
415
|
-
|
416
|
-
|
417
|
-
|
418
|
-
|
419
|
-
|
420
|
-
|
421
|
-
|
422
|
-
|
423
|
-
|
424
|
-
|
425
|
-
|
426
|
-
|
427
|
-
|
428
|
-
|
429
|
-
|
430
|
-
|
431
|
-
|
432
|
-
|
433
|
-
|
434
|
-
|
435
|
-
|
436
|
-
|
437
|
-
|
438
|
-
|
439
|
-
|
440
|
-
|
441
|
-
|
442
|
-
|
443
|
-
|
444
|
-
|
445
|
-
|
446
|
-
|
447
|
-
|
448
|
-
|
449
|
-
|
450
|
-
|
451
|
-
|
452
|
-
|
453
|
-
|
454
|
-
|
455
|
-
|
456
|
-
|
457
|
-
|
458
|
-
|
459
|
-
|
460
|
-
|
461
|
-
|
462
|
-
|
463
|
-
|
464
|
-
|
465
|
-
|
466
|
-
|
467
|
-
|
468
|
-
|
469
|
-
|
470
|
-
|
471
|
-
|
472
|
-
|
473
|
-
|
474
|
-
|
475
|
-
|
476
|
-
|
477
|
-
|
478
|
-
|
479
|
-
|
480
|
-
|
481
|
-
|
482
|
-
|
483
|
-
|
484
|
-
|
485
|
-
|
486
|
-
|
487
|
-
|
488
|
-
|
489
|
-
|
490
|
-
|
491
|
-
|
492
|
-
|
493
|
-
|
494
|
-
|
495
|
-
|
496
|
-
|
497
|
-
|
498
|
-
|
499
|
-
|
500
|
-
|
501
|
-
|
502
|
-
|
503
|
-
|
504
|
-
|
505
|
-
|
506
|
-
|
507
|
-
|
508
|
-
|
509
|
-
|
510
|
-
|
511
|
-
|
512
|
-
|
513
|
-
|
514
|
-
|
515
|
-
|
516
|
-
|
517
|
-
|
518
|
-
|
519
|
-
|
520
|
-
|
521
|
-
|
522
|
-
|
523
|
-
|
524
|
-
|
525
|
-
|
526
|
-
|
527
|
-
|
528
|
-
|
529
|
-
|
530
|
-
|
531
|
-
|
532
|
-
|
533
|
-
|
534
|
-
|
535
|
-
|
536
|
-
|
537
|
-
|
538
|
-
|
539
|
-
|
540
|
-
|
541
|
-
|
542
|
-
|
543
|
-
|
544
|
-
|
545
|
-
|
546
|
-
|
547
|
-
|
548
|
-
|
549
|
-
|
550
|
-
|
551
|
-
|
552
|
-
|
553
|
-
|
554
|
-
|
555
|
-
|
556
|
-
|
557
|
-
|
558
|
-
|
559
|
-
|
560
|
-
|
561
|
-
|
562
|
-
|
563
|
-
|
564
|
-
|
565
|
-
|
566
|
-
|
567
|
-
|
568
|
-
|
569
|
-
|
570
|
-
|
571
|
-
|
572
|
-
|
573
|
-
|
574
|
-
|
575
|
-
|
576
|
-
|
577
|
-
|
578
|
-
|
579
|
-
|
580
|
-
|
581
|
-
|
582
|
-
|
583
|
-
|
584
|
-
|
585
|
-
|
586
|
-
|
587
|
-
|
588
|
-
|
589
|
-
|
590
|
-
|
591
|
-
|
592
|
-
|
593
|
-
|
594
|
-
|
595
|
-
|
596
|
-
|
597
|
-
|
598
|
-
|
599
|
-
|
600
|
-
|
601
|
-
|
602
|
-
|
603
|
-
|
604
|
-
|
605
|
-
|
606
|
-
|
607
|
-
|
608
|
-
|
609
|
-
|
610
|
-
|
611
|
-
|
612
|
-
|
613
|
-
|
614
|
-
|
615
|
-
|
616
|
-
|
617
|
-
|
618
|
-
|
619
|
-
|
620
|
-
|
621
|
-
|
622
|
-
|
623
|
-
|
624
|
-
|
625
|
-
|
626
|
-
|
627
|
-
|
628
|
-
|
629
|
-
|
630
|
-
|
631
|
-
|
632
|
-
|
633
|
-
|
634
|
-
|
635
|
-
|
636
|
-
|
637
|
-
|
638
|
-
|
639
|
-
|
640
|
-
|
641
|
-
|
642
|
-
|
643
|
-
|
644
|
-
|
645
|
-
|
646
|
-
|
647
|
-
|
648
|
-
|
649
|
-
|
650
|
-
|
651
|
-
|
652
|
-
|
653
|
-
|
654
|
-
|
655
|
-
|
656
|
-
|
657
|
-
|
658
|
-
|
659
|
-
|
660
|
-
|
661
|
-
|
662
|
-
|
663
|
-
|
664
|
-
|
665
|
-
|
666
|
-
|
667
|
-
|
668
|
-
|
669
|
-
|
670
|
-
|
671
|
-
|
672
|
-
|
673
|
-
|
674
|
-
|
675
|
-
|
676
|
-
|
677
|
-
|
678
|
-
|
679
|
-
|
680
|
-
|
681
|
-
|
682
|
-
|
683
|
-
|
684
|
-
|
685
|
-
|
686
|
-
|
687
|
-
|
688
|
-
|
689
|
-
|
690
|
-
|
691
|
-
|
692
|
-
|
693
|
-
|
694
|
-
|
695
|
-
|
696
|
-
|
697
|
-
|
698
|
-
|
699
|
-
|
700
|
-
|
701
|
-
|
702
|
-
|
703
|
-
|
704
|
-
|
705
|
-
|
706
|
-
|
707
|
-
|
708
|
-
|
709
|
-
|
710
|
-
|
711
|
-
|
712
|
-
|
713
|
-
|
714
|
-
|
715
|
-
|
716
|
-
|
717
|
-
|
718
|
-
|
719
|
-
|
720
|
-
|
721
|
-
|
722
|
-
|
723
|
-
|
724
|
-
|
725
|
-
|
726
|
-
|
727
|
-
|
728
|
-
|
729
|
-
|
730
|
-
|
731
|
-
|
732
|
-
|
733
|
-
|
734
|
-
|
735
|
-
|
736
|
-
|
737
|
-
|
738
|
-
|
739
|
-
|
740
|
-
|
741
|
-
|
742
|
-
|
743
|
-
|
744
|
-
|
745
|
-
//
|
746
|
-
|
747
|
-
|
748
|
-
|
749
|
-
);
|
750
|
-
|
751
|
-
|
752
|
-
//
|
753
|
-
|
754
|
-
|
755
|
-
|
756
|
-
|
757
|
-
|
758
|
-
|
759
|
-
|
760
|
-
|
761
|
-
|
762
|
-
|
763
|
-
|
764
|
-
|
765
|
-
|
766
|
-
|
767
|
-
|
768
|
-
|
769
|
-
|
770
|
-
|
771
|
-
|
772
|
-
|
773
|
-
|
774
|
-
|
775
|
-
|
776
|
-
|
777
|
-
|
778
|
-
|
779
|
-
|
780
|
-
|
781
|
-
|
782
|
-
|
783
|
-
|
784
|
-
|
785
|
-
|
786
|
-
|
787
|
-
|
788
|
-
|
789
|
-
|
790
|
-
|
791
|
-
|
792
|
-
|
793
|
-
|
794
|
-
|
795
|
-
|
796
|
-
|
797
|
-
|
798
|
-
|
799
|
-
|
800
|
-
|
801
|
-
|
802
|
-
|
803
|
-
|
804
|
-
|
805
|
-
|
806
|
-
|
807
|
-
|
808
|
-
|
809
|
-
|
810
|
-
|
811
|
-
|
812
|
-
|
813
|
-
|
814
|
-
|
815
|
-
|
816
|
-
|
817
|
-
|
818
|
-
|
819
|
-
|
820
|
-
|
821
|
-
|
822
|
-
|
823
|
-
|
824
|
-
|
825
|
-
|
826
|
-
|
827
|
-
|
828
|
-
|
829
|
-
|
830
|
-
|
831
|
-
|
832
|
-
|
833
|
-
|
834
|
-
|
835
|
-
|
836
|
-
|
837
|
-
|
838
|
-
|
839
|
-
|
840
|
-
|
841
|
-
|
842
|
-
|
843
|
-
|
844
|
-
|
845
|
-
|
846
|
-
|
847
|
-
|
848
|
-
|
849
|
-
|
850
|
-
|
851
|
-
|
852
|
-
|
853
|
-
|
854
|
-
|
855
|
-
|
856
|
-
|
857
|
-
|
858
|
-
|
859
|
-
|
860
|
-
)
|
861
|
-
|
862
|
-
|
863
|
-
|
864
|
-
|
865
|
-
|
866
|
-
|
867
|
-
|
868
|
-
|
869
|
-
transformedItem
|
870
|
-
|
871
|
-
|
872
|
-
|
873
|
-
|
874
|
-
|
875
|
-
|
876
|
-
|
877
|
-
|
878
|
-
|
879
|
-
|
880
|
-
|
881
|
-
|
882
|
-
|
883
|
-
|
884
|
-
|
885
|
-
|
886
|
-
|
887
|
-
|
888
|
-
|
889
|
-
|
890
|
-
|
891
|
-
|
892
|
-
|
893
|
-
|
894
|
-
|
895
|
-
|
896
|
-
|
897
|
-
|
898
|
-
|
899
|
-
}
|
900
|
-
|
901
|
-
|
902
|
-
|
903
|
-
|
904
|
-
|
905
|
-
|
906
|
-
|
907
|
-
|
908
|
-
|
909
|
-
|
910
|
-
|
911
|
-
|
912
|
-
|
913
|
-
|
914
|
-
|
915
|
-
|
916
|
-
|
917
|
-
|
918
|
-
|
919
|
-
|
920
|
-
*
|
921
|
-
*
|
922
|
-
*
|
923
|
-
*
|
924
|
-
|
925
|
-
|
926
|
-
|
927
|
-
|
928
|
-
|
929
|
-
|
930
|
-
|
931
|
-
|
932
|
-
|
933
|
-
|
934
|
-
);
|
935
|
-
|
936
|
-
|
937
|
-
|
938
|
-
|
939
|
-
|
940
|
-
|
941
|
-
|
942
|
-
|
943
|
-
|
944
|
-
|
945
|
-
|
946
|
-
|
947
|
-
|
948
|
-
|
949
|
-
|
950
|
-
|
951
|
-
|
952
|
-
|
953
|
-
|
954
|
-
|
955
|
-
|
956
|
-
);
|
957
|
-
//
|
958
|
-
|
959
|
-
|
960
|
-
|
961
|
-
|
962
|
-
|
963
|
-
|
964
|
-
|
965
|
-
|
966
|
-
);
|
967
|
-
|
968
|
-
|
969
|
-
|
970
|
-
|
971
|
-
|
972
|
-
|
973
|
-
|
974
|
-
|
975
|
-
|
976
|
-
|
977
|
-
|
978
|
-
|
979
|
-
|
980
|
-
|
981
|
-
|
982
|
-
|
983
|
-
|
984
|
-
|
985
|
-
|
986
|
-
|
987
|
-
|
988
|
-
|
989
|
-
|
990
|
-
|
991
|
-
|
992
|
-
|
993
|
-
if (!
|
994
|
-
|
995
|
-
|
996
|
-
|
997
|
-
|
998
|
-
|
999
|
-
|
1000
|
-
|
1001
|
-
|
1002
|
-
|
1003
|
-
|
1004
|
-
|
1005
|
-
|
1006
|
-
|
1007
|
-
|
1008
|
-
|
1009
|
-
|
1010
|
-
|
1011
|
-
|
1012
|
-
|
1013
|
-
|
1014
|
-
|
1015
|
-
|
1016
|
-
|
1017
|
-
|
1018
|
-
|
1019
|
-
|
1020
|
-
|
1021
|
-
|
1022
|
-
|
1023
|
-
|
1024
|
-
|
1025
|
-
|
1026
|
-
|
1027
|
-
|
1028
|
-
|
1029
|
-
|
1030
|
-
|
1031
|
-
|
1032
|
-
|
1033
|
-
|
1034
|
-
|
1035
|
-
|
1036
|
-
|
1037
|
-
|
1038
|
-
|
1039
|
-
|
1040
|
-
|
1041
|
-
|
1042
|
-
|
1043
|
-
|
1044
|
-
|
1045
|
-
|
1046
|
-
|
1047
|
-
|
1048
|
-
|
1049
|
-
|
1050
|
-
|
1051
|
-
|
1052
|
-
|
1053
|
-
|
1054
|
-
|
1055
|
-
|
1056
|
-
|
1057
|
-
|
1058
|
-
|
1059
|
-
|
1060
|
-
|
1061
|
-
|
1062
|
-
|
1063
|
-
|
1064
|
-
|
1065
|
-
|
1066
|
-
|
1067
|
-
|
1068
|
-
|
1069
|
-
|
1070
|
-
|
1071
|
-
|
1072
|
-
|
1073
|
-
|
1074
|
-
|
1075
|
-
|
1076
|
-
|
1077
|
-
|
1078
|
-
|
1079
|
-
|
1080
|
-
|
1081
|
-
|
1082
|
-
|
1083
|
-
|
1084
|
-
|
1085
|
-
|
1086
|
-
|
1087
|
-
|
1088
|
-
|
1089
|
-
|
1090
|
-
|
1091
|
-
|
1092
|
-
|
1093
|
-
|
1094
|
-
|
1095
|
-
|
1096
|
-
|
1097
|
-
|
1098
|
-
|
1099
|
-
|
1100
|
-
|
1101
|
-
|
1102
|
-
|
1103
|
-
|
1104
|
-
|
1105
|
-
|
1106
|
-
|
1107
|
-
|
1108
|
-
|
1109
|
-
|
1110
|
-
|
1111
|
-
|
1112
|
-
|
1113
|
-
|
1114
|
-
|
1115
|
-
|
1116
|
-
|
1117
|
-
|
1118
|
-
|
1119
|
-
|
1120
|
-
|
1121
|
-
|
1122
|
-
|
1123
|
-
|
1124
|
-
|
1125
|
-
|
1126
|
-
|
1127
|
-
|
1128
|
-
|
1129
|
-
|
1130
|
-
|
1131
|
-
|
1132
|
-
|
1133
|
-
|
1134
|
-
|
1135
|
-
|
1136
|
-
|
1137
|
-
|
1138
|
-
|
1139
|
-
|
1140
|
-
|
1141
|
-
|
1142
|
-
|
1143
|
-
|
1144
|
-
|
1145
|
-
|
1146
|
-
|
1147
|
-
|
1148
|
-
|
1149
|
-
|
1150
|
-
|
1151
|
-
|
1152
|
-
|
1153
|
-
|
1154
|
-
|
1155
|
-
|
1156
|
-
|
1157
|
-
|
1158
|
-
|
1159
|
-
|
1160
|
-
|
1161
|
-
|
1162
|
-
|
1163
|
-
|
1164
|
-
|
1165
|
-
|
1166
|
-
|
1167
|
-
|
1168
|
-
|
1169
|
-
|
1170
|
-
|
1171
|
-
|
1172
|
-
|
1173
|
-
|
1174
|
-
|
1175
|
-
|
1176
|
-
|
1177
|
-
|
1178
|
-
|
1179
|
-
|
1180
|
-
|
1181
|
-
|
1182
|
-
|
1183
|
-
|
1184
|
-
|
1185
|
-
|
1186
|
-
|
1187
|
-
|
1188
|
-
|
1189
|
-
|
1190
|
-
|
1191
|
-
|
1192
|
-
|
1193
|
-
|
1194
|
-
|
1195
|
-
|
1196
|
-
const
|
1197
|
-
this.getCollectionKey(
|
1198
|
-
);
|
1199
|
-
|
1200
|
-
|
1201
|
-
|
1202
|
-
|
1203
|
-
|
1204
|
-
|
1205
|
-
|
1206
|
-
|
1207
|
-
|
1208
|
-
|
1209
|
-
|
1210
|
-
|
1211
|
-
|
1212
|
-
|
1213
|
-
|
1214
|
-
|
1215
|
-
|
1216
|
-
|
1217
|
-
|
1218
|
-
|
1219
|
-
|
1220
|
-
|
1221
|
-
|
1222
|
-
|
1223
|
-
|
1224
|
-
|
1225
|
-
|
1226
|
-
|
1227
|
-
|
1228
|
-
|
1229
|
-
|
1230
|
-
|
1231
|
-
|
1232
|
-
|
1233
|
-
|
1234
|
-
|
1235
|
-
|
1236
|
-
|
1237
|
-
|
1238
|
-
|
1239
|
-
|
1240
|
-
|
1241
|
-
|
1242
|
-
|
1243
|
-
|
1244
|
-
|
1245
|
-
|
1246
|
-
|
1247
|
-
|
1248
|
-
|
1249
|
-
|
1250
|
-
|
1251
|
-
|
1252
|
-
|
1253
|
-
|
1254
|
-
|
1255
|
-
|
1256
|
-
|
1257
|
-
|
1258
|
-
|
1259
|
-
|
1260
|
-
|
1261
|
-
|
1262
|
-
|
1263
|
-
|
1264
|
-
|
1265
|
-
|
1266
|
-
|
1267
|
-
|
1268
|
-
|
1269
|
-
|
1270
|
-
|
1271
|
-
|
1272
|
-
|
1273
|
-
|
1274
|
-
|
1275
|
-
|
1276
|
-
|
1277
|
-
|
1278
|
-
|
1279
|
-
|
1280
|
-
|
1281
|
-
|
1282
|
-
|
1283
|
-
|
1284
|
-
|
1285
|
-
|
1286
|
-
|
1287
|
-
|
1288
|
-
|
1289
|
-
|
1290
|
-
|
1291
|
-
|
1292
|
-
|
1293
|
-
|
1294
|
-
|
1295
|
-
|
1296
|
-
|
1297
|
-
|
1298
|
-
|
1299
|
-
|
1300
|
-
|
1301
|
-
|
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
|
+
}
|