appwrite-utils-cli 1.5.2 → 1.6.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 (233) hide show
  1. package/CHANGELOG.md +199 -0
  2. package/README.md +251 -29
  3. package/dist/adapters/AdapterFactory.d.ts +10 -3
  4. package/dist/adapters/AdapterFactory.js +213 -17
  5. package/dist/adapters/TablesDBAdapter.js +60 -17
  6. package/dist/backups/operations/bucketBackup.d.ts +19 -0
  7. package/dist/backups/operations/bucketBackup.js +197 -0
  8. package/dist/backups/operations/collectionBackup.d.ts +30 -0
  9. package/dist/backups/operations/collectionBackup.js +201 -0
  10. package/dist/backups/operations/comprehensiveBackup.d.ts +25 -0
  11. package/dist/backups/operations/comprehensiveBackup.js +238 -0
  12. package/dist/backups/schemas/bucketManifest.d.ts +93 -0
  13. package/dist/backups/schemas/bucketManifest.js +33 -0
  14. package/dist/backups/schemas/comprehensiveManifest.d.ts +108 -0
  15. package/dist/backups/schemas/comprehensiveManifest.js +32 -0
  16. package/dist/backups/tracking/centralizedTracking.d.ts +34 -0
  17. package/dist/backups/tracking/centralizedTracking.js +274 -0
  18. package/dist/cli/commands/configCommands.d.ts +8 -0
  19. package/dist/cli/commands/configCommands.js +160 -0
  20. package/dist/cli/commands/databaseCommands.d.ts +13 -0
  21. package/dist/cli/commands/databaseCommands.js +478 -0
  22. package/dist/cli/commands/functionCommands.d.ts +7 -0
  23. package/dist/cli/commands/functionCommands.js +289 -0
  24. package/dist/cli/commands/schemaCommands.d.ts +7 -0
  25. package/dist/cli/commands/schemaCommands.js +134 -0
  26. package/dist/cli/commands/transferCommands.d.ts +5 -0
  27. package/dist/cli/commands/transferCommands.js +384 -0
  28. package/dist/collections/attributes.d.ts +5 -4
  29. package/dist/collections/attributes.js +539 -246
  30. package/dist/collections/indexes.js +39 -37
  31. package/dist/collections/methods.d.ts +2 -16
  32. package/dist/collections/methods.js +90 -538
  33. package/dist/collections/transferOperations.d.ts +7 -0
  34. package/dist/collections/transferOperations.js +331 -0
  35. package/dist/collections/wipeOperations.d.ts +16 -0
  36. package/dist/collections/wipeOperations.js +328 -0
  37. package/dist/config/configMigration.d.ts +87 -0
  38. package/dist/config/configMigration.js +390 -0
  39. package/dist/config/configValidation.d.ts +66 -0
  40. package/dist/config/configValidation.js +358 -0
  41. package/dist/config/yamlConfig.d.ts +455 -1
  42. package/dist/config/yamlConfig.js +145 -52
  43. package/dist/databases/methods.js +3 -2
  44. package/dist/databases/setup.d.ts +1 -2
  45. package/dist/databases/setup.js +9 -87
  46. package/dist/examples/yamlTerminologyExample.d.ts +42 -0
  47. package/dist/examples/yamlTerminologyExample.js +269 -0
  48. package/dist/functions/deployments.js +11 -10
  49. package/dist/functions/methods.d.ts +1 -1
  50. package/dist/functions/methods.js +5 -4
  51. package/dist/init.js +9 -9
  52. package/dist/interactiveCLI.d.ts +8 -17
  53. package/dist/interactiveCLI.js +181 -1172
  54. package/dist/main.js +364 -21
  55. package/dist/migrations/afterImportActions.js +22 -30
  56. package/dist/migrations/appwriteToX.js +71 -25
  57. package/dist/migrations/dataLoader.js +35 -26
  58. package/dist/migrations/importController.js +29 -30
  59. package/dist/migrations/relationships.js +13 -12
  60. package/dist/migrations/services/ImportOrchestrator.js +16 -19
  61. package/dist/migrations/transfer.js +46 -46
  62. package/dist/migrations/yaml/YamlImportConfigLoader.d.ts +3 -1
  63. package/dist/migrations/yaml/YamlImportConfigLoader.js +6 -3
  64. package/dist/migrations/yaml/YamlImportIntegration.d.ts +9 -3
  65. package/dist/migrations/yaml/YamlImportIntegration.js +22 -11
  66. package/dist/migrations/yaml/generateImportSchemas.d.ts +14 -1
  67. package/dist/migrations/yaml/generateImportSchemas.js +736 -7
  68. package/dist/schemas/authUser.d.ts +1 -1
  69. package/dist/setupController.js +3 -2
  70. package/dist/shared/backupMetadataSchema.d.ts +94 -0
  71. package/dist/shared/backupMetadataSchema.js +38 -0
  72. package/dist/shared/backupTracking.d.ts +18 -0
  73. package/dist/shared/backupTracking.js +176 -0
  74. package/dist/shared/confirmationDialogs.js +15 -15
  75. package/dist/shared/errorUtils.d.ts +54 -0
  76. package/dist/shared/errorUtils.js +95 -0
  77. package/dist/shared/functionManager.js +20 -19
  78. package/dist/shared/indexManager.js +12 -11
  79. package/dist/shared/jsonSchemaGenerator.js +10 -26
  80. package/dist/shared/logging.d.ts +51 -0
  81. package/dist/shared/logging.js +70 -0
  82. package/dist/shared/messageFormatter.d.ts +2 -0
  83. package/dist/shared/messageFormatter.js +10 -0
  84. package/dist/shared/migrationHelpers.d.ts +6 -16
  85. package/dist/shared/migrationHelpers.js +24 -21
  86. package/dist/shared/operationLogger.d.ts +8 -1
  87. package/dist/shared/operationLogger.js +11 -24
  88. package/dist/shared/operationQueue.d.ts +28 -1
  89. package/dist/shared/operationQueue.js +268 -66
  90. package/dist/shared/operationsTable.d.ts +26 -0
  91. package/dist/shared/operationsTable.js +286 -0
  92. package/dist/shared/operationsTableSchema.d.ts +48 -0
  93. package/dist/shared/operationsTableSchema.js +35 -0
  94. package/dist/shared/relationshipExtractor.d.ts +56 -0
  95. package/dist/shared/relationshipExtractor.js +138 -0
  96. package/dist/shared/schemaGenerator.d.ts +19 -1
  97. package/dist/shared/schemaGenerator.js +56 -75
  98. package/dist/storage/backupCompression.d.ts +20 -0
  99. package/dist/storage/backupCompression.js +67 -0
  100. package/dist/storage/methods.d.ts +16 -2
  101. package/dist/storage/methods.js +98 -14
  102. package/dist/users/methods.js +9 -8
  103. package/dist/utils/configDiscovery.d.ts +78 -0
  104. package/dist/utils/configDiscovery.js +430 -0
  105. package/dist/utils/directoryUtils.d.ts +22 -0
  106. package/dist/utils/directoryUtils.js +59 -0
  107. package/dist/utils/getClientFromConfig.d.ts +17 -8
  108. package/dist/utils/getClientFromConfig.js +162 -17
  109. package/dist/utils/helperFunctions.d.ts +16 -2
  110. package/dist/utils/helperFunctions.js +19 -5
  111. package/dist/utils/loadConfigs.d.ts +34 -9
  112. package/dist/utils/loadConfigs.js +236 -316
  113. package/dist/utils/pathResolvers.d.ts +53 -0
  114. package/dist/utils/pathResolvers.js +72 -0
  115. package/dist/utils/projectConfig.d.ts +119 -0
  116. package/dist/utils/projectConfig.js +171 -0
  117. package/dist/utils/retryFailedPromises.js +4 -2
  118. package/dist/utils/sessionAuth.d.ts +48 -0
  119. package/dist/utils/sessionAuth.js +164 -0
  120. package/dist/utils/sessionPreservationExample.d.ts +1666 -0
  121. package/dist/utils/sessionPreservationExample.js +101 -0
  122. package/dist/utils/setupFiles.js +301 -41
  123. package/dist/utils/typeGuards.d.ts +35 -0
  124. package/dist/utils/typeGuards.js +57 -0
  125. package/dist/utils/versionDetection.js +145 -9
  126. package/dist/utils/yamlConverter.d.ts +53 -3
  127. package/dist/utils/yamlConverter.js +232 -13
  128. package/dist/utils/yamlLoader.d.ts +70 -0
  129. package/dist/utils/yamlLoader.js +263 -0
  130. package/dist/utilsController.d.ts +36 -3
  131. package/dist/utilsController.js +186 -56
  132. package/package.json +12 -2
  133. package/src/adapters/AdapterFactory.ts +263 -35
  134. package/src/adapters/TablesDBAdapter.ts +225 -36
  135. package/src/backups/operations/bucketBackup.ts +277 -0
  136. package/src/backups/operations/collectionBackup.ts +310 -0
  137. package/src/backups/operations/comprehensiveBackup.ts +342 -0
  138. package/src/backups/schemas/bucketManifest.ts +78 -0
  139. package/src/backups/schemas/comprehensiveManifest.ts +76 -0
  140. package/src/backups/tracking/centralizedTracking.ts +352 -0
  141. package/src/cli/commands/configCommands.ts +194 -0
  142. package/src/cli/commands/databaseCommands.ts +635 -0
  143. package/src/cli/commands/functionCommands.ts +379 -0
  144. package/src/cli/commands/schemaCommands.ts +163 -0
  145. package/src/cli/commands/transferCommands.ts +457 -0
  146. package/src/collections/attributes.ts +900 -621
  147. package/src/collections/attributes.ts.backup +1555 -0
  148. package/src/collections/indexes.ts +116 -114
  149. package/src/collections/methods.ts +295 -968
  150. package/src/collections/transferOperations.ts +516 -0
  151. package/src/collections/wipeOperations.ts +501 -0
  152. package/src/config/README.md +274 -0
  153. package/src/config/configMigration.ts +575 -0
  154. package/src/config/configValidation.ts +445 -0
  155. package/src/config/yamlConfig.ts +168 -55
  156. package/src/databases/methods.ts +3 -2
  157. package/src/databases/setup.ts +11 -138
  158. package/src/examples/yamlTerminologyExample.ts +341 -0
  159. package/src/functions/deployments.ts +14 -12
  160. package/src/functions/methods.ts +11 -11
  161. package/src/functions/templates/hono-typescript/README.md +286 -0
  162. package/src/functions/templates/hono-typescript/package.json +26 -0
  163. package/src/functions/templates/hono-typescript/src/adapters/request.ts +74 -0
  164. package/src/functions/templates/hono-typescript/src/adapters/response.ts +106 -0
  165. package/src/functions/templates/hono-typescript/src/app.ts +180 -0
  166. package/src/functions/templates/hono-typescript/src/context.ts +103 -0
  167. package/src/functions/templates/hono-typescript/src/index.ts +54 -0
  168. package/src/functions/templates/hono-typescript/src/middleware/appwrite.ts +119 -0
  169. package/src/functions/templates/hono-typescript/tsconfig.json +20 -0
  170. package/src/functions/templates/typescript-node/package.json +2 -1
  171. package/src/functions/templates/typescript-node/src/context.ts +103 -0
  172. package/src/functions/templates/typescript-node/src/index.ts +18 -12
  173. package/src/functions/templates/uv/pyproject.toml +1 -0
  174. package/src/functions/templates/uv/src/context.py +125 -0
  175. package/src/functions/templates/uv/src/index.py +35 -5
  176. package/src/init.ts +9 -11
  177. package/src/interactiveCLI.ts +278 -1596
  178. package/src/main.ts +418 -24
  179. package/src/migrations/afterImportActions.ts +71 -44
  180. package/src/migrations/appwriteToX.ts +100 -34
  181. package/src/migrations/dataLoader.ts +48 -34
  182. package/src/migrations/importController.ts +44 -39
  183. package/src/migrations/relationships.ts +28 -18
  184. package/src/migrations/services/ImportOrchestrator.ts +24 -27
  185. package/src/migrations/transfer.ts +159 -121
  186. package/src/migrations/yaml/YamlImportConfigLoader.ts +11 -4
  187. package/src/migrations/yaml/YamlImportIntegration.ts +47 -20
  188. package/src/migrations/yaml/generateImportSchemas.ts +751 -12
  189. package/src/setupController.ts +3 -2
  190. package/src/shared/backupMetadataSchema.ts +93 -0
  191. package/src/shared/backupTracking.ts +211 -0
  192. package/src/shared/confirmationDialogs.ts +19 -19
  193. package/src/shared/errorUtils.ts +110 -0
  194. package/src/shared/functionManager.ts +21 -20
  195. package/src/shared/indexManager.ts +12 -11
  196. package/src/shared/jsonSchemaGenerator.ts +38 -52
  197. package/src/shared/logging.ts +75 -0
  198. package/src/shared/messageFormatter.ts +14 -1
  199. package/src/shared/migrationHelpers.ts +45 -38
  200. package/src/shared/operationLogger.ts +11 -36
  201. package/src/shared/operationQueue.ts +322 -93
  202. package/src/shared/operationsTable.ts +338 -0
  203. package/src/shared/operationsTableSchema.ts +60 -0
  204. package/src/shared/relationshipExtractor.ts +214 -0
  205. package/src/shared/schemaGenerator.ts +179 -219
  206. package/src/storage/backupCompression.ts +88 -0
  207. package/src/storage/methods.ts +131 -34
  208. package/src/users/methods.ts +11 -9
  209. package/src/utils/configDiscovery.ts +502 -0
  210. package/src/utils/directoryUtils.ts +61 -0
  211. package/src/utils/getClientFromConfig.ts +205 -22
  212. package/src/utils/helperFunctions.ts +23 -5
  213. package/src/utils/loadConfigs.ts +313 -345
  214. package/src/utils/pathResolvers.ts +81 -0
  215. package/src/utils/projectConfig.ts +299 -0
  216. package/src/utils/retryFailedPromises.ts +4 -2
  217. package/src/utils/sessionAuth.ts +230 -0
  218. package/src/utils/setupFiles.ts +322 -54
  219. package/src/utils/typeGuards.ts +65 -0
  220. package/src/utils/versionDetection.ts +218 -64
  221. package/src/utils/yamlConverter.ts +296 -13
  222. package/src/utils/yamlLoader.ts +364 -0
  223. package/src/utilsController.ts +314 -110
  224. package/tests/README.md +497 -0
  225. package/tests/adapters/AdapterFactory.test.ts +277 -0
  226. package/tests/integration/syncOperations.test.ts +463 -0
  227. package/tests/jest.config.js +25 -0
  228. package/tests/migration/configMigration.test.ts +546 -0
  229. package/tests/setup.ts +62 -0
  230. package/tests/testUtils.ts +340 -0
  231. package/tests/utils/loadConfigs.test.ts +350 -0
  232. package/tests/validation/configValidation.test.ts +412 -0
  233. package/src/utils/schemaStrings.ts +0 -517
@@ -0,0 +1,338 @@
1
+ import type { DatabaseAdapter } from "../adapters/DatabaseAdapter.js";
2
+ import { logger } from "./logging.js";
3
+ import { tryAwaitWithRetry } from "../utils/helperFunctions.js";
4
+ import { Query, ID } from "node-appwrite";
5
+ import {
6
+ OPERATIONS_TABLE_ID,
7
+ OPERATIONS_TABLE_NAME,
8
+ type OperationRecord,
9
+ type OperationStatus,
10
+ OperationRecordSchema,
11
+ } from "./operationsTableSchema.js";
12
+
13
+ /**
14
+ * Operations Table Manager
15
+ *
16
+ * Manages dynamic operations tracking tables in each database.
17
+ * Creates _appwrite_operations table on-demand for tracking imports, exports, transfers, etc.
18
+ */
19
+
20
+ /**
21
+ * Checks if operations table exists in database
22
+ */
23
+ async function tableExists(
24
+ db: DatabaseAdapter,
25
+ databaseId: string,
26
+ ): Promise<boolean> {
27
+ try {
28
+ await db.getTable({ databaseId, tableId: OPERATIONS_TABLE_ID });
29
+ return true;
30
+ } catch (error) {
31
+ return false;
32
+ }
33
+ }
34
+
35
+ /**
36
+ * Creates the operations tracking table in the specified database
37
+ * Table is created with underscore prefix to indicate it's a system table
38
+ */
39
+ export async function createOperationsTable(
40
+ db: DatabaseAdapter,
41
+ databaseId: string,
42
+ ): Promise<void> {
43
+ // Check if table already exists
44
+ const exists = await tableExists(db, databaseId);
45
+ if (exists) {
46
+ logger.debug("Operations table already exists", {
47
+ databaseId,
48
+ tableId: OPERATIONS_TABLE_ID,
49
+ });
50
+ return;
51
+ }
52
+
53
+ logger.info("Creating operations tracking table", {
54
+ databaseId,
55
+ tableId: OPERATIONS_TABLE_ID,
56
+ });
57
+
58
+ // Create table
59
+ await tryAwaitWithRetry(async () => {
60
+ await db.createTable({
61
+ databaseId,
62
+ id: OPERATIONS_TABLE_ID,
63
+ name: OPERATIONS_TABLE_NAME,
64
+ });
65
+ });
66
+
67
+ // Create attributes with retry logic
68
+ const attributes = [
69
+ {
70
+ key: "operationType",
71
+ type: "string",
72
+ size: 50,
73
+ required: true,
74
+ },
75
+ {
76
+ key: "targetCollection",
77
+ type: "string",
78
+ size: 50,
79
+ required: false,
80
+ },
81
+ {
82
+ key: "status",
83
+ type: "enum",
84
+ elements: ["pending", "in_progress", "completed", "failed", "cancelled"],
85
+ required: true,
86
+ default: "pending",
87
+ },
88
+ {
89
+ key: "progress",
90
+ type: "integer",
91
+ required: true,
92
+ default: 0,
93
+ },
94
+ {
95
+ key: "total",
96
+ type: "integer",
97
+ required: true,
98
+ default: 0,
99
+ },
100
+ {
101
+ key: "data",
102
+ type: "string",
103
+ size: 1048576, // 1MB for serialized data
104
+ required: false,
105
+ },
106
+ {
107
+ key: "error",
108
+ type: "string",
109
+ size: 10000,
110
+ required: false,
111
+ },
112
+ ];
113
+
114
+ for (const attr of attributes) {
115
+ await tryAwaitWithRetry(async () => {
116
+ await db.createAttribute({
117
+ databaseId,
118
+ tableId: OPERATIONS_TABLE_ID,
119
+ ...attr,
120
+ });
121
+ });
122
+ }
123
+
124
+ logger.info("Operations tracking table created successfully", {
125
+ databaseId,
126
+ tableId: OPERATIONS_TABLE_ID,
127
+ attributes: attributes.length,
128
+ });
129
+ }
130
+
131
+ /**
132
+ * Finds an existing operation or creates a new one
133
+ * Useful for resuming interrupted operations
134
+ */
135
+ export async function findOrCreateOperation(
136
+ db: DatabaseAdapter,
137
+ databaseId: string,
138
+ operationType: string,
139
+ params?: Partial<OperationRecord>,
140
+ ): Promise<OperationRecord> {
141
+ // Ensure operations table exists
142
+ await createOperationsTable(db, databaseId);
143
+
144
+ // Try to find existing pending operation
145
+ try {
146
+ const queries = [
147
+ Query.equal("operationType", operationType),
148
+ Query.equal("status", "pending"),
149
+ ];
150
+
151
+ if (params?.targetCollection) {
152
+ queries.push(Query.equal("targetCollection", params.targetCollection));
153
+ }
154
+
155
+ const response = await db.listRows({
156
+ databaseId,
157
+ tableId: OPERATIONS_TABLE_ID,
158
+ queries,
159
+ });
160
+
161
+ if (response.rows && response.rows.length > 0) {
162
+ logger.debug("Found existing operation", {
163
+ operationId: response.rows[0].$id,
164
+ operationType,
165
+ });
166
+ return response.rows[0] as OperationRecord;
167
+ }
168
+ } catch (error) {
169
+ logger.debug("No existing operation found, creating new one", {
170
+ operationType,
171
+ error: error instanceof Error ? error.message : String(error),
172
+ });
173
+ }
174
+
175
+ // Create new operation
176
+ const newOperation = {
177
+ operationType,
178
+ targetCollection: params?.targetCollection,
179
+ status: "pending" as OperationStatus,
180
+ progress: params?.progress ?? 0,
181
+ total: params?.total ?? 0,
182
+ data: params?.data ? JSON.stringify(params.data) : undefined,
183
+ error: params?.error,
184
+ };
185
+
186
+ const result = await db.createRow({
187
+ databaseId,
188
+ tableId: OPERATIONS_TABLE_ID,
189
+ id: ID.unique(),
190
+ data: newOperation,
191
+ });
192
+
193
+ logger.info("Created new operation", {
194
+ operationId: result.data?.$id,
195
+ operationType,
196
+ });
197
+
198
+ return result.data as OperationRecord;
199
+ }
200
+
201
+ /**
202
+ * Updates an existing operation record
203
+ */
204
+ export async function updateOperation(
205
+ db: DatabaseAdapter,
206
+ databaseId: string,
207
+ operationId: string,
208
+ updates: Partial<OperationRecord>,
209
+ ): Promise<OperationRecord> {
210
+ // Prepare update data (exclude system fields like $id, $createdAt, $updatedAt)
211
+ const updateData: Record<string, any> = {};
212
+
213
+ if (updates.operationType !== undefined)
214
+ updateData.operationType = updates.operationType;
215
+ if (updates.targetCollection !== undefined)
216
+ updateData.targetCollection = updates.targetCollection;
217
+ if (updates.status !== undefined) updateData.status = updates.status;
218
+ if (updates.progress !== undefined) updateData.progress = updates.progress;
219
+ if (updates.total !== undefined) updateData.total = updates.total;
220
+ if (updates.error !== undefined) updateData.error = updates.error;
221
+ if (updates.data !== undefined) {
222
+ updateData.data =
223
+ typeof updates.data === "string"
224
+ ? updates.data
225
+ : JSON.stringify(updates.data);
226
+ }
227
+
228
+ try {
229
+ const result = await db.updateRow({
230
+ databaseId,
231
+ tableId: OPERATIONS_TABLE_ID,
232
+ id: operationId,
233
+ data: updateData,
234
+ });
235
+
236
+ logger.debug("Updated operation", {
237
+ operationId,
238
+ updates: Object.keys(updateData),
239
+ });
240
+
241
+ return result.data as OperationRecord;
242
+ } catch (error) {
243
+ logger.error("Failed to update operation", {
244
+ operationId,
245
+ error: error instanceof Error ? error.message : String(error),
246
+ });
247
+ throw error;
248
+ }
249
+ }
250
+
251
+ /**
252
+ * Gets a single operation by ID
253
+ */
254
+ export async function getOperation(
255
+ db: DatabaseAdapter,
256
+ databaseId: string,
257
+ operationId: string,
258
+ ): Promise<OperationRecord | null> {
259
+ try {
260
+ const result = await db.getRow({
261
+ databaseId,
262
+ tableId: OPERATIONS_TABLE_ID,
263
+ id: operationId,
264
+ });
265
+
266
+ return result.data as OperationRecord;
267
+ } catch (error) {
268
+ logger.debug("Operation not found", {
269
+ operationId,
270
+ error: error instanceof Error ? error.message : String(error),
271
+ });
272
+ return null;
273
+ }
274
+ }
275
+
276
+ /**
277
+ * Cleans up old completed operations
278
+ * @param olderThan - Optional date, defaults to operations older than 7 days
279
+ * @returns Number of operations deleted
280
+ */
281
+ export async function cleanupOperations(
282
+ db: DatabaseAdapter,
283
+ databaseId: string,
284
+ olderThan?: Date,
285
+ ): Promise<number> {
286
+ const cutoffDate =
287
+ olderThan || new Date(Date.now() - 7 * 24 * 60 * 60 * 1000); // 7 days ago
288
+ const cutoffIso = cutoffDate.toISOString();
289
+
290
+ try {
291
+ // Query for completed operations older than cutoff
292
+ const response = await db.listRows({
293
+ databaseId,
294
+ tableId: OPERATIONS_TABLE_ID,
295
+ queries: [
296
+ Query.equal("status", ["completed", "failed", "cancelled"]),
297
+ Query.lessThan("$createdAt", cutoffIso),
298
+ ],
299
+ });
300
+
301
+ if (!response.rows || response.rows.length === 0) {
302
+ logger.debug("No old operations to clean up", { databaseId });
303
+ return 0;
304
+ }
305
+
306
+ // Delete in batches
307
+ let deletedCount = 0;
308
+ for (const operation of response.rows) {
309
+ try {
310
+ await db.deleteRow({
311
+ databaseId,
312
+ tableId: OPERATIONS_TABLE_ID,
313
+ id: operation.$id,
314
+ });
315
+ deletedCount++;
316
+ } catch (error) {
317
+ logger.warn("Failed to delete operation", {
318
+ operationId: operation.$id,
319
+ error: error instanceof Error ? error.message : String(error),
320
+ });
321
+ }
322
+ }
323
+
324
+ logger.info("Cleaned up old operations", {
325
+ databaseId,
326
+ deletedCount,
327
+ cutoffDate: cutoffIso,
328
+ });
329
+
330
+ return deletedCount;
331
+ } catch (error) {
332
+ logger.error("Failed to cleanup operations", {
333
+ databaseId,
334
+ error: error instanceof Error ? error.message : String(error),
335
+ });
336
+ return 0;
337
+ }
338
+ }
@@ -0,0 +1,60 @@
1
+ /**
2
+ * Schema for operation tracking table (_appwrite_operations)
3
+ *
4
+ * This table is created dynamically in each database to track long-running operations
5
+ * like imports, exports, transfers, and backups.
6
+ */
7
+
8
+ import { z } from "zod";
9
+
10
+ export interface OperationRecord {
11
+ $id: string;
12
+ $createdAt: string;
13
+ $updatedAt: string;
14
+ operationType: string; // 'import', 'export', 'transfer', 'backup', etc.
15
+ targetCollection?: string; // Optional: which collection is being operated on
16
+ status: OperationStatus;
17
+ progress: number; // Current progress count
18
+ total: number; // Total items to process
19
+ data?: any; // Optional: serialized operation data
20
+ error?: string; // Optional: error message if failed
21
+ }
22
+
23
+ export type OperationStatus =
24
+ | 'pending'
25
+ | 'in_progress'
26
+ | 'completed'
27
+ | 'failed'
28
+ | 'cancelled';
29
+
30
+ export const OPERATION_STATUSES = [
31
+ 'pending',
32
+ 'in_progress',
33
+ 'completed',
34
+ 'failed',
35
+ 'cancelled'
36
+ ] as const;
37
+
38
+ export const OperationStatusSchema = z.enum([
39
+ 'pending',
40
+ 'in_progress',
41
+ 'completed',
42
+ 'failed',
43
+ 'cancelled'
44
+ ]);
45
+
46
+ export const OperationRecordSchema = z.object({
47
+ $id: z.string(),
48
+ $createdAt: z.string(),
49
+ $updatedAt: z.string(),
50
+ operationType: z.string(),
51
+ targetCollection: z.string().optional(),
52
+ status: OperationStatusSchema,
53
+ progress: z.number(),
54
+ total: z.number(),
55
+ data: z.any().optional(),
56
+ error: z.string().optional()
57
+ });
58
+
59
+ export const OPERATIONS_TABLE_ID = "_appwrite_operations";
60
+ export const OPERATIONS_TABLE_NAME = "Operations Tracking";
@@ -0,0 +1,214 @@
1
+ import type { AppwriteConfig, Attribute, RelationshipAttribute } from "appwrite-utils";
2
+
3
+ /**
4
+ * Represents detailed information about a two-way relationship between collections
5
+ */
6
+ export interface RelationshipDetail {
7
+ parentCollection: string;
8
+ childCollection: string;
9
+ parentKey: string;
10
+ childKey: string;
11
+ isArray: boolean;
12
+ isChild: boolean;
13
+ }
14
+
15
+ /**
16
+ * Represents basic relationship information for JSON schema generation
17
+ */
18
+ export interface SimpleRelationship {
19
+ attributeKey: string;
20
+ relatedCollection: string;
21
+ relationType: string;
22
+ isArray: boolean;
23
+ }
24
+
25
+ /**
26
+ * Helper function to resolve collection name from ID or name
27
+ * @param config - Appwrite configuration containing collections
28
+ * @param idOrName - Collection ID or name to resolve
29
+ * @returns Resolved collection name, or the original input if not found
30
+ */
31
+ export function resolveCollectionName(config: AppwriteConfig, idOrName: string): string {
32
+ const col = config.collections?.find(
33
+ (c) => c.$id === (idOrName as any) || c.name === idOrName
34
+ );
35
+ return col?.name ?? idOrName;
36
+ }
37
+
38
+ /**
39
+ * Determines if a relationship type results in an array
40
+ * @param relationType - The type of relationship (oneToOne, oneToMany, manyToOne, manyToMany)
41
+ * @returns true if the relationship results in an array
42
+ */
43
+ export function isArrayRelationship(relationType: string): boolean {
44
+ return relationType === "oneToMany" || relationType === "manyToMany";
45
+ }
46
+
47
+ /**
48
+ * Extracts two-way relationship details from collections for Zod schema generation
49
+ * This handles complex bidirectional relationships with parent-child tracking
50
+ *
51
+ * @param config - Appwrite configuration containing collections
52
+ * @returns Map of collection names to their relationship details
53
+ */
54
+ export function extractTwoWayRelationships(
55
+ config: AppwriteConfig
56
+ ): Map<string, RelationshipDetail[]> {
57
+ const relationshipMap = new Map<string, RelationshipDetail[]>();
58
+
59
+ if (!config.collections) {
60
+ return relationshipMap;
61
+ }
62
+
63
+ config.collections.forEach((collection) => {
64
+ if (!collection.attributes) {
65
+ return;
66
+ }
67
+
68
+ collection.attributes.forEach((attr) => {
69
+ if (attr.type === "relationship" && attr.twoWay && attr.twoWayKey) {
70
+ const relationshipAttr = attr as RelationshipAttribute;
71
+
72
+ let isArrayParent = false;
73
+ let isArrayChild = false;
74
+
75
+ switch (relationshipAttr.relationType) {
76
+ case "oneToMany":
77
+ isArrayParent = true;
78
+ isArrayChild = false;
79
+ break;
80
+ case "manyToMany":
81
+ isArrayParent = true;
82
+ isArrayChild = true;
83
+ break;
84
+ case "oneToOne":
85
+ isArrayParent = false;
86
+ isArrayChild = false;
87
+ break;
88
+ case "manyToOne":
89
+ isArrayParent = false;
90
+ isArrayChild = true;
91
+ break;
92
+ default:
93
+ break;
94
+ }
95
+
96
+ const relatedCollectionName = resolveCollectionName(
97
+ config,
98
+ relationshipAttr.relatedCollection
99
+ );
100
+
101
+ addTwoWayRelationship(
102
+ relationshipMap,
103
+ collection.name,
104
+ relatedCollectionName,
105
+ attr.key,
106
+ relationshipAttr.twoWayKey!,
107
+ isArrayParent,
108
+ isArrayChild
109
+ );
110
+
111
+ console.log(
112
+ `Extracted relationship: ${attr.key}\n\t${collection.name} -> ${relationshipAttr.relatedCollection}, databaseId: ${collection.databaseId}`
113
+ );
114
+ }
115
+ });
116
+ });
117
+
118
+ return relationshipMap;
119
+ }
120
+
121
+ /**
122
+ * Helper to add two-way relationship details to both parent and child collections
123
+ * @param relationshipMap - The map to add relationships to
124
+ * @param parentCollection - Parent collection name
125
+ * @param childCollection - Child collection name
126
+ * @param parentKey - Attribute key in parent collection
127
+ * @param childKey - Attribute key in child collection
128
+ * @param isArrayParent - Whether parent side is an array
129
+ * @param isArrayChild - Whether child side is an array
130
+ */
131
+ function addTwoWayRelationship(
132
+ relationshipMap: Map<string, RelationshipDetail[]>,
133
+ parentCollection: string,
134
+ childCollection: string,
135
+ parentKey: string,
136
+ childKey: string,
137
+ isArrayParent: boolean,
138
+ isArrayChild: boolean
139
+ ): void {
140
+ const relationshipsChild = relationshipMap.get(childCollection) || [];
141
+ const relationshipsParent = relationshipMap.get(parentCollection) || [];
142
+
143
+ relationshipsParent.push({
144
+ parentCollection,
145
+ childCollection,
146
+ parentKey,
147
+ childKey,
148
+ isArray: isArrayParent,
149
+ isChild: false,
150
+ });
151
+
152
+ relationshipsChild.push({
153
+ parentCollection,
154
+ childCollection,
155
+ parentKey,
156
+ childKey,
157
+ isArray: isArrayChild,
158
+ isChild: true,
159
+ });
160
+
161
+ relationshipMap.set(childCollection, relationshipsChild);
162
+ relationshipMap.set(parentCollection, relationshipsParent);
163
+ }
164
+
165
+ /**
166
+ * Extracts simple relationship information from collections for JSON schema generation
167
+ * This handles one-way and basic relationship tracking
168
+ *
169
+ * @param config - Appwrite configuration containing collections
170
+ * @returns Map of collection names to their simple relationships
171
+ */
172
+ export function extractSimpleRelationships(
173
+ config: AppwriteConfig
174
+ ): Map<string, SimpleRelationship[]> {
175
+ const relationshipMap = new Map<string, SimpleRelationship[]>();
176
+
177
+ if (!config.collections) {
178
+ return relationshipMap;
179
+ }
180
+
181
+ config.collections.forEach((collection) => {
182
+ if (!collection.attributes) {
183
+ return;
184
+ }
185
+
186
+ collection.attributes.forEach((attr) => {
187
+ if (attr.type === "relationship" && attr.relatedCollection) {
188
+ const relationships = relationshipMap.get(collection.name) || [];
189
+
190
+ relationships.push({
191
+ attributeKey: attr.key,
192
+ relatedCollection: resolveCollectionName(config, attr.relatedCollection),
193
+ relationType: attr.relationType || "oneToOne",
194
+ isArray: isArrayRelationship(attr.relationType || "oneToOne")
195
+ });
196
+
197
+ relationshipMap.set(collection.name, relationships);
198
+ }
199
+ });
200
+ });
201
+
202
+ return relationshipMap;
203
+ }
204
+
205
+ /**
206
+ * Extracts all relationship attributes from a collection's attributes
207
+ * @param attributes - Array of collection attributes
208
+ * @returns Array of relationship attributes only
209
+ */
210
+ export function filterRelationshipAttributes(attributes: Attribute[]): RelationshipAttribute[] {
211
+ return attributes.filter(
212
+ (attr): attr is RelationshipAttribute => attr.type === "relationship"
213
+ );
214
+ }