appwrite-utils-cli 1.11.0 → 1.12.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 (250) hide show
  1. package/{src/adapters/index.ts → dist/adapters/index.d.ts} +0 -1
  2. package/dist/adapters/index.js +10 -0
  3. package/dist/backups/operations/bucketBackup.d.ts +19 -0
  4. package/dist/backups/operations/bucketBackup.js +197 -0
  5. package/dist/backups/operations/collectionBackup.d.ts +30 -0
  6. package/dist/backups/operations/collectionBackup.js +201 -0
  7. package/dist/backups/operations/comprehensiveBackup.d.ts +25 -0
  8. package/dist/backups/operations/comprehensiveBackup.js +238 -0
  9. package/dist/backups/schemas/bucketManifest.d.ts +93 -0
  10. package/dist/backups/schemas/bucketManifest.js +33 -0
  11. package/dist/backups/schemas/comprehensiveManifest.d.ts +108 -0
  12. package/dist/backups/schemas/comprehensiveManifest.js +32 -0
  13. package/dist/backups/tracking/centralizedTracking.d.ts +34 -0
  14. package/dist/backups/tracking/centralizedTracking.js +274 -0
  15. package/dist/cli/commands/configCommands.d.ts +8 -0
  16. package/dist/cli/commands/configCommands.js +210 -0
  17. package/dist/cli/commands/databaseCommands.d.ts +14 -0
  18. package/dist/cli/commands/databaseCommands.js +696 -0
  19. package/dist/cli/commands/functionCommands.d.ts +7 -0
  20. package/dist/cli/commands/functionCommands.js +330 -0
  21. package/dist/cli/commands/importFileCommands.d.ts +7 -0
  22. package/dist/cli/commands/importFileCommands.js +674 -0
  23. package/dist/cli/commands/schemaCommands.d.ts +7 -0
  24. package/dist/cli/commands/schemaCommands.js +169 -0
  25. package/dist/cli/commands/storageCommands.d.ts +5 -0
  26. package/dist/cli/commands/storageCommands.js +142 -0
  27. package/dist/cli/commands/transferCommands.d.ts +5 -0
  28. package/dist/cli/commands/transferCommands.js +382 -0
  29. package/dist/collections/columns.d.ts +13 -0
  30. package/dist/collections/columns.js +1339 -0
  31. package/dist/collections/indexes.d.ts +12 -0
  32. package/dist/collections/indexes.js +215 -0
  33. package/dist/collections/methods.d.ts +19 -0
  34. package/dist/collections/methods.js +605 -0
  35. package/dist/collections/tableOperations.d.ts +87 -0
  36. package/dist/collections/tableOperations.js +466 -0
  37. package/dist/collections/transferOperations.d.ts +8 -0
  38. package/dist/collections/transferOperations.js +411 -0
  39. package/dist/collections/wipeOperations.d.ts +17 -0
  40. package/dist/collections/wipeOperations.js +306 -0
  41. package/dist/databases/methods.d.ts +6 -0
  42. package/dist/databases/methods.js +35 -0
  43. package/dist/databases/setup.d.ts +5 -0
  44. package/dist/databases/setup.js +45 -0
  45. package/dist/examples/yamlTerminologyExample.d.ts +42 -0
  46. package/dist/examples/yamlTerminologyExample.js +272 -0
  47. package/dist/functions/deployments.d.ts +4 -0
  48. package/dist/functions/deployments.js +146 -0
  49. package/dist/functions/fnConfigDiscovery.d.ts +3 -0
  50. package/dist/functions/fnConfigDiscovery.js +108 -0
  51. package/dist/functions/methods.d.ts +16 -0
  52. package/dist/functions/methods.js +174 -0
  53. package/dist/init.d.ts +2 -0
  54. package/dist/init.js +57 -0
  55. package/dist/interactiveCLI.d.ts +36 -0
  56. package/dist/interactiveCLI.js +952 -0
  57. package/dist/main.d.ts +2 -0
  58. package/dist/main.js +1125 -0
  59. package/dist/migrations/afterImportActions.d.ts +17 -0
  60. package/dist/migrations/afterImportActions.js +305 -0
  61. package/dist/migrations/appwriteToX.d.ts +211 -0
  62. package/dist/migrations/appwriteToX.js +493 -0
  63. package/dist/migrations/comprehensiveTransfer.d.ts +147 -0
  64. package/dist/migrations/comprehensiveTransfer.js +1315 -0
  65. package/dist/migrations/dataLoader.d.ts +755 -0
  66. package/dist/migrations/dataLoader.js +1272 -0
  67. package/dist/migrations/importController.d.ts +25 -0
  68. package/dist/migrations/importController.js +283 -0
  69. package/dist/migrations/importDataActions.d.ts +50 -0
  70. package/dist/migrations/importDataActions.js +230 -0
  71. package/dist/migrations/relationships.d.ts +29 -0
  72. package/dist/migrations/relationships.js +203 -0
  73. package/dist/migrations/services/DataTransformationService.d.ts +55 -0
  74. package/dist/migrations/services/DataTransformationService.js +158 -0
  75. package/dist/migrations/services/FileHandlerService.d.ts +75 -0
  76. package/dist/migrations/services/FileHandlerService.js +236 -0
  77. package/dist/migrations/services/ImportOrchestrator.d.ts +99 -0
  78. package/dist/migrations/services/ImportOrchestrator.js +493 -0
  79. package/dist/migrations/services/RateLimitManager.d.ts +138 -0
  80. package/dist/migrations/services/RateLimitManager.js +279 -0
  81. package/dist/migrations/services/RelationshipResolver.d.ts +120 -0
  82. package/dist/migrations/services/RelationshipResolver.js +332 -0
  83. package/dist/migrations/services/UserMappingService.d.ts +109 -0
  84. package/dist/migrations/services/UserMappingService.js +277 -0
  85. package/dist/migrations/services/ValidationService.d.ts +74 -0
  86. package/dist/migrations/services/ValidationService.js +260 -0
  87. package/dist/migrations/transfer.d.ts +30 -0
  88. package/dist/migrations/transfer.js +661 -0
  89. package/dist/migrations/yaml/YamlImportConfigLoader.d.ts +131 -0
  90. package/dist/migrations/yaml/YamlImportConfigLoader.js +383 -0
  91. package/dist/migrations/yaml/YamlImportIntegration.d.ts +93 -0
  92. package/dist/migrations/yaml/YamlImportIntegration.js +341 -0
  93. package/dist/migrations/yaml/generateImportSchemas.d.ts +30 -0
  94. package/dist/migrations/yaml/generateImportSchemas.js +1327 -0
  95. package/dist/schemas/authUser.d.ts +24 -0
  96. package/dist/schemas/authUser.js +17 -0
  97. package/dist/setup.d.ts +2 -0
  98. package/{src/setup.ts → dist/setup.js} +0 -3
  99. package/dist/setupCommands.d.ts +58 -0
  100. package/dist/setupCommands.js +489 -0
  101. package/dist/setupController.d.ts +9 -0
  102. package/dist/setupController.js +34 -0
  103. package/dist/shared/backupMetadataSchema.d.ts +94 -0
  104. package/dist/shared/backupMetadataSchema.js +38 -0
  105. package/dist/shared/backupTracking.d.ts +18 -0
  106. package/dist/shared/backupTracking.js +176 -0
  107. package/dist/shared/confirmationDialogs.d.ts +75 -0
  108. package/dist/shared/confirmationDialogs.js +236 -0
  109. package/dist/shared/migrationHelpers.d.ts +61 -0
  110. package/dist/shared/migrationHelpers.js +145 -0
  111. package/{src/shared/operationLogger.ts → dist/shared/operationLogger.d.ts} +1 -11
  112. package/dist/shared/operationLogger.js +12 -0
  113. package/dist/shared/operationQueue.d.ts +40 -0
  114. package/dist/shared/operationQueue.js +310 -0
  115. package/dist/shared/operationsTable.d.ts +26 -0
  116. package/dist/shared/operationsTable.js +287 -0
  117. package/dist/shared/operationsTableSchema.d.ts +48 -0
  118. package/dist/shared/operationsTableSchema.js +35 -0
  119. package/dist/shared/progressManager.d.ts +62 -0
  120. package/dist/shared/progressManager.js +215 -0
  121. package/dist/shared/relationshipExtractor.d.ts +56 -0
  122. package/dist/shared/relationshipExtractor.js +138 -0
  123. package/dist/shared/selectionDialogs.d.ts +220 -0
  124. package/dist/shared/selectionDialogs.js +588 -0
  125. package/dist/storage/backupCompression.d.ts +20 -0
  126. package/dist/storage/backupCompression.js +67 -0
  127. package/dist/storage/methods.d.ts +44 -0
  128. package/dist/storage/methods.js +475 -0
  129. package/dist/storage/schemas.d.ts +842 -0
  130. package/dist/storage/schemas.js +175 -0
  131. package/dist/tables/indexManager.d.ts +65 -0
  132. package/dist/tables/indexManager.js +294 -0
  133. package/{src/types.ts → dist/types.d.ts} +1 -6
  134. package/dist/types.js +3 -0
  135. package/dist/users/methods.d.ts +16 -0
  136. package/dist/users/methods.js +276 -0
  137. package/dist/utils/configMigration.d.ts +1 -0
  138. package/dist/utils/configMigration.js +261 -0
  139. package/dist/utils/index.js +2 -0
  140. package/dist/utils/loadConfigs.d.ts +50 -0
  141. package/dist/utils/loadConfigs.js +357 -0
  142. package/dist/utils/setupFiles.d.ts +4 -0
  143. package/dist/utils/setupFiles.js +1190 -0
  144. package/dist/utilsController.d.ts +114 -0
  145. package/dist/utilsController.js +898 -0
  146. package/package.json +6 -3
  147. package/CHANGELOG.md +0 -35
  148. package/CONFIG_TODO.md +0 -1189
  149. package/SELECTION_DIALOGS.md +0 -146
  150. package/SERVICE_IMPLEMENTATION_REPORT.md +0 -462
  151. package/scripts/copy-templates.ts +0 -23
  152. package/src/backups/operations/bucketBackup.ts +0 -277
  153. package/src/backups/operations/collectionBackup.ts +0 -310
  154. package/src/backups/operations/comprehensiveBackup.ts +0 -342
  155. package/src/backups/schemas/bucketManifest.ts +0 -78
  156. package/src/backups/schemas/comprehensiveManifest.ts +0 -76
  157. package/src/backups/tracking/centralizedTracking.ts +0 -352
  158. package/src/cli/commands/configCommands.ts +0 -265
  159. package/src/cli/commands/databaseCommands.ts +0 -931
  160. package/src/cli/commands/functionCommands.ts +0 -419
  161. package/src/cli/commands/importFileCommands.ts +0 -815
  162. package/src/cli/commands/schemaCommands.ts +0 -200
  163. package/src/cli/commands/storageCommands.ts +0 -151
  164. package/src/cli/commands/transferCommands.ts +0 -454
  165. package/src/collections/attributes.ts.backup +0 -1555
  166. package/src/collections/columns.ts +0 -2025
  167. package/src/collections/indexes.ts +0 -350
  168. package/src/collections/methods.ts +0 -714
  169. package/src/collections/tableOperations.ts +0 -542
  170. package/src/collections/transferOperations.ts +0 -589
  171. package/src/collections/wipeOperations.ts +0 -449
  172. package/src/databases/methods.ts +0 -49
  173. package/src/databases/setup.ts +0 -77
  174. package/src/examples/yamlTerminologyExample.ts +0 -346
  175. package/src/functions/deployments.ts +0 -221
  176. package/src/functions/fnConfigDiscovery.ts +0 -103
  177. package/src/functions/methods.ts +0 -284
  178. package/src/init.ts +0 -62
  179. package/src/interactiveCLI.ts +0 -1201
  180. package/src/main.ts +0 -1517
  181. package/src/migrations/afterImportActions.ts +0 -579
  182. package/src/migrations/appwriteToX.ts +0 -668
  183. package/src/migrations/comprehensiveTransfer.ts +0 -2285
  184. package/src/migrations/dataLoader.ts +0 -1729
  185. package/src/migrations/importController.ts +0 -440
  186. package/src/migrations/importDataActions.ts +0 -315
  187. package/src/migrations/relationships.ts +0 -333
  188. package/src/migrations/services/DataTransformationService.ts +0 -196
  189. package/src/migrations/services/FileHandlerService.ts +0 -311
  190. package/src/migrations/services/ImportOrchestrator.ts +0 -675
  191. package/src/migrations/services/RateLimitManager.ts +0 -363
  192. package/src/migrations/services/RelationshipResolver.ts +0 -461
  193. package/src/migrations/services/UserMappingService.ts +0 -345
  194. package/src/migrations/services/ValidationService.ts +0 -349
  195. package/src/migrations/transfer.ts +0 -1113
  196. package/src/migrations/yaml/YamlImportConfigLoader.ts +0 -439
  197. package/src/migrations/yaml/YamlImportIntegration.ts +0 -446
  198. package/src/migrations/yaml/generateImportSchemas.ts +0 -1354
  199. package/src/schemas/authUser.ts +0 -23
  200. package/src/setupCommands.ts +0 -602
  201. package/src/setupController.ts +0 -43
  202. package/src/shared/backupMetadataSchema.ts +0 -93
  203. package/src/shared/backupTracking.ts +0 -211
  204. package/src/shared/confirmationDialogs.ts +0 -327
  205. package/src/shared/migrationHelpers.ts +0 -232
  206. package/src/shared/operationQueue.ts +0 -376
  207. package/src/shared/operationsTable.ts +0 -338
  208. package/src/shared/operationsTableSchema.ts +0 -60
  209. package/src/shared/progressManager.ts +0 -278
  210. package/src/shared/relationshipExtractor.ts +0 -214
  211. package/src/shared/selectionDialogs.ts +0 -802
  212. package/src/storage/backupCompression.ts +0 -88
  213. package/src/storage/methods.ts +0 -711
  214. package/src/storage/schemas.ts +0 -205
  215. package/src/tables/indexManager.ts +0 -409
  216. package/src/types/node-appwrite-tablesdb.d.ts +0 -44
  217. package/src/users/methods.ts +0 -358
  218. package/src/utils/configMigration.ts +0 -348
  219. package/src/utils/loadConfigs.ts +0 -457
  220. package/src/utils/setupFiles.ts +0 -1236
  221. package/src/utilsController.ts +0 -1263
  222. package/tests/README.md +0 -497
  223. package/tests/adapters/AdapterFactory.test.ts +0 -277
  224. package/tests/integration/syncOperations.test.ts +0 -463
  225. package/tests/jest.config.js +0 -25
  226. package/tests/migration/configMigration.test.ts +0 -546
  227. package/tests/setup.ts +0 -62
  228. package/tests/testUtils.ts +0 -340
  229. package/tests/utils/loadConfigs.test.ts +0 -350
  230. package/tests/validation/configValidation.test.ts +0 -412
  231. package/tsconfig.json +0 -44
  232. /package/{src → dist}/functions/templates/count-docs-in-collection/README.md +0 -0
  233. /package/{src → dist}/functions/templates/count-docs-in-collection/src/main.ts +0 -0
  234. /package/{src → dist}/functions/templates/count-docs-in-collection/src/request.ts +0 -0
  235. /package/{src → dist}/functions/templates/hono-typescript/README.md +0 -0
  236. /package/{src → dist}/functions/templates/hono-typescript/src/adapters/request.ts +0 -0
  237. /package/{src → dist}/functions/templates/hono-typescript/src/adapters/response.ts +0 -0
  238. /package/{src → dist}/functions/templates/hono-typescript/src/app.ts +0 -0
  239. /package/{src → dist}/functions/templates/hono-typescript/src/context.ts +0 -0
  240. /package/{src → dist}/functions/templates/hono-typescript/src/main.ts +0 -0
  241. /package/{src → dist}/functions/templates/hono-typescript/src/middleware/appwrite.ts +0 -0
  242. /package/{src → dist}/functions/templates/typescript-node/README.md +0 -0
  243. /package/{src → dist}/functions/templates/typescript-node/src/context.ts +0 -0
  244. /package/{src → dist}/functions/templates/typescript-node/src/main.ts +0 -0
  245. /package/{src → dist}/functions/templates/uv/README.md +0 -0
  246. /package/{src → dist}/functions/templates/uv/pyproject.toml +0 -0
  247. /package/{src → dist}/functions/templates/uv/src/__init__.py +0 -0
  248. /package/{src → dist}/functions/templates/uv/src/context.py +0 -0
  249. /package/{src → dist}/functions/templates/uv/src/main.py +0 -0
  250. /package/{src/utils/index.ts → dist/utils/index.d.ts} +0 -0
@@ -1,232 +0,0 @@
1
- import { ID, Query, type Databases } from "node-appwrite";
2
- import { BatchSchema, OperationSchema, type Operation } from "../storage/schemas.js";
3
- import { AttributeMappingsSchema } from "appwrite-utils";
4
- import { z } from "zod";
5
- import { logger } from 'appwrite-utils-helpers';
6
- import { tryAwaitWithRetry } from "appwrite-utils-helpers";
7
- import {
8
- findOrCreateOperation as findOrCreateOp,
9
- updateOperation as updateOp,
10
- getOperation as getOp
11
- } from "./operationsTable.js";
12
- import type { DatabaseAdapter } from "appwrite-utils-helpers";
13
- import { MessageFormatter } from 'appwrite-utils-helpers';
14
-
15
- /**
16
- * Object that contains the context for an action that needs to be executed after import
17
- * Used in the afterImportActionsDefinitions
18
- * @type {ContextObject}
19
- * @typedef {Object} ContextObject
20
- * @property {string} collectionId - The ID of the collection
21
- * @property {any} finalItem - The final item that was imported
22
- * @property {string} action - The name of the action
23
- * @property {string[]} params - The parameters for the action
24
- * @property {Object} context - The context object for the action (all the data of this specific item)
25
- */
26
- export const ContextObject = z.object({
27
- dbId: z.string(),
28
- collectionId: z.string(),
29
- finalItem: z.any(),
30
- attributeMappings: AttributeMappingsSchema,
31
- context: z.any(),
32
- });
33
-
34
- export type ContextObject = z.infer<typeof ContextObject>;
35
-
36
- export const createOrFindAfterImportOperation = async (
37
- database: Databases,
38
- collectionId: string,
39
- context: ContextObject
40
- ) => {
41
- let operation = await findOrCreateOperationLegacy(
42
- database,
43
- collectionId,
44
- "afterImportAction"
45
- );
46
- if (!operation.batches) {
47
- operation.batches = [];
48
- }
49
-
50
- // Directly create a new batch for the context without checking for an existing batch
51
- const contextData = JSON.stringify(context);
52
- // Create a new batch with the contextData
53
- const newBatchId = await addBatch(database, contextData);
54
- // Update the operation with the new batch's $id
55
- operation.batches = [...operation.batches, newBatchId];
56
- await database.updateDocument(
57
- "migrations",
58
- "currentOperations",
59
- operation.$id,
60
- { batches: operation.batches }
61
- );
62
- };
63
-
64
- export const addBatch = async (database: Databases, data: string) => {
65
- const batch = await database.createDocument(
66
- "migrations",
67
- "batches",
68
- ID.unique(),
69
- {
70
- data,
71
- processed: false,
72
- }
73
- );
74
- return batch.$id;
75
- };
76
-
77
- export const getAfterImportOperations = async (
78
- database: Databases,
79
- collectionId: string
80
- ) => {
81
- let lastDocumentId: string | undefined;
82
- const allOperations = [];
83
- let total = 0;
84
-
85
- do {
86
- const query = [
87
- Query.equal("collectionId", collectionId),
88
- Query.equal("operationType", "afterImportAction"),
89
- Query.limit(100),
90
- ];
91
-
92
- if (lastDocumentId) {
93
- query.push(Query.cursorAfter(lastDocumentId));
94
- }
95
-
96
- const operations = await database.listDocuments(
97
- "migrations",
98
- "currentOperations",
99
- query
100
- );
101
- total = operations.total; // Update total with the latest fetch
102
- allOperations.push(...operations.documents);
103
-
104
- if (operations.documents.length > 0 && operations.documents.length >= 100) {
105
- lastDocumentId =
106
- operations.documents[operations.documents.length - 1].$id;
107
- }
108
- } while (allOperations.length < total);
109
-
110
- const allOps = allOperations.map((op) => OperationSchema.parse(op));
111
- return allOps;
112
- };
113
-
114
- // Legacy function for backward compatibility with old migrations database
115
- const findOrCreateOperationLegacy = async (
116
- database: Databases,
117
- collectionId: string,
118
- operationType: string,
119
- additionalQueries?: string[]
120
- ) => {
121
- const operations = await tryAwaitWithRetry(
122
- async () =>
123
- await database.listDocuments("migrations", "currentOperations", [
124
- Query.equal("collectionId", collectionId),
125
- Query.equal("operationType", operationType),
126
- Query.equal("status", "pending"),
127
- ...(additionalQueries || []),
128
- ])
129
- );
130
-
131
- if (operations.documents.length > 0) {
132
- return OperationSchema.parse(operations.documents[0]);
133
- } else {
134
- const op = await tryAwaitWithRetry(
135
- async () =>
136
- await database.createDocument(
137
- "migrations",
138
- "currentOperations",
139
- ID.unique(),
140
- {
141
- operationType,
142
- collectionId,
143
- status: "pending",
144
- batches: [],
145
- progress: 0,
146
- total: 0,
147
- error: "",
148
- }
149
- )
150
- );
151
-
152
- return OperationSchema.parse(op);
153
- }
154
- };
155
-
156
- export const findOrCreateOperation = async (
157
- db: DatabaseAdapter,
158
- databaseId: string,
159
- operationType: string,
160
- collectionId?: string,
161
- data?: any
162
- ): Promise<any> => {
163
- // Use new operations table system
164
- return await findOrCreateOp(db, databaseId, operationType, {
165
- targetTable: collectionId,
166
- data: data
167
- });
168
- };
169
-
170
- export const updateOperation = async (
171
- db: DatabaseAdapter,
172
- databaseId: string,
173
- operationId: string,
174
- updates: any
175
- ): Promise<any> => {
176
- // Use new operations table system
177
- return await updateOp(db, databaseId, operationId, updates);
178
- };
179
-
180
- export const getOperation = async (
181
- db: DatabaseAdapter,
182
- databaseId: string,
183
- operationId: string
184
- ): Promise<any> => {
185
- // Use new operations table system
186
- return await getOp(db, databaseId, operationId);
187
- };
188
-
189
- // Actual max 1073741824
190
- export const maxDataLength = 1073741820;
191
- export const maxBatchItems = 25;
192
-
193
- export const splitIntoBatches = (data: any[]): any[][] => {
194
- let batches = [];
195
- let currentBatch: any[] = [];
196
- let currentBatchLength = 0;
197
- let currentBatchItemCount = 0;
198
-
199
- data.forEach((item, index) => {
200
- const itemLength = JSON.stringify(item).length;
201
- if (itemLength > maxDataLength) {
202
- MessageFormatter.warning(
203
- `Large item found at index ${index} with length ${itemLength}`,
204
- { prefix: "Batch Splitter" }
205
- );
206
- logger.debug("Large item data:", item);
207
- }
208
- // Check if adding the current item would exceed the max length or max items per batch
209
- if (
210
- currentBatchLength + itemLength >= maxDataLength ||
211
- currentBatchItemCount >= maxBatchItems
212
- ) {
213
- // If so, start a new batch
214
- batches.push(currentBatch);
215
- currentBatch = [item];
216
- currentBatchLength = itemLength;
217
- currentBatchItemCount = 1; // Reset item count for the new batch
218
- } else {
219
- // Otherwise, add the item to the current batch
220
- currentBatch.push(item);
221
- currentBatchLength += itemLength;
222
- currentBatchItemCount++;
223
- }
224
- });
225
-
226
- // Don't forget to add the last batch if it's not empty
227
- if (currentBatch.length > 0) {
228
- batches.push(currentBatch);
229
- }
230
-
231
- return batches;
232
- };
@@ -1,376 +0,0 @@
1
- import { Query, type Databases, type Models } from "node-appwrite";
2
- import type { Attribute } from "appwrite-utils";
3
- import { createOrUpdateAttributeWithStatusCheck } from "../collections/columns.js";
4
- import { fetchAndCacheCollectionByName } from "../collections/methods.js";
5
- import { tryAwaitWithRetry } from "appwrite-utils-helpers";
6
- import type { DatabaseAdapter } from "appwrite-utils-helpers";
7
- import { logger, MessageFormatter } from "appwrite-utils-helpers";
8
-
9
- export interface QueuedOperation {
10
- type: "attribute";
11
- collectionId?: string;
12
- attribute?: Attribute;
13
- collection?: Models.Collection;
14
- dependencies?: string[];
15
- }
16
-
17
- // Global state management
18
- export const queuedOperations: QueuedOperation[] = [];
19
- export const nameToIdMapping: Map<string, string> = new Map();
20
- // Keys are scoped per database to avoid cross-database collisions
21
- // Collections key format: `${databaseId}::${collectionId}`
22
- // Attributes key format: `${databaseId}::${collectionId}::${attributeKey}`
23
- export const processedCollections: Set<string> = new Set();
24
- export const processedAttributes: Set<string> = new Set();
25
-
26
- // Helpers to build scoped keys
27
- const collectionKey = (databaseId: string, collectionId: string) => `${databaseId}::${collectionId}`;
28
- const attributeKeyScoped = (databaseId: string, collectionId: string, key: string) => `${databaseId}::${collectionId}::${key}`;
29
-
30
- export const enqueueOperation = (operation: QueuedOperation) => {
31
- // Avoid duplicate queue entries for same attribute
32
- const attributeKey = operation.attribute?.key;
33
- const collectionId = operation.collectionId;
34
-
35
- logger.info('Enqueueing operation', {
36
- type: operation.type,
37
- attributeKey,
38
- collectionId,
39
- dependencies: operation.dependencies,
40
- queueSizeBefore: queuedOperations.length,
41
- operation: 'enqueueOperation'
42
- });
43
-
44
- if (attributeKey && collectionId) {
45
- const duplicateIndex = queuedOperations.findIndex(
46
- (op) => op.collectionId === collectionId && op.attribute?.key === attributeKey
47
- );
48
-
49
- if (duplicateIndex !== -1) {
50
- MessageFormatter.info(`Replacing existing queue entry for attribute: ${attributeKey}`);
51
- logger.info('Replacing duplicate queue entry', {
52
- attributeKey,
53
- collectionId,
54
- duplicateIndex,
55
- operation: 'enqueueOperation'
56
- });
57
- queuedOperations[duplicateIndex] = operation;
58
- return;
59
- }
60
- }
61
-
62
- queuedOperations.push(operation);
63
- logger.debug('Operation enqueued successfully', {
64
- attributeKey,
65
- collectionId,
66
- queueSizeAfter: queuedOperations.length,
67
- operation: 'enqueueOperation'
68
- });
69
- };
70
-
71
- /**
72
- * Clear all caches and processing state - use between operations
73
- */
74
- export const clearProcessingState = () => {
75
- const sizeBefore = {
76
- collections: processedCollections.size,
77
- attributes: processedAttributes.size,
78
- nameMapping: nameToIdMapping.size
79
- };
80
-
81
- processedCollections.clear();
82
- processedAttributes.clear();
83
- nameToIdMapping.clear();
84
-
85
- logger.debug("Cleared processing state caches", { operation: "clearProcessingState", sizeBefore });
86
- logger.info('Processing state cleared', {
87
- sizeBefore,
88
- operation: 'clearProcessingState'
89
- });
90
- };
91
-
92
- /**
93
- * Check if a collection has already been fully processed
94
- */
95
- export const isCollectionProcessed = (collectionId: string, databaseId: string): boolean => {
96
- return processedCollections.has(collectionKey(databaseId, collectionId));
97
- };
98
-
99
- /**
100
- * Mark a collection as fully processed
101
- */
102
- export const markCollectionProcessed = (collectionId: string, collectionName: string | undefined, databaseId: string) => {
103
- processedCollections.add(collectionKey(databaseId, collectionId));
104
-
105
- const logData = {
106
- databaseId,
107
- collectionId,
108
- collectionName,
109
- totalProcessedCollections: processedCollections.size,
110
- operation: 'markCollectionProcessed'
111
- };
112
-
113
- if (collectionName) {
114
- MessageFormatter.success(`Marked collection '${collectionName}' (${collectionId}) as processed`, { prefix: 'Tables' });
115
- }
116
-
117
- logger.info('Collection marked as processed', logData);
118
- };
119
-
120
- /**
121
- * Check if a specific attribute has been processed
122
- */
123
- export const isAttributeProcessed = (databaseId: string, collectionId: string, attributeKey: string): boolean => {
124
- return processedAttributes.has(attributeKeyScoped(databaseId, collectionId, attributeKey));
125
- };
126
-
127
- /**
128
- * Mark a specific attribute as processed
129
- */
130
- export const markAttributeProcessed = (databaseId: string, collectionId: string, attributeKey: string) => {
131
- const identifier = attributeKeyScoped(databaseId, collectionId, attributeKey);
132
- processedAttributes.add(identifier);
133
-
134
- logger.debug('Attribute marked as processed', {
135
- databaseId,
136
- collectionId,
137
- attributeKey,
138
- identifier,
139
- totalProcessedAttributes: processedAttributes.size,
140
- operation: 'markAttributeProcessed'
141
- });
142
- };
143
-
144
- /**
145
- * Process only specific attributes in the queue, not entire collections
146
- * This prevents triggering full collection re-processing cycles
147
- */
148
- export const processQueue = async (db: Databases | DatabaseAdapter, dbId: string) => {
149
- const startTime = Date.now();
150
-
151
- if (queuedOperations.length === 0) {
152
- MessageFormatter.info("No queued operations to process");
153
- logger.info('Queue processing skipped - no operations', {
154
- dbId,
155
- operation: 'processQueue'
156
- });
157
- return;
158
- }
159
-
160
- MessageFormatter.section(`Starting surgical queue processing of ${queuedOperations.length} operations for ${dbId}`);
161
-
162
- logger.info('Starting queue processing', {
163
- dbId,
164
- queueSize: queuedOperations.length,
165
- operations: queuedOperations.map(op => ({
166
- type: op.type,
167
- attributeKey: op.attribute?.key,
168
- collectionId: op.collectionId,
169
- dependencies: op.dependencies
170
- })),
171
- operation: 'processQueue'
172
- });
173
-
174
- let progress = true;
175
- let attempts = 0;
176
- const maxAttempts = 3; // Prevent infinite loops
177
-
178
- while (progress && attempts < maxAttempts) {
179
- progress = false;
180
- attempts++;
181
-
182
- MessageFormatter.info(`Queue processing attempt ${attempts}/${maxAttempts}`);
183
-
184
- logger.info('Queue processing attempt started', {
185
- attempt: attempts,
186
- maxAttempts,
187
- remainingOperations: queuedOperations.length,
188
- dbId,
189
- operation: 'processQueue'
190
- });
191
-
192
- for (let i = queuedOperations.length - 1; i >= 0; i--) {
193
- const operation = queuedOperations[i];
194
-
195
- if (!operation.attribute || !operation.collectionId) {
196
- MessageFormatter.warning("Invalid operation, removing from queue");
197
- queuedOperations.splice(i, 1);
198
- continue;
199
- }
200
-
201
- const attributeKey = operation.attribute.key;
202
- const collectionId = operation.collectionId;
203
-
204
- // Skip if this specific attribute was already processed (per database)
205
- if (isAttributeProcessed(dbId, collectionId, attributeKey)) {
206
- MessageFormatter.debug(`Attribute '${attributeKey}' already processed, removing from queue`);
207
- logger.debug('Removing already processed attribute from queue', {
208
- attributeKey,
209
- collectionId,
210
- queueIndex: i,
211
- operation: 'processQueue'
212
- });
213
- queuedOperations.splice(i, 1);
214
- continue;
215
- }
216
-
217
- let targetCollection: Models.Collection | undefined;
218
-
219
- // Resolve the target collection (where the attribute will be created)
220
- try {
221
- targetCollection = await tryAwaitWithRetry(
222
- async () => {
223
- if ('getMetadata' in db && typeof db.getMetadata === 'function') {
224
- // DatabaseAdapter
225
- return (await (db as DatabaseAdapter).getTable({ databaseId: dbId, tableId: collectionId })).data;
226
- } else {
227
- // Legacy Databases
228
- return await (db as Databases).getCollection(dbId, collectionId);
229
- }
230
- }
231
- );
232
- } catch (e) {
233
- const errorMessage = e instanceof Error ? e.message : String(e);
234
- MessageFormatter.error(`Target collection ${collectionId} not found, removing from queue`);
235
- logger.error('Target collection not found during queue processing', {
236
- collectionId,
237
- attributeKey,
238
- error: errorMessage,
239
- operation: 'processQueue'
240
- });
241
- queuedOperations.splice(i, 1);
242
- continue;
243
- }
244
-
245
- // For relationship attributes, ensure the related collection exists
246
- let canProcess = true;
247
- if (operation.attribute.type === "relationship") {
248
- const relatedCollection = operation.attribute.relatedCollection;
249
- if (relatedCollection) {
250
- // Try to resolve related collection by ID first, then by name
251
- let relatedFound = false;
252
-
253
- try {
254
- await tryAwaitWithRetry(
255
- async () => {
256
- if ('getMetadata' in db && typeof db.getMetadata === 'function') {
257
- // DatabaseAdapter
258
- return (await (db as DatabaseAdapter).getTable({ databaseId: dbId, tableId: relatedCollection })).data;
259
- } else {
260
- // Legacy Databases
261
- return await (db as Databases).getCollection(dbId, relatedCollection);
262
- }
263
- }
264
- );
265
- relatedFound = true;
266
- nameToIdMapping.set(relatedCollection, relatedCollection); // Cache the ID mapping
267
- } catch (_) {
268
- // Try by name lookup
269
- const cachedId = nameToIdMapping.get(relatedCollection);
270
- if (cachedId) {
271
- try {
272
- await tryAwaitWithRetry(
273
- async () => {
274
- if ('getMetadata' in db && typeof db.getMetadata === 'function') {
275
- // DatabaseAdapter
276
- return (await (db as DatabaseAdapter).getTable({ databaseId: dbId, tableId: cachedId })).data;
277
- } else {
278
- // Legacy Databases
279
- return await (db as Databases).getCollection(dbId, cachedId);
280
- }
281
- }
282
- );
283
- relatedFound = true;
284
- } catch (_) {
285
- nameToIdMapping.delete(relatedCollection); // Remove stale cache
286
- }
287
- }
288
-
289
- if (!relatedFound) {
290
- // Final attempt: search by name
291
- try {
292
- const collections = 'getMetadata' in db && typeof db.getMetadata === 'function'
293
- ? await (db as DatabaseAdapter).listTables({ databaseId: dbId, queries: [Query.equal("name", relatedCollection)] })
294
- : await (db as Databases).listCollections(dbId, [Query.equal("name", relatedCollection)]);
295
-
296
- if (collections.total && collections.total > 0) {
297
- const firstCollection = 'getMetadata' in db && typeof db.getMetadata === 'function'
298
- ? (collections as any).tables?.[0]
299
- : (collections as any).collections?.[0];
300
- nameToIdMapping.set(relatedCollection, firstCollection.$id);
301
- relatedFound = true;
302
- }
303
- } catch (_) {
304
- // Related collection truly doesn't exist yet
305
- }
306
- }
307
- }
308
-
309
- if (!relatedFound) {
310
- MessageFormatter.warning(
311
- `Related collection '${relatedCollection}' not ready for attribute '${attributeKey}', keeping in queue`
312
- );
313
- canProcess = false;
314
- }
315
- }
316
- }
317
-
318
- if (canProcess && targetCollection) {
319
- MessageFormatter.progress(
320
- `Processing queued ${operation.attribute.type} attribute: '${attributeKey}' for collection: '${targetCollection.name}'`
321
- );
322
-
323
- const success = await createOrUpdateAttributeWithStatusCheck(
324
- db,
325
- dbId,
326
- targetCollection,
327
- operation.attribute
328
- );
329
-
330
- if (success) {
331
- MessageFormatter.success(`Successfully processed queued attribute: '${attributeKey}'`);
332
- logger.info('Queued attribute processed successfully', {
333
- attributeKey,
334
- collectionId,
335
- targetCollectionName: targetCollection.name,
336
- operation: 'processQueue'
337
- });
338
- markAttributeProcessed(dbId, collectionId, attributeKey);
339
- queuedOperations.splice(i, 1);
340
- progress = true;
341
- } else {
342
- MessageFormatter.error(`Failed to process queued attribute: '${attributeKey}', removing from queue`);
343
- logger.error('Failed to process queued attribute', {
344
- attributeKey,
345
- collectionId,
346
- targetCollectionName: targetCollection.name,
347
- operation: 'processQueue'
348
- });
349
- queuedOperations.splice(i, 1);
350
- }
351
- }
352
- }
353
-
354
- if (queuedOperations.length === 0) {
355
- break;
356
- }
357
-
358
- MessageFormatter.info(`Remaining operations after attempt ${attempts}: ${queuedOperations.length}`);
359
- }
360
-
361
- if (queuedOperations.length > 0) {
362
- MessageFormatter.warning(
363
- `${queuedOperations.length} operations remain unresolved after ${maxAttempts} attempts:`
364
- );
365
- queuedOperations.forEach((op, index) => {
366
- MessageFormatter.warning(
367
- ` ${index + 1}. ${op.attribute?.type} attribute '${op.attribute?.key}' for collection ${op.collectionId}`
368
- );
369
- });
370
- MessageFormatter.warning("These may have unmet dependencies or require manual intervention");
371
- } else {
372
- MessageFormatter.success("All queued operations processed successfully");
373
- }
374
-
375
- MessageFormatter.section(`Surgical queue processing complete for ${dbId}`);
376
- };