appwrite-utils-cli 1.7.9 → 1.8.2
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/CHANGELOG.md +14 -199
- package/README.md +87 -30
- package/dist/adapters/AdapterFactory.js +5 -25
- package/dist/adapters/DatabaseAdapter.d.ts +17 -2
- package/dist/adapters/LegacyAdapter.d.ts +2 -1
- package/dist/adapters/LegacyAdapter.js +212 -16
- package/dist/adapters/TablesDBAdapter.d.ts +2 -12
- package/dist/adapters/TablesDBAdapter.js +261 -57
- package/dist/cli/commands/databaseCommands.js +4 -3
- package/dist/cli/commands/functionCommands.js +17 -8
- package/dist/collections/attributes.js +447 -125
- package/dist/collections/methods.js +197 -186
- package/dist/collections/tableOperations.d.ts +86 -0
- package/dist/collections/tableOperations.js +434 -0
- package/dist/collections/transferOperations.d.ts +3 -2
- package/dist/collections/transferOperations.js +93 -12
- package/dist/config/yamlConfig.d.ts +221 -88
- package/dist/examples/yamlTerminologyExample.d.ts +1 -1
- package/dist/examples/yamlTerminologyExample.js +6 -3
- package/dist/functions/fnConfigDiscovery.d.ts +3 -0
- package/dist/functions/fnConfigDiscovery.js +108 -0
- package/dist/interactiveCLI.js +18 -15
- package/dist/main.js +211 -73
- package/dist/migrations/appwriteToX.d.ts +88 -23
- package/dist/migrations/comprehensiveTransfer.d.ts +2 -0
- package/dist/migrations/comprehensiveTransfer.js +83 -6
- package/dist/migrations/dataLoader.d.ts +227 -69
- package/dist/migrations/dataLoader.js +3 -3
- package/dist/migrations/importController.js +3 -3
- package/dist/migrations/relationships.d.ts +8 -2
- package/dist/migrations/services/ImportOrchestrator.js +3 -3
- package/dist/migrations/transfer.js +159 -37
- package/dist/shared/attributeMapper.d.ts +20 -0
- package/dist/shared/attributeMapper.js +203 -0
- package/dist/shared/selectionDialogs.js +8 -4
- package/dist/storage/schemas.d.ts +354 -92
- package/dist/utils/configDiscovery.js +4 -3
- package/dist/utils/versionDetection.d.ts +0 -4
- package/dist/utils/versionDetection.js +41 -173
- package/dist/utils/yamlConverter.js +89 -16
- package/dist/utils/yamlLoader.d.ts +1 -1
- package/dist/utils/yamlLoader.js +6 -2
- package/dist/utilsController.js +56 -19
- package/package.json +4 -4
- package/src/adapters/AdapterFactory.ts +119 -143
- package/src/adapters/DatabaseAdapter.ts +18 -3
- package/src/adapters/LegacyAdapter.ts +236 -105
- package/src/adapters/TablesDBAdapter.ts +773 -643
- package/src/cli/commands/databaseCommands.ts +13 -12
- package/src/cli/commands/functionCommands.ts +23 -14
- package/src/collections/attributes.ts +2054 -1611
- package/src/collections/methods.ts +208 -293
- package/src/collections/tableOperations.ts +506 -0
- package/src/collections/transferOperations.ts +218 -144
- package/src/examples/yamlTerminologyExample.ts +10 -5
- package/src/functions/fnConfigDiscovery.ts +103 -0
- package/src/interactiveCLI.ts +25 -20
- package/src/main.ts +549 -194
- package/src/migrations/comprehensiveTransfer.ts +126 -50
- package/src/migrations/dataLoader.ts +3 -3
- package/src/migrations/importController.ts +3 -3
- package/src/migrations/services/ImportOrchestrator.ts +3 -3
- package/src/migrations/transfer.ts +148 -131
- package/src/shared/attributeMapper.ts +229 -0
- package/src/shared/selectionDialogs.ts +29 -25
- package/src/utils/configDiscovery.ts +9 -3
- package/src/utils/versionDetection.ts +74 -228
- package/src/utils/yamlConverter.ts +94 -17
- package/src/utils/yamlLoader.ts +11 -4
- package/src/utilsController.ts +80 -30
|
@@ -1,1611 +1,2054 @@
|
|
|
1
|
-
import { Query, type Databases, type Models } from "node-appwrite";
|
|
2
|
-
import {
|
|
3
|
-
attributeSchema,
|
|
4
|
-
parseAttribute,
|
|
5
|
-
type Attribute,
|
|
6
|
-
} from "appwrite-utils";
|
|
7
|
-
import {
|
|
8
|
-
nameToIdMapping,
|
|
9
|
-
enqueueOperation,
|
|
10
|
-
markAttributeProcessed,
|
|
11
|
-
isAttributeProcessed
|
|
12
|
-
} from "../shared/operationQueue.js";
|
|
13
|
-
import {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
import
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
const
|
|
27
|
-
const
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
if (
|
|
81
|
-
logger.debug(`Min value normalized to undefined for attribute '${attribute.key}'`, {
|
|
82
|
-
type,
|
|
83
|
-
originalValue: originalMin,
|
|
84
|
-
numericValue: minValue,
|
|
85
|
-
reason:
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
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
|
-
attribute
|
|
336
|
-
)
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
)
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
attribute
|
|
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
|
-
* Legacy attribute
|
|
431
|
-
*/
|
|
432
|
-
const
|
|
433
|
-
db: Databases,
|
|
434
|
-
dbId: string,
|
|
435
|
-
collectionId: string,
|
|
436
|
-
attribute: Attribute
|
|
437
|
-
): Promise<void> => {
|
|
438
|
-
const
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
attribute.
|
|
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
|
-
break;
|
|
510
|
-
case "
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
attribute.
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
);
|
|
536
|
-
break;
|
|
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
|
-
await db.
|
|
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
|
-
const
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
1197
|
-
|
|
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
|
-
|
|
1305
|
-
|
|
1306
|
-
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
|
|
1311
|
-
|
|
1312
|
-
);
|
|
1313
|
-
|
|
1314
|
-
|
|
1315
|
-
|
|
1316
|
-
|
|
1317
|
-
|
|
1318
|
-
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
|
|
1339
|
-
|
|
1340
|
-
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
|
|
1344
|
-
|
|
1345
|
-
|
|
1346
|
-
|
|
1347
|
-
|
|
1348
|
-
|
|
1349
|
-
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
|
|
1353
|
-
|
|
1354
|
-
|
|
1355
|
-
|
|
1356
|
-
|
|
1357
|
-
|
|
1358
|
-
|
|
1359
|
-
|
|
1360
|
-
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
|
|
1364
|
-
|
|
1365
|
-
|
|
1366
|
-
|
|
1367
|
-
|
|
1368
|
-
|
|
1369
|
-
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
|
|
1379
|
-
|
|
1380
|
-
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
1384
|
-
|
|
1385
|
-
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
|
|
1398
|
-
|
|
1399
|
-
|
|
1400
|
-
|
|
1401
|
-
|
|
1402
|
-
|
|
1403
|
-
|
|
1404
|
-
|
|
1405
|
-
|
|
1406
|
-
|
|
1407
|
-
|
|
1408
|
-
|
|
1409
|
-
|
|
1410
|
-
|
|
1411
|
-
|
|
1412
|
-
|
|
1413
|
-
|
|
1414
|
-
|
|
1415
|
-
|
|
1416
|
-
|
|
1417
|
-
|
|
1418
|
-
|
|
1419
|
-
|
|
1420
|
-
|
|
1421
|
-
|
|
1422
|
-
|
|
1423
|
-
|
|
1424
|
-
|
|
1425
|
-
|
|
1426
|
-
|
|
1427
|
-
|
|
1428
|
-
|
|
1429
|
-
|
|
1430
|
-
|
|
1431
|
-
|
|
1432
|
-
|
|
1433
|
-
|
|
1434
|
-
|
|
1435
|
-
|
|
1436
|
-
|
|
1437
|
-
|
|
1438
|
-
|
|
1439
|
-
|
|
1440
|
-
|
|
1441
|
-
|
|
1442
|
-
|
|
1443
|
-
|
|
1444
|
-
|
|
1445
|
-
|
|
1446
|
-
|
|
1447
|
-
|
|
1448
|
-
|
|
1449
|
-
|
|
1450
|
-
|
|
1451
|
-
|
|
1452
|
-
|
|
1453
|
-
|
|
1454
|
-
|
|
1455
|
-
|
|
1456
|
-
|
|
1457
|
-
|
|
1458
|
-
|
|
1459
|
-
|
|
1460
|
-
|
|
1461
|
-
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
|
|
1467
|
-
|
|
1468
|
-
|
|
1469
|
-
|
|
1470
|
-
|
|
1471
|
-
|
|
1472
|
-
|
|
1473
|
-
|
|
1474
|
-
|
|
1475
|
-
|
|
1476
|
-
|
|
1477
|
-
|
|
1478
|
-
|
|
1479
|
-
|
|
1480
|
-
|
|
1481
|
-
|
|
1482
|
-
|
|
1483
|
-
|
|
1484
|
-
|
|
1485
|
-
|
|
1486
|
-
|
|
1487
|
-
|
|
1488
|
-
|
|
1489
|
-
|
|
1490
|
-
|
|
1491
|
-
|
|
1492
|
-
|
|
1493
|
-
|
|
1494
|
-
}
|
|
1495
|
-
|
|
1496
|
-
|
|
1497
|
-
|
|
1498
|
-
|
|
1499
|
-
|
|
1500
|
-
|
|
1501
|
-
|
|
1502
|
-
|
|
1503
|
-
|
|
1504
|
-
|
|
1505
|
-
|
|
1506
|
-
|
|
1507
|
-
|
|
1508
|
-
|
|
1509
|
-
|
|
1510
|
-
|
|
1511
|
-
|
|
1512
|
-
|
|
1513
|
-
|
|
1514
|
-
|
|
1515
|
-
|
|
1516
|
-
|
|
1517
|
-
|
|
1518
|
-
|
|
1519
|
-
)
|
|
1520
|
-
|
|
1521
|
-
|
|
1522
|
-
|
|
1523
|
-
|
|
1524
|
-
|
|
1525
|
-
|
|
1526
|
-
|
|
1527
|
-
|
|
1528
|
-
|
|
1529
|
-
|
|
1530
|
-
|
|
1531
|
-
|
|
1532
|
-
|
|
1533
|
-
|
|
1534
|
-
|
|
1535
|
-
|
|
1536
|
-
|
|
1537
|
-
|
|
1538
|
-
|
|
1539
|
-
|
|
1540
|
-
|
|
1541
|
-
|
|
1542
|
-
|
|
1543
|
-
|
|
1544
|
-
|
|
1545
|
-
|
|
1546
|
-
|
|
1547
|
-
|
|
1548
|
-
|
|
1549
|
-
|
|
1550
|
-
|
|
1551
|
-
|
|
1552
|
-
|
|
1553
|
-
|
|
1554
|
-
|
|
1555
|
-
|
|
1556
|
-
|
|
1557
|
-
|
|
1558
|
-
|
|
1559
|
-
|
|
1560
|
-
|
|
1561
|
-
|
|
1562
|
-
|
|
1563
|
-
|
|
1564
|
-
|
|
1565
|
-
|
|
1566
|
-
|
|
1567
|
-
|
|
1568
|
-
|
|
1569
|
-
|
|
1570
|
-
|
|
1571
|
-
|
|
1572
|
-
|
|
1573
|
-
|
|
1574
|
-
|
|
1575
|
-
|
|
1576
|
-
|
|
1577
|
-
|
|
1578
|
-
|
|
1579
|
-
|
|
1580
|
-
|
|
1581
|
-
|
|
1582
|
-
|
|
1583
|
-
|
|
1584
|
-
|
|
1585
|
-
|
|
1586
|
-
|
|
1587
|
-
|
|
1588
|
-
|
|
1589
|
-
|
|
1590
|
-
|
|
1591
|
-
|
|
1592
|
-
|
|
1593
|
-
|
|
1594
|
-
|
|
1595
|
-
|
|
1596
|
-
|
|
1597
|
-
|
|
1598
|
-
|
|
1599
|
-
|
|
1600
|
-
|
|
1601
|
-
|
|
1602
|
-
|
|
1603
|
-
|
|
1604
|
-
|
|
1605
|
-
|
|
1606
|
-
|
|
1607
|
-
|
|
1608
|
-
|
|
1609
|
-
|
|
1610
|
-
|
|
1611
|
-
}
|
|
1
|
+
import { Query, type Databases, type Models } from "node-appwrite";
|
|
2
|
+
import {
|
|
3
|
+
attributeSchema,
|
|
4
|
+
parseAttribute,
|
|
5
|
+
type Attribute,
|
|
6
|
+
} from "appwrite-utils";
|
|
7
|
+
import {
|
|
8
|
+
nameToIdMapping,
|
|
9
|
+
enqueueOperation,
|
|
10
|
+
markAttributeProcessed,
|
|
11
|
+
isAttributeProcessed,
|
|
12
|
+
} from "../shared/operationQueue.js";
|
|
13
|
+
import {
|
|
14
|
+
delay,
|
|
15
|
+
tryAwaitWithRetry,
|
|
16
|
+
calculateExponentialBackoff,
|
|
17
|
+
} from "../utils/helperFunctions.js";
|
|
18
|
+
import chalk from "chalk";
|
|
19
|
+
import { Decimal } from "decimal.js";
|
|
20
|
+
import type { DatabaseAdapter, CreateAttributeParams, UpdateAttributeParams, DeleteAttributeParams } from "../adapters/DatabaseAdapter.js";
|
|
21
|
+
import { logger } from "../shared/logging.js";
|
|
22
|
+
import { MessageFormatter } from "../shared/messageFormatter.js";
|
|
23
|
+
import { isDatabaseAdapter } from "../utils/typeGuards.js";
|
|
24
|
+
|
|
25
|
+
// Extreme values that Appwrite may return, which should be treated as undefined
|
|
26
|
+
const EXTREME_MIN_INTEGER = -9223372036854776000;
|
|
27
|
+
const EXTREME_MAX_INTEGER = 9223372036854776000;
|
|
28
|
+
const EXTREME_MIN_FLOAT = -1.7976931348623157e308;
|
|
29
|
+
const EXTREME_MAX_FLOAT = 1.7976931348623157e308;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Type guard to check if an attribute has min/max properties
|
|
33
|
+
*/
|
|
34
|
+
const hasMinMaxProperties = (
|
|
35
|
+
attribute: Attribute
|
|
36
|
+
): attribute is Attribute & { min?: number; max?: number } => {
|
|
37
|
+
return (
|
|
38
|
+
attribute.type === "integer" ||
|
|
39
|
+
attribute.type === "double" ||
|
|
40
|
+
attribute.type === "float"
|
|
41
|
+
);
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Normalizes min/max values for integer and float attributes using Decimal.js for precision
|
|
46
|
+
* Validates that min < max and handles extreme database values
|
|
47
|
+
*/
|
|
48
|
+
const normalizeMinMaxValues = (
|
|
49
|
+
attribute: Attribute
|
|
50
|
+
): { min?: number; max?: number } => {
|
|
51
|
+
if (!hasMinMaxProperties(attribute)) {
|
|
52
|
+
logger.debug(
|
|
53
|
+
`Attribute '${attribute.key}' does not have min/max properties`,
|
|
54
|
+
{
|
|
55
|
+
type: attribute.type,
|
|
56
|
+
operation: "normalizeMinMaxValues",
|
|
57
|
+
}
|
|
58
|
+
);
|
|
59
|
+
return {};
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const { type, min, max } = attribute;
|
|
63
|
+
let normalizedMin = min;
|
|
64
|
+
let normalizedMax = max;
|
|
65
|
+
|
|
66
|
+
logger.debug(`Normalizing min/max values for attribute '${attribute.key}'`, {
|
|
67
|
+
type,
|
|
68
|
+
originalMin: min,
|
|
69
|
+
originalMax: max,
|
|
70
|
+
operation: "normalizeMinMaxValues",
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
// Handle min value - only filter out extreme database values
|
|
74
|
+
if (normalizedMin !== undefined && normalizedMin !== null) {
|
|
75
|
+
const minValue = Number(normalizedMin);
|
|
76
|
+
const originalMin = normalizedMin;
|
|
77
|
+
|
|
78
|
+
// Check if it's an extreme database value (but don't filter out large numbers)
|
|
79
|
+
if (type === 'integer') {
|
|
80
|
+
if (minValue === EXTREME_MIN_INTEGER) {
|
|
81
|
+
logger.debug(`Min value normalized to undefined for attribute '${attribute.key}'`, {
|
|
82
|
+
type,
|
|
83
|
+
originalValue: originalMin,
|
|
84
|
+
numericValue: minValue,
|
|
85
|
+
reason: 'extreme_database_value',
|
|
86
|
+
extremeValue: EXTREME_MIN_INTEGER,
|
|
87
|
+
operation: 'normalizeMinMaxValues'
|
|
88
|
+
});
|
|
89
|
+
normalizedMin = undefined;
|
|
90
|
+
}
|
|
91
|
+
} else { // float/double
|
|
92
|
+
if (minValue === EXTREME_MIN_FLOAT) {
|
|
93
|
+
logger.debug(`Min value normalized to undefined for attribute '${attribute.key}'`, {
|
|
94
|
+
type,
|
|
95
|
+
originalValue: originalMin,
|
|
96
|
+
numericValue: minValue,
|
|
97
|
+
reason: 'extreme_database_value',
|
|
98
|
+
extremeValue: EXTREME_MIN_FLOAT,
|
|
99
|
+
operation: 'normalizeMinMaxValues'
|
|
100
|
+
});
|
|
101
|
+
normalizedMin = undefined;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// Handle max value - only filter out extreme database values
|
|
107
|
+
if (normalizedMax !== undefined && normalizedMax !== null) {
|
|
108
|
+
const maxValue = Number(normalizedMax);
|
|
109
|
+
const originalMax = normalizedMax;
|
|
110
|
+
|
|
111
|
+
// Check if it's an extreme database value (but don't filter out large numbers)
|
|
112
|
+
if (type === 'integer') {
|
|
113
|
+
if (maxValue === EXTREME_MAX_INTEGER) {
|
|
114
|
+
logger.debug(`Max value normalized to undefined for attribute '${attribute.key}'`, {
|
|
115
|
+
type,
|
|
116
|
+
originalValue: originalMax,
|
|
117
|
+
numericValue: maxValue,
|
|
118
|
+
reason: 'extreme_database_value',
|
|
119
|
+
extremeValue: EXTREME_MAX_INTEGER,
|
|
120
|
+
operation: 'normalizeMinMaxValues'
|
|
121
|
+
});
|
|
122
|
+
normalizedMax = undefined;
|
|
123
|
+
}
|
|
124
|
+
} else { // float/double
|
|
125
|
+
if (maxValue === EXTREME_MAX_FLOAT) {
|
|
126
|
+
logger.debug(`Max value normalized to undefined for attribute '${attribute.key}'`, {
|
|
127
|
+
type,
|
|
128
|
+
originalValue: originalMax,
|
|
129
|
+
numericValue: maxValue,
|
|
130
|
+
reason: 'extreme_database_value',
|
|
131
|
+
extremeValue: EXTREME_MAX_FLOAT,
|
|
132
|
+
operation: 'normalizeMinMaxValues'
|
|
133
|
+
});
|
|
134
|
+
normalizedMax = undefined;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// Validate that min < max using multiple comparison methods for reliability
|
|
140
|
+
if (normalizedMin !== undefined && normalizedMax !== undefined &&
|
|
141
|
+
normalizedMin !== null && normalizedMax !== null) {
|
|
142
|
+
|
|
143
|
+
logger.debug(`Validating min/max values for attribute '${attribute.key}'`, {
|
|
144
|
+
type,
|
|
145
|
+
normalizedMin,
|
|
146
|
+
normalizedMax,
|
|
147
|
+
normalizedMinType: typeof normalizedMin,
|
|
148
|
+
normalizedMaxType: typeof normalizedMax,
|
|
149
|
+
operation: 'normalizeMinMaxValues'
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
// Use multiple validation approaches to ensure reliability
|
|
153
|
+
let needsSwap = false;
|
|
154
|
+
let comparisonMethod = '';
|
|
155
|
+
|
|
156
|
+
try {
|
|
157
|
+
// Method 1: Direct number comparison (most reliable for normal numbers)
|
|
158
|
+
const minNum = Number(normalizedMin);
|
|
159
|
+
const maxNum = Number(normalizedMax);
|
|
160
|
+
|
|
161
|
+
if (!isNaN(minNum) && !isNaN(maxNum)) {
|
|
162
|
+
needsSwap = minNum >= maxNum;
|
|
163
|
+
comparisonMethod = 'direct_number_comparison';
|
|
164
|
+
logger.debug(`Direct number comparison: ${minNum} >= ${maxNum} = ${needsSwap}`, {
|
|
165
|
+
operation: 'normalizeMinMaxValues'
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// Method 2: Fallback to string comparison for very large numbers
|
|
170
|
+
if (!needsSwap && (isNaN(minNum) || isNaN(maxNum) || Math.abs(minNum) > Number.MAX_SAFE_INTEGER || Math.abs(maxNum) > Number.MAX_SAFE_INTEGER)) {
|
|
171
|
+
const minStr = normalizedMin.toString();
|
|
172
|
+
const maxStr = normalizedMax.toString();
|
|
173
|
+
|
|
174
|
+
// Simple string length and lexicographical comparison for very large numbers
|
|
175
|
+
if (minStr.length !== maxStr.length) {
|
|
176
|
+
needsSwap = minStr.length > maxStr.length;
|
|
177
|
+
} else {
|
|
178
|
+
needsSwap = minStr >= maxStr;
|
|
179
|
+
}
|
|
180
|
+
comparisonMethod = 'string_comparison_fallback';
|
|
181
|
+
logger.debug(`String comparison fallback: '${minStr}' >= '${maxStr}' = ${needsSwap}`, {
|
|
182
|
+
operation: 'normalizeMinMaxValues'
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
// Method 3: Final validation using Decimal.js as last resort
|
|
187
|
+
if (!needsSwap && (typeof normalizedMin === 'string' || typeof normalizedMax === 'string')) {
|
|
188
|
+
try {
|
|
189
|
+
const minDecimal = new Decimal(normalizedMin.toString());
|
|
190
|
+
const maxDecimal = new Decimal(normalizedMax.toString());
|
|
191
|
+
needsSwap = minDecimal.greaterThanOrEqualTo(maxDecimal);
|
|
192
|
+
comparisonMethod = 'decimal_js_fallback';
|
|
193
|
+
logger.debug(`Decimal.js fallback: ${normalizedMin} >= ${normalizedMax} = ${needsSwap}`, {
|
|
194
|
+
operation: 'normalizeMinMaxValues'
|
|
195
|
+
});
|
|
196
|
+
} catch (decimalError) {
|
|
197
|
+
logger.warn(`Decimal.js comparison failed for attribute '${attribute.key}': ${decimalError instanceof Error ? decimalError.message : String(decimalError)}`, {
|
|
198
|
+
operation: 'normalizeMinMaxValues'
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
// Log final validation result
|
|
204
|
+
if (needsSwap) {
|
|
205
|
+
logger.error(`Invalid min/max values detected for attribute '${attribute.key}': min (${normalizedMin}) must be less than max (${normalizedMax})`, {
|
|
206
|
+
type,
|
|
207
|
+
min: normalizedMin,
|
|
208
|
+
max: normalizedMax,
|
|
209
|
+
comparisonMethod,
|
|
210
|
+
operation: 'normalizeMinMaxValues'
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
// Swap values to ensure min < max (graceful handling)
|
|
214
|
+
logger.warn(`Swapping min/max values for attribute '${attribute.key}' to fix validation`, {
|
|
215
|
+
type,
|
|
216
|
+
originalMin: normalizedMin,
|
|
217
|
+
originalMax: normalizedMax,
|
|
218
|
+
newMin: normalizedMax,
|
|
219
|
+
newMax: normalizedMin,
|
|
220
|
+
comparisonMethod,
|
|
221
|
+
operation: 'normalizeMinMaxValues'
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
const temp = normalizedMin;
|
|
225
|
+
normalizedMin = normalizedMax;
|
|
226
|
+
normalizedMax = temp;
|
|
227
|
+
} else {
|
|
228
|
+
logger.debug(`Min/max validation passed for attribute '${attribute.key}'`, {
|
|
229
|
+
type,
|
|
230
|
+
min: normalizedMin,
|
|
231
|
+
max: normalizedMax,
|
|
232
|
+
comparisonMethod,
|
|
233
|
+
operation: 'normalizeMinMaxValues'
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
} catch (error) {
|
|
238
|
+
logger.error(`Critical error during min/max validation for attribute '${attribute.key}'`, {
|
|
239
|
+
type,
|
|
240
|
+
min: normalizedMin,
|
|
241
|
+
max: normalizedMax,
|
|
242
|
+
error: error instanceof Error ? error.message : String(error),
|
|
243
|
+
operation: 'normalizeMinMaxValues'
|
|
244
|
+
});
|
|
245
|
+
|
|
246
|
+
// If all comparison methods fail, set both to undefined to avoid API errors
|
|
247
|
+
normalizedMin = undefined;
|
|
248
|
+
normalizedMax = undefined;
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
const result = { min: normalizedMin, max: normalizedMax };
|
|
253
|
+
logger.debug(
|
|
254
|
+
`Min/max normalization complete for attribute '${attribute.key}'`,
|
|
255
|
+
{
|
|
256
|
+
type,
|
|
257
|
+
result,
|
|
258
|
+
operation: "normalizeMinMaxValues",
|
|
259
|
+
}
|
|
260
|
+
);
|
|
261
|
+
|
|
262
|
+
return result;
|
|
263
|
+
};
|
|
264
|
+
|
|
265
|
+
/**
|
|
266
|
+
* Normalizes an attribute for comparison by handling extreme database values
|
|
267
|
+
* This is used when comparing database attributes with config attributes
|
|
268
|
+
*/
|
|
269
|
+
const normalizeAttributeForComparison = (attribute: Attribute): Attribute => {
|
|
270
|
+
const normalized: any = { ...attribute };
|
|
271
|
+
|
|
272
|
+
// Ignore defaults on required attributes to prevent false positives
|
|
273
|
+
if (normalized.required === true && "xdefault" in normalized) {
|
|
274
|
+
delete normalized.xdefault;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
// Normalize min/max for numeric types
|
|
278
|
+
if (hasMinMaxProperties(attribute)) {
|
|
279
|
+
const { min, max } = normalizeMinMaxValues(attribute);
|
|
280
|
+
normalized.min = min;
|
|
281
|
+
normalized.max = max;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
// Remove xdefault if null/undefined to ensure consistent comparison
|
|
285
|
+
// Appwrite sets xdefault: null for required attributes, but config files omit it
|
|
286
|
+
if (
|
|
287
|
+
"xdefault" in normalized &&
|
|
288
|
+
(normalized.xdefault === null || normalized.xdefault === undefined)
|
|
289
|
+
) {
|
|
290
|
+
delete normalized.xdefault;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
return normalized;
|
|
294
|
+
};
|
|
295
|
+
|
|
296
|
+
/**
|
|
297
|
+
* Helper function to create an attribute using either the adapter or legacy API
|
|
298
|
+
*/
|
|
299
|
+
const createAttributeViaAdapter = async (
|
|
300
|
+
db: Databases | DatabaseAdapter,
|
|
301
|
+
dbId: string,
|
|
302
|
+
collectionId: string,
|
|
303
|
+
attribute: Attribute
|
|
304
|
+
): Promise<void> => {
|
|
305
|
+
const startTime = Date.now();
|
|
306
|
+
const adapterType = isDatabaseAdapter(db) ? "adapter" : "legacy";
|
|
307
|
+
|
|
308
|
+
logger.info(`Creating attribute '${attribute.key}' via ${adapterType}`, {
|
|
309
|
+
type: attribute.type,
|
|
310
|
+
dbId,
|
|
311
|
+
collectionId,
|
|
312
|
+
adapterType,
|
|
313
|
+
operation: "createAttributeViaAdapter",
|
|
314
|
+
});
|
|
315
|
+
|
|
316
|
+
if (isDatabaseAdapter(db)) {
|
|
317
|
+
// Use the adapter's unified createAttribute method
|
|
318
|
+
const params: CreateAttributeParams = {
|
|
319
|
+
databaseId: dbId,
|
|
320
|
+
tableId: collectionId,
|
|
321
|
+
key: attribute.key,
|
|
322
|
+
type: attribute.type,
|
|
323
|
+
required: attribute.required || false,
|
|
324
|
+
array: attribute.array || false,
|
|
325
|
+
...((attribute as any).size && { size: (attribute as any).size }),
|
|
326
|
+
...((attribute as any).xdefault !== undefined &&
|
|
327
|
+
!attribute.required && { default: (attribute as any).xdefault }),
|
|
328
|
+
...((attribute as any).encrypted && {
|
|
329
|
+
encrypt: (attribute as any).encrypted,
|
|
330
|
+
}),
|
|
331
|
+
...((attribute as any).min !== undefined && {
|
|
332
|
+
min: (attribute as any).min,
|
|
333
|
+
}),
|
|
334
|
+
...((attribute as any).max !== undefined && {
|
|
335
|
+
max: (attribute as any).max,
|
|
336
|
+
}),
|
|
337
|
+
...((attribute as any).elements && {
|
|
338
|
+
elements: (attribute as any).elements,
|
|
339
|
+
}),
|
|
340
|
+
...((attribute as any).relatedCollection && {
|
|
341
|
+
relatedCollection: (attribute as any).relatedCollection,
|
|
342
|
+
}),
|
|
343
|
+
...((attribute as any).relationType && {
|
|
344
|
+
relationType: (attribute as any).relationType,
|
|
345
|
+
}),
|
|
346
|
+
...((attribute as any).twoWay !== undefined && {
|
|
347
|
+
twoWay: (attribute as any).twoWay,
|
|
348
|
+
}),
|
|
349
|
+
...((attribute as any).onDelete && {
|
|
350
|
+
onDelete: (attribute as any).onDelete,
|
|
351
|
+
}),
|
|
352
|
+
...((attribute as any).twoWayKey && {
|
|
353
|
+
twoWayKey: (attribute as any).twoWayKey,
|
|
354
|
+
}),
|
|
355
|
+
};
|
|
356
|
+
|
|
357
|
+
logger.debug(`Adapter create parameters for '${attribute.key}'`, {
|
|
358
|
+
params,
|
|
359
|
+
operation: "createAttributeViaAdapter",
|
|
360
|
+
});
|
|
361
|
+
|
|
362
|
+
await db.createAttribute(params);
|
|
363
|
+
|
|
364
|
+
const duration = Date.now() - startTime;
|
|
365
|
+
logger.info(
|
|
366
|
+
`Successfully created attribute '${attribute.key}' via adapter`,
|
|
367
|
+
{
|
|
368
|
+
duration,
|
|
369
|
+
operation: "createAttributeViaAdapter",
|
|
370
|
+
}
|
|
371
|
+
);
|
|
372
|
+
} else {
|
|
373
|
+
// Use legacy type-specific methods
|
|
374
|
+
logger.debug(`Using legacy creation for attribute '${attribute.key}'`, {
|
|
375
|
+
operation: "createAttributeViaAdapter",
|
|
376
|
+
});
|
|
377
|
+
await createLegacyAttribute(db, dbId, collectionId, attribute);
|
|
378
|
+
|
|
379
|
+
const duration = Date.now() - startTime;
|
|
380
|
+
logger.info(
|
|
381
|
+
`Successfully created attribute '${attribute.key}' via legacy`,
|
|
382
|
+
{
|
|
383
|
+
duration,
|
|
384
|
+
operation: "createAttributeViaAdapter",
|
|
385
|
+
}
|
|
386
|
+
);
|
|
387
|
+
}
|
|
388
|
+
};
|
|
389
|
+
|
|
390
|
+
/**
|
|
391
|
+
* Helper function to update an attribute using either the adapter or legacy API
|
|
392
|
+
*/
|
|
393
|
+
const updateAttributeViaAdapter = async (
|
|
394
|
+
db: Databases | DatabaseAdapter,
|
|
395
|
+
dbId: string,
|
|
396
|
+
collectionId: string,
|
|
397
|
+
attribute: Attribute
|
|
398
|
+
): Promise<void> => {
|
|
399
|
+
if (isDatabaseAdapter(db)) {
|
|
400
|
+
// Use the adapter's unified updateAttribute method
|
|
401
|
+
const params: UpdateAttributeParams = {
|
|
402
|
+
databaseId: dbId,
|
|
403
|
+
tableId: collectionId,
|
|
404
|
+
key: attribute.key,
|
|
405
|
+
type: attribute.type,
|
|
406
|
+
required: attribute.required || false,
|
|
407
|
+
array: attribute.array || false,
|
|
408
|
+
size: (attribute as any).size,
|
|
409
|
+
min: (attribute as any).min,
|
|
410
|
+
max: (attribute as any).max,
|
|
411
|
+
encrypt: (attribute as any).encrypted ?? (attribute as any).encrypt,
|
|
412
|
+
elements: (attribute as any).elements,
|
|
413
|
+
relatedCollection: (attribute as any).relatedCollection,
|
|
414
|
+
relationType: (attribute as any).relationType,
|
|
415
|
+
twoWay: (attribute as any).twoWay,
|
|
416
|
+
twoWayKey: (attribute as any).twoWayKey,
|
|
417
|
+
onDelete: (attribute as any).onDelete
|
|
418
|
+
};
|
|
419
|
+
if (!attribute.required && (attribute as any).xdefault !== undefined) {
|
|
420
|
+
params.default = (attribute as any).xdefault;
|
|
421
|
+
}
|
|
422
|
+
await db.updateAttribute(params);
|
|
423
|
+
} else {
|
|
424
|
+
// Use legacy type-specific methods
|
|
425
|
+
await updateLegacyAttribute(db, dbId, collectionId, attribute);
|
|
426
|
+
}
|
|
427
|
+
};
|
|
428
|
+
|
|
429
|
+
/**
|
|
430
|
+
* Legacy attribute creation using type-specific methods
|
|
431
|
+
*/
|
|
432
|
+
const createLegacyAttribute = async (
|
|
433
|
+
db: Databases,
|
|
434
|
+
dbId: string,
|
|
435
|
+
collectionId: string,
|
|
436
|
+
attribute: Attribute
|
|
437
|
+
): Promise<void> => {
|
|
438
|
+
const startTime = Date.now();
|
|
439
|
+
const { min: normalizedMin, max: normalizedMax } =
|
|
440
|
+
normalizeMinMaxValues(attribute);
|
|
441
|
+
|
|
442
|
+
logger.info(`Creating legacy attribute '${attribute.key}'`, {
|
|
443
|
+
type: attribute.type,
|
|
444
|
+
dbId,
|
|
445
|
+
collectionId,
|
|
446
|
+
normalizedMin,
|
|
447
|
+
normalizedMax,
|
|
448
|
+
operation: "createLegacyAttribute",
|
|
449
|
+
});
|
|
450
|
+
|
|
451
|
+
switch (attribute.type) {
|
|
452
|
+
case "string":
|
|
453
|
+
const stringParams = {
|
|
454
|
+
size: (attribute as any).size || 255,
|
|
455
|
+
required: attribute.required || false,
|
|
456
|
+
defaultValue:
|
|
457
|
+
(attribute as any).xdefault !== undefined && !attribute.required
|
|
458
|
+
? (attribute as any).xdefault
|
|
459
|
+
: undefined,
|
|
460
|
+
array: attribute.array || false,
|
|
461
|
+
encrypted: (attribute as any).encrypted,
|
|
462
|
+
};
|
|
463
|
+
logger.debug(`Creating string attribute '${attribute.key}'`, {
|
|
464
|
+
...stringParams,
|
|
465
|
+
operation: "createLegacyAttribute",
|
|
466
|
+
});
|
|
467
|
+
await db.createStringAttribute(
|
|
468
|
+
dbId,
|
|
469
|
+
collectionId,
|
|
470
|
+
attribute.key,
|
|
471
|
+
stringParams.size,
|
|
472
|
+
stringParams.required,
|
|
473
|
+
stringParams.defaultValue,
|
|
474
|
+
stringParams.array,
|
|
475
|
+
stringParams.encrypted
|
|
476
|
+
);
|
|
477
|
+
break;
|
|
478
|
+
case "integer":
|
|
479
|
+
const integerParams = {
|
|
480
|
+
required: attribute.required || false,
|
|
481
|
+
min:
|
|
482
|
+
normalizedMin !== undefined
|
|
483
|
+
? parseInt(String(normalizedMin))
|
|
484
|
+
: undefined,
|
|
485
|
+
max:
|
|
486
|
+
normalizedMax !== undefined
|
|
487
|
+
? parseInt(String(normalizedMax))
|
|
488
|
+
: undefined,
|
|
489
|
+
defaultValue:
|
|
490
|
+
(attribute as any).xdefault !== undefined && !attribute.required
|
|
491
|
+
? (attribute as any).xdefault
|
|
492
|
+
: undefined,
|
|
493
|
+
array: attribute.array || false,
|
|
494
|
+
};
|
|
495
|
+
logger.debug(`Creating integer attribute '${attribute.key}'`, {
|
|
496
|
+
...integerParams,
|
|
497
|
+
operation: "createLegacyAttribute",
|
|
498
|
+
});
|
|
499
|
+
await db.createIntegerAttribute(
|
|
500
|
+
dbId,
|
|
501
|
+
collectionId,
|
|
502
|
+
attribute.key,
|
|
503
|
+
integerParams.required,
|
|
504
|
+
integerParams.min,
|
|
505
|
+
integerParams.max,
|
|
506
|
+
integerParams.defaultValue,
|
|
507
|
+
integerParams.array
|
|
508
|
+
);
|
|
509
|
+
break;
|
|
510
|
+
case "double":
|
|
511
|
+
case "float":
|
|
512
|
+
await db.createFloatAttribute(
|
|
513
|
+
dbId,
|
|
514
|
+
collectionId,
|
|
515
|
+
attribute.key,
|
|
516
|
+
attribute.required || false,
|
|
517
|
+
normalizedMin !== undefined ? Number(normalizedMin) : undefined,
|
|
518
|
+
normalizedMax !== undefined ? Number(normalizedMax) : undefined,
|
|
519
|
+
(attribute as any).xdefault !== undefined && !attribute.required
|
|
520
|
+
? (attribute as any).xdefault
|
|
521
|
+
: undefined,
|
|
522
|
+
attribute.array || false
|
|
523
|
+
);
|
|
524
|
+
break;
|
|
525
|
+
case "boolean":
|
|
526
|
+
await db.createBooleanAttribute(
|
|
527
|
+
dbId,
|
|
528
|
+
collectionId,
|
|
529
|
+
attribute.key,
|
|
530
|
+
attribute.required || false,
|
|
531
|
+
(attribute as any).xdefault !== undefined && !attribute.required
|
|
532
|
+
? (attribute as any).xdefault
|
|
533
|
+
: undefined,
|
|
534
|
+
attribute.array || false
|
|
535
|
+
);
|
|
536
|
+
break;
|
|
537
|
+
case "datetime":
|
|
538
|
+
await db.createDatetimeAttribute(
|
|
539
|
+
dbId,
|
|
540
|
+
collectionId,
|
|
541
|
+
attribute.key,
|
|
542
|
+
attribute.required || false,
|
|
543
|
+
(attribute as any).xdefault !== undefined && !attribute.required
|
|
544
|
+
? (attribute as any).xdefault
|
|
545
|
+
: undefined,
|
|
546
|
+
attribute.array || false
|
|
547
|
+
);
|
|
548
|
+
break;
|
|
549
|
+
case "email":
|
|
550
|
+
await db.createEmailAttribute(
|
|
551
|
+
dbId,
|
|
552
|
+
collectionId,
|
|
553
|
+
attribute.key,
|
|
554
|
+
attribute.required || false,
|
|
555
|
+
(attribute as any).xdefault !== undefined && !attribute.required
|
|
556
|
+
? (attribute as any).xdefault
|
|
557
|
+
: undefined,
|
|
558
|
+
attribute.array || false
|
|
559
|
+
);
|
|
560
|
+
break;
|
|
561
|
+
case "ip":
|
|
562
|
+
await db.createIpAttribute(
|
|
563
|
+
dbId,
|
|
564
|
+
collectionId,
|
|
565
|
+
attribute.key,
|
|
566
|
+
attribute.required || false,
|
|
567
|
+
(attribute as any).xdefault !== undefined && !attribute.required
|
|
568
|
+
? (attribute as any).xdefault
|
|
569
|
+
: undefined,
|
|
570
|
+
attribute.array || false
|
|
571
|
+
);
|
|
572
|
+
break;
|
|
573
|
+
case "url":
|
|
574
|
+
await db.createUrlAttribute(
|
|
575
|
+
dbId,
|
|
576
|
+
collectionId,
|
|
577
|
+
attribute.key,
|
|
578
|
+
attribute.required || false,
|
|
579
|
+
(attribute as any).xdefault !== undefined && !attribute.required
|
|
580
|
+
? (attribute as any).xdefault
|
|
581
|
+
: undefined,
|
|
582
|
+
attribute.array || false
|
|
583
|
+
);
|
|
584
|
+
break;
|
|
585
|
+
case "enum":
|
|
586
|
+
await db.createEnumAttribute(
|
|
587
|
+
dbId,
|
|
588
|
+
collectionId,
|
|
589
|
+
attribute.key,
|
|
590
|
+
(attribute as any).elements || [],
|
|
591
|
+
attribute.required || false,
|
|
592
|
+
(attribute as any).xdefault !== undefined && !attribute.required
|
|
593
|
+
? (attribute as any).xdefault
|
|
594
|
+
: undefined,
|
|
595
|
+
attribute.array || false
|
|
596
|
+
);
|
|
597
|
+
break;
|
|
598
|
+
case "relationship":
|
|
599
|
+
await db.createRelationshipAttribute(
|
|
600
|
+
dbId,
|
|
601
|
+
collectionId,
|
|
602
|
+
(attribute as any).relatedCollection!,
|
|
603
|
+
(attribute as any).relationType!,
|
|
604
|
+
(attribute as any).twoWay,
|
|
605
|
+
attribute.key,
|
|
606
|
+
(attribute as any).twoWayKey,
|
|
607
|
+
(attribute as any).onDelete
|
|
608
|
+
);
|
|
609
|
+
break;
|
|
610
|
+
default:
|
|
611
|
+
const error = new Error(
|
|
612
|
+
`Unsupported attribute type: ${(attribute as any).type}`
|
|
613
|
+
);
|
|
614
|
+
logger.error(
|
|
615
|
+
`Unsupported attribute type for '${(attribute as any).key}'`,
|
|
616
|
+
{
|
|
617
|
+
type: (attribute as any).type,
|
|
618
|
+
supportedTypes: [
|
|
619
|
+
"string",
|
|
620
|
+
"integer",
|
|
621
|
+
"double",
|
|
622
|
+
"float",
|
|
623
|
+
"boolean",
|
|
624
|
+
"datetime",
|
|
625
|
+
"email",
|
|
626
|
+
"ip",
|
|
627
|
+
"url",
|
|
628
|
+
"enum",
|
|
629
|
+
"relationship",
|
|
630
|
+
],
|
|
631
|
+
operation: "createLegacyAttribute",
|
|
632
|
+
}
|
|
633
|
+
);
|
|
634
|
+
throw error;
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
const duration = Date.now() - startTime;
|
|
638
|
+
logger.info(`Successfully created legacy attribute '${attribute.key}'`, {
|
|
639
|
+
type: attribute.type,
|
|
640
|
+
duration,
|
|
641
|
+
operation: "createLegacyAttribute",
|
|
642
|
+
});
|
|
643
|
+
};
|
|
644
|
+
|
|
645
|
+
/**
|
|
646
|
+
* Legacy attribute update using type-specific methods
|
|
647
|
+
*/
|
|
648
|
+
const updateLegacyAttribute = async (
|
|
649
|
+
db: Databases,
|
|
650
|
+
dbId: string,
|
|
651
|
+
collectionId: string,
|
|
652
|
+
attribute: Attribute
|
|
653
|
+
): Promise<void> => {
|
|
654
|
+
console.log(`DEBUG updateLegacyAttribute before normalizeMinMaxValues:`, {
|
|
655
|
+
key: attribute.key,
|
|
656
|
+
type: attribute.type,
|
|
657
|
+
min: (attribute as any).min,
|
|
658
|
+
max: (attribute as any).max
|
|
659
|
+
});
|
|
660
|
+
|
|
661
|
+
const { min: normalizedMin, max: normalizedMax } =
|
|
662
|
+
normalizeMinMaxValues(attribute);
|
|
663
|
+
|
|
664
|
+
|
|
665
|
+
|
|
666
|
+
switch (attribute.type) {
|
|
667
|
+
case "string":
|
|
668
|
+
await db.updateStringAttribute(
|
|
669
|
+
dbId,
|
|
670
|
+
collectionId,
|
|
671
|
+
attribute.key,
|
|
672
|
+
attribute.required || false,
|
|
673
|
+
!attribute.required && (attribute as any).xdefault !== undefined
|
|
674
|
+
? (attribute as any).xdefault
|
|
675
|
+
: null,
|
|
676
|
+
attribute.size
|
|
677
|
+
);
|
|
678
|
+
break;
|
|
679
|
+
case "integer":
|
|
680
|
+
await db.updateIntegerAttribute(
|
|
681
|
+
dbId,
|
|
682
|
+
collectionId,
|
|
683
|
+
attribute.key,
|
|
684
|
+
attribute.required || false,
|
|
685
|
+
!attribute.required && (attribute as any).xdefault !== undefined
|
|
686
|
+
? (attribute as any).xdefault
|
|
687
|
+
: null,
|
|
688
|
+
normalizedMin !== undefined
|
|
689
|
+
? parseInt(String(normalizedMin))
|
|
690
|
+
: undefined,
|
|
691
|
+
normalizedMax !== undefined
|
|
692
|
+
? parseInt(String(normalizedMax))
|
|
693
|
+
: undefined
|
|
694
|
+
);
|
|
695
|
+
break;
|
|
696
|
+
case "double":
|
|
697
|
+
case "float":
|
|
698
|
+
const minParam = normalizedMin !== undefined ? Number(normalizedMin) : undefined;
|
|
699
|
+
const maxParam = normalizedMax !== undefined ? Number(normalizedMax) : undefined;
|
|
700
|
+
|
|
701
|
+
|
|
702
|
+
|
|
703
|
+
await db.updateFloatAttribute(
|
|
704
|
+
dbId,
|
|
705
|
+
collectionId,
|
|
706
|
+
attribute.key,
|
|
707
|
+
attribute.required || false,
|
|
708
|
+
minParam,
|
|
709
|
+
maxParam,
|
|
710
|
+
!attribute.required && (attribute as any).xdefault !== undefined
|
|
711
|
+
? (attribute as any).xdefault
|
|
712
|
+
: null
|
|
713
|
+
);
|
|
714
|
+
break;
|
|
715
|
+
case "boolean":
|
|
716
|
+
await db.updateBooleanAttribute(
|
|
717
|
+
dbId,
|
|
718
|
+
collectionId,
|
|
719
|
+
attribute.key,
|
|
720
|
+
attribute.required || false,
|
|
721
|
+
!attribute.required && (attribute as any).xdefault !== undefined
|
|
722
|
+
? (attribute as any).xdefault
|
|
723
|
+
: null
|
|
724
|
+
);
|
|
725
|
+
break;
|
|
726
|
+
case "datetime":
|
|
727
|
+
await db.updateDatetimeAttribute(
|
|
728
|
+
dbId,
|
|
729
|
+
collectionId,
|
|
730
|
+
attribute.key,
|
|
731
|
+
attribute.required || false,
|
|
732
|
+
!attribute.required && (attribute as any).xdefault !== undefined
|
|
733
|
+
? (attribute as any).xdefault
|
|
734
|
+
: null
|
|
735
|
+
);
|
|
736
|
+
break;
|
|
737
|
+
case "email":
|
|
738
|
+
await db.updateEmailAttribute(
|
|
739
|
+
dbId,
|
|
740
|
+
collectionId,
|
|
741
|
+
attribute.key,
|
|
742
|
+
attribute.required || false,
|
|
743
|
+
!attribute.required && (attribute as any).xdefault !== undefined
|
|
744
|
+
? (attribute as any).xdefault
|
|
745
|
+
: null
|
|
746
|
+
);
|
|
747
|
+
break;
|
|
748
|
+
case "ip":
|
|
749
|
+
await db.updateIpAttribute(
|
|
750
|
+
dbId,
|
|
751
|
+
collectionId,
|
|
752
|
+
attribute.key,
|
|
753
|
+
attribute.required || false,
|
|
754
|
+
!attribute.required && (attribute as any).xdefault !== undefined
|
|
755
|
+
? (attribute as any).xdefault
|
|
756
|
+
: null
|
|
757
|
+
);
|
|
758
|
+
break;
|
|
759
|
+
case "url":
|
|
760
|
+
await db.updateUrlAttribute(
|
|
761
|
+
dbId,
|
|
762
|
+
collectionId,
|
|
763
|
+
attribute.key,
|
|
764
|
+
attribute.required || false,
|
|
765
|
+
!attribute.required && (attribute as any).xdefault !== undefined
|
|
766
|
+
? (attribute as any).xdefault
|
|
767
|
+
: null
|
|
768
|
+
);
|
|
769
|
+
break;
|
|
770
|
+
case "enum":
|
|
771
|
+
await db.updateEnumAttribute(
|
|
772
|
+
dbId,
|
|
773
|
+
collectionId,
|
|
774
|
+
attribute.key,
|
|
775
|
+
(attribute as any).elements || [],
|
|
776
|
+
attribute.required || false,
|
|
777
|
+
!attribute.required && (attribute as any).xdefault !== undefined
|
|
778
|
+
? (attribute as any).xdefault
|
|
779
|
+
: null
|
|
780
|
+
);
|
|
781
|
+
break;
|
|
782
|
+
case "relationship":
|
|
783
|
+
await db.updateRelationshipAttribute(
|
|
784
|
+
dbId,
|
|
785
|
+
collectionId,
|
|
786
|
+
attribute.key,
|
|
787
|
+
(attribute as any).onDelete
|
|
788
|
+
);
|
|
789
|
+
break;
|
|
790
|
+
default:
|
|
791
|
+
throw new Error(
|
|
792
|
+
`Unsupported attribute type for update: ${(attribute as any).type}`
|
|
793
|
+
);
|
|
794
|
+
}
|
|
795
|
+
};
|
|
796
|
+
|
|
797
|
+
// Interface for attribute with status (fixing the type issue)
|
|
798
|
+
interface AttributeWithStatus {
|
|
799
|
+
key: string;
|
|
800
|
+
type: string;
|
|
801
|
+
status: "available" | "processing" | "deleting" | "stuck" | "failed";
|
|
802
|
+
error: string;
|
|
803
|
+
required: boolean;
|
|
804
|
+
array?: boolean;
|
|
805
|
+
$createdAt: string;
|
|
806
|
+
$updatedAt: string;
|
|
807
|
+
[key: string]: any; // For type-specific fields
|
|
808
|
+
}
|
|
809
|
+
|
|
810
|
+
/**
|
|
811
|
+
* Wait for attribute to become available, with retry logic for stuck attributes and exponential backoff
|
|
812
|
+
*/
|
|
813
|
+
const waitForAttributeAvailable = async (
|
|
814
|
+
db: Databases | DatabaseAdapter,
|
|
815
|
+
dbId: string,
|
|
816
|
+
collectionId: string,
|
|
817
|
+
attributeKey: string,
|
|
818
|
+
maxWaitTime: number = 60000, // 1 minute
|
|
819
|
+
retryCount: number = 0,
|
|
820
|
+
maxRetries: number = 5
|
|
821
|
+
): Promise<boolean> => {
|
|
822
|
+
const startTime = Date.now();
|
|
823
|
+
let checkInterval = 2000; // Start with 2 seconds
|
|
824
|
+
|
|
825
|
+
logger.info(`Waiting for attribute '${attributeKey}' to become available`, {
|
|
826
|
+
dbId,
|
|
827
|
+
collectionId,
|
|
828
|
+
maxWaitTime,
|
|
829
|
+
retryCount,
|
|
830
|
+
maxRetries,
|
|
831
|
+
operation: "waitForAttributeAvailable",
|
|
832
|
+
});
|
|
833
|
+
|
|
834
|
+
// Calculate exponential backoff: 2s, 4s, 8s, 16s, 30s (capped at 30s)
|
|
835
|
+
if (retryCount > 0) {
|
|
836
|
+
const exponentialDelay = calculateExponentialBackoff(retryCount);
|
|
837
|
+
await delay(exponentialDelay);
|
|
838
|
+
}
|
|
839
|
+
|
|
840
|
+
while (Date.now() - startTime < maxWaitTime) {
|
|
841
|
+
try {
|
|
842
|
+
const collection = isDatabaseAdapter(db)
|
|
843
|
+
? (await db.getTable({ databaseId: dbId, tableId: collectionId })).data
|
|
844
|
+
: await db.getCollection(dbId, collectionId);
|
|
845
|
+
const attribute = (collection.attributes as any[]).find(
|
|
846
|
+
(attr: AttributeWithStatus) => attr.key === attributeKey
|
|
847
|
+
) as AttributeWithStatus | undefined;
|
|
848
|
+
|
|
849
|
+
if (!attribute) {
|
|
850
|
+
MessageFormatter.error(`Attribute '${attributeKey}' not found`);
|
|
851
|
+
return false;
|
|
852
|
+
}
|
|
853
|
+
|
|
854
|
+
const statusInfo = {
|
|
855
|
+
attributeKey,
|
|
856
|
+
status: attribute.status,
|
|
857
|
+
error: attribute.error,
|
|
858
|
+
dbId,
|
|
859
|
+
collectionId,
|
|
860
|
+
waitTime: Date.now() - startTime,
|
|
861
|
+
operation: "waitForAttributeAvailable",
|
|
862
|
+
};
|
|
863
|
+
|
|
864
|
+
switch (attribute.status) {
|
|
865
|
+
case "available":
|
|
866
|
+
logger.info(
|
|
867
|
+
`Attribute '${attributeKey}' became available`,
|
|
868
|
+
statusInfo
|
|
869
|
+
);
|
|
870
|
+
return true;
|
|
871
|
+
|
|
872
|
+
case "failed":
|
|
873
|
+
logger.error(`Attribute '${attributeKey}' failed`, statusInfo);
|
|
874
|
+
return false;
|
|
875
|
+
|
|
876
|
+
case "stuck":
|
|
877
|
+
logger.warn(`Attribute '${attributeKey}' is stuck`, statusInfo);
|
|
878
|
+
return false;
|
|
879
|
+
|
|
880
|
+
case "processing":
|
|
881
|
+
// Continue waiting
|
|
882
|
+
logger.debug(
|
|
883
|
+
`Attribute '${attributeKey}' still processing`,
|
|
884
|
+
statusInfo
|
|
885
|
+
);
|
|
886
|
+
break;
|
|
887
|
+
|
|
888
|
+
case "deleting":
|
|
889
|
+
MessageFormatter.info(
|
|
890
|
+
chalk.yellow(`Attribute '${attributeKey}' is being deleted`)
|
|
891
|
+
);
|
|
892
|
+
logger.warn(
|
|
893
|
+
`Attribute '${attributeKey}' is being deleted`,
|
|
894
|
+
statusInfo
|
|
895
|
+
);
|
|
896
|
+
break;
|
|
897
|
+
|
|
898
|
+
default:
|
|
899
|
+
MessageFormatter.info(
|
|
900
|
+
chalk.yellow(
|
|
901
|
+
`Unknown status '${attribute.status}' for attribute '${attributeKey}'`
|
|
902
|
+
)
|
|
903
|
+
);
|
|
904
|
+
logger.warn(
|
|
905
|
+
`Unknown status for attribute '${attributeKey}'`,
|
|
906
|
+
statusInfo
|
|
907
|
+
);
|
|
908
|
+
break;
|
|
909
|
+
}
|
|
910
|
+
|
|
911
|
+
await delay(checkInterval);
|
|
912
|
+
} catch (error) {
|
|
913
|
+
const errorMessage =
|
|
914
|
+
error instanceof Error ? error.message : String(error);
|
|
915
|
+
MessageFormatter.error(
|
|
916
|
+
`Error checking attribute status: ${errorMessage}`
|
|
917
|
+
);
|
|
918
|
+
|
|
919
|
+
logger.error("Error checking attribute status", {
|
|
920
|
+
attributeKey,
|
|
921
|
+
dbId,
|
|
922
|
+
collectionId,
|
|
923
|
+
error: errorMessage,
|
|
924
|
+
waitTime: Date.now() - startTime,
|
|
925
|
+
operation: "waitForAttributeAvailable",
|
|
926
|
+
});
|
|
927
|
+
|
|
928
|
+
return false;
|
|
929
|
+
}
|
|
930
|
+
}
|
|
931
|
+
|
|
932
|
+
// Timeout reached
|
|
933
|
+
MessageFormatter.info(
|
|
934
|
+
chalk.yellow(
|
|
935
|
+
`⏰ Timeout waiting for attribute '${attributeKey}' (${maxWaitTime}ms)`
|
|
936
|
+
)
|
|
937
|
+
);
|
|
938
|
+
|
|
939
|
+
// If we have retries left and this isn't the last retry, try recreating
|
|
940
|
+
if (retryCount < maxRetries) {
|
|
941
|
+
MessageFormatter.info(
|
|
942
|
+
chalk.yellow(
|
|
943
|
+
`🔄 Retrying attribute creation (attempt ${
|
|
944
|
+
retryCount + 1
|
|
945
|
+
}/${maxRetries})`
|
|
946
|
+
)
|
|
947
|
+
);
|
|
948
|
+
return false; // Signal that we need to retry
|
|
949
|
+
}
|
|
950
|
+
|
|
951
|
+
return false;
|
|
952
|
+
};
|
|
953
|
+
|
|
954
|
+
/**
|
|
955
|
+
* Wait for all attributes in a collection to become available
|
|
956
|
+
*/
|
|
957
|
+
const waitForAllAttributesAvailable = async (
|
|
958
|
+
db: Databases | DatabaseAdapter,
|
|
959
|
+
dbId: string,
|
|
960
|
+
collectionId: string,
|
|
961
|
+
attributeKeys: string[],
|
|
962
|
+
maxWaitTime: number = 60000
|
|
963
|
+
): Promise<string[]> => {
|
|
964
|
+
MessageFormatter.info(
|
|
965
|
+
chalk.blue(
|
|
966
|
+
`Waiting for ${attributeKeys.length} attributes to become available...`
|
|
967
|
+
)
|
|
968
|
+
);
|
|
969
|
+
|
|
970
|
+
const failedAttributes: string[] = [];
|
|
971
|
+
|
|
972
|
+
for (const attributeKey of attributeKeys) {
|
|
973
|
+
const success = await waitForAttributeAvailable(
|
|
974
|
+
db,
|
|
975
|
+
dbId,
|
|
976
|
+
collectionId,
|
|
977
|
+
attributeKey,
|
|
978
|
+
maxWaitTime
|
|
979
|
+
);
|
|
980
|
+
if (!success) {
|
|
981
|
+
failedAttributes.push(attributeKey);
|
|
982
|
+
}
|
|
983
|
+
}
|
|
984
|
+
|
|
985
|
+
return failedAttributes;
|
|
986
|
+
};
|
|
987
|
+
|
|
988
|
+
/**
|
|
989
|
+
* Delete collection and recreate with retry logic
|
|
990
|
+
*/
|
|
991
|
+
const deleteAndRecreateCollection = async (
|
|
992
|
+
db: Databases | DatabaseAdapter,
|
|
993
|
+
dbId: string,
|
|
994
|
+
collection: Models.Collection,
|
|
995
|
+
retryCount: number
|
|
996
|
+
): Promise<Models.Collection | null> => {
|
|
997
|
+
try {
|
|
998
|
+
MessageFormatter.info(
|
|
999
|
+
chalk.yellow(
|
|
1000
|
+
`🗑️ Deleting collection '${collection.name}' for retry ${retryCount}`
|
|
1001
|
+
)
|
|
1002
|
+
);
|
|
1003
|
+
|
|
1004
|
+
// Delete the collection
|
|
1005
|
+
if (isDatabaseAdapter(db)) {
|
|
1006
|
+
await db.deleteTable({ databaseId: dbId, tableId: collection.$id });
|
|
1007
|
+
} else {
|
|
1008
|
+
await db.deleteCollection(dbId, collection.$id);
|
|
1009
|
+
}
|
|
1010
|
+
MessageFormatter.warning(`Deleted collection '${collection.name}'`);
|
|
1011
|
+
|
|
1012
|
+
// Wait a bit before recreating
|
|
1013
|
+
await delay(2000);
|
|
1014
|
+
|
|
1015
|
+
// Recreate the collection
|
|
1016
|
+
MessageFormatter.info(`🔄 Recreating collection '${collection.name}'`);
|
|
1017
|
+
const newCollection = isDatabaseAdapter(db)
|
|
1018
|
+
? (
|
|
1019
|
+
await db.createTable({
|
|
1020
|
+
databaseId: dbId,
|
|
1021
|
+
id: collection.$id,
|
|
1022
|
+
name: collection.name,
|
|
1023
|
+
permissions: collection.$permissions,
|
|
1024
|
+
documentSecurity: collection.documentSecurity,
|
|
1025
|
+
enabled: collection.enabled,
|
|
1026
|
+
})
|
|
1027
|
+
).data
|
|
1028
|
+
: await db.createCollection(
|
|
1029
|
+
dbId,
|
|
1030
|
+
collection.$id,
|
|
1031
|
+
collection.name,
|
|
1032
|
+
collection.$permissions,
|
|
1033
|
+
collection.documentSecurity,
|
|
1034
|
+
collection.enabled
|
|
1035
|
+
);
|
|
1036
|
+
|
|
1037
|
+
MessageFormatter.success(`✅ Recreated collection '${collection.name}'`);
|
|
1038
|
+
return newCollection;
|
|
1039
|
+
} catch (error) {
|
|
1040
|
+
MessageFormatter.info(
|
|
1041
|
+
chalk.red(
|
|
1042
|
+
`Failed to delete/recreate collection '${collection.name}': ${error}`
|
|
1043
|
+
)
|
|
1044
|
+
);
|
|
1045
|
+
return null;
|
|
1046
|
+
}
|
|
1047
|
+
};
|
|
1048
|
+
|
|
1049
|
+
/**
|
|
1050
|
+
* Get the fields that should be compared for a specific attribute type
|
|
1051
|
+
* Only returns fields that are valid for the given type to avoid false positives
|
|
1052
|
+
*/
|
|
1053
|
+
const getComparableFields = (type: string): string[] => {
|
|
1054
|
+
const baseFields = ["key", "type", "array", "required", "xdefault"];
|
|
1055
|
+
|
|
1056
|
+
switch (type) {
|
|
1057
|
+
case "string":
|
|
1058
|
+
return [...baseFields, "size", "encrypted"];
|
|
1059
|
+
|
|
1060
|
+
case "integer":
|
|
1061
|
+
case "double":
|
|
1062
|
+
case "float":
|
|
1063
|
+
return [...baseFields, "min", "max"];
|
|
1064
|
+
|
|
1065
|
+
case "enum":
|
|
1066
|
+
return [...baseFields, "elements"];
|
|
1067
|
+
|
|
1068
|
+
case "relationship":
|
|
1069
|
+
return [
|
|
1070
|
+
...baseFields,
|
|
1071
|
+
"relationType",
|
|
1072
|
+
"twoWay",
|
|
1073
|
+
"twoWayKey",
|
|
1074
|
+
"onDelete",
|
|
1075
|
+
"relatedCollection",
|
|
1076
|
+
];
|
|
1077
|
+
|
|
1078
|
+
case "boolean":
|
|
1079
|
+
case "datetime":
|
|
1080
|
+
case "email":
|
|
1081
|
+
case "ip":
|
|
1082
|
+
case "url":
|
|
1083
|
+
return baseFields;
|
|
1084
|
+
|
|
1085
|
+
default:
|
|
1086
|
+
// Fallback to all fields for unknown types
|
|
1087
|
+
return [
|
|
1088
|
+
"key",
|
|
1089
|
+
"type",
|
|
1090
|
+
"array",
|
|
1091
|
+
"encrypted",
|
|
1092
|
+
"required",
|
|
1093
|
+
"size",
|
|
1094
|
+
"min",
|
|
1095
|
+
"max",
|
|
1096
|
+
"xdefault",
|
|
1097
|
+
"elements",
|
|
1098
|
+
"relationType",
|
|
1099
|
+
"twoWay",
|
|
1100
|
+
"twoWayKey",
|
|
1101
|
+
"onDelete",
|
|
1102
|
+
"relatedCollection",
|
|
1103
|
+
];
|
|
1104
|
+
}
|
|
1105
|
+
};
|
|
1106
|
+
|
|
1107
|
+
const attributesSame = (
|
|
1108
|
+
databaseAttribute: Attribute,
|
|
1109
|
+
configAttribute: Attribute
|
|
1110
|
+
): boolean => {
|
|
1111
|
+
// Normalize both attributes for comparison (handle extreme database values)
|
|
1112
|
+
const normalizedDbAttr = normalizeAttributeForComparison(databaseAttribute);
|
|
1113
|
+
const normalizedConfigAttr = normalizeAttributeForComparison(configAttribute);
|
|
1114
|
+
|
|
1115
|
+
// Use type-specific field list to avoid false positives from irrelevant fields
|
|
1116
|
+
const attributesToCheck = getComparableFields(normalizedConfigAttr.type);
|
|
1117
|
+
const fieldsToCheck = attributesToCheck.filter((attr) => {
|
|
1118
|
+
if (attr !== "xdefault") {
|
|
1119
|
+
return true;
|
|
1120
|
+
}
|
|
1121
|
+
const dbRequired = Boolean((normalizedDbAttr as any).required);
|
|
1122
|
+
const configRequired = Boolean((normalizedConfigAttr as any).required);
|
|
1123
|
+
return !(dbRequired || configRequired);
|
|
1124
|
+
});
|
|
1125
|
+
|
|
1126
|
+
const differences: string[] = [];
|
|
1127
|
+
|
|
1128
|
+
const result = fieldsToCheck.every((attr) => {
|
|
1129
|
+
// Check if both objects have the attribute
|
|
1130
|
+
const dbHasAttr = attr in normalizedDbAttr;
|
|
1131
|
+
const configHasAttr = attr in normalizedConfigAttr;
|
|
1132
|
+
|
|
1133
|
+
// If both have the attribute, compare values
|
|
1134
|
+
if (dbHasAttr && configHasAttr) {
|
|
1135
|
+
const dbValue = normalizedDbAttr[attr as keyof typeof normalizedDbAttr];
|
|
1136
|
+
const configValue =
|
|
1137
|
+
normalizedConfigAttr[attr as keyof typeof normalizedConfigAttr];
|
|
1138
|
+
|
|
1139
|
+
// Consider undefined and null as equivalent
|
|
1140
|
+
if (
|
|
1141
|
+
(dbValue === undefined || dbValue === null) &&
|
|
1142
|
+
(configValue === undefined || configValue === null)
|
|
1143
|
+
) {
|
|
1144
|
+
return true;
|
|
1145
|
+
}
|
|
1146
|
+
|
|
1147
|
+
// Normalize booleans: treat undefined and false as equivalent
|
|
1148
|
+
if (typeof dbValue === "boolean" || typeof configValue === "boolean") {
|
|
1149
|
+
const boolMatch = Boolean(dbValue) === Boolean(configValue);
|
|
1150
|
+
if (!boolMatch) {
|
|
1151
|
+
differences.push(`${attr}: db=${dbValue} config=${configValue}`);
|
|
1152
|
+
}
|
|
1153
|
+
return boolMatch;
|
|
1154
|
+
}
|
|
1155
|
+
// For numeric comparisons, compare numbers if both are numeric-like
|
|
1156
|
+
if (
|
|
1157
|
+
(typeof dbValue === "number" ||
|
|
1158
|
+
(typeof dbValue === "string" &&
|
|
1159
|
+
dbValue !== "" &&
|
|
1160
|
+
!isNaN(Number(dbValue)))) &&
|
|
1161
|
+
(typeof configValue === "number" ||
|
|
1162
|
+
(typeof configValue === "string" &&
|
|
1163
|
+
configValue !== "" &&
|
|
1164
|
+
!isNaN(Number(configValue))))
|
|
1165
|
+
) {
|
|
1166
|
+
const numMatch = Number(dbValue) === Number(configValue);
|
|
1167
|
+
if (!numMatch) {
|
|
1168
|
+
differences.push(`${attr}: db=${dbValue} config=${configValue}`);
|
|
1169
|
+
}
|
|
1170
|
+
return numMatch;
|
|
1171
|
+
}
|
|
1172
|
+
|
|
1173
|
+
// For array comparisons (e.g., enum elements), use order-independent equality
|
|
1174
|
+
if (Array.isArray(dbValue) && Array.isArray(configValue)) {
|
|
1175
|
+
const arrayMatch =
|
|
1176
|
+
dbValue.length === configValue.length &&
|
|
1177
|
+
dbValue.every((val) => configValue.includes(val));
|
|
1178
|
+
if (!arrayMatch) {
|
|
1179
|
+
differences.push(
|
|
1180
|
+
`${attr}: db=${JSON.stringify(dbValue)} config=${JSON.stringify(
|
|
1181
|
+
configValue
|
|
1182
|
+
)}`
|
|
1183
|
+
);
|
|
1184
|
+
}
|
|
1185
|
+
return arrayMatch;
|
|
1186
|
+
}
|
|
1187
|
+
|
|
1188
|
+
const match = dbValue === configValue;
|
|
1189
|
+
if (!match) {
|
|
1190
|
+
differences.push(
|
|
1191
|
+
`${attr}: db=${JSON.stringify(dbValue)} config=${JSON.stringify(
|
|
1192
|
+
configValue
|
|
1193
|
+
)}`
|
|
1194
|
+
);
|
|
1195
|
+
}
|
|
1196
|
+
return match;
|
|
1197
|
+
}
|
|
1198
|
+
|
|
1199
|
+
// If neither has the attribute, consider it the same
|
|
1200
|
+
if (!dbHasAttr && !configHasAttr) {
|
|
1201
|
+
return true;
|
|
1202
|
+
}
|
|
1203
|
+
|
|
1204
|
+
// If one has the attribute and the other doesn't, check if it's undefined or null
|
|
1205
|
+
if (dbHasAttr && !configHasAttr) {
|
|
1206
|
+
const dbValue = normalizedDbAttr[attr as keyof typeof normalizedDbAttr];
|
|
1207
|
+
// Consider default-false booleans as equal to missing in config
|
|
1208
|
+
if (typeof dbValue === "boolean") {
|
|
1209
|
+
const match = dbValue === false; // missing in config equals false in db
|
|
1210
|
+
if (!match) {
|
|
1211
|
+
differences.push(`${attr}: db=${dbValue} config=<missing>`);
|
|
1212
|
+
}
|
|
1213
|
+
return match;
|
|
1214
|
+
}
|
|
1215
|
+
const match = dbValue === undefined || dbValue === null;
|
|
1216
|
+
if (!match) {
|
|
1217
|
+
differences.push(
|
|
1218
|
+
`${attr}: db=${JSON.stringify(dbValue)} config=<missing>`
|
|
1219
|
+
);
|
|
1220
|
+
}
|
|
1221
|
+
return match;
|
|
1222
|
+
}
|
|
1223
|
+
|
|
1224
|
+
if (!dbHasAttr && configHasAttr) {
|
|
1225
|
+
const configValue =
|
|
1226
|
+
normalizedConfigAttr[attr as keyof typeof normalizedConfigAttr];
|
|
1227
|
+
// Consider default-false booleans as equal to missing in db
|
|
1228
|
+
if (typeof configValue === "boolean") {
|
|
1229
|
+
const match = configValue === false; // missing in db equals false in config
|
|
1230
|
+
if (!match) {
|
|
1231
|
+
differences.push(`${attr}: db=<missing> config=${configValue}`);
|
|
1232
|
+
}
|
|
1233
|
+
return match;
|
|
1234
|
+
}
|
|
1235
|
+
const match = configValue === undefined || configValue === null;
|
|
1236
|
+
if (!match) {
|
|
1237
|
+
differences.push(
|
|
1238
|
+
`${attr}: db=<missing> config=${JSON.stringify(configValue)}`
|
|
1239
|
+
);
|
|
1240
|
+
}
|
|
1241
|
+
return match;
|
|
1242
|
+
}
|
|
1243
|
+
|
|
1244
|
+
// If we reach here, the attributes are different
|
|
1245
|
+
differences.push(`${attr}: unexpected comparison state`);
|
|
1246
|
+
return false;
|
|
1247
|
+
});
|
|
1248
|
+
|
|
1249
|
+
if (!result && differences.length > 0) {
|
|
1250
|
+
logger.debug(
|
|
1251
|
+
`Attribute mismatch detected for '${normalizedConfigAttr.key}'`,
|
|
1252
|
+
{
|
|
1253
|
+
differences,
|
|
1254
|
+
dbAttribute: normalizedDbAttr,
|
|
1255
|
+
configAttribute: normalizedConfigAttr,
|
|
1256
|
+
operation: "attributesSame",
|
|
1257
|
+
}
|
|
1258
|
+
);
|
|
1259
|
+
}
|
|
1260
|
+
|
|
1261
|
+
return result;
|
|
1262
|
+
};
|
|
1263
|
+
|
|
1264
|
+
/**
|
|
1265
|
+
* Enhanced attribute creation with proper status monitoring and retry logic
|
|
1266
|
+
*/
|
|
1267
|
+
export const createOrUpdateAttributeWithStatusCheck = async (
|
|
1268
|
+
db: Databases | DatabaseAdapter,
|
|
1269
|
+
dbId: string,
|
|
1270
|
+
collection: Models.Collection,
|
|
1271
|
+
attribute: Attribute,
|
|
1272
|
+
retryCount: number = 0,
|
|
1273
|
+
maxRetries: number = 5
|
|
1274
|
+
): Promise<boolean> => {
|
|
1275
|
+
try {
|
|
1276
|
+
// First, try to create/update the attribute using existing logic
|
|
1277
|
+
const result = await createOrUpdateAttribute(
|
|
1278
|
+
db,
|
|
1279
|
+
dbId,
|
|
1280
|
+
collection,
|
|
1281
|
+
attribute
|
|
1282
|
+
);
|
|
1283
|
+
|
|
1284
|
+
// If the attribute was queued (relationship dependency unresolved),
|
|
1285
|
+
// skip status polling and retry logic — the queue will handle it later.
|
|
1286
|
+
if (result === "queued") {
|
|
1287
|
+
MessageFormatter.info(
|
|
1288
|
+
chalk.yellow(
|
|
1289
|
+
`⏭️ Deferred relationship attribute '${attribute.key}' — queued for later once dependencies are available`
|
|
1290
|
+
)
|
|
1291
|
+
);
|
|
1292
|
+
return true;
|
|
1293
|
+
}
|
|
1294
|
+
|
|
1295
|
+
// If collection creation failed, return false to indicate failure
|
|
1296
|
+
if (result === "error") {
|
|
1297
|
+
MessageFormatter.error(
|
|
1298
|
+
`Failed to create collection for attribute '${attribute.key}'`
|
|
1299
|
+
);
|
|
1300
|
+
return false;
|
|
1301
|
+
}
|
|
1302
|
+
|
|
1303
|
+
// Now wait for the attribute to become available
|
|
1304
|
+
const success = await waitForAttributeAvailable(
|
|
1305
|
+
db,
|
|
1306
|
+
dbId,
|
|
1307
|
+
collection.$id,
|
|
1308
|
+
attribute.key,
|
|
1309
|
+
60000, // 1 minute timeout
|
|
1310
|
+
retryCount,
|
|
1311
|
+
maxRetries
|
|
1312
|
+
);
|
|
1313
|
+
|
|
1314
|
+
if (success) {
|
|
1315
|
+
return true;
|
|
1316
|
+
}
|
|
1317
|
+
|
|
1318
|
+
// If not successful and we have retries left, delete specific attribute and try again
|
|
1319
|
+
if (retryCount < maxRetries) {
|
|
1320
|
+
MessageFormatter.info(
|
|
1321
|
+
chalk.yellow(
|
|
1322
|
+
`Attribute '${attribute.key}' failed/stuck, deleting and retrying...`
|
|
1323
|
+
)
|
|
1324
|
+
);
|
|
1325
|
+
|
|
1326
|
+
// Try to delete the specific stuck attribute instead of the entire collection
|
|
1327
|
+
try {
|
|
1328
|
+
if (isDatabaseAdapter(db)) {
|
|
1329
|
+
await db.deleteAttribute({
|
|
1330
|
+
databaseId: dbId,
|
|
1331
|
+
tableId: collection.$id,
|
|
1332
|
+
key: attribute.key,
|
|
1333
|
+
});
|
|
1334
|
+
} else {
|
|
1335
|
+
await db.deleteAttribute(dbId, collection.$id, attribute.key);
|
|
1336
|
+
}
|
|
1337
|
+
MessageFormatter.info(
|
|
1338
|
+
chalk.yellow(
|
|
1339
|
+
`Deleted stuck attribute '${attribute.key}', will retry creation`
|
|
1340
|
+
)
|
|
1341
|
+
);
|
|
1342
|
+
|
|
1343
|
+
// Wait a bit before retry
|
|
1344
|
+
await delay(3000);
|
|
1345
|
+
|
|
1346
|
+
// Get fresh collection data
|
|
1347
|
+
const freshCollection = isDatabaseAdapter(db)
|
|
1348
|
+
? (await db.getTable({ databaseId: dbId, tableId: collection.$id }))
|
|
1349
|
+
.data
|
|
1350
|
+
: await db.getCollection(dbId, collection.$id);
|
|
1351
|
+
|
|
1352
|
+
// Retry with the same collection (attribute should be gone now)
|
|
1353
|
+
return await createOrUpdateAttributeWithStatusCheck(
|
|
1354
|
+
db,
|
|
1355
|
+
dbId,
|
|
1356
|
+
freshCollection,
|
|
1357
|
+
attribute,
|
|
1358
|
+
retryCount + 1,
|
|
1359
|
+
maxRetries
|
|
1360
|
+
);
|
|
1361
|
+
} catch (deleteError) {
|
|
1362
|
+
MessageFormatter.info(
|
|
1363
|
+
chalk.red(
|
|
1364
|
+
`Failed to delete stuck attribute '${attribute.key}': ${deleteError}`
|
|
1365
|
+
)
|
|
1366
|
+
);
|
|
1367
|
+
|
|
1368
|
+
// If attribute deletion fails, only then try collection recreation as last resort
|
|
1369
|
+
if (retryCount >= maxRetries - 1) {
|
|
1370
|
+
MessageFormatter.info(
|
|
1371
|
+
chalk.yellow(
|
|
1372
|
+
`Last resort: Recreating collection for attribute '${attribute.key}'`
|
|
1373
|
+
)
|
|
1374
|
+
);
|
|
1375
|
+
|
|
1376
|
+
// Get fresh collection data
|
|
1377
|
+
const freshCollection = isDatabaseAdapter(db)
|
|
1378
|
+
? (await db.getTable({ databaseId: dbId, tableId: collection.$id }))
|
|
1379
|
+
.data
|
|
1380
|
+
: await db.getCollection(dbId, collection.$id);
|
|
1381
|
+
|
|
1382
|
+
// Delete and recreate collection
|
|
1383
|
+
const newCollection = await deleteAndRecreateCollection(
|
|
1384
|
+
db,
|
|
1385
|
+
dbId,
|
|
1386
|
+
freshCollection,
|
|
1387
|
+
retryCount + 1
|
|
1388
|
+
);
|
|
1389
|
+
|
|
1390
|
+
if (newCollection) {
|
|
1391
|
+
// Retry with the new collection
|
|
1392
|
+
return await createOrUpdateAttributeWithStatusCheck(
|
|
1393
|
+
db,
|
|
1394
|
+
dbId,
|
|
1395
|
+
newCollection,
|
|
1396
|
+
attribute,
|
|
1397
|
+
retryCount + 1,
|
|
1398
|
+
maxRetries
|
|
1399
|
+
);
|
|
1400
|
+
}
|
|
1401
|
+
} else {
|
|
1402
|
+
// Continue to next retry without collection recreation
|
|
1403
|
+
return await createOrUpdateAttributeWithStatusCheck(
|
|
1404
|
+
db,
|
|
1405
|
+
dbId,
|
|
1406
|
+
collection,
|
|
1407
|
+
attribute,
|
|
1408
|
+
retryCount + 1,
|
|
1409
|
+
maxRetries
|
|
1410
|
+
);
|
|
1411
|
+
}
|
|
1412
|
+
}
|
|
1413
|
+
}
|
|
1414
|
+
|
|
1415
|
+
MessageFormatter.info(
|
|
1416
|
+
chalk.red(
|
|
1417
|
+
`❌ Failed to create attribute '${attribute.key}' after ${
|
|
1418
|
+
maxRetries + 1
|
|
1419
|
+
} attempts`
|
|
1420
|
+
)
|
|
1421
|
+
);
|
|
1422
|
+
return false;
|
|
1423
|
+
} catch (error) {
|
|
1424
|
+
MessageFormatter.info(
|
|
1425
|
+
chalk.red(`Error creating attribute '${attribute.key}': ${error}`)
|
|
1426
|
+
);
|
|
1427
|
+
|
|
1428
|
+
if (retryCount < maxRetries) {
|
|
1429
|
+
MessageFormatter.info(
|
|
1430
|
+
chalk.yellow(`Retrying attribute '${attribute.key}' due to error...`)
|
|
1431
|
+
);
|
|
1432
|
+
|
|
1433
|
+
// Wait a bit before retry
|
|
1434
|
+
await delay(2000);
|
|
1435
|
+
|
|
1436
|
+
return await createOrUpdateAttributeWithStatusCheck(
|
|
1437
|
+
db,
|
|
1438
|
+
dbId,
|
|
1439
|
+
collection,
|
|
1440
|
+
attribute,
|
|
1441
|
+
retryCount + 1,
|
|
1442
|
+
maxRetries
|
|
1443
|
+
);
|
|
1444
|
+
}
|
|
1445
|
+
|
|
1446
|
+
return false;
|
|
1447
|
+
}
|
|
1448
|
+
};
|
|
1449
|
+
|
|
1450
|
+
export const createOrUpdateAttribute = async (
|
|
1451
|
+
db: Databases | DatabaseAdapter,
|
|
1452
|
+
dbId: string,
|
|
1453
|
+
collection: Models.Collection,
|
|
1454
|
+
attribute: Attribute
|
|
1455
|
+
): Promise<"queued" | "processed" | "error"> => {
|
|
1456
|
+
let action = "create";
|
|
1457
|
+
let foundAttribute: Attribute | undefined;
|
|
1458
|
+
const updateEnabled = true;
|
|
1459
|
+
let finalAttribute: any = attribute;
|
|
1460
|
+
try {
|
|
1461
|
+
const collectionAttr = collection.attributes.find(
|
|
1462
|
+
(attr: any) => attr.key === attribute.key
|
|
1463
|
+
) as unknown as any;
|
|
1464
|
+
foundAttribute = parseAttribute(collectionAttr);
|
|
1465
|
+
} catch (error) {
|
|
1466
|
+
foundAttribute = undefined;
|
|
1467
|
+
}
|
|
1468
|
+
|
|
1469
|
+
// If attribute exists but type changed, delete it so we can recreate with new type
|
|
1470
|
+
if (
|
|
1471
|
+
foundAttribute &&
|
|
1472
|
+
foundAttribute.type !== attribute.type
|
|
1473
|
+
) {
|
|
1474
|
+
MessageFormatter.info(
|
|
1475
|
+
chalk.yellow(
|
|
1476
|
+
`Attribute '${attribute.key}' type changed from '${foundAttribute.type}' to '${attribute.type}'. Recreating attribute.`
|
|
1477
|
+
)
|
|
1478
|
+
);
|
|
1479
|
+
try {
|
|
1480
|
+
if (isDatabaseAdapter(db)) {
|
|
1481
|
+
await db.deleteAttribute({
|
|
1482
|
+
databaseId: dbId,
|
|
1483
|
+
tableId: collection.$id,
|
|
1484
|
+
key: attribute.key
|
|
1485
|
+
});
|
|
1486
|
+
} else {
|
|
1487
|
+
await db.deleteAttribute(dbId, collection.$id, attribute.key);
|
|
1488
|
+
}
|
|
1489
|
+
// Remove from local collection metadata so downstream logic treats it as new
|
|
1490
|
+
collection.attributes = collection.attributes.filter(
|
|
1491
|
+
(attr: any) => attr.key !== attribute.key
|
|
1492
|
+
);
|
|
1493
|
+
foundAttribute = undefined;
|
|
1494
|
+
} catch (deleteError) {
|
|
1495
|
+
MessageFormatter.error(
|
|
1496
|
+
`Failed to delete attribute '${attribute.key}' before recreation: ${deleteError}`
|
|
1497
|
+
);
|
|
1498
|
+
return "error";
|
|
1499
|
+
}
|
|
1500
|
+
}
|
|
1501
|
+
|
|
1502
|
+
if (
|
|
1503
|
+
foundAttribute &&
|
|
1504
|
+
attributesSame(foundAttribute, attribute) &&
|
|
1505
|
+
updateEnabled
|
|
1506
|
+
) {
|
|
1507
|
+
// No need to do anything, they are the same
|
|
1508
|
+
return "processed";
|
|
1509
|
+
} else if (
|
|
1510
|
+
foundAttribute &&
|
|
1511
|
+
!attributesSame(foundAttribute, attribute) &&
|
|
1512
|
+
updateEnabled
|
|
1513
|
+
) {
|
|
1514
|
+
// MessageFormatter.info(
|
|
1515
|
+
// `Updating attribute with same key ${attribute.key} but different values`
|
|
1516
|
+
// );
|
|
1517
|
+
|
|
1518
|
+
// DEBUG: Log before object merge to detect corruption
|
|
1519
|
+
if ((attribute.key === 'conversationType' || attribute.key === 'messageStreakCount')) {
|
|
1520
|
+
console.log(`[DEBUG] MERGE - key="${attribute.key}"`, {
|
|
1521
|
+
found: {
|
|
1522
|
+
elements: (foundAttribute as any)?.elements,
|
|
1523
|
+
min: (foundAttribute as any)?.min,
|
|
1524
|
+
max: (foundAttribute as any)?.max
|
|
1525
|
+
},
|
|
1526
|
+
desired: {
|
|
1527
|
+
elements: (attribute as any)?.elements,
|
|
1528
|
+
min: (attribute as any)?.min,
|
|
1529
|
+
max: (attribute as any)?.max
|
|
1530
|
+
}
|
|
1531
|
+
});
|
|
1532
|
+
}
|
|
1533
|
+
|
|
1534
|
+
finalAttribute = {
|
|
1535
|
+
...foundAttribute,
|
|
1536
|
+
...attribute,
|
|
1537
|
+
};
|
|
1538
|
+
|
|
1539
|
+
// DEBUG: Log after object merge to detect corruption
|
|
1540
|
+
if ((finalAttribute.key === 'conversationType' || finalAttribute.key === 'messageStreakCount')) {
|
|
1541
|
+
console.log(`[DEBUG] AFTER_MERGE - key="${finalAttribute.key}"`, {
|
|
1542
|
+
merged: {
|
|
1543
|
+
elements: finalAttribute?.elements,
|
|
1544
|
+
min: (finalAttribute as any)?.min,
|
|
1545
|
+
max: (finalAttribute as any)?.max
|
|
1546
|
+
}
|
|
1547
|
+
});
|
|
1548
|
+
}
|
|
1549
|
+
action = "update";
|
|
1550
|
+
} else if (
|
|
1551
|
+
!updateEnabled &&
|
|
1552
|
+
foundAttribute &&
|
|
1553
|
+
!attributesSame(foundAttribute, attribute)
|
|
1554
|
+
) {
|
|
1555
|
+
if (isDatabaseAdapter(db)) {
|
|
1556
|
+
await db.deleteAttribute({
|
|
1557
|
+
databaseId: dbId,
|
|
1558
|
+
tableId: collection.$id,
|
|
1559
|
+
key: attribute.key,
|
|
1560
|
+
});
|
|
1561
|
+
} else {
|
|
1562
|
+
await db.deleteAttribute(dbId, collection.$id, attribute.key);
|
|
1563
|
+
}
|
|
1564
|
+
MessageFormatter.info(
|
|
1565
|
+
`Deleted attribute: ${attribute.key} to recreate it because they diff (update disabled temporarily)`
|
|
1566
|
+
);
|
|
1567
|
+
return "processed";
|
|
1568
|
+
}
|
|
1569
|
+
|
|
1570
|
+
// Relationship attribute logic with adjustments
|
|
1571
|
+
let collectionFoundViaRelatedCollection: Models.Collection | undefined;
|
|
1572
|
+
let relatedCollectionId: string | undefined;
|
|
1573
|
+
if (
|
|
1574
|
+
finalAttribute.type === "relationship" &&
|
|
1575
|
+
finalAttribute.relatedCollection
|
|
1576
|
+
) {
|
|
1577
|
+
// First try treating relatedCollection as an ID directly
|
|
1578
|
+
try {
|
|
1579
|
+
const byIdCollection = isDatabaseAdapter(db)
|
|
1580
|
+
? (
|
|
1581
|
+
await db.getTable({
|
|
1582
|
+
databaseId: dbId,
|
|
1583
|
+
tableId: finalAttribute.relatedCollection,
|
|
1584
|
+
})
|
|
1585
|
+
).data
|
|
1586
|
+
: await db.getCollection(dbId, finalAttribute.relatedCollection);
|
|
1587
|
+
collectionFoundViaRelatedCollection = byIdCollection;
|
|
1588
|
+
relatedCollectionId = byIdCollection.$id;
|
|
1589
|
+
// Cache by name for subsequent lookups
|
|
1590
|
+
nameToIdMapping.set(byIdCollection.name, byIdCollection.$id);
|
|
1591
|
+
} catch (_) {
|
|
1592
|
+
// Not an ID or not found — fall back to name-based resolution below
|
|
1593
|
+
}
|
|
1594
|
+
|
|
1595
|
+
if (
|
|
1596
|
+
!collectionFoundViaRelatedCollection &&
|
|
1597
|
+
nameToIdMapping.has(finalAttribute.relatedCollection)
|
|
1598
|
+
) {
|
|
1599
|
+
relatedCollectionId = nameToIdMapping.get(
|
|
1600
|
+
finalAttribute.relatedCollection
|
|
1601
|
+
);
|
|
1602
|
+
try {
|
|
1603
|
+
collectionFoundViaRelatedCollection = isDatabaseAdapter(db)
|
|
1604
|
+
? (
|
|
1605
|
+
await db.getTable({
|
|
1606
|
+
databaseId: dbId,
|
|
1607
|
+
tableId: relatedCollectionId!,
|
|
1608
|
+
})
|
|
1609
|
+
).data
|
|
1610
|
+
: await db.getCollection(dbId, relatedCollectionId!);
|
|
1611
|
+
} catch (e) {
|
|
1612
|
+
// MessageFormatter.info(
|
|
1613
|
+
// `Collection not found: ${finalAttribute.relatedCollection} when nameToIdMapping was set`
|
|
1614
|
+
// );
|
|
1615
|
+
collectionFoundViaRelatedCollection = undefined;
|
|
1616
|
+
}
|
|
1617
|
+
} else if (!collectionFoundViaRelatedCollection) {
|
|
1618
|
+
const collectionsPulled = isDatabaseAdapter(db)
|
|
1619
|
+
? await db.listTables({
|
|
1620
|
+
databaseId: dbId,
|
|
1621
|
+
queries: [Query.equal("name", finalAttribute.relatedCollection)],
|
|
1622
|
+
})
|
|
1623
|
+
: await db.listCollections(dbId, [
|
|
1624
|
+
Query.equal("name", finalAttribute.relatedCollection),
|
|
1625
|
+
]);
|
|
1626
|
+
if (collectionsPulled.total && collectionsPulled.total > 0) {
|
|
1627
|
+
collectionFoundViaRelatedCollection = isDatabaseAdapter(db)
|
|
1628
|
+
? (collectionsPulled as any).tables?.[0]
|
|
1629
|
+
: (collectionsPulled as any).collections?.[0];
|
|
1630
|
+
relatedCollectionId = collectionFoundViaRelatedCollection?.$id;
|
|
1631
|
+
if (relatedCollectionId) {
|
|
1632
|
+
nameToIdMapping.set(
|
|
1633
|
+
finalAttribute.relatedCollection,
|
|
1634
|
+
relatedCollectionId
|
|
1635
|
+
);
|
|
1636
|
+
}
|
|
1637
|
+
}
|
|
1638
|
+
}
|
|
1639
|
+
// ONLY queue relationship attributes that have actual unresolved dependencies
|
|
1640
|
+
if (!(relatedCollectionId && collectionFoundViaRelatedCollection)) {
|
|
1641
|
+
MessageFormatter.info(
|
|
1642
|
+
chalk.yellow(
|
|
1643
|
+
`⏳ Queueing relationship attribute '${finalAttribute.key}' - related collection '${finalAttribute.relatedCollection}' not found yet`
|
|
1644
|
+
)
|
|
1645
|
+
);
|
|
1646
|
+
enqueueOperation({
|
|
1647
|
+
type: "attribute",
|
|
1648
|
+
collectionId: collection.$id,
|
|
1649
|
+
collection: collection,
|
|
1650
|
+
attribute,
|
|
1651
|
+
dependencies: [finalAttribute.relatedCollection],
|
|
1652
|
+
});
|
|
1653
|
+
return "queued";
|
|
1654
|
+
}
|
|
1655
|
+
}
|
|
1656
|
+
finalAttribute = parseAttribute(finalAttribute);
|
|
1657
|
+
|
|
1658
|
+
// Ensure collection/table exists - create it if it doesn't
|
|
1659
|
+
try {
|
|
1660
|
+
await (isDatabaseAdapter(db)
|
|
1661
|
+
? db.getTable({ databaseId: dbId, tableId: collection.$id })
|
|
1662
|
+
: db.getCollection(dbId, collection.$id));
|
|
1663
|
+
} catch (error) {
|
|
1664
|
+
// Collection doesn't exist - create it
|
|
1665
|
+
if (
|
|
1666
|
+
(error as any).code === 404 ||
|
|
1667
|
+
(error instanceof Error &&
|
|
1668
|
+
(error.message.includes("collection_not_found") ||
|
|
1669
|
+
error.message.includes(
|
|
1670
|
+
"Collection with the requested ID could not be found"
|
|
1671
|
+
)))
|
|
1672
|
+
) {
|
|
1673
|
+
MessageFormatter.info(
|
|
1674
|
+
`Collection '${collection.name}' doesn't exist, creating it first...`
|
|
1675
|
+
);
|
|
1676
|
+
|
|
1677
|
+
try {
|
|
1678
|
+
if (isDatabaseAdapter(db)) {
|
|
1679
|
+
await db.createTable({
|
|
1680
|
+
databaseId: dbId,
|
|
1681
|
+
id: collection.$id,
|
|
1682
|
+
name: collection.name,
|
|
1683
|
+
permissions: collection.$permissions || [],
|
|
1684
|
+
documentSecurity: collection.documentSecurity ?? false,
|
|
1685
|
+
enabled: collection.enabled ?? true,
|
|
1686
|
+
});
|
|
1687
|
+
} else {
|
|
1688
|
+
await db.createCollection(
|
|
1689
|
+
dbId,
|
|
1690
|
+
collection.$id,
|
|
1691
|
+
collection.name,
|
|
1692
|
+
collection.$permissions || [],
|
|
1693
|
+
collection.documentSecurity ?? false,
|
|
1694
|
+
collection.enabled ?? true
|
|
1695
|
+
);
|
|
1696
|
+
}
|
|
1697
|
+
|
|
1698
|
+
MessageFormatter.success(`Created collection '${collection.name}'`);
|
|
1699
|
+
await delay(500); // Wait for collection to be ready
|
|
1700
|
+
} catch (createError) {
|
|
1701
|
+
MessageFormatter.error(
|
|
1702
|
+
`Failed to create collection '${collection.name}'`,
|
|
1703
|
+
createError instanceof Error
|
|
1704
|
+
? createError
|
|
1705
|
+
: new Error(String(createError))
|
|
1706
|
+
);
|
|
1707
|
+
return "error";
|
|
1708
|
+
}
|
|
1709
|
+
} else {
|
|
1710
|
+
// Other error - re-throw
|
|
1711
|
+
throw error;
|
|
1712
|
+
}
|
|
1713
|
+
}
|
|
1714
|
+
|
|
1715
|
+
// Use adapter-based attribute creation/update
|
|
1716
|
+
if (action === "create") {
|
|
1717
|
+
await tryAwaitWithRetry(
|
|
1718
|
+
async () =>
|
|
1719
|
+
await createAttributeViaAdapter(
|
|
1720
|
+
db,
|
|
1721
|
+
dbId,
|
|
1722
|
+
collection.$id,
|
|
1723
|
+
finalAttribute
|
|
1724
|
+
)
|
|
1725
|
+
);
|
|
1726
|
+
} else {
|
|
1727
|
+
console.log(`Updating attribute '${finalAttribute.key}'...`);
|
|
1728
|
+
if (finalAttribute.type === "double" || finalAttribute.type === "integer") {
|
|
1729
|
+
console.log("finalAttribute:", finalAttribute);
|
|
1730
|
+
}
|
|
1731
|
+
await tryAwaitWithRetry(
|
|
1732
|
+
async () =>
|
|
1733
|
+
await updateAttributeViaAdapter(
|
|
1734
|
+
db,
|
|
1735
|
+
dbId,
|
|
1736
|
+
collection.$id,
|
|
1737
|
+
finalAttribute
|
|
1738
|
+
)
|
|
1739
|
+
);
|
|
1740
|
+
}
|
|
1741
|
+
return "processed";
|
|
1742
|
+
};
|
|
1743
|
+
|
|
1744
|
+
/**
|
|
1745
|
+
* Enhanced collection attribute creation with proper status monitoring
|
|
1746
|
+
*/
|
|
1747
|
+
export const createUpdateCollectionAttributesWithStatusCheck = async (
|
|
1748
|
+
db: Databases | DatabaseAdapter,
|
|
1749
|
+
dbId: string,
|
|
1750
|
+
collection: Models.Collection,
|
|
1751
|
+
attributes: Attribute[]
|
|
1752
|
+
): Promise<boolean> => {
|
|
1753
|
+
const existingAttributes: Attribute[] =
|
|
1754
|
+
collection.attributes.map((attr) => parseAttribute(attr as any)) || [];
|
|
1755
|
+
|
|
1756
|
+
const attributesToRemove = existingAttributes.filter(
|
|
1757
|
+
(attr) => !attributes.some((a) => a.key === attr.key)
|
|
1758
|
+
);
|
|
1759
|
+
const indexesToRemove = collection.indexes.filter((index) =>
|
|
1760
|
+
attributesToRemove.some((attr) => index.attributes.includes(attr.key))
|
|
1761
|
+
);
|
|
1762
|
+
|
|
1763
|
+
// Handle attribute removal first
|
|
1764
|
+
if (attributesToRemove.length > 0) {
|
|
1765
|
+
if (indexesToRemove.length > 0) {
|
|
1766
|
+
MessageFormatter.info(
|
|
1767
|
+
chalk.red(
|
|
1768
|
+
`Removing indexes as they rely on an attribute that is being removed: ${indexesToRemove
|
|
1769
|
+
.map((index) => index.key)
|
|
1770
|
+
.join(", ")}`
|
|
1771
|
+
)
|
|
1772
|
+
);
|
|
1773
|
+
for (const index of indexesToRemove) {
|
|
1774
|
+
await tryAwaitWithRetry(async () => {
|
|
1775
|
+
if (isDatabaseAdapter(db)) {
|
|
1776
|
+
await db.deleteIndex({
|
|
1777
|
+
databaseId: dbId,
|
|
1778
|
+
tableId: collection.$id,
|
|
1779
|
+
key: index.key,
|
|
1780
|
+
});
|
|
1781
|
+
} else {
|
|
1782
|
+
await db.deleteIndex(dbId, collection.$id, index.key);
|
|
1783
|
+
}
|
|
1784
|
+
});
|
|
1785
|
+
await delay(500); // Longer delay for deletions
|
|
1786
|
+
}
|
|
1787
|
+
}
|
|
1788
|
+
for (const attr of attributesToRemove) {
|
|
1789
|
+
MessageFormatter.info(
|
|
1790
|
+
chalk.red(
|
|
1791
|
+
`Removing attribute: ${attr.key} as it is no longer in the collection`
|
|
1792
|
+
)
|
|
1793
|
+
);
|
|
1794
|
+
await tryAwaitWithRetry(async () => {
|
|
1795
|
+
if (isDatabaseAdapter(db)) {
|
|
1796
|
+
await db.deleteAttribute({
|
|
1797
|
+
databaseId: dbId,
|
|
1798
|
+
tableId: collection.$id,
|
|
1799
|
+
key: attr.key,
|
|
1800
|
+
});
|
|
1801
|
+
} else {
|
|
1802
|
+
await db.deleteAttribute(dbId, collection.$id, attr.key);
|
|
1803
|
+
}
|
|
1804
|
+
});
|
|
1805
|
+
await delay(500); // Longer delay for deletions
|
|
1806
|
+
}
|
|
1807
|
+
}
|
|
1808
|
+
|
|
1809
|
+
// First, get fresh collection data and determine which attributes actually need processing
|
|
1810
|
+
let currentCollection = collection;
|
|
1811
|
+
try {
|
|
1812
|
+
currentCollection = isDatabaseAdapter(db)
|
|
1813
|
+
? (await db.getTable({ databaseId: dbId, tableId: collection.$id })).data
|
|
1814
|
+
: await db.getCollection(dbId, collection.$id);
|
|
1815
|
+
} catch (error) {
|
|
1816
|
+
MessageFormatter.info(
|
|
1817
|
+
chalk.yellow(`Warning: Could not refresh collection data: ${error}`)
|
|
1818
|
+
);
|
|
1819
|
+
}
|
|
1820
|
+
|
|
1821
|
+
const existingAttributesMap = new Map<string, Attribute>();
|
|
1822
|
+
try {
|
|
1823
|
+
const parsedAttributes = currentCollection.attributes.map((attr) =>
|
|
1824
|
+
parseAttribute(attr as any)
|
|
1825
|
+
);
|
|
1826
|
+
parsedAttributes.forEach((attr) =>
|
|
1827
|
+
existingAttributesMap.set(attr.key, attr)
|
|
1828
|
+
);
|
|
1829
|
+
} catch (error) {
|
|
1830
|
+
MessageFormatter.info(
|
|
1831
|
+
chalk.yellow(`Warning: Could not parse existing attributes: ${error}`)
|
|
1832
|
+
);
|
|
1833
|
+
}
|
|
1834
|
+
|
|
1835
|
+
// Filter to only attributes that need processing (new, changed, or not yet processed)
|
|
1836
|
+
const attributesToProcess = attributes.filter((attribute) => {
|
|
1837
|
+
// Skip if already processed in this session
|
|
1838
|
+
if (isAttributeProcessed(currentCollection.$id, attribute.key)) {
|
|
1839
|
+
return false;
|
|
1840
|
+
}
|
|
1841
|
+
|
|
1842
|
+
const existing = existingAttributesMap.get(attribute.key);
|
|
1843
|
+
if (!existing) {
|
|
1844
|
+
MessageFormatter.info(`➕ ${attribute.key}`);
|
|
1845
|
+
return true;
|
|
1846
|
+
}
|
|
1847
|
+
|
|
1848
|
+
const needsUpdate = !attributesSame(existing, parseAttribute(attribute));
|
|
1849
|
+
if (needsUpdate) {
|
|
1850
|
+
MessageFormatter.info(`🔄 ${attribute.key}`);
|
|
1851
|
+
} else {
|
|
1852
|
+
MessageFormatter.info(chalk.gray(`✅ ${attribute.key}`));
|
|
1853
|
+
}
|
|
1854
|
+
return needsUpdate;
|
|
1855
|
+
});
|
|
1856
|
+
|
|
1857
|
+
if (attributesToProcess.length === 0) {
|
|
1858
|
+
return true;
|
|
1859
|
+
}
|
|
1860
|
+
|
|
1861
|
+
let remainingAttributes = [...attributesToProcess];
|
|
1862
|
+
let overallRetryCount = 0;
|
|
1863
|
+
const maxOverallRetries = 3;
|
|
1864
|
+
|
|
1865
|
+
while (
|
|
1866
|
+
remainingAttributes.length > 0 &&
|
|
1867
|
+
overallRetryCount < maxOverallRetries
|
|
1868
|
+
) {
|
|
1869
|
+
const attributesToProcessThisRound = [...remainingAttributes];
|
|
1870
|
+
remainingAttributes = []; // Reset for next iteration
|
|
1871
|
+
|
|
1872
|
+
for (const attribute of attributesToProcessThisRound) {
|
|
1873
|
+
const success = await createOrUpdateAttributeWithStatusCheck(
|
|
1874
|
+
db,
|
|
1875
|
+
dbId,
|
|
1876
|
+
currentCollection,
|
|
1877
|
+
attribute
|
|
1878
|
+
);
|
|
1879
|
+
|
|
1880
|
+
if (success) {
|
|
1881
|
+
// Mark this specific attribute as processed
|
|
1882
|
+
markAttributeProcessed(currentCollection.$id, attribute.key);
|
|
1883
|
+
|
|
1884
|
+
// Get updated collection data for next iteration
|
|
1885
|
+
try {
|
|
1886
|
+
currentCollection = isDatabaseAdapter(db)
|
|
1887
|
+
? ((
|
|
1888
|
+
await db.getTable({ databaseId: dbId, tableId: collection.$id })
|
|
1889
|
+
).data as Models.Collection)
|
|
1890
|
+
: await db.getCollection({
|
|
1891
|
+
databaseId: dbId,
|
|
1892
|
+
collectionId: collection.$id,
|
|
1893
|
+
});
|
|
1894
|
+
} catch (error) {
|
|
1895
|
+
MessageFormatter.info(
|
|
1896
|
+
chalk.yellow(`Warning: Could not refresh collection data: ${error}`)
|
|
1897
|
+
);
|
|
1898
|
+
}
|
|
1899
|
+
|
|
1900
|
+
// Add delay between successful attributes
|
|
1901
|
+
await delay(1000);
|
|
1902
|
+
} else {
|
|
1903
|
+
MessageFormatter.info(chalk.red(`❌ ${attribute.key}`));
|
|
1904
|
+
remainingAttributes.push(attribute); // Add back to retry list
|
|
1905
|
+
}
|
|
1906
|
+
}
|
|
1907
|
+
|
|
1908
|
+
if (remainingAttributes.length === 0) {
|
|
1909
|
+
return true;
|
|
1910
|
+
}
|
|
1911
|
+
|
|
1912
|
+
overallRetryCount++;
|
|
1913
|
+
|
|
1914
|
+
if (overallRetryCount < maxOverallRetries) {
|
|
1915
|
+
MessageFormatter.info(
|
|
1916
|
+
chalk.yellow(
|
|
1917
|
+
`⏳ Retrying ${remainingAttributes.length} failed attributes...`
|
|
1918
|
+
)
|
|
1919
|
+
);
|
|
1920
|
+
await delay(5000);
|
|
1921
|
+
|
|
1922
|
+
// Refresh collection data before retry
|
|
1923
|
+
try {
|
|
1924
|
+
currentCollection = isDatabaseAdapter(db)
|
|
1925
|
+
? ((await db.getTable({ databaseId: dbId, tableId: collection.$id }))
|
|
1926
|
+
.data as Models.Collection)
|
|
1927
|
+
: await db.getCollection(dbId, collection.$id);
|
|
1928
|
+
} catch (error) {
|
|
1929
|
+
// Silently continue if refresh fails
|
|
1930
|
+
}
|
|
1931
|
+
}
|
|
1932
|
+
}
|
|
1933
|
+
|
|
1934
|
+
// If we get here, some attributes still failed after all retries
|
|
1935
|
+
if (attributesToProcess.length > 0) {
|
|
1936
|
+
MessageFormatter.info(
|
|
1937
|
+
chalk.red(
|
|
1938
|
+
`\n❌ Failed to create ${
|
|
1939
|
+
attributesToProcess.length
|
|
1940
|
+
} attributes after ${maxOverallRetries} attempts: ${attributesToProcess
|
|
1941
|
+
.map((a) => a.key)
|
|
1942
|
+
.join(", ")}`
|
|
1943
|
+
)
|
|
1944
|
+
);
|
|
1945
|
+
MessageFormatter.info(
|
|
1946
|
+
chalk.red(
|
|
1947
|
+
`This may indicate a fundamental issue with the attribute definitions or Appwrite instance`
|
|
1948
|
+
)
|
|
1949
|
+
);
|
|
1950
|
+
return false;
|
|
1951
|
+
}
|
|
1952
|
+
|
|
1953
|
+
MessageFormatter.info(
|
|
1954
|
+
chalk.green(
|
|
1955
|
+
`\n✅ Successfully created all ${attributes.length} attributes for collection: ${collection.name}`
|
|
1956
|
+
)
|
|
1957
|
+
);
|
|
1958
|
+
return true;
|
|
1959
|
+
};
|
|
1960
|
+
|
|
1961
|
+
export const createUpdateCollectionAttributes = async (
|
|
1962
|
+
db: Databases | DatabaseAdapter,
|
|
1963
|
+
dbId: string,
|
|
1964
|
+
collection: Models.Collection,
|
|
1965
|
+
attributes: Attribute[]
|
|
1966
|
+
): Promise<void> => {
|
|
1967
|
+
MessageFormatter.info(
|
|
1968
|
+
chalk.green(
|
|
1969
|
+
`Creating/Updating attributes for collection: ${collection.name}`
|
|
1970
|
+
)
|
|
1971
|
+
);
|
|
1972
|
+
|
|
1973
|
+
const existingAttributes: Attribute[] =
|
|
1974
|
+
collection.attributes.map((attr) => parseAttribute(attr as any)) || [];
|
|
1975
|
+
|
|
1976
|
+
const attributesToRemove = existingAttributes.filter(
|
|
1977
|
+
(attr) => !attributes.some((a) => a.key === attr.key)
|
|
1978
|
+
);
|
|
1979
|
+
const indexesToRemove = collection.indexes.filter((index) =>
|
|
1980
|
+
attributesToRemove.some((attr) => index.attributes.includes(attr.key))
|
|
1981
|
+
);
|
|
1982
|
+
|
|
1983
|
+
if (attributesToRemove.length > 0) {
|
|
1984
|
+
if (indexesToRemove.length > 0) {
|
|
1985
|
+
MessageFormatter.info(
|
|
1986
|
+
chalk.red(
|
|
1987
|
+
`Removing indexes as they rely on an attribute that is being removed: ${indexesToRemove
|
|
1988
|
+
.map((index) => index.key)
|
|
1989
|
+
.join(", ")}`
|
|
1990
|
+
)
|
|
1991
|
+
);
|
|
1992
|
+
for (const index of indexesToRemove) {
|
|
1993
|
+
await tryAwaitWithRetry(async () => {
|
|
1994
|
+
if (isDatabaseAdapter(db)) {
|
|
1995
|
+
await db.deleteIndex({
|
|
1996
|
+
databaseId: dbId,
|
|
1997
|
+
tableId: collection.$id,
|
|
1998
|
+
key: index.key,
|
|
1999
|
+
});
|
|
2000
|
+
} else {
|
|
2001
|
+
await db.deleteIndex(dbId, collection.$id, index.key);
|
|
2002
|
+
}
|
|
2003
|
+
});
|
|
2004
|
+
await delay(100);
|
|
2005
|
+
}
|
|
2006
|
+
}
|
|
2007
|
+
for (const attr of attributesToRemove) {
|
|
2008
|
+
MessageFormatter.info(
|
|
2009
|
+
chalk.red(
|
|
2010
|
+
`Removing attribute: ${attr.key} as it is no longer in the collection`
|
|
2011
|
+
)
|
|
2012
|
+
);
|
|
2013
|
+
await tryAwaitWithRetry(async () => {
|
|
2014
|
+
if (isDatabaseAdapter(db)) {
|
|
2015
|
+
await db.deleteAttribute({
|
|
2016
|
+
databaseId: dbId,
|
|
2017
|
+
tableId: collection.$id,
|
|
2018
|
+
key: attr.key,
|
|
2019
|
+
});
|
|
2020
|
+
} else {
|
|
2021
|
+
await db.deleteAttribute(dbId, collection.$id, attr.key);
|
|
2022
|
+
}
|
|
2023
|
+
});
|
|
2024
|
+
await delay(50);
|
|
2025
|
+
}
|
|
2026
|
+
}
|
|
2027
|
+
|
|
2028
|
+
const batchSize = 3;
|
|
2029
|
+
for (let i = 0; i < attributes.length; i += batchSize) {
|
|
2030
|
+
const batch = attributes.slice(i, i + batchSize);
|
|
2031
|
+
const attributePromises = batch.map((attribute) =>
|
|
2032
|
+
tryAwaitWithRetry(
|
|
2033
|
+
async () =>
|
|
2034
|
+
await createOrUpdateAttribute(db, dbId, collection, attribute)
|
|
2035
|
+
)
|
|
2036
|
+
);
|
|
2037
|
+
|
|
2038
|
+
const results = await Promise.allSettled(attributePromises);
|
|
2039
|
+
results.forEach((result) => {
|
|
2040
|
+
if (result.status === "rejected") {
|
|
2041
|
+
MessageFormatter.error(
|
|
2042
|
+
"An attribute promise was rejected:",
|
|
2043
|
+
result.reason
|
|
2044
|
+
);
|
|
2045
|
+
}
|
|
2046
|
+
});
|
|
2047
|
+
|
|
2048
|
+
// Add delay after each batch
|
|
2049
|
+
await delay(200);
|
|
2050
|
+
}
|
|
2051
|
+
MessageFormatter.info(
|
|
2052
|
+
`Finished creating/updating attributes for collection: ${collection.name}`
|
|
2053
|
+
);
|
|
2054
|
+
};
|