appwrite-utils-cli 0.0.286 → 0.9.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/README.md +162 -96
- package/dist/collections/attributes.d.ts +4 -0
- package/dist/collections/attributes.js +224 -0
- package/dist/collections/indexes.d.ts +4 -0
- package/dist/collections/indexes.js +27 -0
- package/dist/collections/methods.d.ts +16 -0
- package/dist/collections/methods.js +216 -0
- package/dist/databases/methods.d.ts +6 -0
- package/dist/databases/methods.js +33 -0
- package/dist/interactiveCLI.d.ts +19 -0
- package/dist/interactiveCLI.js +555 -0
- package/dist/main.js +224 -62
- package/dist/migrations/afterImportActions.js +37 -40
- package/dist/migrations/appwriteToX.d.ts +26 -25
- package/dist/migrations/appwriteToX.js +42 -6
- package/dist/migrations/attributes.js +21 -20
- package/dist/migrations/backup.d.ts +93 -87
- package/dist/migrations/collections.d.ts +6 -0
- package/dist/migrations/collections.js +149 -20
- package/dist/migrations/converters.d.ts +2 -18
- package/dist/migrations/converters.js +13 -2
- package/dist/migrations/dataLoader.d.ts +276 -161
- package/dist/migrations/dataLoader.js +535 -292
- package/dist/migrations/databases.js +8 -2
- package/dist/migrations/helper.d.ts +3 -0
- package/dist/migrations/helper.js +21 -0
- package/dist/migrations/importController.d.ts +5 -2
- package/dist/migrations/importController.js +125 -88
- package/dist/migrations/importDataActions.d.ts +9 -1
- package/dist/migrations/importDataActions.js +15 -3
- package/dist/migrations/indexes.js +3 -2
- package/dist/migrations/logging.js +20 -8
- package/dist/migrations/migrationHelper.d.ts +9 -4
- package/dist/migrations/migrationHelper.js +6 -5
- package/dist/migrations/openapi.d.ts +1 -1
- package/dist/migrations/openapi.js +33 -18
- package/dist/migrations/queue.js +3 -2
- package/dist/migrations/relationships.d.ts +2 -2
- package/dist/migrations/schemaStrings.js +53 -41
- package/dist/migrations/setupDatabase.d.ts +2 -4
- package/dist/migrations/setupDatabase.js +24 -105
- package/dist/migrations/storage.d.ts +3 -1
- package/dist/migrations/storage.js +110 -16
- package/dist/migrations/transfer.d.ts +30 -0
- package/dist/migrations/transfer.js +337 -0
- package/dist/migrations/users.d.ts +2 -1
- package/dist/migrations/users.js +78 -43
- package/dist/schemas/authUser.d.ts +2 -2
- package/dist/storage/methods.d.ts +15 -0
- package/dist/storage/methods.js +207 -0
- package/dist/storage/schemas.d.ts +687 -0
- package/dist/storage/schemas.js +175 -0
- package/dist/utils/getClientFromConfig.d.ts +4 -0
- package/dist/utils/getClientFromConfig.js +16 -0
- package/dist/utils/helperFunctions.d.ts +11 -1
- package/dist/utils/helperFunctions.js +38 -0
- package/dist/utils/retryFailedPromises.d.ts +2 -0
- package/dist/utils/retryFailedPromises.js +21 -0
- package/dist/utils/schemaStrings.d.ts +13 -0
- package/dist/utils/schemaStrings.js +403 -0
- package/dist/utils/setupFiles.js +110 -61
- package/dist/utilsController.d.ts +40 -22
- package/dist/utilsController.js +164 -84
- package/package.json +13 -15
- package/src/collections/attributes.ts +483 -0
- package/src/collections/indexes.ts +53 -0
- package/src/collections/methods.ts +331 -0
- package/src/databases/methods.ts +47 -0
- package/src/init.ts +64 -64
- package/src/interactiveCLI.ts +767 -0
- package/src/main.ts +289 -83
- package/src/migrations/afterImportActions.ts +553 -490
- package/src/migrations/appwriteToX.ts +237 -174
- package/src/migrations/attributes.ts +483 -422
- package/src/migrations/backup.ts +205 -205
- package/src/migrations/collections.ts +545 -300
- package/src/migrations/converters.ts +161 -150
- package/src/migrations/dataLoader.ts +1615 -1304
- package/src/migrations/databases.ts +44 -25
- package/src/migrations/dbHelpers.ts +92 -92
- package/src/migrations/helper.ts +40 -0
- package/src/migrations/importController.ts +448 -384
- package/src/migrations/importDataActions.ts +315 -307
- package/src/migrations/indexes.ts +40 -37
- package/src/migrations/logging.ts +29 -16
- package/src/migrations/migrationHelper.ts +207 -201
- package/src/migrations/openapi.ts +83 -70
- package/src/migrations/queue.ts +118 -119
- package/src/migrations/relationships.ts +324 -324
- package/src/migrations/schemaStrings.ts +472 -460
- package/src/migrations/setupDatabase.ts +118 -219
- package/src/migrations/storage.ts +538 -358
- package/src/migrations/transfer.ts +608 -0
- package/src/migrations/users.ts +362 -285
- package/src/migrations/validationRules.ts +63 -63
- package/src/schemas/authUser.ts +23 -23
- package/src/setup.ts +8 -8
- package/src/storage/methods.ts +371 -0
- package/src/storage/schemas.ts +205 -0
- package/src/types.ts +9 -9
- package/src/utils/getClientFromConfig.ts +17 -0
- package/src/utils/helperFunctions.ts +181 -127
- package/src/utils/index.ts +2 -2
- package/src/utils/loadConfigs.ts +59 -59
- package/src/utils/retryFailedPromises.ts +27 -0
- package/src/utils/schemaStrings.ts +473 -0
- package/src/utils/setupFiles.ts +228 -182
- package/src/utilsController.ts +325 -194
- package/tsconfig.json +37 -37
@@ -1,422 +1,483 @@
|
|
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 { nameToIdMapping, enqueueOperation } from "./queue.js";
|
8
|
-
import _ from "lodash";
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
databaseAttribute.
|
17
|
-
databaseAttribute.
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
let
|
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
|
-
"onDelete" in
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
...
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
return
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
let
|
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
|
-
finalAttribute.
|
190
|
-
|
191
|
-
finalAttribute.
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
finalAttribute.
|
215
|
-
finalAttribute.max
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
);
|
302
|
-
}
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
|
336
|
-
|
337
|
-
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
|
350
|
-
|
351
|
-
|
352
|
-
|
353
|
-
|
354
|
-
|
355
|
-
|
356
|
-
|
357
|
-
|
358
|
-
|
359
|
-
|
360
|
-
|
361
|
-
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
|
366
|
-
|
367
|
-
|
368
|
-
|
369
|
-
|
370
|
-
|
371
|
-
|
372
|
-
|
373
|
-
|
374
|
-
|
375
|
-
|
376
|
-
|
377
|
-
|
378
|
-
|
379
|
-
|
380
|
-
|
381
|
-
|
382
|
-
|
383
|
-
|
384
|
-
|
385
|
-
|
386
|
-
|
387
|
-
|
388
|
-
|
389
|
-
|
390
|
-
|
391
|
-
}
|
392
|
-
|
393
|
-
|
394
|
-
|
395
|
-
|
396
|
-
|
397
|
-
|
398
|
-
|
399
|
-
|
400
|
-
|
401
|
-
|
402
|
-
|
403
|
-
|
404
|
-
|
405
|
-
|
406
|
-
|
407
|
-
|
408
|
-
|
409
|
-
|
410
|
-
|
411
|
-
|
412
|
-
|
413
|
-
|
414
|
-
|
415
|
-
|
416
|
-
|
417
|
-
|
418
|
-
|
419
|
-
|
420
|
-
|
421
|
-
|
422
|
-
|
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 { nameToIdMapping, enqueueOperation } from "./queue.js";
|
8
|
+
import _ from "lodash";
|
9
|
+
import { tryAwaitWithRetry } from "../utils/helperFunctions.js";
|
10
|
+
|
11
|
+
const attributesSame = (
|
12
|
+
databaseAttribute: Attribute,
|
13
|
+
configAttribute: Attribute
|
14
|
+
): boolean => {
|
15
|
+
return (
|
16
|
+
databaseAttribute.key == configAttribute.key &&
|
17
|
+
databaseAttribute.type == configAttribute.type &&
|
18
|
+
databaseAttribute.array == configAttribute.array
|
19
|
+
);
|
20
|
+
};
|
21
|
+
|
22
|
+
export const createOrUpdateAttribute = async (
|
23
|
+
db: Databases,
|
24
|
+
dbId: string,
|
25
|
+
collection: Models.Collection,
|
26
|
+
attribute: Attribute
|
27
|
+
): Promise<void> => {
|
28
|
+
let action = "create";
|
29
|
+
let foundAttribute: Attribute | undefined;
|
30
|
+
const updateEnabled = false;
|
31
|
+
let finalAttribute: any = attribute;
|
32
|
+
try {
|
33
|
+
const collectionAttr = collection.attributes.find(
|
34
|
+
// @ts-expect-error
|
35
|
+
(attr) => attr.key === attribute.key
|
36
|
+
) as unknown as any;
|
37
|
+
foundAttribute = parseAttribute(collectionAttr);
|
38
|
+
} catch (error) {
|
39
|
+
foundAttribute = undefined;
|
40
|
+
}
|
41
|
+
|
42
|
+
if (
|
43
|
+
foundAttribute &&
|
44
|
+
attributesSame(foundAttribute, attribute) &&
|
45
|
+
updateEnabled
|
46
|
+
) {
|
47
|
+
// Check if mutable properties have changed and set action to "update" if necessary
|
48
|
+
const requiredChanged =
|
49
|
+
"required" in foundAttribute && "required" in attribute
|
50
|
+
? foundAttribute.required !== attribute.required
|
51
|
+
: false;
|
52
|
+
|
53
|
+
// const xdefaultChanged =
|
54
|
+
// "xdefault" in foundAttribute && "xdefault" in attribute
|
55
|
+
// ? foundAttribute.xdefault !== attribute.xdefault
|
56
|
+
// : false;
|
57
|
+
|
58
|
+
const onDeleteChanged =
|
59
|
+
foundAttribute.type === "relationship" &&
|
60
|
+
attribute.type === "relationship" &&
|
61
|
+
"onDelete" in foundAttribute &&
|
62
|
+
"onDelete" in attribute
|
63
|
+
? foundAttribute.onDelete !== attribute.onDelete
|
64
|
+
: false;
|
65
|
+
|
66
|
+
if (requiredChanged || onDeleteChanged) {
|
67
|
+
console.log(
|
68
|
+
`Required changed: ${requiredChanged}\nOnDelete changed: ${onDeleteChanged}`
|
69
|
+
);
|
70
|
+
console.log(
|
71
|
+
`Found attribute: ${JSON.stringify(foundAttribute, null, 2)}`
|
72
|
+
);
|
73
|
+
console.log(`Attribute: ${JSON.stringify(attribute, null, 2)}`);
|
74
|
+
finalAttribute = {
|
75
|
+
...attribute,
|
76
|
+
...foundAttribute,
|
77
|
+
};
|
78
|
+
action = "update";
|
79
|
+
} else {
|
80
|
+
// If no properties that can be updated have changed, return early
|
81
|
+
return;
|
82
|
+
}
|
83
|
+
} else if (
|
84
|
+
foundAttribute &&
|
85
|
+
!attributesSame(foundAttribute, attribute) &&
|
86
|
+
updateEnabled
|
87
|
+
) {
|
88
|
+
console.log(
|
89
|
+
`Deleting attribute with same key ${
|
90
|
+
attribute.key
|
91
|
+
} -- but different values -- ${JSON.stringify(
|
92
|
+
attribute,
|
93
|
+
null,
|
94
|
+
2
|
95
|
+
)} -- ${JSON.stringify(foundAttribute, null, 2)}`
|
96
|
+
);
|
97
|
+
await db.deleteAttribute(dbId, collection.$id, attribute.key);
|
98
|
+
// After deletion, you might want to create the attribute anew
|
99
|
+
finalAttribute = attribute;
|
100
|
+
action = "create";
|
101
|
+
} else if (!updateEnabled && foundAttribute) {
|
102
|
+
return;
|
103
|
+
}
|
104
|
+
|
105
|
+
// Relationship attribute logic with adjustments
|
106
|
+
let collectionFoundViaRelatedCollection: Models.Collection | undefined;
|
107
|
+
let relatedCollectionId: string | undefined;
|
108
|
+
if (finalAttribute.type === "relationship") {
|
109
|
+
if (nameToIdMapping.has(finalAttribute.relatedCollection)) {
|
110
|
+
relatedCollectionId = nameToIdMapping.get(
|
111
|
+
finalAttribute.relatedCollection
|
112
|
+
);
|
113
|
+
try {
|
114
|
+
collectionFoundViaRelatedCollection = await db.getCollection(
|
115
|
+
dbId,
|
116
|
+
relatedCollectionId!
|
117
|
+
);
|
118
|
+
} catch (e) {
|
119
|
+
console.log(
|
120
|
+
`Collection not found: ${finalAttribute.relatedCollection} when nameToIdMapping was set`
|
121
|
+
);
|
122
|
+
collectionFoundViaRelatedCollection = undefined;
|
123
|
+
}
|
124
|
+
} else {
|
125
|
+
const collectionsPulled = await db.listCollections(dbId, [
|
126
|
+
Query.equal("name", finalAttribute.relatedCollection),
|
127
|
+
]);
|
128
|
+
if (collectionsPulled.total > 0) {
|
129
|
+
collectionFoundViaRelatedCollection = collectionsPulled.collections[0];
|
130
|
+
relatedCollectionId = collectionFoundViaRelatedCollection.$id;
|
131
|
+
nameToIdMapping.set(
|
132
|
+
finalAttribute.relatedCollection,
|
133
|
+
relatedCollectionId
|
134
|
+
);
|
135
|
+
}
|
136
|
+
}
|
137
|
+
if (!(relatedCollectionId && collectionFoundViaRelatedCollection)) {
|
138
|
+
console.log(`Enqueueing operation for attribute: ${finalAttribute.key}`);
|
139
|
+
enqueueOperation({
|
140
|
+
type: "attribute",
|
141
|
+
collectionId: collection.$id,
|
142
|
+
collection: collection,
|
143
|
+
attribute,
|
144
|
+
dependencies: [finalAttribute.relatedCollection],
|
145
|
+
});
|
146
|
+
return;
|
147
|
+
}
|
148
|
+
}
|
149
|
+
finalAttribute = attributeSchema.parse(finalAttribute);
|
150
|
+
switch (finalAttribute.type) {
|
151
|
+
case "string":
|
152
|
+
if (action === "create") {
|
153
|
+
await tryAwaitWithRetry(
|
154
|
+
async () =>
|
155
|
+
await db.createStringAttribute(
|
156
|
+
dbId,
|
157
|
+
collection.$id,
|
158
|
+
finalAttribute.key,
|
159
|
+
finalAttribute.size,
|
160
|
+
finalAttribute.required || false,
|
161
|
+
(finalAttribute.xdefault as string) || undefined,
|
162
|
+
finalAttribute.array || false,
|
163
|
+
finalAttribute.encrypted
|
164
|
+
)
|
165
|
+
);
|
166
|
+
} else {
|
167
|
+
await tryAwaitWithRetry(
|
168
|
+
async () =>
|
169
|
+
await db.updateStringAttribute(
|
170
|
+
dbId,
|
171
|
+
collection.$id,
|
172
|
+
finalAttribute.key,
|
173
|
+
finalAttribute.required || false,
|
174
|
+
(finalAttribute.xdefault as string) || undefined
|
175
|
+
)
|
176
|
+
);
|
177
|
+
}
|
178
|
+
break;
|
179
|
+
case "integer":
|
180
|
+
if (action === "create") {
|
181
|
+
if (
|
182
|
+
finalAttribute.min &&
|
183
|
+
BigInt(finalAttribute.min) === BigInt(-9223372036854776000)
|
184
|
+
) {
|
185
|
+
delete finalAttribute.min;
|
186
|
+
}
|
187
|
+
if (
|
188
|
+
finalAttribute.max &&
|
189
|
+
BigInt(finalAttribute.max) === BigInt(9223372036854776000)
|
190
|
+
) {
|
191
|
+
delete finalAttribute.max;
|
192
|
+
}
|
193
|
+
await tryAwaitWithRetry(
|
194
|
+
async () =>
|
195
|
+
await db.createIntegerAttribute(
|
196
|
+
dbId,
|
197
|
+
collection.$id,
|
198
|
+
finalAttribute.key,
|
199
|
+
finalAttribute.required || false,
|
200
|
+
finalAttribute.min,
|
201
|
+
finalAttribute.max,
|
202
|
+
finalAttribute.xdefault || undefined,
|
203
|
+
finalAttribute.array
|
204
|
+
)
|
205
|
+
);
|
206
|
+
} else {
|
207
|
+
if (
|
208
|
+
finalAttribute.min &&
|
209
|
+
BigInt(finalAttribute.min) === BigInt(-9223372036854776000)
|
210
|
+
) {
|
211
|
+
delete finalAttribute.min;
|
212
|
+
}
|
213
|
+
if (
|
214
|
+
finalAttribute.max &&
|
215
|
+
BigInt(finalAttribute.max) === BigInt(9223372036854776000)
|
216
|
+
) {
|
217
|
+
delete finalAttribute.max;
|
218
|
+
}
|
219
|
+
await tryAwaitWithRetry(
|
220
|
+
async () =>
|
221
|
+
await db.updateIntegerAttribute(
|
222
|
+
dbId,
|
223
|
+
collection.$id,
|
224
|
+
finalAttribute.key,
|
225
|
+
finalAttribute.required || false,
|
226
|
+
finalAttribute.min || 0,
|
227
|
+
finalAttribute.max || 2147483647,
|
228
|
+
finalAttribute.xdefault || undefined
|
229
|
+
)
|
230
|
+
);
|
231
|
+
}
|
232
|
+
break;
|
233
|
+
case "float":
|
234
|
+
if (action === "create") {
|
235
|
+
await tryAwaitWithRetry(
|
236
|
+
async () =>
|
237
|
+
await db.createFloatAttribute(
|
238
|
+
dbId,
|
239
|
+
collection.$id,
|
240
|
+
finalAttribute.key,
|
241
|
+
finalAttribute.required || false,
|
242
|
+
finalAttribute.min,
|
243
|
+
finalAttribute.max,
|
244
|
+
finalAttribute.xdefault || undefined,
|
245
|
+
finalAttribute.array
|
246
|
+
)
|
247
|
+
);
|
248
|
+
} else {
|
249
|
+
await tryAwaitWithRetry(
|
250
|
+
async () =>
|
251
|
+
await db.updateFloatAttribute(
|
252
|
+
dbId,
|
253
|
+
collection.$id,
|
254
|
+
finalAttribute.key,
|
255
|
+
finalAttribute.required || false,
|
256
|
+
finalAttribute.min || 0,
|
257
|
+
finalAttribute.max || 2147483647,
|
258
|
+
finalAttribute.xdefault || undefined
|
259
|
+
)
|
260
|
+
);
|
261
|
+
}
|
262
|
+
break;
|
263
|
+
case "boolean":
|
264
|
+
if (action === "create") {
|
265
|
+
await tryAwaitWithRetry(
|
266
|
+
async () =>
|
267
|
+
await db.createBooleanAttribute(
|
268
|
+
dbId,
|
269
|
+
collection.$id,
|
270
|
+
finalAttribute.key,
|
271
|
+
finalAttribute.required || false,
|
272
|
+
finalAttribute.xdefault || undefined,
|
273
|
+
finalAttribute.array
|
274
|
+
)
|
275
|
+
);
|
276
|
+
} else {
|
277
|
+
await tryAwaitWithRetry(
|
278
|
+
async () =>
|
279
|
+
await db.updateBooleanAttribute(
|
280
|
+
dbId,
|
281
|
+
collection.$id,
|
282
|
+
finalAttribute.key,
|
283
|
+
finalAttribute.required || false,
|
284
|
+
finalAttribute.xdefault || null
|
285
|
+
)
|
286
|
+
);
|
287
|
+
}
|
288
|
+
break;
|
289
|
+
case "datetime":
|
290
|
+
if (action === "create") {
|
291
|
+
await tryAwaitWithRetry(
|
292
|
+
async () =>
|
293
|
+
await db.createDatetimeAttribute(
|
294
|
+
dbId,
|
295
|
+
collection.$id,
|
296
|
+
finalAttribute.key,
|
297
|
+
finalAttribute.required || false,
|
298
|
+
finalAttribute.xdefault || undefined,
|
299
|
+
finalAttribute.array
|
300
|
+
)
|
301
|
+
);
|
302
|
+
} else {
|
303
|
+
await tryAwaitWithRetry(
|
304
|
+
async () =>
|
305
|
+
await db.updateDatetimeAttribute(
|
306
|
+
dbId,
|
307
|
+
collection.$id,
|
308
|
+
finalAttribute.key,
|
309
|
+
finalAttribute.required || false,
|
310
|
+
finalAttribute.xdefault || undefined
|
311
|
+
)
|
312
|
+
);
|
313
|
+
}
|
314
|
+
break;
|
315
|
+
case "email":
|
316
|
+
if (action === "create") {
|
317
|
+
await tryAwaitWithRetry(
|
318
|
+
async () =>
|
319
|
+
await db.createEmailAttribute(
|
320
|
+
dbId,
|
321
|
+
collection.$id,
|
322
|
+
finalAttribute.key,
|
323
|
+
finalAttribute.required || false,
|
324
|
+
finalAttribute.xdefault || undefined,
|
325
|
+
finalAttribute.array
|
326
|
+
)
|
327
|
+
);
|
328
|
+
} else {
|
329
|
+
await tryAwaitWithRetry(
|
330
|
+
async () =>
|
331
|
+
await db.updateEmailAttribute(
|
332
|
+
dbId,
|
333
|
+
collection.$id,
|
334
|
+
finalAttribute.key,
|
335
|
+
finalAttribute.required || false,
|
336
|
+
finalAttribute.xdefault || undefined
|
337
|
+
)
|
338
|
+
);
|
339
|
+
}
|
340
|
+
break;
|
341
|
+
case "ip":
|
342
|
+
if (action === "create") {
|
343
|
+
await tryAwaitWithRetry(
|
344
|
+
async () =>
|
345
|
+
await db.createIpAttribute(
|
346
|
+
dbId,
|
347
|
+
collection.$id,
|
348
|
+
finalAttribute.key,
|
349
|
+
finalAttribute.required || false,
|
350
|
+
finalAttribute.xdefault || undefined,
|
351
|
+
finalAttribute.array
|
352
|
+
)
|
353
|
+
);
|
354
|
+
} else {
|
355
|
+
await tryAwaitWithRetry(
|
356
|
+
async () =>
|
357
|
+
await db.updateIpAttribute(
|
358
|
+
dbId,
|
359
|
+
collection.$id,
|
360
|
+
finalAttribute.key,
|
361
|
+
finalAttribute.required || false,
|
362
|
+
finalAttribute.xdefault || undefined
|
363
|
+
)
|
364
|
+
);
|
365
|
+
}
|
366
|
+
break;
|
367
|
+
case "url":
|
368
|
+
if (action === "create") {
|
369
|
+
await tryAwaitWithRetry(
|
370
|
+
async () =>
|
371
|
+
await db.createUrlAttribute(
|
372
|
+
dbId,
|
373
|
+
collection.$id,
|
374
|
+
finalAttribute.key,
|
375
|
+
finalAttribute.required || false,
|
376
|
+
finalAttribute.xdefault || undefined,
|
377
|
+
finalAttribute.array
|
378
|
+
)
|
379
|
+
);
|
380
|
+
} else {
|
381
|
+
await tryAwaitWithRetry(
|
382
|
+
async () =>
|
383
|
+
await db.updateUrlAttribute(
|
384
|
+
dbId,
|
385
|
+
collection.$id,
|
386
|
+
finalAttribute.key,
|
387
|
+
finalAttribute.required || false,
|
388
|
+
finalAttribute.xdefault || undefined
|
389
|
+
)
|
390
|
+
);
|
391
|
+
}
|
392
|
+
break;
|
393
|
+
case "enum":
|
394
|
+
if (action === "create") {
|
395
|
+
await tryAwaitWithRetry(
|
396
|
+
async () =>
|
397
|
+
await db.createEnumAttribute(
|
398
|
+
dbId,
|
399
|
+
collection.$id,
|
400
|
+
finalAttribute.key,
|
401
|
+
finalAttribute.elements,
|
402
|
+
finalAttribute.required || false,
|
403
|
+
finalAttribute.xdefault || undefined,
|
404
|
+
finalAttribute.array
|
405
|
+
)
|
406
|
+
);
|
407
|
+
} else {
|
408
|
+
await tryAwaitWithRetry(
|
409
|
+
async () =>
|
410
|
+
await db.updateEnumAttribute(
|
411
|
+
dbId,
|
412
|
+
collection.$id,
|
413
|
+
finalAttribute.key,
|
414
|
+
finalAttribute.elements,
|
415
|
+
finalAttribute.required || false,
|
416
|
+
finalAttribute.xdefault || undefined
|
417
|
+
)
|
418
|
+
);
|
419
|
+
}
|
420
|
+
break;
|
421
|
+
case "relationship":
|
422
|
+
if (action === "create") {
|
423
|
+
await tryAwaitWithRetry(
|
424
|
+
async () =>
|
425
|
+
await db.createRelationshipAttribute(
|
426
|
+
dbId,
|
427
|
+
collection.$id,
|
428
|
+
relatedCollectionId!,
|
429
|
+
finalAttribute.relationType,
|
430
|
+
finalAttribute.twoWay,
|
431
|
+
finalAttribute.key,
|
432
|
+
finalAttribute.twoWayKey,
|
433
|
+
finalAttribute.onDelete
|
434
|
+
)
|
435
|
+
);
|
436
|
+
} else {
|
437
|
+
await tryAwaitWithRetry(
|
438
|
+
async () =>
|
439
|
+
await db.updateRelationshipAttribute(
|
440
|
+
dbId,
|
441
|
+
collection.$id,
|
442
|
+
finalAttribute.key,
|
443
|
+
finalAttribute.onDelete
|
444
|
+
)
|
445
|
+
);
|
446
|
+
}
|
447
|
+
break;
|
448
|
+
default:
|
449
|
+
console.error("Invalid attribute type");
|
450
|
+
break;
|
451
|
+
}
|
452
|
+
};
|
453
|
+
|
454
|
+
export const createUpdateCollectionAttributes = async (
|
455
|
+
db: Databases,
|
456
|
+
dbId: string,
|
457
|
+
collection: Models.Collection,
|
458
|
+
attributes: Attribute[]
|
459
|
+
): Promise<void> => {
|
460
|
+
console.log(
|
461
|
+
`Creating/Updating attributes for collection: ${collection.name}`
|
462
|
+
);
|
463
|
+
|
464
|
+
const batchSize = 3; // Size of each batch
|
465
|
+
for (let i = 0; i < attributes.length; i += batchSize) {
|
466
|
+
// Slice the attributes array to get a batch of at most batchSize elements
|
467
|
+
const batch = attributes.slice(i, i + batchSize);
|
468
|
+
const attributePromises = batch.map((attribute) =>
|
469
|
+
createOrUpdateAttribute(db, dbId, collection, attribute)
|
470
|
+
);
|
471
|
+
|
472
|
+
// Await the completion of all promises in the current batch
|
473
|
+
const results = await Promise.allSettled(attributePromises);
|
474
|
+
results.forEach((result) => {
|
475
|
+
if (result.status === "rejected") {
|
476
|
+
console.error("An attribute promise was rejected:", result.reason);
|
477
|
+
}
|
478
|
+
});
|
479
|
+
}
|
480
|
+
console.log(
|
481
|
+
`Finished creating/updating attributes for collection: ${collection.name}`
|
482
|
+
);
|
483
|
+
};
|