appwrite-utils-cli 1.3.4 → 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (116) hide show
  1. package/dist/adapters/AdapterFactory.d.ts +87 -0
  2. package/dist/adapters/AdapterFactory.js +217 -0
  3. package/dist/adapters/DatabaseAdapter.d.ts +217 -0
  4. package/dist/adapters/DatabaseAdapter.js +50 -0
  5. package/dist/adapters/LegacyAdapter.d.ts +49 -0
  6. package/dist/adapters/LegacyAdapter.js +382 -0
  7. package/dist/adapters/TablesDBAdapter.d.ts +55 -0
  8. package/dist/adapters/TablesDBAdapter.js +302 -0
  9. package/dist/adapters/index.d.ts +11 -0
  10. package/dist/adapters/index.js +12 -0
  11. package/dist/collections/attributes.js +43 -24
  12. package/dist/collections/methods.d.ts +4 -3
  13. package/dist/collections/methods.js +34 -14
  14. package/dist/config/yamlConfig.d.ts +40 -437
  15. package/dist/config/yamlConfig.js +8 -2
  16. package/dist/databases/setup.js +2 -2
  17. package/dist/main.js +0 -0
  18. package/dist/migrations/appwriteToX.d.ts +26 -37
  19. package/dist/migrations/comprehensiveTransfer.js +4 -4
  20. package/dist/migrations/dataLoader.d.ts +124 -1484
  21. package/dist/migrations/dataLoader.js +2 -1
  22. package/dist/migrations/relationships.d.ts +2 -3
  23. package/dist/migrations/relationships.js +1 -1
  24. package/dist/migrations/services/UserMappingService.js +1 -1
  25. package/dist/migrations/yaml/YamlImportConfigLoader.d.ts +24 -279
  26. package/dist/migrations/yaml/YamlImportConfigLoader.js +7 -2
  27. package/dist/schemas/authUser.d.ts +7 -47
  28. package/dist/schemas/authUser.js +1 -1
  29. package/dist/shared/jsonSchemaGenerator.d.ts +0 -2
  30. package/dist/shared/jsonSchemaGenerator.js +4 -17
  31. package/dist/shared/migrationHelpers.d.ts +17 -119
  32. package/dist/shared/operationQueue.js +16 -7
  33. package/dist/shared/schemaGenerator.js +2 -17
  34. package/dist/storage/schemas.d.ts +149 -296
  35. package/dist/users/methods.d.ts +2 -2
  36. package/dist/utils/configMigration.js +0 -1
  37. package/dist/utils/getClientFromConfig.d.ts +26 -0
  38. package/dist/utils/getClientFromConfig.js +37 -0
  39. package/dist/utils/loadConfigs.js +0 -2
  40. package/dist/utils/schemaStrings.js +2 -17
  41. package/dist/utils/setupFiles.js +2 -0
  42. package/dist/utils/versionDetection.d.ts +56 -0
  43. package/dist/utils/versionDetection.js +217 -0
  44. package/dist/utils/yamlConverter.d.ts +0 -1
  45. package/dist/utils/yamlConverter.js +0 -2
  46. package/dist/utilsController.js +2 -0
  47. package/package.json +3 -2
  48. package/src/adapters/AdapterFactory.ts +296 -0
  49. package/src/adapters/DatabaseAdapter.ts +290 -0
  50. package/src/adapters/LegacyAdapter.ts +667 -0
  51. package/src/adapters/TablesDBAdapter.ts +429 -0
  52. package/src/adapters/index.ts +37 -0
  53. package/src/collections/attributes.ts +351 -157
  54. package/src/collections/methods.ts +43 -28
  55. package/src/config/yamlConfig.ts +8 -2
  56. package/src/databases/setup.ts +2 -2
  57. package/src/migrations/afterImportActions.ts +2 -2
  58. package/src/migrations/comprehensiveTransfer.ts +4 -0
  59. package/src/migrations/dataLoader.ts +2 -1
  60. package/src/migrations/relationships.ts +1 -1
  61. package/src/migrations/services/UserMappingService.ts +1 -1
  62. package/src/migrations/yaml/YamlImportConfigLoader.ts +7 -2
  63. package/src/schemas/authUser.ts +1 -1
  64. package/src/shared/jsonSchemaGenerator.ts +4 -19
  65. package/src/shared/operationQueue.ts +20 -13
  66. package/src/shared/schemaGenerator.ts +2 -16
  67. package/src/types/node-appwrite-tablesdb.d.ts +44 -0
  68. package/src/users/methods.ts +2 -2
  69. package/src/utils/configMigration.ts +0 -1
  70. package/src/utils/getClientFromConfig.ts +56 -0
  71. package/src/utils/loadConfigs.ts +0 -2
  72. package/src/utils/schemaStrings.ts +2 -16
  73. package/src/utils/setupFiles.ts +2 -0
  74. package/src/utils/versionDetection.ts +265 -0
  75. package/src/utils/yamlConverter.ts +0 -2
  76. package/src/utilsController.ts +2 -0
  77. package/dist/functions/openapi.d.ts +0 -4
  78. package/dist/functions/openapi.js +0 -60
  79. package/dist/migrations/attributes.d.ts +0 -4
  80. package/dist/migrations/attributes.js +0 -301
  81. package/dist/migrations/backup.d.ts +0 -687
  82. package/dist/migrations/backup.js +0 -175
  83. package/dist/migrations/collections.d.ts +0 -22
  84. package/dist/migrations/collections.js +0 -347
  85. package/dist/migrations/converters.d.ts +0 -46
  86. package/dist/migrations/converters.js +0 -139
  87. package/dist/migrations/databases.d.ts +0 -2
  88. package/dist/migrations/databases.js +0 -28
  89. package/dist/migrations/dbHelpers.d.ts +0 -5
  90. package/dist/migrations/dbHelpers.js +0 -57
  91. package/dist/migrations/helper.d.ts +0 -3
  92. package/dist/migrations/helper.js +0 -21
  93. package/dist/migrations/indexes.d.ts +0 -4
  94. package/dist/migrations/indexes.js +0 -19
  95. package/dist/migrations/logging.d.ts +0 -10
  96. package/dist/migrations/logging.js +0 -46
  97. package/dist/migrations/migrationHelper.d.ts +0 -173
  98. package/dist/migrations/migrationHelper.js +0 -130
  99. package/dist/migrations/openapi.d.ts +0 -4
  100. package/dist/migrations/openapi.js +0 -60
  101. package/dist/migrations/queue.d.ts +0 -13
  102. package/dist/migrations/queue.js +0 -79
  103. package/dist/migrations/schemaStrings.d.ts +0 -14
  104. package/dist/migrations/schemaStrings.js +0 -478
  105. package/dist/migrations/setupDatabase.d.ts +0 -6
  106. package/dist/migrations/setupDatabase.js +0 -115
  107. package/dist/migrations/storage.d.ts +0 -10
  108. package/dist/migrations/storage.js +0 -340
  109. package/dist/migrations/users.d.ts +0 -16
  110. package/dist/migrations/users.js +0 -276
  111. package/dist/migrations/validationRules.d.ts +0 -43
  112. package/dist/migrations/validationRules.js +0 -42
  113. package/dist/shared/attributeManager.d.ts +0 -17
  114. package/dist/shared/attributeManager.js +0 -272
  115. package/src/functions/openapi.ts +0 -83
  116. package/src/shared/attributeManager.ts +0 -428
@@ -1,301 +0,0 @@
1
- import { Query } from "node-appwrite";
2
- import { attributeSchema, parseAttribute, } from "appwrite-utils";
3
- import { nameToIdMapping, enqueueOperation } from "./queue.js";
4
- import { tryAwaitWithRetry } from "../utils/helperFunctions.js";
5
- const attributesSame = (databaseAttribute, configAttribute) => {
6
- const attributesToCheck = [
7
- "key",
8
- "type",
9
- "array",
10
- "encrypted",
11
- "required",
12
- "size",
13
- "min",
14
- "max",
15
- "xdefault",
16
- "elements",
17
- "relationType",
18
- "twoWay",
19
- "twoWayKey",
20
- "onDelete",
21
- "relatedCollection",
22
- ];
23
- return attributesToCheck.every((attr) => {
24
- // Special handling for min/max values
25
- if (attr === "min" || attr === "max") {
26
- const dbValue = databaseAttribute[attr];
27
- const configValue = configAttribute[attr];
28
- // Use type-specific default values when comparing
29
- if (databaseAttribute.type === "integer") {
30
- const defaultMin = attr === "min" ? -2147483647 : undefined;
31
- const defaultMax = attr === "max" ? 2147483647 : undefined;
32
- return (dbValue ?? defaultMin) === (configValue ?? defaultMax);
33
- }
34
- if (databaseAttribute.type === "float") {
35
- const defaultMin = attr === "min" ? -2147483647 : undefined;
36
- const defaultMax = attr === "max" ? 2147483647 : undefined;
37
- return (dbValue ?? defaultMin) === (configValue ?? defaultMax);
38
- }
39
- }
40
- // Check if both objects have the attribute
41
- const dbHasAttr = attr in databaseAttribute;
42
- const configHasAttr = attr in configAttribute;
43
- // If both have the attribute, compare values
44
- if (dbHasAttr && configHasAttr) {
45
- const dbValue = databaseAttribute[attr];
46
- const configValue = configAttribute[attr];
47
- // Consider undefined and null as equivalent
48
- if ((dbValue === undefined || dbValue === null) &&
49
- (configValue === undefined || configValue === null)) {
50
- return true;
51
- }
52
- return dbValue === configValue;
53
- }
54
- // If neither has the attribute, consider it the same
55
- if (!dbHasAttr && !configHasAttr) {
56
- return true;
57
- }
58
- // If one has the attribute and the other doesn't, check if it's undefined or null
59
- if (dbHasAttr && !configHasAttr) {
60
- const dbValue = databaseAttribute[attr];
61
- return dbValue === undefined || dbValue === null;
62
- }
63
- if (!dbHasAttr && configHasAttr) {
64
- const configValue = configAttribute[attr];
65
- return configValue === undefined || configValue === null;
66
- }
67
- // If we reach here, the attributes are different
68
- return false;
69
- });
70
- };
71
- export const createOrUpdateAttribute = async (db, dbId, collection, attribute) => {
72
- let action = "create";
73
- let foundAttribute;
74
- const updateEnabled = true;
75
- let finalAttribute = attribute;
76
- try {
77
- const collectionAttr = collection.attributes.find(
78
- // @ts-expect-error
79
- (attr) => attr.key === attribute.key);
80
- foundAttribute = parseAttribute(collectionAttr);
81
- }
82
- catch (error) {
83
- foundAttribute = undefined;
84
- }
85
- if (foundAttribute &&
86
- attributesSame(foundAttribute, attribute) &&
87
- updateEnabled) {
88
- finalAttribute = {
89
- ...attribute,
90
- ...foundAttribute,
91
- };
92
- action = "update";
93
- }
94
- else if (foundAttribute &&
95
- !attributesSame(foundAttribute, attribute) &&
96
- updateEnabled) {
97
- console.log(`Updating attribute with same key ${attribute.key} but different values`);
98
- finalAttribute = attribute;
99
- action = "update";
100
- }
101
- else if (!updateEnabled &&
102
- foundAttribute &&
103
- !attributesSame(foundAttribute, attribute)) {
104
- await db.deleteAttribute(dbId, collection.$id, attribute.key);
105
- console.log(`Deleted attribute: ${attribute.key} to recreate it because they diff (update disabled temporarily)`);
106
- return;
107
- }
108
- console.log(`${action}-ing attribute: ${finalAttribute.key}`);
109
- // Relationship attribute logic with adjustments
110
- let collectionFoundViaRelatedCollection;
111
- let relatedCollectionId;
112
- if (finalAttribute.type === "relationship") {
113
- if (nameToIdMapping.has(finalAttribute.relatedCollection)) {
114
- relatedCollectionId = nameToIdMapping.get(finalAttribute.relatedCollection);
115
- try {
116
- collectionFoundViaRelatedCollection = await db.getCollection(dbId, relatedCollectionId);
117
- }
118
- catch (e) {
119
- console.log(`Collection not found: ${finalAttribute.relatedCollection} when nameToIdMapping was set`);
120
- collectionFoundViaRelatedCollection = undefined;
121
- }
122
- }
123
- else {
124
- const collectionsPulled = await db.listCollections(dbId, [
125
- Query.equal("name", finalAttribute.relatedCollection),
126
- ]);
127
- if (collectionsPulled.total > 0) {
128
- collectionFoundViaRelatedCollection = collectionsPulled.collections[0];
129
- relatedCollectionId = collectionFoundViaRelatedCollection.$id;
130
- nameToIdMapping.set(finalAttribute.relatedCollection, relatedCollectionId);
131
- }
132
- }
133
- if (!(relatedCollectionId && collectionFoundViaRelatedCollection)) {
134
- console.log(`Enqueueing operation for attribute: ${finalAttribute.key}`);
135
- enqueueOperation({
136
- type: "attribute",
137
- collectionId: collection.$id,
138
- collection: collection,
139
- attribute,
140
- dependencies: [finalAttribute.relatedCollection],
141
- });
142
- return;
143
- }
144
- }
145
- finalAttribute = attributeSchema.parse(finalAttribute);
146
- console.log(`Final Attribute: ${JSON.stringify(finalAttribute)}`);
147
- switch (finalAttribute.type) {
148
- case "string":
149
- if (action === "create") {
150
- await tryAwaitWithRetry(async () => await db.createStringAttribute(dbId, collection.$id, finalAttribute.key, finalAttribute.size, finalAttribute.required || false, finalAttribute.xdefault !== undefined && !finalAttribute.required
151
- ? finalAttribute.xdefault
152
- : null, finalAttribute.array || false, finalAttribute.encrypted));
153
- }
154
- else {
155
- await tryAwaitWithRetry(async () => await db.updateStringAttribute(dbId, collection.$id, finalAttribute.key, finalAttribute.required || false, finalAttribute.xdefault !== undefined && !finalAttribute.required
156
- ? finalAttribute.xdefault
157
- : null));
158
- }
159
- break;
160
- case "integer":
161
- if (action === "create") {
162
- if (finalAttribute.min &&
163
- BigInt(finalAttribute.min) === BigInt(-9223372036854776000)) {
164
- delete finalAttribute.min;
165
- }
166
- if (finalAttribute.max &&
167
- BigInt(finalAttribute.max) === BigInt(9223372036854776000)) {
168
- delete finalAttribute.max;
169
- }
170
- await tryAwaitWithRetry(async () => await db.createIntegerAttribute(dbId, collection.$id, finalAttribute.key, finalAttribute.required || false, finalAttribute.min || -2147483647, finalAttribute.max || 2147483647, finalAttribute.xdefault !== undefined && !finalAttribute.required
171
- ? finalAttribute.xdefault
172
- : null, finalAttribute.array || false));
173
- }
174
- else {
175
- if (finalAttribute.min &&
176
- BigInt(finalAttribute.min) === BigInt(-9223372036854776000)) {
177
- delete finalAttribute.min;
178
- }
179
- if (finalAttribute.max &&
180
- BigInt(finalAttribute.max) === BigInt(9223372036854776000)) {
181
- delete finalAttribute.max;
182
- }
183
- await tryAwaitWithRetry(async () => await db.updateIntegerAttribute(dbId, collection.$id, finalAttribute.key, finalAttribute.required || false, finalAttribute.min || -2147483647, finalAttribute.max || 2147483647, finalAttribute.xdefault !== undefined && !finalAttribute.required
184
- ? finalAttribute.xdefault
185
- : null));
186
- }
187
- break;
188
- case "float":
189
- if (action === "create") {
190
- await tryAwaitWithRetry(async () => await db.createFloatAttribute(dbId, collection.$id, finalAttribute.key, finalAttribute.required || false, finalAttribute.min || -2147483647, finalAttribute.max || 2147483647, finalAttribute.xdefault !== undefined && !finalAttribute.required
191
- ? finalAttribute.xdefault
192
- : null, finalAttribute.array || false));
193
- }
194
- else {
195
- await tryAwaitWithRetry(async () => await db.updateFloatAttribute(dbId, collection.$id, finalAttribute.key, finalAttribute.required || false, finalAttribute.min || -2147483647, finalAttribute.max || 2147483647, finalAttribute.xdefault !== undefined && !finalAttribute.required
196
- ? finalAttribute.xdefault
197
- : null));
198
- }
199
- break;
200
- case "boolean":
201
- if (action === "create") {
202
- await tryAwaitWithRetry(async () => await db.createBooleanAttribute(dbId, collection.$id, finalAttribute.key, finalAttribute.required || false, finalAttribute.xdefault !== undefined && !finalAttribute.required
203
- ? finalAttribute.xdefault
204
- : null, finalAttribute.array || false));
205
- }
206
- else {
207
- await tryAwaitWithRetry(async () => await db.updateBooleanAttribute(dbId, collection.$id, finalAttribute.key, finalAttribute.required || false, finalAttribute.xdefault !== undefined && !finalAttribute.required
208
- ? finalAttribute.xdefault
209
- : null));
210
- }
211
- break;
212
- case "datetime":
213
- if (action === "create") {
214
- await tryAwaitWithRetry(async () => await db.createDatetimeAttribute(dbId, collection.$id, finalAttribute.key, finalAttribute.required || false, finalAttribute.xdefault !== undefined && !finalAttribute.required
215
- ? finalAttribute.xdefault
216
- : null, finalAttribute.array || false));
217
- }
218
- else {
219
- await tryAwaitWithRetry(async () => await db.updateDatetimeAttribute(dbId, collection.$id, finalAttribute.key, finalAttribute.required || false, finalAttribute.xdefault !== undefined && !finalAttribute.required
220
- ? finalAttribute.xdefault
221
- : null));
222
- }
223
- break;
224
- case "email":
225
- if (action === "create") {
226
- await tryAwaitWithRetry(async () => await db.createEmailAttribute(dbId, collection.$id, finalAttribute.key, finalAttribute.required || false, finalAttribute.xdefault !== undefined && !finalAttribute.required
227
- ? finalAttribute.xdefault
228
- : null, finalAttribute.array || false));
229
- }
230
- else {
231
- await tryAwaitWithRetry(async () => await db.updateEmailAttribute(dbId, collection.$id, finalAttribute.key, finalAttribute.required || false, finalAttribute.xdefault !== undefined && !finalAttribute.required
232
- ? finalAttribute.xdefault
233
- : null));
234
- }
235
- break;
236
- case "ip":
237
- if (action === "create") {
238
- await tryAwaitWithRetry(async () => await db.createIpAttribute(dbId, collection.$id, finalAttribute.key, finalAttribute.required || false, finalAttribute.xdefault !== undefined && !finalAttribute.required
239
- ? finalAttribute.xdefault
240
- : null, finalAttribute.array || false));
241
- }
242
- else {
243
- await tryAwaitWithRetry(async () => await db.updateIpAttribute(dbId, collection.$id, finalAttribute.key, finalAttribute.required || false, finalAttribute.xdefault !== undefined && !finalAttribute.required
244
- ? finalAttribute.xdefault
245
- : null));
246
- }
247
- break;
248
- case "url":
249
- if (action === "create") {
250
- await tryAwaitWithRetry(async () => await db.createUrlAttribute(dbId, collection.$id, finalAttribute.key, finalAttribute.required || false, finalAttribute.xdefault !== undefined && !finalAttribute.required
251
- ? finalAttribute.xdefault
252
- : null, finalAttribute.array || false));
253
- }
254
- else {
255
- await tryAwaitWithRetry(async () => await db.updateUrlAttribute(dbId, collection.$id, finalAttribute.key, finalAttribute.required || false, finalAttribute.xdefault !== undefined && !finalAttribute.required
256
- ? finalAttribute.xdefault
257
- : null));
258
- }
259
- break;
260
- case "enum":
261
- if (action === "create") {
262
- await tryAwaitWithRetry(async () => await db.createEnumAttribute(dbId, collection.$id, finalAttribute.key, finalAttribute.elements, finalAttribute.required || false, finalAttribute.xdefault !== undefined && !finalAttribute.required
263
- ? finalAttribute.xdefault
264
- : null, finalAttribute.array || false));
265
- }
266
- else {
267
- await tryAwaitWithRetry(async () => await db.updateEnumAttribute(dbId, collection.$id, finalAttribute.key, finalAttribute.elements, finalAttribute.required || false, finalAttribute.xdefault !== undefined && !finalAttribute.required
268
- ? finalAttribute.xdefault
269
- : null));
270
- }
271
- break;
272
- case "relationship":
273
- if (action === "create") {
274
- await tryAwaitWithRetry(async () => await db.createRelationshipAttribute(dbId, collection.$id, relatedCollectionId, finalAttribute.relationType, finalAttribute.twoWay, finalAttribute.key, finalAttribute.twoWayKey, finalAttribute.onDelete));
275
- }
276
- else {
277
- await tryAwaitWithRetry(async () => await db.updateRelationshipAttribute(dbId, collection.$id, finalAttribute.key, finalAttribute.onDelete));
278
- }
279
- break;
280
- default:
281
- console.error("Invalid attribute type");
282
- break;
283
- }
284
- };
285
- export const createUpdateCollectionAttributes = async (db, dbId, collection, attributes) => {
286
- console.log(`Creating/Updating attributes for collection: ${collection.name}`);
287
- const batchSize = 3; // Size of each batch
288
- for (let i = 0; i < attributes.length; i += batchSize) {
289
- // Slice the attributes array to get a batch of at most batchSize elements
290
- const batch = attributes.slice(i, i + batchSize);
291
- const attributePromises = batch.map((attribute) => createOrUpdateAttribute(db, dbId, collection, attribute));
292
- // Await the completion of all promises in the current batch
293
- const results = await Promise.allSettled(attributePromises);
294
- results.forEach((result) => {
295
- if (result.status === "rejected") {
296
- console.error("An attribute promise was rejected:", result.reason);
297
- }
298
- });
299
- }
300
- console.log(`Finished creating/updating attributes for collection: ${collection.name}`);
301
- };