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,714 +0,0 @@
1
- import {
2
- Databases,
3
- ID,
4
- Permission,
5
- Query,
6
- type Models,
7
- } from "node-appwrite";
8
- import type { AppwriteConfig, CollectionCreate, Indexes, Attribute } from "appwrite-utils";
9
- import type { DatabaseAdapter } from "appwrite-utils-helpers";
10
- import { getAdapterFromConfig } from "appwrite-utils-helpers";
11
- import {
12
- nameToIdMapping,
13
- processQueue,
14
- queuedOperations,
15
- clearProcessingState,
16
- isCollectionProcessed,
17
- markCollectionProcessed,
18
- enqueueOperation
19
- } from "../shared/operationQueue.js";
20
- import { logger, SchemaGenerator } from "appwrite-utils-helpers";
21
- // Legacy attribute/index helpers removed in favor of unified adapter path
22
- import {
23
- isNull,
24
- isUndefined,
25
- isNil,
26
- isPlainObject,
27
- isString,
28
- } from "es-toolkit";
29
- import { delay, tryAwaitWithRetry } from "appwrite-utils-helpers";
30
- import { MessageFormatter, mapToCreateAttributeParams, mapToUpdateAttributeParams } from "appwrite-utils-helpers";
31
- import { isLegacyDatabases } from "appwrite-utils-helpers";
32
- import { diffTableColumns, isIndexEqualToIndex, diffColumnsDetailed, executeColumnOperations } from "./tableOperations.js";
33
- import { createOrUpdateIndexesViaAdapter, deleteObsoleteIndexesViaAdapter } from "../tables/indexManager.js";
34
-
35
- // Re-export wipe operations
36
- export {
37
- wipeDatabase,
38
- wipeCollection,
39
- wipeAllTables,
40
- wipeTableRows,
41
- } from "./wipeOperations.js";
42
-
43
- // Re-export transfer operations
44
- export {
45
- transferDocumentsBetweenDbsLocalToLocal,
46
- transferDocumentsBetweenDbsLocalToRemote,
47
- } from "./transferOperations.js";
48
-
49
- export const documentExists = async (
50
- db: Databases | DatabaseAdapter,
51
- dbId: string,
52
- targetCollectionId: string,
53
- toCreateObject: any
54
- ): Promise<Models.Document | null> => {
55
- const collection = await (isLegacyDatabases(db) ?
56
- db.getCollection(dbId, targetCollectionId) :
57
- db.getTable({ databaseId: dbId, tableId: targetCollectionId }));
58
- const attributes = (collection as any).attributes as any[];
59
- let arrayTypeAttributes = attributes
60
- .filter((attribute: any) => attribute.array === true)
61
- .map((attribute: any) => attribute.key);
62
-
63
- const isJsonString = (str: string) => {
64
- try {
65
- const json = JSON.parse(str);
66
- return typeof json === "object" && json !== null;
67
- } catch (e) {
68
- return false;
69
- }
70
- };
71
-
72
- // Convert object to entries and filter
73
- const validEntries = Object.entries(toCreateObject).filter(
74
- ([key, value]) =>
75
- !arrayTypeAttributes.includes(key) &&
76
- !key.startsWith("$") &&
77
- !isNull(value) &&
78
- !isUndefined(value) &&
79
- !isNil(value) &&
80
- !isPlainObject(value) &&
81
- !Array.isArray(value) &&
82
- !(isString(value) && isJsonString(value)) &&
83
- (isString(value) ? value.length < 4096 && value.length > 0 : true)
84
- );
85
-
86
- // Map and filter valid entries
87
- const validMappedEntries = validEntries
88
- .map(([key, value]) => [
89
- key,
90
- isString(value) || typeof value === "number" || typeof value === "boolean"
91
- ? value
92
- : null,
93
- ])
94
- .filter(([key, value]) => !isNull(value) && isString(key))
95
- .slice(0, 25);
96
-
97
- // Convert to Query parameters
98
- const validQueryParams = validMappedEntries.map(([key, value]) =>
99
- Query.equal(key as string, value as any)
100
- );
101
-
102
- // Execute the query with the validated and prepared parameters
103
- const result = await (isLegacyDatabases(db) ?
104
- db.listDocuments(dbId, targetCollectionId, validQueryParams) :
105
- db.listRows({ databaseId: dbId, tableId: targetCollectionId, queries: validQueryParams }));
106
-
107
- const items = isLegacyDatabases(db) ? result.documents : ((result as any).rows || result.documents);
108
- return items?.[0] || null;
109
- };
110
-
111
- export const checkForCollection = async (
112
- db: Databases | DatabaseAdapter,
113
- dbId: string,
114
- collection: Partial<CollectionCreate>
115
- ): Promise<Models.Collection | null> => {
116
- try {
117
- const isLegacy = isLegacyDatabases(db);
118
- const entityType = isLegacy ? "Collection" : "Table";
119
- MessageFormatter.progress(`Checking for ${entityType.toLowerCase()} with name: ${collection.name}`, { prefix: entityType + "s" });
120
- const response = await tryAwaitWithRetry(
121
- async () => isLegacy ?
122
- await db.listCollections(dbId, [Query.equal("name", collection.name!)]) :
123
- await db.listTables({ databaseId: dbId, queries: [Query.equal("name", collection.name!)] })
124
- );
125
- const items = isLegacy ? response.collections : ((response as any).tables || response.collections);
126
- if (items && items.length > 0) {
127
- MessageFormatter.info(`${entityType} found: ${items[0].$id}`, { prefix: entityType + "s" });
128
- // Return remote collection for update operations (don't merge local config over it)
129
- return items[0] as Models.Collection;
130
- } else {
131
- MessageFormatter.info(`No ${entityType.toLowerCase()} found with name: ${collection.name}`, { prefix: entityType + "s" });
132
- return null;
133
- }
134
- } catch (error) {
135
- const errorMessage = error instanceof Error ? error.message : String(error);
136
- MessageFormatter.error(`Error checking for collection: ${collection.name}`, error instanceof Error ? error : new Error(String(error)), { prefix: "Collections" });
137
- logger.error('Collection check failed', {
138
- collectionName: collection.name,
139
- dbId,
140
- error: errorMessage,
141
- operation: 'checkForCollection'
142
- });
143
- return null;
144
- }
145
- };
146
-
147
- // Helper function to fetch and cache collection by name
148
- export const fetchAndCacheCollectionByName = async (
149
- db: Databases | DatabaseAdapter,
150
- dbId: string,
151
- collectionName: string
152
- ): Promise<Models.Collection | undefined> => {
153
- const isLegacy = isLegacyDatabases(db);
154
- const entityType = isLegacy ? "Collection" : "Table";
155
- if (nameToIdMapping.has(collectionName)) {
156
- const collectionId = nameToIdMapping.get(collectionName);
157
- MessageFormatter.debug(`${entityType} found in cache: ${collectionId}`, undefined, { prefix: entityType + "s" });
158
- return await tryAwaitWithRetry(
159
- async () => isLegacy ?
160
- await db.getCollection(dbId, collectionId!) :
161
- await db.getTable({ databaseId: dbId, tableId: collectionId! })
162
- ) as Models.Collection;
163
- } else {
164
- MessageFormatter.progress(`Fetching ${entityType.toLowerCase()} by name: ${collectionName}`, { prefix: entityType + "s" });
165
- const collectionsPulled = await tryAwaitWithRetry(
166
- async () => isLegacy ?
167
- await db.listCollections(dbId, [Query.equal("name", collectionName)]) :
168
- await db.listTables({ databaseId: dbId, queries: [Query.equal("name", collectionName)] })
169
- );
170
- const items = isLegacy ? collectionsPulled.collections : ((collectionsPulled as any).tables || collectionsPulled.collections);
171
- if ((collectionsPulled.total || items?.length) > 0) {
172
- const collection = items[0];
173
- MessageFormatter.info(`${entityType} found: ${collection.$id}`, { prefix: entityType + "s" });
174
- nameToIdMapping.set(collectionName, collection.$id);
175
- return collection;
176
- } else {
177
- MessageFormatter.warning(`${entityType} not found by name: ${collectionName}`, { prefix: entityType + "s" });
178
- return undefined;
179
- }
180
- }
181
- };
182
-
183
- export const generateSchemas = async (
184
- config: AppwriteConfig,
185
- appwriteFolderPath: string
186
- ): Promise<void> => {
187
- const schemaGenerator = new SchemaGenerator(config, appwriteFolderPath);
188
- await schemaGenerator.generateSchemas();
189
- };
190
-
191
- export const createOrUpdateCollections = async (
192
- database: Databases,
193
- databaseId: string,
194
- config: AppwriteConfig,
195
- deletedCollections?: { collectionId: string; collectionName: string }[],
196
- selectedCollections: Models.Collection[] = []
197
- ): Promise<void> => {
198
- // Clear processing state at the start of a new operation
199
- clearProcessingState();
200
-
201
- // Always use adapter path (LegacyAdapter translates when pre-1.8)
202
- const { adapter } = await getAdapterFromConfig(config);
203
- await createOrUpdateCollectionsViaAdapter(
204
- adapter,
205
- databaseId,
206
- config,
207
- deletedCollections,
208
- selectedCollections
209
- );
210
- };
211
-
212
- // New: Adapter-based implementation for TablesDB with state management
213
- export const createOrUpdateCollectionsViaAdapter = async (
214
- adapter: DatabaseAdapter,
215
- databaseId: string,
216
- config: AppwriteConfig,
217
- deletedCollections?: { collectionId: string; collectionName: string }[],
218
- selectedCollections: Models.Collection[] = []
219
- ): Promise<void> => {
220
- const collectionsToProcess =
221
- selectedCollections.length > 0 ? selectedCollections : (config.collections || []);
222
- if (!collectionsToProcess || collectionsToProcess.length === 0) return;
223
-
224
- const usedIds = new Set<string>();
225
- MessageFormatter.info(`Processing ${collectionsToProcess.length} tables via adapter with intelligent state management`, { prefix: "Tables" });
226
-
227
- // Helpers for attribute operations through adapter
228
- const createAttr = async (tableId: string, attr: Attribute) => {
229
- const params = mapToCreateAttributeParams(attr as any, { databaseId, tableId });
230
- await adapter.createAttribute(params);
231
- await delay(150);
232
- };
233
- const updateAttr = async (tableId: string, attr: Attribute) => {
234
- const params = mapToUpdateAttributeParams(attr as any, { databaseId, tableId }) as any;
235
- await adapter.updateAttribute(params);
236
- await delay(150);
237
- };
238
-
239
- // Local queue for unresolved relationships
240
- const relQueue: { tableId: string; attr: Attribute }[] = [];
241
-
242
- for (const collection of collectionsToProcess) {
243
- const { attributes, indexes, ...collectionData } = collection as any;
244
-
245
- // Check if this table has already been processed in this session (per database)
246
- if (collectionData.$id && isCollectionProcessed(collectionData.$id, databaseId)) {
247
- MessageFormatter.info(`Table '${collectionData.name}' already processed, skipping`, { prefix: "Tables" });
248
- continue;
249
- }
250
-
251
- // Prepare permissions as strings (reuse Permission helper)
252
- const permissions: string[] = [];
253
- if (collection.$permissions && collection.$permissions.length > 0) {
254
- for (const p of collection.$permissions as any[]) {
255
- if (typeof p === 'string') permissions.push(p);
256
- else {
257
- switch (p.permission) {
258
- case 'read': permissions.push(Permission.read(p.target)); break;
259
- case 'create': permissions.push(Permission.create(p.target)); break;
260
- case 'update': permissions.push(Permission.update(p.target)); break;
261
- case 'delete': permissions.push(Permission.delete(p.target)); break;
262
- case 'write': permissions.push(Permission.write(p.target)); break;
263
- default: break;
264
- }
265
- }
266
- }
267
- }
268
-
269
- // Find existing table — prefer lookup by ID (if provided), then by name
270
- let table: any | undefined;
271
- let tableId: string;
272
-
273
- // 1) Try by explicit $id first (handles rename scenarios)
274
- if (collectionData.$id) {
275
- try {
276
- const byId = await adapter.getTable({ databaseId, tableId: collectionData.$id });
277
- table = (byId as any).data || (byId as any).tables?.[0];
278
- if (table?.$id) {
279
- MessageFormatter.info(`Found existing table by ID: ${table.$id}`, { prefix: 'Tables' });
280
- }
281
- } catch {
282
- // Not found by ID; fall back to name lookup
283
- }
284
- }
285
-
286
- // 2) If not found by ID, try by name
287
- if (!table) {
288
- const list = await adapter.listTables({ databaseId, queries: [Query.equal('name', collectionData.name)] });
289
- const items: any[] = (list as any).tables || [];
290
- table = items[0];
291
- if (table?.$id) {
292
- // If local has $id that differs from remote, prefer remote (IDs are immutable)
293
- if (collectionData.$id && collectionData.$id !== table.$id) {
294
- MessageFormatter.warning(`Config $id '${collectionData.$id}' differs from existing table ID '${table.$id}'. Using existing table.`, { prefix: 'Tables' });
295
- }
296
- }
297
- }
298
-
299
- if (!table) {
300
- // Determine ID (prefer provided $id or re-use deleted one)
301
- let foundColl = deletedCollections?.find(
302
- (coll) => coll.collectionName.toLowerCase().trim().replace(" ", "") === collectionData.name.toLowerCase().trim().replace(" ", "")
303
- );
304
- if (collectionData.$id) tableId = collectionData.$id;
305
- else if (foundColl && !usedIds.has(foundColl.collectionId)) tableId = foundColl.collectionId;
306
- else tableId = ID.unique();
307
- usedIds.add(tableId);
308
-
309
- const res = await adapter.createTable({
310
- databaseId,
311
- id: tableId,
312
- name: collectionData.name,
313
- permissions,
314
- documentSecurity: !!collectionData.documentSecurity,
315
- enabled: collectionData.enabled !== false
316
- });
317
- table = (res as any).data || res;
318
- nameToIdMapping.set(collectionData.name, tableId);
319
- } else {
320
- tableId = table.$id;
321
- await adapter.updateTable({
322
- databaseId,
323
- id: tableId,
324
- name: collectionData.name,
325
- permissions,
326
- documentSecurity: !!collectionData.documentSecurity,
327
- enabled: collectionData.enabled !== false
328
- });
329
- // Cache the existing table ID
330
- nameToIdMapping.set(collectionData.name, tableId);
331
- }
332
-
333
- // Add small delay after table create/update
334
- await delay(250);
335
-
336
- // Create/Update attributes: non-relationship first using enhanced planning
337
- const nonRel = (attributes || []).filter((a: Attribute) => a.type !== 'relationship');
338
- if (nonRel.length > 0) {
339
- // Fetch existing columns once
340
- const tableInfo = await adapter.getTable({ databaseId, tableId });
341
- const existingCols: any[] = (tableInfo as any).data?.columns || (tableInfo as any).data?.attributes || [];
342
-
343
- // Plan with icons
344
- const plan = diffColumnsDetailed(nonRel as any, existingCols);
345
- const plus = plan.toCreate.map((a: any) => a.key);
346
- const plusminus = plan.toUpdate.map((u: any) => (u.attribute as any).key);
347
- const minus = plan.toRecreate.map((r: any) => (r.newAttribute as any).key);
348
- const skip = plan.unchanged;
349
-
350
- // Compute deletions (remote extras not present locally)
351
- const desiredKeysForDelete = new Set((attributes || []).map((a: any) => a.key));
352
- const extraRemoteKeys = (existingCols || [])
353
- .map((c: any) => c?.key)
354
- .filter((k: any): k is string => !!k && !desiredKeysForDelete.has(k));
355
-
356
- const parts: string[] = [];
357
- if (plus.length) parts.push(`➕ ${plus.length} (${plus.join(', ')})`);
358
- if (plusminus.length) parts.push(`🔧 ${plusminus.length} (${plusminus.join(', ')})`);
359
- if (minus.length) parts.push(`♻️ ${minus.length} (${minus.join(', ')})`);
360
- if (skip.length) parts.push(`⏭️ ${skip.length}`);
361
- parts.push(`🗑️ ${extraRemoteKeys.length}${extraRemoteKeys.length ? ` (${extraRemoteKeys.join(', ')})` : ''}`);
362
- MessageFormatter.info(`Plan → ${parts.join(' | ') || 'no changes'}`, { prefix: 'Attributes' });
363
-
364
- // Execute
365
- const colResults = await executeColumnOperations(adapter, databaseId, tableId, plan);
366
-
367
- if (colResults.success.length > 0) {
368
- MessageFormatter.success(`Processed ${colResults.success.length} ops`, { prefix: 'Attributes' });
369
- }
370
- if (colResults.errors.length > 0) {
371
- MessageFormatter.error(`${colResults.errors.length} attribute operations failed:`, undefined, { prefix: 'Attributes' });
372
- for (const err of colResults.errors) {
373
- MessageFormatter.error(` ${err.column}: ${err.error}`, undefined, { prefix: 'Attributes' });
374
- }
375
- }
376
- MessageFormatter.info(
377
- `Summary → ➕ ${plan.toCreate.length} | 🔧 ${plan.toUpdate.length} | ♻️ ${plan.toRecreate.length} | ⏭️ ${plan.unchanged.length}`,
378
- { prefix: 'Attributes' }
379
- );
380
- }
381
-
382
- // Relationship attributes — resolve relatedCollection to ID, then diff and create/update with recreate support
383
- const relsAll = (attributes || []).filter((a: Attribute) => a.type === 'relationship') as any[];
384
- if (relsAll.length > 0) {
385
- const relsResolved: any[] = [];
386
- const relsDeferred: any[] = [];
387
-
388
- // Resolve related collections (names -> IDs) using cache or lookup.
389
- // If not resolvable yet (target table created later in the same push), queue for later.
390
- for (const attr of relsAll) {
391
- const relNameOrId = attr.relatedCollection as string | undefined;
392
- if (!relNameOrId) continue;
393
- let relId = nameToIdMapping.get(relNameOrId) || relNameOrId;
394
- let resolved = false;
395
- if (nameToIdMapping.has(relNameOrId)) {
396
- resolved = true;
397
- } else {
398
- // Try resolve by name
399
- try {
400
- const relList = await adapter.listTables({ databaseId, queries: [Query.equal('name', relNameOrId)] });
401
- const relItems: any[] = (relList as any).tables || [];
402
- if (relItems[0]?.$id) {
403
- relId = relItems[0].$id;
404
- nameToIdMapping.set(relNameOrId, relId);
405
- resolved = true;
406
- }
407
- } catch {}
408
-
409
- // If the relNameOrId looks like an ID but isn't resolved yet, attempt a direct get
410
- if (!resolved && relNameOrId && relNameOrId.length >= 10) {
411
- try {
412
- const probe = await adapter.getTable({ databaseId, tableId: relNameOrId });
413
- if ((probe as any).data?.$id) {
414
- nameToIdMapping.set(relNameOrId, relNameOrId);
415
- relId = relNameOrId;
416
- resolved = true;
417
- }
418
- } catch {}
419
- }
420
- }
421
-
422
- if (resolved && relId && typeof relId === 'string') {
423
- attr.relatedCollection = relId;
424
- relsResolved.push(attr);
425
- } else {
426
- // Defer until related table exists; queue a surgical operation
427
- enqueueOperation({
428
- type: 'attribute',
429
- collectionId: tableId,
430
- attribute: attr,
431
- dependencies: [relNameOrId]
432
- });
433
- relsDeferred.push(attr);
434
- }
435
- }
436
-
437
- // Compute a detailed plan for immediately resolvable relationships
438
- const tableInfo2 = await adapter.getTable({ databaseId, tableId });
439
- const existingCols2: any[] = (tableInfo2 as any).data?.columns || (tableInfo2 as any).data?.attributes || [];
440
- const relPlan = diffColumnsDetailed(relsResolved as any, existingCols2);
441
-
442
- // Relationship plan with icons (includes recreates)
443
- {
444
- const parts: string[] = [];
445
- if (relPlan.toCreate.length) parts.push(`➕ ${relPlan.toCreate.length} (${relPlan.toCreate.map((a:any)=>a.key).join(', ')})`);
446
- if (relPlan.toUpdate.length) parts.push(`🔧 ${relPlan.toUpdate.length} (${relPlan.toUpdate.map((u:any)=>u.attribute?.key ?? u.key).join(', ')})`);
447
- if (relPlan.toRecreate.length) parts.push(`♻️ ${relPlan.toRecreate.length} (${relPlan.toRecreate.map((r:any)=>r.newAttribute?.key ?? r?.key).join(', ')})`);
448
- if (relPlan.unchanged.length) parts.push(`⏭️ ${relPlan.unchanged.length}`);
449
- MessageFormatter.info(`Plan → ${parts.join(' | ') || 'no changes'}`, { prefix: 'Relationships' });
450
- }
451
-
452
- // Execute plan using the same operation executor to properly handle deletes/recreates
453
- const relResults = await executeColumnOperations(adapter, databaseId, tableId, relPlan);
454
- if (relResults.success.length > 0) {
455
- const totalRelationships = relPlan.toCreate.length + relPlan.toUpdate.length + relPlan.toRecreate.length + relPlan.unchanged.length;
456
- const activeRelationships = relPlan.toCreate.length + relPlan.toUpdate.length + relPlan.toRecreate.length;
457
-
458
- if (relResults.success.length !== activeRelationships) {
459
- // Show both counts when they differ (usually due to recreations)
460
- MessageFormatter.success(`Processed ${relResults.success.length} operations for ${activeRelationships} relationship${activeRelationships === 1 ? '' : 's'}`, { prefix: 'Relationships' });
461
- } else {
462
- MessageFormatter.success(`Processed ${relResults.success.length} relationship${relResults.success.length === 1 ? '' : 's'}`, { prefix: 'Relationships' });
463
- }
464
- }
465
- if (relResults.errors.length > 0) {
466
- MessageFormatter.error(`${relResults.errors.length} relationship operations failed:`, undefined, { prefix: 'Relationships' });
467
- for (const err of relResults.errors) {
468
- MessageFormatter.error(` ${err.column}: ${err.error}`, undefined, { prefix: 'Relationships' });
469
- }
470
- }
471
-
472
- if (relsDeferred.length > 0) {
473
- MessageFormatter.info(`Deferred ${relsDeferred.length} relationship(s) until related tables become available`, { prefix: 'Relationships' });
474
- }
475
- }
476
-
477
- // Wait for all attributes to become available before creating indexes
478
- const allAttrKeys = [
479
- ...nonRel.map((a: any) => a.key),
480
- ...relsAll.filter((a: any) => a.relatedCollection).map((a: any) => a.key)
481
- ];
482
-
483
- if (allAttrKeys.length > 0) {
484
- for (const attrKey of allAttrKeys) {
485
- const maxWait = 60000; // 60 seconds
486
- const startTime = Date.now();
487
- let lastStatus = '';
488
-
489
- while (Date.now() - startTime < maxWait) {
490
- try {
491
- const tableData = await adapter.getTable({ databaseId, tableId });
492
- const attrs = (tableData as any).data?.columns || (tableData as any).data?.attributes || [];
493
- const attr = attrs.find((a: any) => a.key === attrKey);
494
-
495
- if (attr) {
496
- if (attr.status === 'available') {
497
- break; // Attribute is ready
498
- }
499
- if (attr.status === 'failed' || attr.status === 'stuck') {
500
- throw new Error(`Attribute ${attrKey} failed to create: ${attr.error || 'unknown error'}`);
501
- }
502
- // Still processing, continue waiting
503
- lastStatus = attr.status;
504
- }
505
-
506
- await delay(2000); // Check every 2 seconds
507
- } catch (e) {
508
- // If we can't check status, assume it's processing and continue
509
- await delay(2000);
510
- }
511
- }
512
-
513
- // Timeout check
514
- if (Date.now() - startTime >= maxWait) {
515
- MessageFormatter.warning(
516
- `Attribute ${attrKey} did not become available within ${maxWait / 1000}s (last status: ${lastStatus}). Proceeding anyway.`,
517
- { prefix: 'Attributes' }
518
- );
519
- }
520
- }
521
- }
522
-
523
- // Index management: create/update indexes using clean adapter-based system
524
- const localTableConfig = config.collections?.find(
525
- c => c.name === collectionData.name || c.$id === collectionData.$id
526
- );
527
- const idxs = (localTableConfig?.indexes ?? indexes ?? []) as any[];
528
-
529
- // Create/update indexes with proper planning and execution
530
- await createOrUpdateIndexesViaAdapter(adapter, databaseId, tableId, idxs, indexes);
531
-
532
- // Handle obsolete index deletions
533
- const desiredIndexKeys: Set<string> = new Set((indexes || []).map((i: any) => i.key as string));
534
- await deleteObsoleteIndexesViaAdapter(adapter, databaseId, tableId, desiredIndexKeys);
535
-
536
- // Deletions: remove columns/attributes that are present remotely but not in desired config
537
- try {
538
- const desiredKeys = new Set((attributes || []).map((a: any) => a.key));
539
- // Also track case-insensitive keys to avoid double-deletion of renames (handled as recreates)
540
- const desiredKeysLower = new Set((attributes || []).map((a: any) => a.key?.toLowerCase()));
541
- const tableInfo3 = await adapter.getTable({ databaseId, tableId });
542
- const existingCols3: any[] = (tableInfo3 as any).data?.columns || (tableInfo3 as any).data?.attributes || [];
543
- const toDelete = existingCols3
544
- .filter((col: any) => {
545
- if (!col?.key) return false;
546
- // Exact match - keep it
547
- if (desiredKeys.has(col.key)) return false;
548
- // Case-insensitive match (rename scenario) - already handled as recreate, don't delete again
549
- if (desiredKeysLower.has(col.key?.toLowerCase())) return false;
550
- // Don't delete child-side relationship attributes - they're auto-managed by Appwrite
551
- // for two-way relationships and deleting them would break the parent relationship
552
- if (col.type === 'relationship' && col.side === 'child') return false;
553
- return true;
554
- })
555
- .map((col: any) => col.key as string);
556
-
557
- if (toDelete.length > 0) {
558
- MessageFormatter.info(`Plan → 🗑️ ${toDelete.length} (${toDelete.join(', ')})`, { prefix: 'Attributes' });
559
- const deleted: string[] = [];
560
- const errors: Array<{ key: string; error: string }> = [];
561
- for (const key of toDelete) {
562
- try {
563
- // Drop any indexes that reference this attribute to avoid server errors
564
- try {
565
- const idxRes = await adapter.listIndexes({ databaseId, tableId });
566
- const ilist: any[] = (idxRes as any).data || (idxRes as any).indexes || [];
567
- for (const idx of ilist) {
568
- const attrs: string[] = Array.isArray(idx.attributes)
569
- ? idx.attributes
570
- : (Array.isArray((idx as any).columns) ? (idx as any).columns : []);
571
- if (attrs.includes(key)) {
572
- MessageFormatter.info(`🗑️ Deleting index '${idx.key}' referencing '${key}'`, { prefix: 'Indexes' });
573
- await adapter.deleteIndex({ databaseId, tableId, key: idx.key });
574
- await delay(500);
575
- }
576
- }
577
- } catch {}
578
-
579
- await adapter.deleteAttribute({ databaseId, tableId, key });
580
- // Wait briefly for deletion to settle
581
- const start = Date.now();
582
- const maxWaitMs = 60000;
583
- while (Date.now() - start < maxWaitMs) {
584
- try {
585
- const tinfo = await adapter.getTable({ databaseId, tableId });
586
- const cols = (tinfo as any).data?.columns || (tinfo as any).data?.attributes || [];
587
- const found = cols.find((c: any) => c.key === key);
588
- if (!found) break;
589
- if (found.status && found.status !== 'deleting') break;
590
- } catch {}
591
- await delay(1000);
592
- }
593
- deleted.push(key);
594
- } catch (e: any) {
595
- errors.push({ key, error: e?.message || String(e) });
596
- }
597
- }
598
- if (deleted.length) {
599
- MessageFormatter.success(`Deleted ${deleted.length} attributes: ${deleted.join(', ')}`, { prefix: 'Attributes' });
600
- }
601
- if (errors.length) {
602
- MessageFormatter.error(`${errors.length} deletions failed`, undefined, { prefix: 'Attributes' });
603
- errors.forEach(er => MessageFormatter.error(` ${er.key}: ${er.error}`, undefined, { prefix: 'Attributes' }));
604
- }
605
- } else {
606
- MessageFormatter.info(`Plan → 🗑️ 0`, { prefix: 'Attributes' });
607
- }
608
- } catch (e) {
609
- MessageFormatter.warning(`Could not evaluate deletions: ${(e as Error)?.message || e}`, { prefix: 'Attributes' });
610
- }
611
-
612
- // Mark this table as fully processed for this database to prevent re-processing in the same DB only
613
- markCollectionProcessed(tableId, collectionData.name, databaseId);
614
- }
615
-
616
- // Process queued relationships once mapping likely populated
617
- if (relQueue.length > 0) {
618
- MessageFormatter.info(`🔧 Processing ${relQueue.length} queued relationship attributes for tables`, { prefix: "Tables" });
619
- for (const { tableId, attr } of relQueue) {
620
- const relNameOrId = (attr as any).relatedCollection as string | undefined;
621
- if (!relNameOrId) continue;
622
- const relId = nameToIdMapping.get(relNameOrId) || relNameOrId;
623
- if (relId) {
624
- (attr as any).relatedCollection = relId;
625
- try {
626
- await adapter.createAttribute({
627
- databaseId,
628
- tableId,
629
- key: (attr as any).key,
630
- type: (attr as any).type,
631
- size: (attr as any).size,
632
- required: !!(attr as any).required,
633
- default: (attr as any).xdefault,
634
- array: !!(attr as any).array,
635
- min: (attr as any).min,
636
- max: (attr as any).max,
637
- elements: (attr as any).elements,
638
- relatedCollection: relId,
639
- relationType: (attr as any).relationType,
640
- twoWay: (attr as any).twoWay,
641
- twoWayKey: (attr as any).twoWayKey,
642
- onDelete: (attr as any).onDelete,
643
- side: (attr as any).side
644
- });
645
- await delay(150);
646
- MessageFormatter.info(`✅ Successfully processed queued relationship: ${attr.key}`, { prefix: "Tables" });
647
- } catch (e) {
648
- MessageFormatter.error(`Failed queued relationship ${attr.key}`, e instanceof Error ? e : new Error(String(e)), { prefix: 'Attributes' });
649
- }
650
- } else {
651
- MessageFormatter.warning(`Could not resolve relationship ${attr.key} -> ${relNameOrId}`, { prefix: "Tables" });
652
- }
653
- }
654
- }
655
-
656
- // Process any remaining queued operations to complete relationship sync
657
- try {
658
- MessageFormatter.info(`🔄 Processing final operation queue for database ${databaseId}`, { prefix: "Tables" });
659
- await processQueue(adapter, databaseId);
660
- MessageFormatter.info(`✅ Operation queue processing completed`, { prefix: "Tables" });
661
- } catch (error) {
662
- MessageFormatter.error(`Failed to process operation queue`, error instanceof Error ? error : new Error(String(error)), { prefix: 'Tables' });
663
- }
664
- };
665
-
666
- export const generateMockData = async (
667
- database: Databases,
668
- databaseId: string,
669
- configCollections: any[]
670
- ): Promise<void> => {
671
- for (const { collection, mockFunction } of configCollections) {
672
- if (mockFunction) {
673
- MessageFormatter.progress(`Generating mock data for collection: ${collection.name}`, { prefix: "Mock Data" });
674
- const mockData = mockFunction();
675
- for (const data of mockData) {
676
- await database.createDocument(
677
- databaseId,
678
- collection.$id,
679
- ID.unique(),
680
- data
681
- );
682
- }
683
- }
684
- }
685
- };
686
-
687
- export const fetchAllCollections = async (
688
- dbId: string,
689
- database: Databases
690
- ): Promise<Models.Collection[]> => {
691
- MessageFormatter.progress(`Fetching all collections for database ID: ${dbId}`, { prefix: "Collections" });
692
- let collections: Models.Collection[] = [];
693
- let moreCollections = true;
694
- let lastCollectionId: string | undefined;
695
-
696
- while (moreCollections) {
697
- const queries = [Query.limit(500)];
698
- if (lastCollectionId) {
699
- queries.push(Query.cursorAfter(lastCollectionId));
700
- }
701
- const response = await tryAwaitWithRetry(
702
- async () => await database.listCollections(dbId, queries)
703
- );
704
- collections = collections.concat(response.collections);
705
- moreCollections = response.collections.length === 500;
706
- if (moreCollections) {
707
- lastCollectionId =
708
- response.collections[response.collections.length - 1].$id;
709
- }
710
- }
711
-
712
- MessageFormatter.success(`Fetched a total of ${collections.length} collections`, { prefix: "Collections" });
713
- return collections;
714
- };