appwrite-utils-cli 0.10.86 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (178) hide show
  1. package/.appwrite/.yaml_schemas/appwrite-config.schema.json +380 -0
  2. package/.appwrite/.yaml_schemas/collection.schema.json +255 -0
  3. package/.appwrite/collections/Categories.yaml +182 -0
  4. package/.appwrite/collections/ExampleCollection.yaml +36 -0
  5. package/.appwrite/collections/Posts.yaml +227 -0
  6. package/.appwrite/collections/Users.yaml +149 -0
  7. package/.appwrite/config.yaml +109 -0
  8. package/.appwrite/import/README.md +148 -0
  9. package/.appwrite/import/categories-import.yaml +129 -0
  10. package/.appwrite/import/posts-import.yaml +208 -0
  11. package/.appwrite/import/users-import.yaml +130 -0
  12. package/.appwrite/importData/categories.json +194 -0
  13. package/.appwrite/importData/posts.json +270 -0
  14. package/.appwrite/importData/users.json +220 -0
  15. package/.appwrite/schemas/categories.json +128 -0
  16. package/.appwrite/schemas/exampleCollection.json +52 -0
  17. package/.appwrite/schemas/posts.json +173 -0
  18. package/.appwrite/schemas/users.json +125 -0
  19. package/README.md +260 -33
  20. package/dist/collections/attributes.js +3 -2
  21. package/dist/collections/methods.js +56 -38
  22. package/dist/config/yamlConfig.d.ts +501 -0
  23. package/dist/config/yamlConfig.js +452 -0
  24. package/dist/databases/setup.d.ts +6 -0
  25. package/dist/databases/setup.js +119 -0
  26. package/dist/functions/methods.d.ts +1 -1
  27. package/dist/functions/methods.js +5 -2
  28. package/dist/functions/openapi.d.ts +4 -0
  29. package/dist/functions/openapi.js +60 -0
  30. package/dist/interactiveCLI.d.ts +5 -0
  31. package/dist/interactiveCLI.js +194 -49
  32. package/dist/main.js +91 -30
  33. package/dist/migrations/afterImportActions.js +2 -2
  34. package/dist/migrations/appwriteToX.d.ts +10 -0
  35. package/dist/migrations/appwriteToX.js +15 -4
  36. package/dist/migrations/backup.d.ts +16 -16
  37. package/dist/migrations/dataLoader.d.ts +83 -1
  38. package/dist/migrations/dataLoader.js +4 -4
  39. package/dist/migrations/importController.js +25 -18
  40. package/dist/migrations/importDataActions.js +2 -2
  41. package/dist/migrations/logging.d.ts +9 -1
  42. package/dist/migrations/logging.js +41 -22
  43. package/dist/migrations/migrationHelper.d.ts +4 -4
  44. package/dist/migrations/relationships.js +1 -1
  45. package/dist/migrations/services/DataTransformationService.d.ts +55 -0
  46. package/dist/migrations/services/DataTransformationService.js +158 -0
  47. package/dist/migrations/services/FileHandlerService.d.ts +75 -0
  48. package/dist/migrations/services/FileHandlerService.js +236 -0
  49. package/dist/migrations/services/ImportOrchestrator.d.ts +97 -0
  50. package/dist/migrations/services/ImportOrchestrator.js +488 -0
  51. package/dist/migrations/services/RateLimitManager.d.ts +138 -0
  52. package/dist/migrations/services/RateLimitManager.js +279 -0
  53. package/dist/migrations/services/RelationshipResolver.d.ts +120 -0
  54. package/dist/migrations/services/RelationshipResolver.js +332 -0
  55. package/dist/migrations/services/UserMappingService.d.ts +109 -0
  56. package/dist/migrations/services/UserMappingService.js +277 -0
  57. package/dist/migrations/services/ValidationService.d.ts +74 -0
  58. package/dist/migrations/services/ValidationService.js +260 -0
  59. package/dist/migrations/transfer.d.ts +0 -6
  60. package/dist/migrations/transfer.js +16 -132
  61. package/dist/migrations/yaml/YamlImportConfigLoader.d.ts +384 -0
  62. package/dist/migrations/yaml/YamlImportConfigLoader.js +375 -0
  63. package/dist/migrations/yaml/YamlImportIntegration.d.ts +87 -0
  64. package/dist/migrations/yaml/YamlImportIntegration.js +330 -0
  65. package/dist/migrations/yaml/generateImportSchemas.d.ts +17 -0
  66. package/dist/migrations/yaml/generateImportSchemas.js +575 -0
  67. package/dist/schemas/authUser.d.ts +9 -9
  68. package/dist/shared/attributeManager.d.ts +17 -0
  69. package/dist/shared/attributeManager.js +273 -0
  70. package/dist/shared/confirmationDialogs.d.ts +75 -0
  71. package/dist/shared/confirmationDialogs.js +236 -0
  72. package/dist/shared/functionManager.d.ts +48 -0
  73. package/dist/shared/functionManager.js +322 -0
  74. package/dist/shared/indexManager.d.ts +24 -0
  75. package/dist/shared/indexManager.js +150 -0
  76. package/dist/shared/jsonSchemaGenerator.d.ts +51 -0
  77. package/dist/shared/jsonSchemaGenerator.js +313 -0
  78. package/dist/shared/logging.d.ts +10 -0
  79. package/dist/shared/logging.js +46 -0
  80. package/dist/shared/messageFormatter.d.ts +37 -0
  81. package/dist/shared/messageFormatter.js +152 -0
  82. package/dist/shared/migrationHelpers.d.ts +173 -0
  83. package/dist/shared/migrationHelpers.js +142 -0
  84. package/dist/shared/operationLogger.d.ts +3 -0
  85. package/dist/shared/operationLogger.js +25 -0
  86. package/dist/shared/operationQueue.d.ts +13 -0
  87. package/dist/shared/operationQueue.js +79 -0
  88. package/dist/shared/progressManager.d.ts +62 -0
  89. package/dist/shared/progressManager.js +215 -0
  90. package/dist/shared/schemaGenerator.d.ts +18 -0
  91. package/dist/shared/schemaGenerator.js +523 -0
  92. package/dist/storage/methods.d.ts +3 -1
  93. package/dist/storage/methods.js +144 -55
  94. package/dist/storage/schemas.d.ts +56 -16
  95. package/dist/types.d.ts +2 -2
  96. package/dist/types.js +1 -1
  97. package/dist/users/methods.d.ts +16 -0
  98. package/dist/users/methods.js +276 -0
  99. package/dist/utils/configMigration.d.ts +1 -0
  100. package/dist/utils/configMigration.js +156 -0
  101. package/dist/utils/dataConverters.d.ts +46 -0
  102. package/dist/utils/dataConverters.js +139 -0
  103. package/dist/utils/loadConfigs.d.ts +15 -4
  104. package/dist/utils/loadConfigs.js +377 -51
  105. package/dist/utils/schemaStrings.js +2 -1
  106. package/dist/utils/setupFiles.d.ts +2 -1
  107. package/dist/utils/setupFiles.js +723 -28
  108. package/dist/utils/validationRules.d.ts +43 -0
  109. package/dist/utils/validationRules.js +42 -0
  110. package/dist/utils/yamlConverter.d.ts +48 -0
  111. package/dist/utils/yamlConverter.js +98 -0
  112. package/dist/utilsController.js +65 -43
  113. package/package.json +19 -15
  114. package/src/collections/attributes.ts +3 -2
  115. package/src/collections/methods.ts +85 -51
  116. package/src/config/yamlConfig.ts +488 -0
  117. package/src/{migrations/setupDatabase.ts → databases/setup.ts} +11 -5
  118. package/src/functions/methods.ts +8 -4
  119. package/src/functions/templates/count-docs-in-collection/package.json +25 -0
  120. package/src/functions/templates/count-docs-in-collection/tsconfig.json +28 -0
  121. package/src/functions/templates/typescript-node/package.json +24 -0
  122. package/src/functions/templates/typescript-node/tsconfig.json +28 -0
  123. package/src/functions/templates/uv/README.md +31 -0
  124. package/src/functions/templates/uv/pyproject.toml +29 -0
  125. package/src/interactiveCLI.ts +226 -61
  126. package/src/main.ts +111 -37
  127. package/src/migrations/afterImportActions.ts +2 -2
  128. package/src/migrations/appwriteToX.ts +17 -4
  129. package/src/migrations/dataLoader.ts +4 -4
  130. package/src/migrations/importController.ts +30 -22
  131. package/src/migrations/importDataActions.ts +2 -2
  132. package/src/migrations/relationships.ts +1 -1
  133. package/src/migrations/services/DataTransformationService.ts +196 -0
  134. package/src/migrations/services/FileHandlerService.ts +311 -0
  135. package/src/migrations/services/ImportOrchestrator.ts +669 -0
  136. package/src/migrations/services/RateLimitManager.ts +363 -0
  137. package/src/migrations/services/RelationshipResolver.ts +461 -0
  138. package/src/migrations/services/UserMappingService.ts +345 -0
  139. package/src/migrations/services/ValidationService.ts +349 -0
  140. package/src/migrations/transfer.ts +22 -228
  141. package/src/migrations/yaml/YamlImportConfigLoader.ts +427 -0
  142. package/src/migrations/yaml/YamlImportIntegration.ts +419 -0
  143. package/src/migrations/yaml/generateImportSchemas.ts +589 -0
  144. package/src/shared/attributeManager.ts +429 -0
  145. package/src/shared/confirmationDialogs.ts +327 -0
  146. package/src/shared/functionManager.ts +515 -0
  147. package/src/shared/indexManager.ts +253 -0
  148. package/src/shared/jsonSchemaGenerator.ts +403 -0
  149. package/src/shared/logging.ts +74 -0
  150. package/src/shared/messageFormatter.ts +195 -0
  151. package/src/{migrations/migrationHelper.ts → shared/migrationHelpers.ts} +22 -4
  152. package/src/{migrations/helper.ts → shared/operationLogger.ts} +7 -2
  153. package/src/{migrations/queue.ts → shared/operationQueue.ts} +1 -1
  154. package/src/shared/progressManager.ts +278 -0
  155. package/src/{migrations/schemaStrings.ts → shared/schemaGenerator.ts} +71 -17
  156. package/src/storage/methods.ts +199 -78
  157. package/src/types.ts +2 -2
  158. package/src/{migrations/users.ts → users/methods.ts} +2 -2
  159. package/src/utils/configMigration.ts +212 -0
  160. package/src/utils/loadConfigs.ts +414 -52
  161. package/src/utils/schemaStrings.ts +2 -1
  162. package/src/utils/setupFiles.ts +742 -40
  163. package/src/{migrations → utils}/validationRules.ts +1 -1
  164. package/src/utils/yamlConverter.ts +131 -0
  165. package/src/utilsController.ts +75 -54
  166. package/src/functions/templates/poetry/README.md +0 -30
  167. package/src/functions/templates/poetry/pyproject.toml +0 -16
  168. package/src/migrations/attributes.ts +0 -561
  169. package/src/migrations/backup.ts +0 -205
  170. package/src/migrations/databases.ts +0 -39
  171. package/src/migrations/dbHelpers.ts +0 -92
  172. package/src/migrations/indexes.ts +0 -40
  173. package/src/migrations/logging.ts +0 -29
  174. package/src/migrations/storage.ts +0 -538
  175. /package/src/{migrations → functions}/openapi.ts +0 -0
  176. /package/src/functions/templates/{poetry → uv}/src/__init__.py +0 -0
  177. /package/src/functions/templates/{poetry → uv}/src/index.py +0 -0
  178. /package/src/{migrations/converters.ts → utils/dataConverters.ts} +0 -0
@@ -1,561 +0,0 @@
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 { tryAwaitWithRetry } from "../utils/helperFunctions.js";
9
-
10
- const attributesSame = (
11
- databaseAttribute: Attribute,
12
- configAttribute: Attribute
13
- ): boolean => {
14
- const attributesToCheck = [
15
- "key",
16
- "type",
17
- "array",
18
- "encrypted",
19
- "required",
20
- "size",
21
- "min",
22
- "max",
23
- "xdefault",
24
- "elements",
25
- "relationType",
26
- "twoWay",
27
- "twoWayKey",
28
- "onDelete",
29
- "relatedCollection",
30
- ];
31
-
32
- return attributesToCheck.every((attr) => {
33
- // Special handling for min/max values
34
- if (attr === "min" || attr === "max") {
35
- const dbValue = databaseAttribute[attr as keyof typeof databaseAttribute];
36
- const configValue = configAttribute[attr as keyof typeof configAttribute];
37
-
38
- // Use type-specific default values when comparing
39
- if (databaseAttribute.type === "integer") {
40
- const defaultMin = attr === "min" ? -2147483647 : undefined;
41
- const defaultMax = attr === "max" ? 2147483647 : undefined;
42
- return (dbValue ?? defaultMin) === (configValue ?? defaultMax);
43
- }
44
- if (databaseAttribute.type === "float") {
45
- const defaultMin = attr === "min" ? -2147483647 : undefined;
46
- const defaultMax = attr === "max" ? 2147483647 : undefined;
47
- return (dbValue ?? defaultMin) === (configValue ?? defaultMax);
48
- }
49
- }
50
-
51
- // Check if both objects have the attribute
52
- const dbHasAttr = attr in databaseAttribute;
53
- const configHasAttr = attr in configAttribute;
54
-
55
- // If both have the attribute, compare values
56
- if (dbHasAttr && configHasAttr) {
57
- const dbValue = databaseAttribute[attr as keyof typeof databaseAttribute];
58
- const configValue = configAttribute[attr as keyof typeof configAttribute];
59
-
60
- // Consider undefined and null as equivalent
61
- if (
62
- (dbValue === undefined || dbValue === null) &&
63
- (configValue === undefined || configValue === null)
64
- ) {
65
- return true;
66
- }
67
-
68
- return dbValue === configValue;
69
- }
70
-
71
- // If neither has the attribute, consider it the same
72
- if (!dbHasAttr && !configHasAttr) {
73
- return true;
74
- }
75
-
76
- // If one has the attribute and the other doesn't, check if it's undefined or null
77
- if (dbHasAttr && !configHasAttr) {
78
- const dbValue = databaseAttribute[attr as keyof typeof databaseAttribute];
79
- return dbValue === undefined || dbValue === null;
80
- }
81
-
82
- if (!dbHasAttr && configHasAttr) {
83
- const configValue = configAttribute[attr as keyof typeof configAttribute];
84
- return configValue === undefined || configValue === null;
85
- }
86
-
87
- // If we reach here, the attributes are different
88
- return false;
89
- });
90
- };
91
-
92
- export const createOrUpdateAttribute = async (
93
- db: Databases,
94
- dbId: string,
95
- collection: Models.Collection,
96
- attribute: Attribute
97
- ): Promise<void> => {
98
- let action = "create";
99
- let foundAttribute: Attribute | undefined;
100
- const updateEnabled = true;
101
- let finalAttribute: any = attribute;
102
- try {
103
- const collectionAttr = collection.attributes.find(
104
- // @ts-expect-error
105
- (attr) => attr.key === attribute.key
106
- ) as unknown as any;
107
- foundAttribute = parseAttribute(collectionAttr);
108
- } catch (error) {
109
- foundAttribute = undefined;
110
- }
111
-
112
- if (
113
- foundAttribute &&
114
- attributesSame(foundAttribute, attribute) &&
115
- updateEnabled
116
- ) {
117
- finalAttribute = {
118
- ...attribute,
119
- ...foundAttribute,
120
- };
121
- action = "update";
122
- } else if (
123
- foundAttribute &&
124
- !attributesSame(foundAttribute, attribute) &&
125
- updateEnabled
126
- ) {
127
- console.log(
128
- `Updating attribute with same key ${attribute.key} but different values`
129
- );
130
- finalAttribute = attribute;
131
- action = "update";
132
- } else if (
133
- !updateEnabled &&
134
- foundAttribute &&
135
- !attributesSame(foundAttribute, attribute)
136
- ) {
137
- await db.deleteAttribute(dbId, collection.$id, attribute.key);
138
- console.log(
139
- `Deleted attribute: ${attribute.key} to recreate it because they diff (update disabled temporarily)`
140
- );
141
- return;
142
- }
143
-
144
- console.log(`${action}-ing attribute: ${finalAttribute.key}`);
145
-
146
- // Relationship attribute logic with adjustments
147
- let collectionFoundViaRelatedCollection: Models.Collection | undefined;
148
- let relatedCollectionId: string | undefined;
149
- if (finalAttribute.type === "relationship") {
150
- if (nameToIdMapping.has(finalAttribute.relatedCollection)) {
151
- relatedCollectionId = nameToIdMapping.get(
152
- finalAttribute.relatedCollection
153
- );
154
- try {
155
- collectionFoundViaRelatedCollection = await db.getCollection(
156
- dbId,
157
- relatedCollectionId!
158
- );
159
- } catch (e) {
160
- console.log(
161
- `Collection not found: ${finalAttribute.relatedCollection} when nameToIdMapping was set`
162
- );
163
- collectionFoundViaRelatedCollection = undefined;
164
- }
165
- } else {
166
- const collectionsPulled = await db.listCollections(dbId, [
167
- Query.equal("name", finalAttribute.relatedCollection),
168
- ]);
169
- if (collectionsPulled.total > 0) {
170
- collectionFoundViaRelatedCollection = collectionsPulled.collections[0];
171
- relatedCollectionId = collectionFoundViaRelatedCollection.$id;
172
- nameToIdMapping.set(
173
- finalAttribute.relatedCollection,
174
- relatedCollectionId
175
- );
176
- }
177
- }
178
- if (!(relatedCollectionId && collectionFoundViaRelatedCollection)) {
179
- console.log(`Enqueueing operation for attribute: ${finalAttribute.key}`);
180
- enqueueOperation({
181
- type: "attribute",
182
- collectionId: collection.$id,
183
- collection: collection,
184
- attribute,
185
- dependencies: [finalAttribute.relatedCollection],
186
- });
187
- return;
188
- }
189
- }
190
- finalAttribute = attributeSchema.parse(finalAttribute);
191
- console.log(`Final Attribute: ${JSON.stringify(finalAttribute)}`);
192
- switch (finalAttribute.type) {
193
- case "string":
194
- if (action === "create") {
195
- await tryAwaitWithRetry(
196
- async () =>
197
- await db.createStringAttribute(
198
- dbId,
199
- collection.$id,
200
- finalAttribute.key,
201
- finalAttribute.size,
202
- finalAttribute.required || false,
203
- finalAttribute.xdefault !== undefined && !finalAttribute.required
204
- ? finalAttribute.xdefault
205
- : null,
206
- finalAttribute.array || false,
207
- finalAttribute.encrypted
208
- )
209
- );
210
- } else {
211
- await tryAwaitWithRetry(
212
- async () =>
213
- await db.updateStringAttribute(
214
- dbId,
215
- collection.$id,
216
- finalAttribute.key,
217
- finalAttribute.required || false,
218
- finalAttribute.xdefault !== undefined && !finalAttribute.required
219
- ? finalAttribute.xdefault
220
- : null
221
- )
222
- );
223
- }
224
- break;
225
- case "integer":
226
- if (action === "create") {
227
- if (
228
- finalAttribute.min &&
229
- BigInt(finalAttribute.min) === BigInt(-9223372036854776000)
230
- ) {
231
- delete finalAttribute.min;
232
- }
233
- if (
234
- finalAttribute.max &&
235
- BigInt(finalAttribute.max) === BigInt(9223372036854776000)
236
- ) {
237
- delete finalAttribute.max;
238
- }
239
- await tryAwaitWithRetry(
240
- async () =>
241
- await db.createIntegerAttribute(
242
- dbId,
243
- collection.$id,
244
- finalAttribute.key,
245
- finalAttribute.required || false,
246
- finalAttribute.min || -2147483647,
247
- finalAttribute.max || 2147483647,
248
- finalAttribute.xdefault !== undefined && !finalAttribute.required
249
- ? finalAttribute.xdefault
250
- : null,
251
- finalAttribute.array || false
252
- )
253
- );
254
- } else {
255
- if (
256
- finalAttribute.min &&
257
- BigInt(finalAttribute.min) === BigInt(-9223372036854776000)
258
- ) {
259
- delete finalAttribute.min;
260
- }
261
- if (
262
- finalAttribute.max &&
263
- BigInt(finalAttribute.max) === BigInt(9223372036854776000)
264
- ) {
265
- delete finalAttribute.max;
266
- }
267
- await tryAwaitWithRetry(
268
- async () =>
269
- await db.updateIntegerAttribute(
270
- dbId,
271
- collection.$id,
272
- finalAttribute.key,
273
- finalAttribute.required || false,
274
- finalAttribute.min || -2147483647,
275
- finalAttribute.max || 2147483647,
276
- finalAttribute.xdefault !== undefined && !finalAttribute.required
277
- ? finalAttribute.xdefault
278
- : null
279
- )
280
- );
281
- }
282
- break;
283
- case "float":
284
- if (action === "create") {
285
- await tryAwaitWithRetry(
286
- async () =>
287
- await db.createFloatAttribute(
288
- dbId,
289
- collection.$id,
290
- finalAttribute.key,
291
- finalAttribute.required || false,
292
- finalAttribute.min || -2147483647,
293
- finalAttribute.max || 2147483647,
294
- finalAttribute.xdefault !== undefined && !finalAttribute.required
295
- ? finalAttribute.xdefault
296
- : null,
297
- finalAttribute.array || false
298
- )
299
- );
300
- } else {
301
- await tryAwaitWithRetry(
302
- async () =>
303
- await db.updateFloatAttribute(
304
- dbId,
305
- collection.$id,
306
- finalAttribute.key,
307
- finalAttribute.required || false,
308
- finalAttribute.min || -2147483647,
309
- finalAttribute.max || 2147483647,
310
- finalAttribute.xdefault !== undefined && !finalAttribute.required
311
- ? finalAttribute.xdefault
312
- : null
313
- )
314
- );
315
- }
316
- break;
317
- case "boolean":
318
- if (action === "create") {
319
- await tryAwaitWithRetry(
320
- async () =>
321
- await db.createBooleanAttribute(
322
- dbId,
323
- collection.$id,
324
- finalAttribute.key,
325
- finalAttribute.required || false,
326
- finalAttribute.xdefault !== undefined && !finalAttribute.required
327
- ? finalAttribute.xdefault
328
- : null,
329
- finalAttribute.array || false
330
- )
331
- );
332
- } else {
333
- await tryAwaitWithRetry(
334
- async () =>
335
- await db.updateBooleanAttribute(
336
- dbId,
337
- collection.$id,
338
- finalAttribute.key,
339
- finalAttribute.required || false,
340
- finalAttribute.xdefault !== undefined && !finalAttribute.required
341
- ? finalAttribute.xdefault
342
- : null
343
- )
344
- );
345
- }
346
- break;
347
- case "datetime":
348
- if (action === "create") {
349
- await tryAwaitWithRetry(
350
- async () =>
351
- await db.createDatetimeAttribute(
352
- dbId,
353
- collection.$id,
354
- finalAttribute.key,
355
- finalAttribute.required || false,
356
- finalAttribute.xdefault !== undefined && !finalAttribute.required
357
- ? finalAttribute.xdefault
358
- : null,
359
- finalAttribute.array || false
360
- )
361
- );
362
- } else {
363
- await tryAwaitWithRetry(
364
- async () =>
365
- await db.updateDatetimeAttribute(
366
- dbId,
367
- collection.$id,
368
- finalAttribute.key,
369
- finalAttribute.required || false,
370
- finalAttribute.xdefault !== undefined && !finalAttribute.required
371
- ? finalAttribute.xdefault
372
- : null
373
- )
374
- );
375
- }
376
- break;
377
- case "email":
378
- if (action === "create") {
379
- await tryAwaitWithRetry(
380
- async () =>
381
- await db.createEmailAttribute(
382
- dbId,
383
- collection.$id,
384
- finalAttribute.key,
385
- finalAttribute.required || false,
386
- finalAttribute.xdefault !== undefined && !finalAttribute.required
387
- ? finalAttribute.xdefault
388
- : null,
389
- finalAttribute.array || false
390
- )
391
- );
392
- } else {
393
- await tryAwaitWithRetry(
394
- async () =>
395
- await db.updateEmailAttribute(
396
- dbId,
397
- collection.$id,
398
- finalAttribute.key,
399
- finalAttribute.required || false,
400
- finalAttribute.xdefault !== undefined && !finalAttribute.required
401
- ? finalAttribute.xdefault
402
- : null
403
- )
404
- );
405
- }
406
- break;
407
- case "ip":
408
- if (action === "create") {
409
- await tryAwaitWithRetry(
410
- async () =>
411
- await db.createIpAttribute(
412
- dbId,
413
- collection.$id,
414
- finalAttribute.key,
415
- finalAttribute.required || false,
416
- finalAttribute.xdefault !== undefined && !finalAttribute.required
417
- ? finalAttribute.xdefault
418
- : null,
419
- finalAttribute.array || false
420
- )
421
- );
422
- } else {
423
- await tryAwaitWithRetry(
424
- async () =>
425
- await db.updateIpAttribute(
426
- dbId,
427
- collection.$id,
428
- finalAttribute.key,
429
- finalAttribute.required || false,
430
- finalAttribute.xdefault !== undefined && !finalAttribute.required
431
- ? finalAttribute.xdefault
432
- : null
433
- )
434
- );
435
- }
436
- break;
437
- case "url":
438
- if (action === "create") {
439
- await tryAwaitWithRetry(
440
- async () =>
441
- await db.createUrlAttribute(
442
- dbId,
443
- collection.$id,
444
- finalAttribute.key,
445
- finalAttribute.required || false,
446
- finalAttribute.xdefault !== undefined && !finalAttribute.required
447
- ? finalAttribute.xdefault
448
- : null,
449
- finalAttribute.array || false
450
- )
451
- );
452
- } else {
453
- await tryAwaitWithRetry(
454
- async () =>
455
- await db.updateUrlAttribute(
456
- dbId,
457
- collection.$id,
458
- finalAttribute.key,
459
- finalAttribute.required || false,
460
- finalAttribute.xdefault !== undefined && !finalAttribute.required
461
- ? finalAttribute.xdefault
462
- : null
463
- )
464
- );
465
- }
466
- break;
467
- case "enum":
468
- if (action === "create") {
469
- await tryAwaitWithRetry(
470
- async () =>
471
- await db.createEnumAttribute(
472
- dbId,
473
- collection.$id,
474
- finalAttribute.key,
475
- finalAttribute.elements,
476
- finalAttribute.required || false,
477
- finalAttribute.xdefault !== undefined && !finalAttribute.required
478
- ? finalAttribute.xdefault
479
- : null,
480
- finalAttribute.array || false
481
- )
482
- );
483
- } else {
484
- await tryAwaitWithRetry(
485
- async () =>
486
- await db.updateEnumAttribute(
487
- dbId,
488
- collection.$id,
489
- finalAttribute.key,
490
- finalAttribute.elements,
491
- finalAttribute.required || false,
492
- finalAttribute.xdefault !== undefined && !finalAttribute.required
493
- ? finalAttribute.xdefault
494
- : null
495
- )
496
- );
497
- }
498
- break;
499
- case "relationship":
500
- if (action === "create") {
501
- await tryAwaitWithRetry(
502
- async () =>
503
- await db.createRelationshipAttribute(
504
- dbId,
505
- collection.$id,
506
- relatedCollectionId!,
507
- finalAttribute.relationType,
508
- finalAttribute.twoWay,
509
- finalAttribute.key,
510
- finalAttribute.twoWayKey,
511
- finalAttribute.onDelete
512
- )
513
- );
514
- } else {
515
- await tryAwaitWithRetry(
516
- async () =>
517
- await db.updateRelationshipAttribute(
518
- dbId,
519
- collection.$id,
520
- finalAttribute.key,
521
- finalAttribute.onDelete
522
- )
523
- );
524
- }
525
- break;
526
- default:
527
- console.error("Invalid attribute type");
528
- break;
529
- }
530
- };
531
-
532
- export const createUpdateCollectionAttributes = async (
533
- db: Databases,
534
- dbId: string,
535
- collection: Models.Collection,
536
- attributes: Attribute[]
537
- ): Promise<void> => {
538
- console.log(
539
- `Creating/Updating attributes for collection: ${collection.name}`
540
- );
541
-
542
- const batchSize = 3; // Size of each batch
543
- for (let i = 0; i < attributes.length; i += batchSize) {
544
- // Slice the attributes array to get a batch of at most batchSize elements
545
- const batch = attributes.slice(i, i + batchSize);
546
- const attributePromises = batch.map((attribute) =>
547
- createOrUpdateAttribute(db, dbId, collection, attribute)
548
- );
549
-
550
- // Await the completion of all promises in the current batch
551
- const results = await Promise.allSettled(attributePromises);
552
- results.forEach((result) => {
553
- if (result.status === "rejected") {
554
- console.error("An attribute promise was rejected:", result.reason);
555
- }
556
- });
557
- }
558
- console.log(
559
- `Finished creating/updating attributes for collection: ${collection.name}`
560
- );
561
- };