appwrite-utils-cli 1.9.7 → 1.11.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 (425) hide show
  1. package/CONFIG_TODO.md +1189 -1189
  2. package/README.md +1004 -1004
  3. package/SELECTION_DIALOGS.md +145 -145
  4. package/SERVICE_IMPLEMENTATION_REPORT.md +462 -462
  5. package/package.json +6 -3
  6. package/scripts/copy-templates.ts +23 -23
  7. package/src/adapters/index.ts +11 -37
  8. package/src/backups/operations/bucketBackup.ts +277 -277
  9. package/src/backups/operations/collectionBackup.ts +310 -310
  10. package/src/backups/operations/comprehensiveBackup.ts +342 -342
  11. package/src/backups/schemas/bucketManifest.ts +78 -78
  12. package/src/backups/schemas/comprehensiveManifest.ts +76 -76
  13. package/src/backups/tracking/centralizedTracking.ts +352 -352
  14. package/src/cli/commands/configCommands.ts +265 -201
  15. package/src/cli/commands/databaseCommands.ts +931 -879
  16. package/src/cli/commands/functionCommands.ts +333 -332
  17. package/src/cli/commands/importFileCommands.ts +815 -0
  18. package/src/cli/commands/schemaCommands.ts +141 -141
  19. package/src/cli/commands/storageCommands.ts +2 -3
  20. package/src/cli/commands/transferCommands.ts +454 -457
  21. package/src/collections/attributes.ts.backup +1555 -1555
  22. package/src/collections/{attributes.ts → columns.ts} +15 -10
  23. package/src/collections/indexes.ts +350 -352
  24. package/src/collections/methods.ts +714 -700
  25. package/src/collections/tableOperations.ts +29 -8
  26. package/src/collections/transferOperations.ts +376 -377
  27. package/src/collections/wipeOperations.ts +449 -346
  28. package/src/databases/methods.ts +49 -49
  29. package/src/databases/setup.ts +77 -77
  30. package/src/examples/yamlTerminologyExample.ts +346 -346
  31. package/src/functions/deployments.ts +221 -220
  32. package/src/functions/fnConfigDiscovery.ts +2 -2
  33. package/src/functions/methods.ts +284 -284
  34. package/src/functions/templates/count-docs-in-collection/README.md +53 -53
  35. package/src/functions/templates/count-docs-in-collection/src/main.ts +159 -159
  36. package/src/functions/templates/count-docs-in-collection/src/request.ts +8 -8
  37. package/src/functions/templates/hono-typescript/README.md +285 -285
  38. package/src/functions/templates/hono-typescript/src/adapters/request.ts +73 -73
  39. package/src/functions/templates/hono-typescript/src/adapters/response.ts +105 -105
  40. package/src/functions/templates/hono-typescript/src/app.ts +179 -179
  41. package/src/functions/templates/hono-typescript/src/context.ts +102 -102
  42. package/src/functions/templates/hono-typescript/src/{index.ts → main.ts} +53 -53
  43. package/src/functions/templates/hono-typescript/src/middleware/appwrite.ts +118 -118
  44. package/src/functions/templates/typescript-node/README.md +31 -31
  45. package/src/functions/templates/typescript-node/src/context.ts +102 -102
  46. package/src/functions/templates/typescript-node/src/{index.ts → main.ts} +29 -29
  47. package/src/functions/templates/uv/README.md +30 -30
  48. package/src/functions/templates/uv/pyproject.toml +29 -29
  49. package/src/functions/templates/uv/src/context.py +124 -124
  50. package/src/functions/templates/uv/src/{index.py → main.py} +45 -45
  51. package/src/init.ts +62 -62
  52. package/src/interactiveCLI.ts +1095 -1030
  53. package/src/main.ts +1517 -1670
  54. package/src/migrations/afterImportActions.ts +579 -580
  55. package/src/migrations/appwriteToX.ts +634 -630
  56. package/src/migrations/comprehensiveTransfer.ts +2149 -2149
  57. package/src/migrations/dataLoader.ts +1729 -1702
  58. package/src/migrations/importController.ts +440 -428
  59. package/src/migrations/importDataActions.ts +315 -315
  60. package/src/migrations/relationships.ts +333 -334
  61. package/src/migrations/services/DataTransformationService.ts +195 -195
  62. package/src/migrations/services/FileHandlerService.ts +310 -310
  63. package/src/migrations/services/ImportOrchestrator.ts +674 -665
  64. package/src/migrations/services/RateLimitManager.ts +362 -362
  65. package/src/migrations/services/RelationshipResolver.ts +460 -460
  66. package/src/migrations/services/UserMappingService.ts +344 -344
  67. package/src/migrations/services/ValidationService.ts +333 -333
  68. package/src/migrations/transfer.ts +987 -942
  69. package/src/migrations/yaml/YamlImportConfigLoader.ts +438 -438
  70. package/src/migrations/yaml/YamlImportIntegration.ts +438 -438
  71. package/src/migrations/yaml/generateImportSchemas.ts +1347 -1347
  72. package/src/schemas/authUser.ts +23 -23
  73. package/src/setup.ts +8 -8
  74. package/src/setupCommands.ts +5 -6
  75. package/src/setupController.ts +42 -42
  76. package/src/shared/backupMetadataSchema.ts +93 -93
  77. package/src/shared/backupTracking.ts +211 -211
  78. package/src/shared/confirmationDialogs.ts +326 -326
  79. package/src/shared/migrationHelpers.ts +232 -232
  80. package/src/shared/operationLogger.ts +20 -20
  81. package/src/shared/operationQueue.ts +326 -327
  82. package/src/shared/operationsTable.ts +338 -338
  83. package/src/shared/operationsTableSchema.ts +60 -60
  84. package/src/shared/progressManager.ts +277 -277
  85. package/src/shared/relationshipExtractor.ts +214 -214
  86. package/src/shared/selectionDialogs.ts +775 -722
  87. package/src/storage/backupCompression.ts +88 -88
  88. package/src/storage/methods.ts +695 -682
  89. package/src/storage/schemas.ts +205 -205
  90. package/src/tables/indexManager.ts +408 -408
  91. package/src/types/node-appwrite-tablesdb.d.ts +43 -43
  92. package/src/types.ts +9 -9
  93. package/src/users/methods.ts +358 -359
  94. package/src/utils/configMigration.ts +347 -347
  95. package/src/utils/index.ts +2 -2
  96. package/src/utils/loadConfigs.ts +457 -449
  97. package/src/utils/setupFiles.ts +1236 -1238
  98. package/src/utilsController.ts +1263 -1213
  99. package/tests/README.md +496 -496
  100. package/tests/adapters/AdapterFactory.test.ts +276 -276
  101. package/tests/integration/syncOperations.test.ts +462 -462
  102. package/tests/jest.config.js +24 -24
  103. package/tests/migration/configMigration.test.ts +545 -545
  104. package/tests/setup.ts +61 -61
  105. package/tests/testUtils.ts +339 -339
  106. package/tests/utils/loadConfigs.test.ts +349 -349
  107. package/tests/validation/configValidation.test.ts +411 -411
  108. package/tsconfig.json +44 -44
  109. package/.appwrite/.yaml_schemas/appwrite-config.schema.json +0 -380
  110. package/.appwrite/.yaml_schemas/collection.schema.json +0 -255
  111. package/.appwrite/collections/Categories.yaml +0 -182
  112. package/.appwrite/collections/ExampleCollection.yaml +0 -36
  113. package/.appwrite/collections/Posts.yaml +0 -227
  114. package/.appwrite/collections/Users.yaml +0 -149
  115. package/.appwrite/config.yaml +0 -109
  116. package/.appwrite/import/README.md +0 -148
  117. package/.appwrite/import/categories-import.yaml +0 -129
  118. package/.appwrite/import/posts-import.yaml +0 -208
  119. package/.appwrite/import/users-import.yaml +0 -130
  120. package/.appwrite/importData/categories.json +0 -194
  121. package/.appwrite/importData/posts.json +0 -270
  122. package/.appwrite/importData/users.json +0 -220
  123. package/.appwrite/schemas/categories.json +0 -128
  124. package/.appwrite/schemas/exampleCollection.json +0 -52
  125. package/.appwrite/schemas/posts.json +0 -173
  126. package/.appwrite/schemas/users.json +0 -125
  127. package/dist/adapters/AdapterFactory.d.ts +0 -94
  128. package/dist/adapters/AdapterFactory.js +0 -420
  129. package/dist/adapters/DatabaseAdapter.d.ts +0 -243
  130. package/dist/adapters/DatabaseAdapter.js +0 -50
  131. package/dist/adapters/LegacyAdapter.d.ts +0 -50
  132. package/dist/adapters/LegacyAdapter.js +0 -615
  133. package/dist/adapters/TablesDBAdapter.d.ts +0 -45
  134. package/dist/adapters/TablesDBAdapter.js +0 -611
  135. package/dist/adapters/index.d.ts +0 -11
  136. package/dist/adapters/index.js +0 -12
  137. package/dist/backups/operations/bucketBackup.d.ts +0 -19
  138. package/dist/backups/operations/bucketBackup.js +0 -197
  139. package/dist/backups/operations/collectionBackup.d.ts +0 -30
  140. package/dist/backups/operations/collectionBackup.js +0 -201
  141. package/dist/backups/operations/comprehensiveBackup.d.ts +0 -25
  142. package/dist/backups/operations/comprehensiveBackup.js +0 -238
  143. package/dist/backups/schemas/bucketManifest.d.ts +0 -93
  144. package/dist/backups/schemas/bucketManifest.js +0 -33
  145. package/dist/backups/schemas/comprehensiveManifest.d.ts +0 -108
  146. package/dist/backups/schemas/comprehensiveManifest.js +0 -32
  147. package/dist/backups/tracking/centralizedTracking.d.ts +0 -34
  148. package/dist/backups/tracking/centralizedTracking.js +0 -274
  149. package/dist/cli/commands/configCommands.d.ts +0 -8
  150. package/dist/cli/commands/configCommands.js +0 -166
  151. package/dist/cli/commands/databaseCommands.d.ts +0 -14
  152. package/dist/cli/commands/databaseCommands.js +0 -644
  153. package/dist/cli/commands/functionCommands.d.ts +0 -7
  154. package/dist/cli/commands/functionCommands.js +0 -330
  155. package/dist/cli/commands/schemaCommands.d.ts +0 -7
  156. package/dist/cli/commands/schemaCommands.js +0 -169
  157. package/dist/cli/commands/storageCommands.d.ts +0 -5
  158. package/dist/cli/commands/storageCommands.js +0 -143
  159. package/dist/cli/commands/transferCommands.d.ts +0 -5
  160. package/dist/cli/commands/transferCommands.js +0 -384
  161. package/dist/collections/attributes.d.ts +0 -13
  162. package/dist/collections/attributes.js +0 -1333
  163. package/dist/collections/indexes.d.ts +0 -12
  164. package/dist/collections/indexes.js +0 -217
  165. package/dist/collections/methods.d.ts +0 -19
  166. package/dist/collections/methods.js +0 -587
  167. package/dist/collections/tableOperations.d.ts +0 -86
  168. package/dist/collections/tableOperations.js +0 -447
  169. package/dist/collections/transferOperations.d.ts +0 -8
  170. package/dist/collections/transferOperations.js +0 -412
  171. package/dist/collections/wipeOperations.d.ts +0 -16
  172. package/dist/collections/wipeOperations.js +0 -233
  173. package/dist/config/ConfigManager.d.ts +0 -450
  174. package/dist/config/ConfigManager.js +0 -650
  175. package/dist/config/configMigration.d.ts +0 -87
  176. package/dist/config/configMigration.js +0 -390
  177. package/dist/config/configValidation.d.ts +0 -66
  178. package/dist/config/configValidation.js +0 -358
  179. package/dist/config/index.d.ts +0 -8
  180. package/dist/config/index.js +0 -7
  181. package/dist/config/services/ConfigDiscoveryService.d.ts +0 -122
  182. package/dist/config/services/ConfigDiscoveryService.js +0 -322
  183. package/dist/config/services/ConfigLoaderService.d.ts +0 -129
  184. package/dist/config/services/ConfigLoaderService.js +0 -535
  185. package/dist/config/services/ConfigMergeService.d.ts +0 -208
  186. package/dist/config/services/ConfigMergeService.js +0 -308
  187. package/dist/config/services/ConfigValidationService.d.ts +0 -214
  188. package/dist/config/services/ConfigValidationService.js +0 -310
  189. package/dist/config/services/SessionAuthService.d.ts +0 -225
  190. package/dist/config/services/SessionAuthService.js +0 -456
  191. package/dist/config/services/__tests__/ConfigMergeService.test.d.ts +0 -1
  192. package/dist/config/services/__tests__/ConfigMergeService.test.js +0 -271
  193. package/dist/config/services/index.d.ts +0 -13
  194. package/dist/config/services/index.js +0 -10
  195. package/dist/config/yamlConfig.d.ts +0 -722
  196. package/dist/config/yamlConfig.js +0 -702
  197. package/dist/databases/methods.d.ts +0 -6
  198. package/dist/databases/methods.js +0 -35
  199. package/dist/databases/setup.d.ts +0 -5
  200. package/dist/databases/setup.js +0 -45
  201. package/dist/examples/yamlTerminologyExample.d.ts +0 -42
  202. package/dist/examples/yamlTerminologyExample.js +0 -272
  203. package/dist/functions/deployments.d.ts +0 -4
  204. package/dist/functions/deployments.js +0 -146
  205. package/dist/functions/fnConfigDiscovery.d.ts +0 -3
  206. package/dist/functions/fnConfigDiscovery.js +0 -108
  207. package/dist/functions/methods.d.ts +0 -16
  208. package/dist/functions/methods.js +0 -174
  209. package/dist/functions/pathResolution.d.ts +0 -37
  210. package/dist/functions/pathResolution.js +0 -185
  211. package/dist/functions/templates/count-docs-in-collection/README.md +0 -54
  212. package/dist/functions/templates/count-docs-in-collection/package.json +0 -25
  213. package/dist/functions/templates/count-docs-in-collection/src/main.ts +0 -159
  214. package/dist/functions/templates/count-docs-in-collection/src/request.ts +0 -9
  215. package/dist/functions/templates/count-docs-in-collection/tsconfig.json +0 -28
  216. package/dist/functions/templates/hono-typescript/README.md +0 -286
  217. package/dist/functions/templates/hono-typescript/package.json +0 -26
  218. package/dist/functions/templates/hono-typescript/src/adapters/request.ts +0 -74
  219. package/dist/functions/templates/hono-typescript/src/adapters/response.ts +0 -106
  220. package/dist/functions/templates/hono-typescript/src/app.ts +0 -180
  221. package/dist/functions/templates/hono-typescript/src/context.ts +0 -103
  222. package/dist/functions/templates/hono-typescript/src/index.ts +0 -54
  223. package/dist/functions/templates/hono-typescript/src/middleware/appwrite.ts +0 -119
  224. package/dist/functions/templates/hono-typescript/tsconfig.json +0 -20
  225. package/dist/functions/templates/typescript-node/README.md +0 -32
  226. package/dist/functions/templates/typescript-node/package.json +0 -25
  227. package/dist/functions/templates/typescript-node/src/context.ts +0 -103
  228. package/dist/functions/templates/typescript-node/src/index.ts +0 -29
  229. package/dist/functions/templates/typescript-node/tsconfig.json +0 -28
  230. package/dist/functions/templates/uv/README.md +0 -31
  231. package/dist/functions/templates/uv/pyproject.toml +0 -30
  232. package/dist/functions/templates/uv/src/__init__.py +0 -0
  233. package/dist/functions/templates/uv/src/context.py +0 -125
  234. package/dist/functions/templates/uv/src/index.py +0 -46
  235. package/dist/init.d.ts +0 -2
  236. package/dist/init.js +0 -57
  237. package/dist/interactiveCLI.d.ts +0 -31
  238. package/dist/interactiveCLI.js +0 -898
  239. package/dist/main.d.ts +0 -2
  240. package/dist/main.js +0 -1180
  241. package/dist/migrations/afterImportActions.d.ts +0 -17
  242. package/dist/migrations/afterImportActions.js +0 -306
  243. package/dist/migrations/appwriteToX.d.ts +0 -211
  244. package/dist/migrations/appwriteToX.js +0 -491
  245. package/dist/migrations/comprehensiveTransfer.d.ts +0 -147
  246. package/dist/migrations/comprehensiveTransfer.js +0 -1317
  247. package/dist/migrations/dataLoader.d.ts +0 -753
  248. package/dist/migrations/dataLoader.js +0 -1250
  249. package/dist/migrations/importController.d.ts +0 -23
  250. package/dist/migrations/importController.js +0 -268
  251. package/dist/migrations/importDataActions.d.ts +0 -50
  252. package/dist/migrations/importDataActions.js +0 -230
  253. package/dist/migrations/relationships.d.ts +0 -29
  254. package/dist/migrations/relationships.js +0 -204
  255. package/dist/migrations/services/DataTransformationService.d.ts +0 -55
  256. package/dist/migrations/services/DataTransformationService.js +0 -158
  257. package/dist/migrations/services/FileHandlerService.d.ts +0 -75
  258. package/dist/migrations/services/FileHandlerService.js +0 -236
  259. package/dist/migrations/services/ImportOrchestrator.d.ts +0 -97
  260. package/dist/migrations/services/ImportOrchestrator.js +0 -485
  261. package/dist/migrations/services/RateLimitManager.d.ts +0 -138
  262. package/dist/migrations/services/RateLimitManager.js +0 -279
  263. package/dist/migrations/services/RelationshipResolver.d.ts +0 -120
  264. package/dist/migrations/services/RelationshipResolver.js +0 -332
  265. package/dist/migrations/services/UserMappingService.d.ts +0 -109
  266. package/dist/migrations/services/UserMappingService.js +0 -277
  267. package/dist/migrations/services/ValidationService.d.ts +0 -74
  268. package/dist/migrations/services/ValidationService.js +0 -260
  269. package/dist/migrations/transfer.d.ts +0 -26
  270. package/dist/migrations/transfer.js +0 -608
  271. package/dist/migrations/yaml/YamlImportConfigLoader.d.ts +0 -131
  272. package/dist/migrations/yaml/YamlImportConfigLoader.js +0 -383
  273. package/dist/migrations/yaml/YamlImportIntegration.d.ts +0 -93
  274. package/dist/migrations/yaml/YamlImportIntegration.js +0 -341
  275. package/dist/migrations/yaml/generateImportSchemas.d.ts +0 -30
  276. package/dist/migrations/yaml/generateImportSchemas.js +0 -1327
  277. package/dist/schemas/authUser.d.ts +0 -24
  278. package/dist/schemas/authUser.js +0 -17
  279. package/dist/setup.d.ts +0 -2
  280. package/dist/setup.js +0 -5
  281. package/dist/setupCommands.d.ts +0 -58
  282. package/dist/setupCommands.js +0 -490
  283. package/dist/setupController.d.ts +0 -9
  284. package/dist/setupController.js +0 -34
  285. package/dist/shared/attributeMapper.d.ts +0 -20
  286. package/dist/shared/attributeMapper.js +0 -203
  287. package/dist/shared/backupMetadataSchema.d.ts +0 -94
  288. package/dist/shared/backupMetadataSchema.js +0 -38
  289. package/dist/shared/backupTracking.d.ts +0 -18
  290. package/dist/shared/backupTracking.js +0 -176
  291. package/dist/shared/confirmationDialogs.d.ts +0 -75
  292. package/dist/shared/confirmationDialogs.js +0 -236
  293. package/dist/shared/errorUtils.d.ts +0 -54
  294. package/dist/shared/errorUtils.js +0 -95
  295. package/dist/shared/functionManager.d.ts +0 -48
  296. package/dist/shared/functionManager.js +0 -348
  297. package/dist/shared/jsonSchemaGenerator.d.ts +0 -50
  298. package/dist/shared/jsonSchemaGenerator.js +0 -290
  299. package/dist/shared/logging.d.ts +0 -61
  300. package/dist/shared/logging.js +0 -116
  301. package/dist/shared/messageFormatter.d.ts +0 -39
  302. package/dist/shared/messageFormatter.js +0 -162
  303. package/dist/shared/migrationHelpers.d.ts +0 -61
  304. package/dist/shared/migrationHelpers.js +0 -145
  305. package/dist/shared/operationLogger.d.ts +0 -10
  306. package/dist/shared/operationLogger.js +0 -12
  307. package/dist/shared/operationQueue.d.ts +0 -40
  308. package/dist/shared/operationQueue.js +0 -311
  309. package/dist/shared/operationsTable.d.ts +0 -26
  310. package/dist/shared/operationsTable.js +0 -286
  311. package/dist/shared/operationsTableSchema.d.ts +0 -48
  312. package/dist/shared/operationsTableSchema.js +0 -35
  313. package/dist/shared/progressManager.d.ts +0 -62
  314. package/dist/shared/progressManager.js +0 -215
  315. package/dist/shared/pydanticModelGenerator.d.ts +0 -17
  316. package/dist/shared/pydanticModelGenerator.js +0 -615
  317. package/dist/shared/relationshipExtractor.d.ts +0 -56
  318. package/dist/shared/relationshipExtractor.js +0 -138
  319. package/dist/shared/schemaGenerator.d.ts +0 -40
  320. package/dist/shared/schemaGenerator.js +0 -556
  321. package/dist/shared/selectionDialogs.d.ts +0 -214
  322. package/dist/shared/selectionDialogs.js +0 -544
  323. package/dist/storage/backupCompression.d.ts +0 -20
  324. package/dist/storage/backupCompression.js +0 -67
  325. package/dist/storage/methods.d.ts +0 -32
  326. package/dist/storage/methods.js +0 -472
  327. package/dist/storage/schemas.d.ts +0 -842
  328. package/dist/storage/schemas.js +0 -175
  329. package/dist/tables/indexManager.d.ts +0 -65
  330. package/dist/tables/indexManager.js +0 -294
  331. package/dist/types.d.ts +0 -4
  332. package/dist/types.js +0 -3
  333. package/dist/users/methods.d.ts +0 -16
  334. package/dist/users/methods.js +0 -277
  335. package/dist/utils/ClientFactory.d.ts +0 -87
  336. package/dist/utils/ClientFactory.js +0 -212
  337. package/dist/utils/configDiscovery.d.ts +0 -78
  338. package/dist/utils/configDiscovery.js +0 -472
  339. package/dist/utils/configMigration.d.ts +0 -1
  340. package/dist/utils/configMigration.js +0 -261
  341. package/dist/utils/constantsGenerator.d.ts +0 -31
  342. package/dist/utils/constantsGenerator.js +0 -321
  343. package/dist/utils/dataConverters.d.ts +0 -46
  344. package/dist/utils/dataConverters.js +0 -139
  345. package/dist/utils/directoryUtils.d.ts +0 -22
  346. package/dist/utils/directoryUtils.js +0 -59
  347. package/dist/utils/getClientFromConfig.d.ts +0 -39
  348. package/dist/utils/getClientFromConfig.js +0 -199
  349. package/dist/utils/helperFunctions.d.ts +0 -63
  350. package/dist/utils/helperFunctions.js +0 -156
  351. package/dist/utils/index.d.ts +0 -2
  352. package/dist/utils/index.js +0 -2
  353. package/dist/utils/loadConfigs.d.ts +0 -50
  354. package/dist/utils/loadConfigs.js +0 -358
  355. package/dist/utils/pathResolvers.d.ts +0 -53
  356. package/dist/utils/pathResolvers.js +0 -72
  357. package/dist/utils/projectConfig.d.ts +0 -122
  358. package/dist/utils/projectConfig.js +0 -206
  359. package/dist/utils/retryFailedPromises.d.ts +0 -2
  360. package/dist/utils/retryFailedPromises.js +0 -23
  361. package/dist/utils/sessionAuth.d.ts +0 -48
  362. package/dist/utils/sessionAuth.js +0 -164
  363. package/dist/utils/setupFiles.d.ts +0 -4
  364. package/dist/utils/setupFiles.js +0 -1192
  365. package/dist/utils/typeGuards.d.ts +0 -35
  366. package/dist/utils/typeGuards.js +0 -57
  367. package/dist/utils/validationRules.d.ts +0 -43
  368. package/dist/utils/validationRules.js +0 -42
  369. package/dist/utils/versionDetection.d.ts +0 -58
  370. package/dist/utils/versionDetection.js +0 -251
  371. package/dist/utils/yamlConverter.d.ts +0 -100
  372. package/dist/utils/yamlConverter.js +0 -428
  373. package/dist/utils/yamlLoader.d.ts +0 -70
  374. package/dist/utils/yamlLoader.js +0 -267
  375. package/dist/utilsController.d.ts +0 -107
  376. package/dist/utilsController.js +0 -873
  377. package/src/adapters/AdapterFactory.ts +0 -529
  378. package/src/adapters/DatabaseAdapter.ts +0 -319
  379. package/src/adapters/LegacyAdapter.ts +0 -844
  380. package/src/adapters/TablesDBAdapter.ts +0 -823
  381. package/src/config/ConfigManager.ts +0 -849
  382. package/src/config/README.md +0 -274
  383. package/src/config/configMigration.ts +0 -575
  384. package/src/config/configValidation.ts +0 -445
  385. package/src/config/index.ts +0 -10
  386. package/src/config/services/ConfigDiscoveryService.ts +0 -410
  387. package/src/config/services/ConfigLoaderService.ts +0 -732
  388. package/src/config/services/ConfigMergeService.ts +0 -388
  389. package/src/config/services/ConfigValidationService.ts +0 -394
  390. package/src/config/services/SessionAuthService.ts +0 -565
  391. package/src/config/services/__tests__/ConfigMergeService.test.ts +0 -351
  392. package/src/config/services/index.ts +0 -29
  393. package/src/config/yamlConfig.ts +0 -761
  394. package/src/functions/pathResolution.ts +0 -227
  395. package/src/functions/templates/count-docs-in-collection/package.json +0 -25
  396. package/src/functions/templates/count-docs-in-collection/tsconfig.json +0 -28
  397. package/src/functions/templates/hono-typescript/package.json +0 -26
  398. package/src/functions/templates/hono-typescript/tsconfig.json +0 -20
  399. package/src/functions/templates/typescript-node/package.json +0 -25
  400. package/src/functions/templates/typescript-node/tsconfig.json +0 -28
  401. package/src/shared/attributeMapper.ts +0 -229
  402. package/src/shared/errorUtils.ts +0 -110
  403. package/src/shared/functionManager.ts +0 -537
  404. package/src/shared/jsonSchemaGenerator.ts +0 -383
  405. package/src/shared/logging.ts +0 -149
  406. package/src/shared/messageFormatter.ts +0 -208
  407. package/src/shared/pydanticModelGenerator.ts +0 -618
  408. package/src/shared/schemaGenerator.ts +0 -644
  409. package/src/utils/ClientFactory.ts +0 -240
  410. package/src/utils/configDiscovery.ts +0 -557
  411. package/src/utils/constantsGenerator.ts +0 -369
  412. package/src/utils/dataConverters.ts +0 -159
  413. package/src/utils/directoryUtils.ts +0 -61
  414. package/src/utils/getClientFromConfig.ts +0 -257
  415. package/src/utils/helperFunctions.ts +0 -228
  416. package/src/utils/pathResolvers.ts +0 -81
  417. package/src/utils/projectConfig.ts +0 -340
  418. package/src/utils/retryFailedPromises.ts +0 -29
  419. package/src/utils/sessionAuth.ts +0 -230
  420. package/src/utils/typeGuards.ts +0 -65
  421. package/src/utils/validationRules.ts +0 -88
  422. package/src/utils/versionDetection.ts +0 -292
  423. package/src/utils/yamlConverter.ts +0 -542
  424. package/src/utils/yamlLoader.ts +0 -371
  425. package/tmp-sync-test/.appwrite/collections/TestCollection.yaml +0 -7
@@ -1,1333 +0,0 @@
1
- import { Query } from "node-appwrite";
2
- import { attributeSchema, parseAttribute, } from "appwrite-utils";
3
- import { nameToIdMapping, enqueueOperation, markAttributeProcessed, isAttributeProcessed, } from "../shared/operationQueue.js";
4
- import { delay, tryAwaitWithRetry, calculateExponentialBackoff, } from "../utils/helperFunctions.js";
5
- import chalk from "chalk";
6
- import { Decimal } from "decimal.js";
7
- import { logger } from "../shared/logging.js";
8
- import { MessageFormatter } from "../shared/messageFormatter.js";
9
- import { isDatabaseAdapter } from "../utils/typeGuards.js";
10
- // Extreme values that Appwrite may return, which should be treated as undefined
11
- const EXTREME_MIN_INTEGER = -9223372036854776000;
12
- const EXTREME_MAX_INTEGER = 9223372036854776000;
13
- const EXTREME_MIN_FLOAT = -1.7976931348623157e308;
14
- const EXTREME_MAX_FLOAT = 1.7976931348623157e308;
15
- /**
16
- * Type guard to check if an attribute has min/max properties
17
- */
18
- const hasMinMaxProperties = (attribute) => {
19
- return (attribute.type === "integer" ||
20
- attribute.type === "double" ||
21
- attribute.type === "float");
22
- };
23
- /**
24
- * Normalizes min/max values for integer and float attributes using Decimal.js for precision
25
- * Validates that min < max and handles extreme database values
26
- */
27
- const normalizeMinMaxValues = (attribute) => {
28
- if (!hasMinMaxProperties(attribute)) {
29
- logger.debug(`Attribute '${attribute.key}' does not have min/max properties`, {
30
- type: attribute.type,
31
- operation: "normalizeMinMaxValues",
32
- });
33
- return {};
34
- }
35
- const { type, min, max } = attribute;
36
- let normalizedMin = min;
37
- let normalizedMax = max;
38
- logger.debug(`Normalizing min/max values for attribute '${attribute.key}'`, {
39
- type,
40
- originalMin: min,
41
- originalMax: max,
42
- operation: "normalizeMinMaxValues",
43
- });
44
- // Handle min value - only filter out extreme database values
45
- if (normalizedMin !== undefined && normalizedMin !== null) {
46
- const minValue = Number(normalizedMin);
47
- const originalMin = normalizedMin;
48
- // Check if it's an extreme database value (but don't filter out large numbers)
49
- if (type === 'integer') {
50
- if (minValue === EXTREME_MIN_INTEGER) {
51
- logger.debug(`Min value normalized to undefined for attribute '${attribute.key}'`, {
52
- type,
53
- originalValue: originalMin,
54
- numericValue: minValue,
55
- reason: 'extreme_database_value',
56
- extremeValue: EXTREME_MIN_INTEGER,
57
- operation: 'normalizeMinMaxValues'
58
- });
59
- normalizedMin = undefined;
60
- }
61
- }
62
- else { // float/double
63
- if (minValue === EXTREME_MIN_FLOAT) {
64
- logger.debug(`Min value normalized to undefined for attribute '${attribute.key}'`, {
65
- type,
66
- originalValue: originalMin,
67
- numericValue: minValue,
68
- reason: 'extreme_database_value',
69
- extremeValue: EXTREME_MIN_FLOAT,
70
- operation: 'normalizeMinMaxValues'
71
- });
72
- normalizedMin = undefined;
73
- }
74
- }
75
- }
76
- // Handle max value - only filter out extreme database values
77
- if (normalizedMax !== undefined && normalizedMax !== null) {
78
- const maxValue = Number(normalizedMax);
79
- const originalMax = normalizedMax;
80
- // Check if it's an extreme database value (but don't filter out large numbers)
81
- if (type === 'integer') {
82
- if (maxValue === EXTREME_MAX_INTEGER) {
83
- logger.debug(`Max value normalized to undefined for attribute '${attribute.key}'`, {
84
- type,
85
- originalValue: originalMax,
86
- numericValue: maxValue,
87
- reason: 'extreme_database_value',
88
- extremeValue: EXTREME_MAX_INTEGER,
89
- operation: 'normalizeMinMaxValues'
90
- });
91
- normalizedMax = undefined;
92
- }
93
- }
94
- else { // float/double
95
- if (maxValue === EXTREME_MAX_FLOAT) {
96
- logger.debug(`Max value normalized to undefined for attribute '${attribute.key}'`, {
97
- type,
98
- originalValue: originalMax,
99
- numericValue: maxValue,
100
- reason: 'extreme_database_value',
101
- extremeValue: EXTREME_MAX_FLOAT,
102
- operation: 'normalizeMinMaxValues'
103
- });
104
- normalizedMax = undefined;
105
- }
106
- }
107
- }
108
- // Validate that min < max using multiple comparison methods for reliability
109
- if (normalizedMin !== undefined && normalizedMax !== undefined &&
110
- normalizedMin !== null && normalizedMax !== null) {
111
- logger.debug(`Validating min/max values for attribute '${attribute.key}'`, {
112
- type,
113
- normalizedMin,
114
- normalizedMax,
115
- normalizedMinType: typeof normalizedMin,
116
- normalizedMaxType: typeof normalizedMax,
117
- operation: 'normalizeMinMaxValues'
118
- });
119
- // Use multiple validation approaches to ensure reliability
120
- let needsSwap = false;
121
- let comparisonMethod = '';
122
- try {
123
- // Method 1: Direct number comparison (most reliable for normal numbers)
124
- const minNum = Number(normalizedMin);
125
- const maxNum = Number(normalizedMax);
126
- if (!isNaN(minNum) && !isNaN(maxNum)) {
127
- needsSwap = minNum >= maxNum;
128
- comparisonMethod = 'direct_number_comparison';
129
- logger.debug(`Direct number comparison: ${minNum} >= ${maxNum} = ${needsSwap}`, {
130
- operation: 'normalizeMinMaxValues'
131
- });
132
- }
133
- // Method 2: Fallback to string comparison for very large numbers
134
- if (!needsSwap && (isNaN(minNum) || isNaN(maxNum) || Math.abs(minNum) > Number.MAX_SAFE_INTEGER || Math.abs(maxNum) > Number.MAX_SAFE_INTEGER)) {
135
- const minStr = normalizedMin.toString();
136
- const maxStr = normalizedMax.toString();
137
- // Simple string length and lexicographical comparison for very large numbers
138
- if (minStr.length !== maxStr.length) {
139
- needsSwap = minStr.length > maxStr.length;
140
- }
141
- else {
142
- needsSwap = minStr >= maxStr;
143
- }
144
- comparisonMethod = 'string_comparison_fallback';
145
- logger.debug(`String comparison fallback: '${minStr}' >= '${maxStr}' = ${needsSwap}`, {
146
- operation: 'normalizeMinMaxValues'
147
- });
148
- }
149
- // Method 3: Final validation using Decimal.js as last resort
150
- if (!needsSwap && (typeof normalizedMin === 'string' || typeof normalizedMax === 'string')) {
151
- try {
152
- const minDecimal = new Decimal(normalizedMin.toString());
153
- const maxDecimal = new Decimal(normalizedMax.toString());
154
- needsSwap = minDecimal.greaterThanOrEqualTo(maxDecimal);
155
- comparisonMethod = 'decimal_js_fallback';
156
- logger.debug(`Decimal.js fallback: ${normalizedMin} >= ${normalizedMax} = ${needsSwap}`, {
157
- operation: 'normalizeMinMaxValues'
158
- });
159
- }
160
- catch (decimalError) {
161
- logger.warn(`Decimal.js comparison failed for attribute '${attribute.key}': ${decimalError instanceof Error ? decimalError.message : String(decimalError)}`, {
162
- operation: 'normalizeMinMaxValues'
163
- });
164
- }
165
- }
166
- // Log final validation result
167
- if (needsSwap) {
168
- logger.error(`Invalid min/max values detected for attribute '${attribute.key}': min (${normalizedMin}) must be less than max (${normalizedMax})`, {
169
- type,
170
- min: normalizedMin,
171
- max: normalizedMax,
172
- comparisonMethod,
173
- operation: 'normalizeMinMaxValues'
174
- });
175
- // Swap values to ensure min < max (graceful handling)
176
- logger.warn(`Swapping min/max values for attribute '${attribute.key}' to fix validation`, {
177
- type,
178
- originalMin: normalizedMin,
179
- originalMax: normalizedMax,
180
- newMin: normalizedMax,
181
- newMax: normalizedMin,
182
- comparisonMethod,
183
- operation: 'normalizeMinMaxValues'
184
- });
185
- const temp = normalizedMin;
186
- normalizedMin = normalizedMax;
187
- normalizedMax = temp;
188
- }
189
- else {
190
- logger.debug(`Min/max validation passed for attribute '${attribute.key}'`, {
191
- type,
192
- min: normalizedMin,
193
- max: normalizedMax,
194
- comparisonMethod,
195
- operation: 'normalizeMinMaxValues'
196
- });
197
- }
198
- }
199
- catch (error) {
200
- logger.error(`Critical error during min/max validation for attribute '${attribute.key}'`, {
201
- type,
202
- min: normalizedMin,
203
- max: normalizedMax,
204
- error: error instanceof Error ? error.message : String(error),
205
- operation: 'normalizeMinMaxValues'
206
- });
207
- // If all comparison methods fail, set both to undefined to avoid API errors
208
- normalizedMin = undefined;
209
- normalizedMax = undefined;
210
- }
211
- }
212
- const result = { min: normalizedMin, max: normalizedMax };
213
- logger.debug(`Min/max normalization complete for attribute '${attribute.key}'`, {
214
- type,
215
- result,
216
- operation: "normalizeMinMaxValues",
217
- });
218
- return result;
219
- };
220
- /**
221
- * Normalizes an attribute for comparison by handling extreme database values
222
- * This is used when comparing database attributes with config attributes
223
- */
224
- const normalizeAttributeForComparison = (attribute) => {
225
- const normalized = { ...attribute };
226
- // Ignore defaults on required attributes to prevent false positives
227
- if (normalized.required === true && "xdefault" in normalized) {
228
- delete normalized.xdefault;
229
- }
230
- // Normalize min/max for numeric types
231
- if (hasMinMaxProperties(attribute)) {
232
- const { min, max } = normalizeMinMaxValues(attribute);
233
- normalized.min = min;
234
- normalized.max = max;
235
- }
236
- // Remove xdefault if null/undefined to ensure consistent comparison
237
- // Appwrite sets xdefault: null for required attributes, but config files omit it
238
- if ("xdefault" in normalized &&
239
- (normalized.xdefault === null || normalized.xdefault === undefined)) {
240
- delete normalized.xdefault;
241
- }
242
- return normalized;
243
- };
244
- /**
245
- * Helper function to create an attribute using either the adapter or legacy API
246
- */
247
- const createAttributeViaAdapter = async (db, dbId, collectionId, attribute) => {
248
- const startTime = Date.now();
249
- const adapterType = isDatabaseAdapter(db) ? "adapter" : "legacy";
250
- logger.info(`Creating attribute '${attribute.key}' via ${adapterType}`, {
251
- type: attribute.type,
252
- dbId,
253
- collectionId,
254
- adapterType,
255
- operation: "createAttributeViaAdapter",
256
- });
257
- if (isDatabaseAdapter(db)) {
258
- // Use the adapter's unified createAttribute method
259
- const params = {
260
- databaseId: dbId,
261
- tableId: collectionId,
262
- key: attribute.key,
263
- type: attribute.type,
264
- required: attribute.required || false,
265
- array: attribute.array || false,
266
- ...(attribute.size && { size: attribute.size }),
267
- ...(attribute.xdefault !== undefined &&
268
- !attribute.required && { default: attribute.xdefault }),
269
- ...(attribute.encrypt && {
270
- encrypt: attribute.encrypt,
271
- }),
272
- ...(attribute.min !== undefined && {
273
- min: attribute.min,
274
- }),
275
- ...(attribute.max !== undefined && {
276
- max: attribute.max,
277
- }),
278
- ...(attribute.elements && {
279
- elements: attribute.elements,
280
- }),
281
- ...(attribute.relatedCollection && {
282
- relatedCollection: attribute.relatedCollection,
283
- }),
284
- ...(attribute.relationType && {
285
- relationType: attribute.relationType,
286
- }),
287
- ...(attribute.twoWay !== undefined && {
288
- twoWay: attribute.twoWay,
289
- }),
290
- ...(attribute.onDelete && {
291
- onDelete: attribute.onDelete,
292
- }),
293
- ...(attribute.twoWayKey && {
294
- twoWayKey: attribute.twoWayKey,
295
- }),
296
- };
297
- logger.debug(`Adapter create parameters for '${attribute.key}'`, {
298
- params,
299
- operation: "createAttributeViaAdapter",
300
- });
301
- await db.createAttribute(params);
302
- const duration = Date.now() - startTime;
303
- logger.info(`Successfully created attribute '${attribute.key}' via adapter`, {
304
- duration,
305
- operation: "createAttributeViaAdapter",
306
- });
307
- }
308
- else {
309
- // Use legacy type-specific methods
310
- logger.debug(`Using legacy creation for attribute '${attribute.key}'`, {
311
- operation: "createAttributeViaAdapter",
312
- });
313
- await createLegacyAttribute(db, dbId, collectionId, attribute);
314
- const duration = Date.now() - startTime;
315
- logger.info(`Successfully created attribute '${attribute.key}' via legacy`, {
316
- duration,
317
- operation: "createAttributeViaAdapter",
318
- });
319
- }
320
- };
321
- /**
322
- * Helper function to update an attribute using either the adapter or legacy API
323
- */
324
- const updateAttributeViaAdapter = async (db, dbId, collectionId, attribute) => {
325
- if (isDatabaseAdapter(db)) {
326
- // Use the adapter's unified updateAttribute method
327
- const params = {
328
- databaseId: dbId,
329
- tableId: collectionId,
330
- key: attribute.key,
331
- type: attribute.type,
332
- required: attribute.required || false,
333
- array: attribute.array || false,
334
- size: attribute.size,
335
- min: attribute.min,
336
- max: attribute.max,
337
- encrypt: attribute.encrypt,
338
- elements: attribute.elements,
339
- relatedCollection: attribute.relatedCollection,
340
- relationType: attribute.relationType,
341
- twoWay: attribute.twoWay,
342
- twoWayKey: attribute.twoWayKey,
343
- onDelete: attribute.onDelete
344
- };
345
- if (!attribute.required && attribute.xdefault !== undefined) {
346
- params.default = attribute.xdefault;
347
- }
348
- await db.updateAttribute(params);
349
- }
350
- else {
351
- // Use legacy type-specific methods
352
- await updateLegacyAttribute(db, dbId, collectionId, attribute);
353
- }
354
- };
355
- /**
356
- * Legacy attribute creation using type-specific methods
357
- */
358
- const createLegacyAttribute = async (db, dbId, collectionId, attribute) => {
359
- const startTime = Date.now();
360
- const { min: normalizedMin, max: normalizedMax } = normalizeMinMaxValues(attribute);
361
- logger.info(`Creating legacy attribute '${attribute.key}'`, {
362
- type: attribute.type,
363
- dbId,
364
- collectionId,
365
- normalizedMin,
366
- normalizedMax,
367
- operation: "createLegacyAttribute",
368
- });
369
- switch (attribute.type) {
370
- case "string":
371
- const stringParams = {
372
- size: attribute.size || 255,
373
- required: attribute.required || false,
374
- defaultValue: attribute.xdefault !== undefined && !attribute.required
375
- ? attribute.xdefault
376
- : undefined,
377
- array: attribute.array || false,
378
- encrypt: attribute.encrypt,
379
- };
380
- logger.debug(`Creating string attribute '${attribute.key}'`, {
381
- ...stringParams,
382
- operation: "createLegacyAttribute",
383
- });
384
- await db.createStringAttribute(dbId, collectionId, attribute.key, stringParams.size, stringParams.required, stringParams.defaultValue, stringParams.array, stringParams.encrypt);
385
- break;
386
- case "integer":
387
- const integerParams = {
388
- required: attribute.required || false,
389
- min: normalizedMin !== undefined
390
- ? parseInt(String(normalizedMin))
391
- : undefined,
392
- max: normalizedMax !== undefined
393
- ? parseInt(String(normalizedMax))
394
- : undefined,
395
- defaultValue: attribute.xdefault !== undefined && !attribute.required
396
- ? attribute.xdefault
397
- : undefined,
398
- array: attribute.array || false,
399
- };
400
- logger.debug(`Creating integer attribute '${attribute.key}'`, {
401
- ...integerParams,
402
- operation: "createLegacyAttribute",
403
- });
404
- await db.createIntegerAttribute(dbId, collectionId, attribute.key, integerParams.required, integerParams.min, integerParams.max, integerParams.defaultValue, integerParams.array);
405
- break;
406
- case "double":
407
- case "float":
408
- await db.createFloatAttribute(dbId, collectionId, attribute.key, attribute.required || false, normalizedMin !== undefined ? Number(normalizedMin) : undefined, normalizedMax !== undefined ? Number(normalizedMax) : undefined, attribute.xdefault !== undefined && !attribute.required
409
- ? attribute.xdefault
410
- : undefined, attribute.array || false);
411
- break;
412
- case "boolean":
413
- await db.createBooleanAttribute(dbId, collectionId, attribute.key, attribute.required || false, attribute.xdefault !== undefined && !attribute.required
414
- ? attribute.xdefault
415
- : undefined, attribute.array || false);
416
- break;
417
- case "datetime":
418
- await db.createDatetimeAttribute(dbId, collectionId, attribute.key, attribute.required || false, attribute.xdefault !== undefined && !attribute.required
419
- ? attribute.xdefault
420
- : undefined, attribute.array || false);
421
- break;
422
- case "email":
423
- await db.createEmailAttribute(dbId, collectionId, attribute.key, attribute.required || false, attribute.xdefault !== undefined && !attribute.required
424
- ? attribute.xdefault
425
- : undefined, attribute.array || false);
426
- break;
427
- case "ip":
428
- await db.createIpAttribute(dbId, collectionId, attribute.key, attribute.required || false, attribute.xdefault !== undefined && !attribute.required
429
- ? attribute.xdefault
430
- : undefined, attribute.array || false);
431
- break;
432
- case "url":
433
- await db.createUrlAttribute(dbId, collectionId, attribute.key, attribute.required || false, attribute.xdefault !== undefined && !attribute.required
434
- ? attribute.xdefault
435
- : undefined, attribute.array || false);
436
- break;
437
- case "enum":
438
- await db.createEnumAttribute(dbId, collectionId, attribute.key, attribute.elements || [], attribute.required || false, attribute.xdefault !== undefined && !attribute.required
439
- ? attribute.xdefault
440
- : undefined, attribute.array || false);
441
- break;
442
- case "relationship":
443
- await db.createRelationshipAttribute(dbId, collectionId, attribute.relatedCollection, attribute.relationType, attribute.twoWay, attribute.key, attribute.twoWayKey, attribute.onDelete);
444
- break;
445
- default:
446
- const error = new Error(`Unsupported attribute type: ${attribute.type}`);
447
- logger.error(`Unsupported attribute type for '${attribute.key}'`, {
448
- type: attribute.type,
449
- supportedTypes: [
450
- "string",
451
- "integer",
452
- "double",
453
- "float",
454
- "boolean",
455
- "datetime",
456
- "email",
457
- "ip",
458
- "url",
459
- "enum",
460
- "relationship",
461
- ],
462
- operation: "createLegacyAttribute",
463
- });
464
- throw error;
465
- }
466
- const duration = Date.now() - startTime;
467
- logger.info(`Successfully created legacy attribute '${attribute.key}'`, {
468
- type: attribute.type,
469
- duration,
470
- operation: "createLegacyAttribute",
471
- });
472
- };
473
- /**
474
- * Legacy attribute update using type-specific methods
475
- */
476
- const updateLegacyAttribute = async (db, dbId, collectionId, attribute) => {
477
- const { min: normalizedMin, max: normalizedMax } = normalizeMinMaxValues(attribute);
478
- switch (attribute.type) {
479
- case "string":
480
- await db.updateStringAttribute(dbId, collectionId, attribute.key, attribute.required || false, !attribute.required && attribute.xdefault !== undefined
481
- ? attribute.xdefault
482
- : null, attribute.size);
483
- break;
484
- case "integer":
485
- await db.updateIntegerAttribute(dbId, collectionId, attribute.key, attribute.required || false, !attribute.required && attribute.xdefault !== undefined
486
- ? attribute.xdefault
487
- : null, normalizedMin !== undefined
488
- ? parseInt(String(normalizedMin))
489
- : undefined, normalizedMax !== undefined
490
- ? parseInt(String(normalizedMax))
491
- : undefined);
492
- break;
493
- case "double":
494
- case "float":
495
- const minParam = normalizedMin !== undefined ? Number(normalizedMin) : undefined;
496
- const maxParam = normalizedMax !== undefined ? Number(normalizedMax) : undefined;
497
- await db.updateFloatAttribute(dbId, collectionId, attribute.key, attribute.required || false, minParam, maxParam, !attribute.required && attribute.xdefault !== undefined
498
- ? attribute.xdefault
499
- : null);
500
- break;
501
- case "boolean":
502
- await db.updateBooleanAttribute(dbId, collectionId, attribute.key, attribute.required || false, !attribute.required && attribute.xdefault !== undefined
503
- ? attribute.xdefault
504
- : null);
505
- break;
506
- case "datetime":
507
- await db.updateDatetimeAttribute(dbId, collectionId, attribute.key, attribute.required || false, !attribute.required && attribute.xdefault !== undefined
508
- ? attribute.xdefault
509
- : null);
510
- break;
511
- case "email":
512
- await db.updateEmailAttribute(dbId, collectionId, attribute.key, attribute.required || false, !attribute.required && attribute.xdefault !== undefined
513
- ? attribute.xdefault
514
- : null);
515
- break;
516
- case "ip":
517
- await db.updateIpAttribute(dbId, collectionId, attribute.key, attribute.required || false, !attribute.required && attribute.xdefault !== undefined
518
- ? attribute.xdefault
519
- : null);
520
- break;
521
- case "url":
522
- await db.updateUrlAttribute(dbId, collectionId, attribute.key, attribute.required || false, !attribute.required && attribute.xdefault !== undefined
523
- ? attribute.xdefault
524
- : null);
525
- break;
526
- case "enum":
527
- await db.updateEnumAttribute(dbId, collectionId, attribute.key, attribute.elements || [], attribute.required || false, !attribute.required && attribute.xdefault !== undefined
528
- ? attribute.xdefault
529
- : null);
530
- break;
531
- case "relationship":
532
- await db.updateRelationshipAttribute(dbId, collectionId, attribute.key, attribute.onDelete);
533
- break;
534
- default:
535
- throw new Error(`Unsupported attribute type for update: ${attribute.type}`);
536
- }
537
- };
538
- /**
539
- * Wait for attribute to become available, with retry logic for stuck attributes and exponential backoff
540
- */
541
- const waitForAttributeAvailable = async (db, dbId, collectionId, attributeKey, maxWaitTime = 60000, // 1 minute
542
- retryCount = 0, maxRetries = 5) => {
543
- const startTime = Date.now();
544
- let checkInterval = 2000; // Start with 2 seconds
545
- logger.info(`Waiting for attribute '${attributeKey}' to become available`, {
546
- dbId,
547
- collectionId,
548
- maxWaitTime,
549
- retryCount,
550
- maxRetries,
551
- operation: "waitForAttributeAvailable",
552
- });
553
- // Calculate exponential backoff: 2s, 4s, 8s, 16s, 30s (capped at 30s)
554
- if (retryCount > 0) {
555
- const exponentialDelay = calculateExponentialBackoff(retryCount);
556
- await delay(exponentialDelay);
557
- }
558
- while (Date.now() - startTime < maxWaitTime) {
559
- try {
560
- const collection = isDatabaseAdapter(db)
561
- ? (await db.getTable({ databaseId: dbId, tableId: collectionId })).data
562
- : await db.getCollection(dbId, collectionId);
563
- const attribute = collection.attributes.find((attr) => attr.key === attributeKey);
564
- if (!attribute) {
565
- MessageFormatter.error(`Attribute '${attributeKey}' not found`);
566
- return false;
567
- }
568
- const statusInfo = {
569
- attributeKey,
570
- status: attribute.status,
571
- error: attribute.error,
572
- dbId,
573
- collectionId,
574
- waitTime: Date.now() - startTime,
575
- operation: "waitForAttributeAvailable",
576
- };
577
- switch (attribute.status) {
578
- case "available":
579
- logger.info(`Attribute '${attributeKey}' became available`, statusInfo);
580
- return true;
581
- case "failed":
582
- logger.error(`Attribute '${attributeKey}' failed`, statusInfo);
583
- return false;
584
- case "stuck":
585
- logger.warn(`Attribute '${attributeKey}' is stuck`, statusInfo);
586
- return false;
587
- case "processing":
588
- // Continue waiting
589
- logger.debug(`Attribute '${attributeKey}' still processing`, statusInfo);
590
- break;
591
- case "deleting":
592
- MessageFormatter.info(chalk.yellow(`Attribute '${attributeKey}' is being deleted`));
593
- logger.warn(`Attribute '${attributeKey}' is being deleted`, statusInfo);
594
- break;
595
- default:
596
- MessageFormatter.info(chalk.yellow(`Unknown status '${attribute.status}' for attribute '${attributeKey}'`));
597
- logger.warn(`Unknown status for attribute '${attributeKey}'`, statusInfo);
598
- break;
599
- }
600
- await delay(checkInterval);
601
- }
602
- catch (error) {
603
- const errorMessage = error instanceof Error ? error.message : String(error);
604
- MessageFormatter.error(`Error checking attribute status: ${errorMessage}`);
605
- logger.error("Error checking attribute status", {
606
- attributeKey,
607
- dbId,
608
- collectionId,
609
- error: errorMessage,
610
- waitTime: Date.now() - startTime,
611
- operation: "waitForAttributeAvailable",
612
- });
613
- return false;
614
- }
615
- }
616
- // Timeout reached
617
- MessageFormatter.info(chalk.yellow(`⏰ Timeout waiting for attribute '${attributeKey}' (${maxWaitTime}ms)`));
618
- // If we have retries left and this isn't the last retry, try recreating
619
- if (retryCount < maxRetries) {
620
- MessageFormatter.info(chalk.yellow(`🔄 Retrying attribute creation (attempt ${retryCount + 1}/${maxRetries})`));
621
- return false; // Signal that we need to retry
622
- }
623
- return false;
624
- };
625
- /**
626
- * Wait for all attributes in a collection to become available
627
- */
628
- const waitForAllAttributesAvailable = async (db, dbId, collectionId, attributeKeys, maxWaitTime = 60000) => {
629
- MessageFormatter.info(chalk.blue(`Waiting for ${attributeKeys.length} attributes to become available...`));
630
- const failedAttributes = [];
631
- for (const attributeKey of attributeKeys) {
632
- const success = await waitForAttributeAvailable(db, dbId, collectionId, attributeKey, maxWaitTime);
633
- if (!success) {
634
- failedAttributes.push(attributeKey);
635
- }
636
- }
637
- return failedAttributes;
638
- };
639
- /**
640
- * Delete collection and recreate with retry logic
641
- */
642
- const deleteAndRecreateCollection = async (db, dbId, collection, retryCount) => {
643
- try {
644
- MessageFormatter.info(chalk.yellow(`🗑️ Deleting collection '${collection.name}' for retry ${retryCount}`));
645
- // Delete the collection
646
- if (isDatabaseAdapter(db)) {
647
- await db.deleteTable({ databaseId: dbId, tableId: collection.$id });
648
- }
649
- else {
650
- await db.deleteCollection(dbId, collection.$id);
651
- }
652
- MessageFormatter.warning(`Deleted collection '${collection.name}'`);
653
- // Wait a bit before recreating
654
- await delay(2000);
655
- // Recreate the collection
656
- MessageFormatter.info(`🔄 Recreating collection '${collection.name}'`);
657
- const newCollection = isDatabaseAdapter(db)
658
- ? (await db.createTable({
659
- databaseId: dbId,
660
- id: collection.$id,
661
- name: collection.name,
662
- permissions: collection.$permissions,
663
- documentSecurity: collection.documentSecurity,
664
- enabled: collection.enabled,
665
- })).data
666
- : await db.createCollection(dbId, collection.$id, collection.name, collection.$permissions, collection.documentSecurity, collection.enabled);
667
- MessageFormatter.success(`✅ Recreated collection '${collection.name}'`);
668
- return newCollection;
669
- }
670
- catch (error) {
671
- MessageFormatter.info(chalk.red(`Failed to delete/recreate collection '${collection.name}': ${error}`));
672
- return null;
673
- }
674
- };
675
- /**
676
- * Get the fields that should be compared for a specific attribute type
677
- * Only returns fields that are valid for the given type to avoid false positives
678
- */
679
- const getComparableFields = (type) => {
680
- const baseFields = ["key", "type", "array", "required", "xdefault"];
681
- switch (type) {
682
- case "string":
683
- return [...baseFields, "size", "encrypt"];
684
- case "integer":
685
- case "double":
686
- case "float":
687
- return [...baseFields, "min", "max"];
688
- case "enum":
689
- return [...baseFields, "elements"];
690
- case "relationship":
691
- return [
692
- ...baseFields,
693
- "relationType",
694
- "twoWay",
695
- "twoWayKey",
696
- "onDelete",
697
- "relatedCollection",
698
- ];
699
- case "boolean":
700
- case "datetime":
701
- case "email":
702
- case "ip":
703
- case "url":
704
- return baseFields;
705
- default:
706
- // Fallback to all fields for unknown types
707
- return [
708
- "key",
709
- "type",
710
- "array",
711
- "encrypt",
712
- "required",
713
- "size",
714
- "min",
715
- "max",
716
- "xdefault",
717
- "elements",
718
- "relationType",
719
- "twoWay",
720
- "twoWayKey",
721
- "onDelete",
722
- "relatedCollection",
723
- ];
724
- }
725
- };
726
- const attributesSame = (databaseAttribute, configAttribute) => {
727
- // Normalize both attributes for comparison (handle extreme database values)
728
- const normalizedDbAttr = normalizeAttributeForComparison(databaseAttribute);
729
- const normalizedConfigAttr = normalizeAttributeForComparison(configAttribute);
730
- // Use type-specific field list to avoid false positives from irrelevant fields
731
- const attributesToCheck = getComparableFields(normalizedConfigAttr.type);
732
- const fieldsToCheck = attributesToCheck.filter((attr) => {
733
- if (attr !== "xdefault") {
734
- return true;
735
- }
736
- const dbRequired = Boolean(normalizedDbAttr.required);
737
- const configRequired = Boolean(normalizedConfigAttr.required);
738
- return !(dbRequired || configRequired);
739
- });
740
- const differences = [];
741
- const result = fieldsToCheck.every((attr) => {
742
- // Check if both objects have the attribute
743
- const dbHasAttr = attr in normalizedDbAttr;
744
- const configHasAttr = attr in normalizedConfigAttr;
745
- // If both have the attribute, compare values
746
- if (dbHasAttr && configHasAttr) {
747
- const dbValue = normalizedDbAttr[attr];
748
- const configValue = normalizedConfigAttr[attr];
749
- // Consider undefined and null as equivalent
750
- if ((dbValue === undefined || dbValue === null) &&
751
- (configValue === undefined || configValue === null)) {
752
- return true;
753
- }
754
- // Normalize booleans: treat undefined and false as equivalent
755
- if (typeof dbValue === "boolean" || typeof configValue === "boolean") {
756
- const boolMatch = Boolean(dbValue) === Boolean(configValue);
757
- if (!boolMatch) {
758
- differences.push(`${attr}: db=${dbValue} config=${configValue}`);
759
- }
760
- return boolMatch;
761
- }
762
- // For numeric comparisons, compare numbers if both are numeric-like
763
- if ((typeof dbValue === "number" ||
764
- (typeof dbValue === "string" &&
765
- dbValue !== "" &&
766
- !isNaN(Number(dbValue)))) &&
767
- (typeof configValue === "number" ||
768
- (typeof configValue === "string" &&
769
- configValue !== "" &&
770
- !isNaN(Number(configValue))))) {
771
- const numMatch = Number(dbValue) === Number(configValue);
772
- if (!numMatch) {
773
- differences.push(`${attr}: db=${dbValue} config=${configValue}`);
774
- }
775
- return numMatch;
776
- }
777
- // For array comparisons (e.g., enum elements), use order-independent equality
778
- if (Array.isArray(dbValue) && Array.isArray(configValue)) {
779
- const arrayMatch = dbValue.length === configValue.length &&
780
- dbValue.every((val) => configValue.includes(val));
781
- if (!arrayMatch) {
782
- differences.push(`${attr}: db=${JSON.stringify(dbValue)} config=${JSON.stringify(configValue)}`);
783
- }
784
- return arrayMatch;
785
- }
786
- const match = dbValue === configValue;
787
- if (!match) {
788
- differences.push(`${attr}: db=${JSON.stringify(dbValue)} config=${JSON.stringify(configValue)}`);
789
- }
790
- return match;
791
- }
792
- // If neither has the attribute, consider it the same
793
- if (!dbHasAttr && !configHasAttr) {
794
- return true;
795
- }
796
- // If one has the attribute and the other doesn't, check if it's undefined or null
797
- if (dbHasAttr && !configHasAttr) {
798
- const dbValue = normalizedDbAttr[attr];
799
- // Consider default-false booleans as equal to missing in config
800
- if (typeof dbValue === "boolean") {
801
- const match = dbValue === false; // missing in config equals false in db
802
- if (!match) {
803
- differences.push(`${attr}: db=${dbValue} config=<missing>`);
804
- }
805
- return match;
806
- }
807
- const match = dbValue === undefined || dbValue === null;
808
- if (!match) {
809
- differences.push(`${attr}: db=${JSON.stringify(dbValue)} config=<missing>`);
810
- }
811
- return match;
812
- }
813
- if (!dbHasAttr && configHasAttr) {
814
- const configValue = normalizedConfigAttr[attr];
815
- // Consider default-false booleans as equal to missing in db
816
- if (typeof configValue === "boolean") {
817
- const match = configValue === false; // missing in db equals false in config
818
- if (!match) {
819
- differences.push(`${attr}: db=<missing> config=${configValue}`);
820
- }
821
- return match;
822
- }
823
- const match = configValue === undefined || configValue === null;
824
- if (!match) {
825
- differences.push(`${attr}: db=<missing> config=${JSON.stringify(configValue)}`);
826
- }
827
- return match;
828
- }
829
- // If we reach here, the attributes are different
830
- differences.push(`${attr}: unexpected comparison state`);
831
- return false;
832
- });
833
- if (!result && differences.length > 0) {
834
- logger.debug(`Attribute mismatch detected for '${normalizedConfigAttr.key}'`, {
835
- differences,
836
- dbAttribute: normalizedDbAttr,
837
- configAttribute: normalizedConfigAttr,
838
- operation: "attributesSame",
839
- });
840
- }
841
- return result;
842
- };
843
- /**
844
- * Enhanced attribute creation with proper status monitoring and retry logic
845
- */
846
- export const createOrUpdateAttributeWithStatusCheck = async (db, dbId, collection, attribute, retryCount = 0, maxRetries = 5) => {
847
- try {
848
- // First, try to create/update the attribute using existing logic
849
- const result = await createOrUpdateAttribute(db, dbId, collection, attribute);
850
- // If the attribute was queued (relationship dependency unresolved),
851
- // skip status polling and retry logic — the queue will handle it later.
852
- if (result === "queued") {
853
- MessageFormatter.info(chalk.yellow(`⏭️ Deferred relationship attribute '${attribute.key}' — queued for later once dependencies are available`));
854
- return true;
855
- }
856
- // If collection creation failed, return false to indicate failure
857
- if (result === "error") {
858
- MessageFormatter.error(`Failed to create collection for attribute '${attribute.key}'`);
859
- return false;
860
- }
861
- // Now wait for the attribute to become available
862
- const success = await waitForAttributeAvailable(db, dbId, collection.$id, attribute.key, 60000, // 1 minute timeout
863
- retryCount, maxRetries);
864
- if (success) {
865
- return true;
866
- }
867
- // If not successful and we have retries left, delete specific attribute and try again
868
- if (retryCount < maxRetries) {
869
- MessageFormatter.info(chalk.yellow(`Attribute '${attribute.key}' failed/stuck, deleting and retrying...`));
870
- // Try to delete the specific stuck attribute instead of the entire collection
871
- try {
872
- if (isDatabaseAdapter(db)) {
873
- await db.deleteAttribute({
874
- databaseId: dbId,
875
- tableId: collection.$id,
876
- key: attribute.key,
877
- });
878
- }
879
- else {
880
- await db.deleteAttribute(dbId, collection.$id, attribute.key);
881
- }
882
- MessageFormatter.info(chalk.yellow(`Deleted stuck attribute '${attribute.key}', will retry creation`));
883
- // Wait a bit before retry
884
- await delay(3000);
885
- // Get fresh collection data
886
- const freshCollection = isDatabaseAdapter(db)
887
- ? (await db.getTable({ databaseId: dbId, tableId: collection.$id }))
888
- .data
889
- : await db.getCollection(dbId, collection.$id);
890
- // Retry with the same collection (attribute should be gone now)
891
- return await createOrUpdateAttributeWithStatusCheck(db, dbId, freshCollection, attribute, retryCount + 1, maxRetries);
892
- }
893
- catch (deleteError) {
894
- MessageFormatter.info(chalk.red(`Failed to delete stuck attribute '${attribute.key}': ${deleteError}`));
895
- // If attribute deletion fails, only then try collection recreation as last resort
896
- if (retryCount >= maxRetries - 1) {
897
- MessageFormatter.info(chalk.yellow(`Last resort: Recreating collection for attribute '${attribute.key}'`));
898
- // Get fresh collection data
899
- const freshCollection = isDatabaseAdapter(db)
900
- ? (await db.getTable({ databaseId: dbId, tableId: collection.$id }))
901
- .data
902
- : await db.getCollection(dbId, collection.$id);
903
- // Delete and recreate collection
904
- const newCollection = await deleteAndRecreateCollection(db, dbId, freshCollection, retryCount + 1);
905
- if (newCollection) {
906
- // Retry with the new collection
907
- return await createOrUpdateAttributeWithStatusCheck(db, dbId, newCollection, attribute, retryCount + 1, maxRetries);
908
- }
909
- }
910
- else {
911
- // Continue to next retry without collection recreation
912
- return await createOrUpdateAttributeWithStatusCheck(db, dbId, collection, attribute, retryCount + 1, maxRetries);
913
- }
914
- }
915
- }
916
- MessageFormatter.info(chalk.red(`❌ Failed to create attribute '${attribute.key}' after ${maxRetries + 1} attempts`));
917
- return false;
918
- }
919
- catch (error) {
920
- MessageFormatter.info(chalk.red(`Error creating attribute '${attribute.key}': ${error}`));
921
- if (retryCount < maxRetries) {
922
- MessageFormatter.info(chalk.yellow(`Retrying attribute '${attribute.key}' due to error...`));
923
- // Wait a bit before retry
924
- await delay(2000);
925
- return await createOrUpdateAttributeWithStatusCheck(db, dbId, collection, attribute, retryCount + 1, maxRetries);
926
- }
927
- return false;
928
- }
929
- };
930
- export const createOrUpdateAttribute = async (db, dbId, collection, attribute) => {
931
- let action = "create";
932
- let foundAttribute;
933
- const updateEnabled = true;
934
- let finalAttribute = attribute;
935
- try {
936
- const collectionAttr = collection.attributes.find((attr) => attr.key === attribute.key);
937
- foundAttribute = parseAttribute(collectionAttr);
938
- }
939
- catch (error) {
940
- foundAttribute = undefined;
941
- }
942
- // If attribute exists but type changed, delete it so we can recreate with new type
943
- if (foundAttribute &&
944
- foundAttribute.type !== attribute.type) {
945
- MessageFormatter.info(chalk.yellow(`Attribute '${attribute.key}' type changed from '${foundAttribute.type}' to '${attribute.type}'. Recreating attribute.`));
946
- try {
947
- if (isDatabaseAdapter(db)) {
948
- await db.deleteAttribute({
949
- databaseId: dbId,
950
- tableId: collection.$id,
951
- key: attribute.key
952
- });
953
- }
954
- else {
955
- await db.deleteAttribute(dbId, collection.$id, attribute.key);
956
- }
957
- // Remove from local collection metadata so downstream logic treats it as new
958
- collection.attributes = collection.attributes.filter((attr) => attr.key !== attribute.key);
959
- foundAttribute = undefined;
960
- }
961
- catch (deleteError) {
962
- MessageFormatter.error(`Failed to delete attribute '${attribute.key}' before recreation: ${deleteError}`);
963
- return "error";
964
- }
965
- }
966
- if (foundAttribute &&
967
- attributesSame(foundAttribute, attribute) &&
968
- updateEnabled) {
969
- // No need to do anything, they are the same
970
- return "processed";
971
- }
972
- else if (foundAttribute &&
973
- !attributesSame(foundAttribute, attribute) &&
974
- updateEnabled) {
975
- // MessageFormatter.info(
976
- // `Updating attribute with same key ${attribute.key} but different values`
977
- // );
978
- finalAttribute = {
979
- ...foundAttribute,
980
- ...attribute,
981
- };
982
- action = "update";
983
- }
984
- else if (!updateEnabled &&
985
- foundAttribute &&
986
- !attributesSame(foundAttribute, attribute)) {
987
- if (isDatabaseAdapter(db)) {
988
- await db.deleteAttribute({
989
- databaseId: dbId,
990
- tableId: collection.$id,
991
- key: attribute.key,
992
- });
993
- }
994
- else {
995
- await db.deleteAttribute(dbId, collection.$id, attribute.key);
996
- }
997
- MessageFormatter.info(`Deleted attribute: ${attribute.key} to recreate it because they diff (update disabled temporarily)`);
998
- return "processed";
999
- }
1000
- // Relationship attribute logic with adjustments
1001
- let collectionFoundViaRelatedCollection;
1002
- let relatedCollectionId;
1003
- if (finalAttribute.type === "relationship" &&
1004
- finalAttribute.relatedCollection) {
1005
- // First try treating relatedCollection as an ID directly
1006
- try {
1007
- const byIdCollection = isDatabaseAdapter(db)
1008
- ? (await db.getTable({
1009
- databaseId: dbId,
1010
- tableId: finalAttribute.relatedCollection,
1011
- })).data
1012
- : await db.getCollection(dbId, finalAttribute.relatedCollection);
1013
- collectionFoundViaRelatedCollection = byIdCollection;
1014
- relatedCollectionId = byIdCollection.$id;
1015
- // Cache by name for subsequent lookups
1016
- nameToIdMapping.set(byIdCollection.name, byIdCollection.$id);
1017
- }
1018
- catch (_) {
1019
- // Not an ID or not found — fall back to name-based resolution below
1020
- }
1021
- if (!collectionFoundViaRelatedCollection &&
1022
- nameToIdMapping.has(finalAttribute.relatedCollection)) {
1023
- relatedCollectionId = nameToIdMapping.get(finalAttribute.relatedCollection);
1024
- try {
1025
- collectionFoundViaRelatedCollection = isDatabaseAdapter(db)
1026
- ? (await db.getTable({
1027
- databaseId: dbId,
1028
- tableId: relatedCollectionId,
1029
- })).data
1030
- : await db.getCollection(dbId, relatedCollectionId);
1031
- }
1032
- catch (e) {
1033
- // MessageFormatter.info(
1034
- // `Collection not found: ${finalAttribute.relatedCollection} when nameToIdMapping was set`
1035
- // );
1036
- collectionFoundViaRelatedCollection = undefined;
1037
- }
1038
- }
1039
- else if (!collectionFoundViaRelatedCollection) {
1040
- const collectionsPulled = isDatabaseAdapter(db)
1041
- ? await db.listTables({
1042
- databaseId: dbId,
1043
- queries: [Query.equal("name", finalAttribute.relatedCollection)],
1044
- })
1045
- : await db.listCollections(dbId, [
1046
- Query.equal("name", finalAttribute.relatedCollection),
1047
- ]);
1048
- if (collectionsPulled.total && collectionsPulled.total > 0) {
1049
- collectionFoundViaRelatedCollection = isDatabaseAdapter(db)
1050
- ? collectionsPulled.tables?.[0]
1051
- : collectionsPulled.collections?.[0];
1052
- relatedCollectionId = collectionFoundViaRelatedCollection?.$id;
1053
- if (relatedCollectionId) {
1054
- nameToIdMapping.set(finalAttribute.relatedCollection, relatedCollectionId);
1055
- }
1056
- }
1057
- }
1058
- // ONLY queue relationship attributes that have actual unresolved dependencies
1059
- if (!(relatedCollectionId && collectionFoundViaRelatedCollection)) {
1060
- MessageFormatter.info(chalk.yellow(`⏳ Queueing relationship attribute '${finalAttribute.key}' - related collection '${finalAttribute.relatedCollection}' not found yet`));
1061
- enqueueOperation({
1062
- type: "attribute",
1063
- collectionId: collection.$id,
1064
- collection: collection,
1065
- attribute,
1066
- dependencies: [finalAttribute.relatedCollection],
1067
- });
1068
- return "queued";
1069
- }
1070
- }
1071
- finalAttribute = parseAttribute(finalAttribute);
1072
- // Ensure collection/table exists - create it if it doesn't
1073
- try {
1074
- await (isDatabaseAdapter(db)
1075
- ? db.getTable({ databaseId: dbId, tableId: collection.$id })
1076
- : db.getCollection(dbId, collection.$id));
1077
- }
1078
- catch (error) {
1079
- // Collection doesn't exist - create it
1080
- if (error.code === 404 ||
1081
- (error instanceof Error &&
1082
- (error.message.includes("collection_not_found") ||
1083
- error.message.includes("Collection with the requested ID could not be found")))) {
1084
- MessageFormatter.info(`Collection '${collection.name}' doesn't exist, creating it first...`);
1085
- try {
1086
- if (isDatabaseAdapter(db)) {
1087
- await db.createTable({
1088
- databaseId: dbId,
1089
- id: collection.$id,
1090
- name: collection.name,
1091
- permissions: collection.$permissions || [],
1092
- documentSecurity: collection.documentSecurity ?? false,
1093
- enabled: collection.enabled ?? true,
1094
- });
1095
- }
1096
- else {
1097
- await db.createCollection(dbId, collection.$id, collection.name, collection.$permissions || [], collection.documentSecurity ?? false, collection.enabled ?? true);
1098
- }
1099
- MessageFormatter.success(`Created collection '${collection.name}'`);
1100
- await delay(500); // Wait for collection to be ready
1101
- }
1102
- catch (createError) {
1103
- MessageFormatter.error(`Failed to create collection '${collection.name}'`, createError instanceof Error
1104
- ? createError
1105
- : new Error(String(createError)));
1106
- return "error";
1107
- }
1108
- }
1109
- else {
1110
- // Other error - re-throw
1111
- throw error;
1112
- }
1113
- }
1114
- // Use adapter-based attribute creation/update
1115
- if (action === "create") {
1116
- await tryAwaitWithRetry(async () => await createAttributeViaAdapter(db, dbId, collection.$id, finalAttribute));
1117
- }
1118
- else {
1119
- console.log(`Updating attribute '${finalAttribute.key}'...`);
1120
- if (finalAttribute.type === "double" || finalAttribute.type === "integer") {
1121
- console.log("finalAttribute:", finalAttribute);
1122
- }
1123
- await tryAwaitWithRetry(async () => await updateAttributeViaAdapter(db, dbId, collection.$id, finalAttribute));
1124
- }
1125
- return "processed";
1126
- };
1127
- /**
1128
- * Enhanced collection attribute creation with proper status monitoring
1129
- */
1130
- export const createUpdateCollectionAttributesWithStatusCheck = async (db, dbId, collection, attributes) => {
1131
- const existingAttributes = collection.attributes.map((attr) => parseAttribute(attr)) || [];
1132
- const attributesToRemove = existingAttributes.filter((attr) => !attributes.some((a) => a.key === attr.key));
1133
- const indexesToRemove = collection.indexes.filter((index) => attributesToRemove.some((attr) => index.attributes.includes(attr.key)));
1134
- // Handle attribute removal first
1135
- if (attributesToRemove.length > 0) {
1136
- if (indexesToRemove.length > 0) {
1137
- MessageFormatter.info(chalk.red(`Removing indexes as they rely on an attribute that is being removed: ${indexesToRemove
1138
- .map((index) => index.key)
1139
- .join(", ")}`));
1140
- for (const index of indexesToRemove) {
1141
- await tryAwaitWithRetry(async () => {
1142
- if (isDatabaseAdapter(db)) {
1143
- await db.deleteIndex({
1144
- databaseId: dbId,
1145
- tableId: collection.$id,
1146
- key: index.key,
1147
- });
1148
- }
1149
- else {
1150
- await db.deleteIndex(dbId, collection.$id, index.key);
1151
- }
1152
- });
1153
- await delay(500); // Longer delay for deletions
1154
- }
1155
- }
1156
- for (const attr of attributesToRemove) {
1157
- MessageFormatter.info(chalk.red(`Removing attribute: ${attr.key} as it is no longer in the collection`));
1158
- await tryAwaitWithRetry(async () => {
1159
- if (isDatabaseAdapter(db)) {
1160
- await db.deleteAttribute({
1161
- databaseId: dbId,
1162
- tableId: collection.$id,
1163
- key: attr.key,
1164
- });
1165
- }
1166
- else {
1167
- await db.deleteAttribute(dbId, collection.$id, attr.key);
1168
- }
1169
- });
1170
- await delay(500); // Longer delay for deletions
1171
- }
1172
- }
1173
- // First, get fresh collection data and determine which attributes actually need processing
1174
- let currentCollection = collection;
1175
- try {
1176
- currentCollection = isDatabaseAdapter(db)
1177
- ? (await db.getTable({ databaseId: dbId, tableId: collection.$id })).data
1178
- : await db.getCollection(dbId, collection.$id);
1179
- }
1180
- catch (error) {
1181
- MessageFormatter.info(chalk.yellow(`Warning: Could not refresh collection data: ${error}`));
1182
- }
1183
- const existingAttributesMap = new Map();
1184
- try {
1185
- const parsedAttributes = currentCollection.attributes.map((attr) => parseAttribute(attr));
1186
- parsedAttributes.forEach((attr) => existingAttributesMap.set(attr.key, attr));
1187
- }
1188
- catch (error) {
1189
- MessageFormatter.info(chalk.yellow(`Warning: Could not parse existing attributes: ${error}`));
1190
- }
1191
- // Filter to only attributes that need processing (new, changed, or not yet processed)
1192
- const attributesToProcess = attributes.filter((attribute) => {
1193
- // Skip if already processed in this session
1194
- if (isAttributeProcessed(dbId, currentCollection.$id, attribute.key)) {
1195
- return false;
1196
- }
1197
- const existing = existingAttributesMap.get(attribute.key);
1198
- if (!existing) {
1199
- MessageFormatter.info(`➕ ${attribute.key}`);
1200
- return true;
1201
- }
1202
- const needsUpdate = !attributesSame(existing, parseAttribute(attribute));
1203
- if (needsUpdate) {
1204
- MessageFormatter.info(`🔄 ${attribute.key}`);
1205
- }
1206
- else {
1207
- MessageFormatter.info(chalk.gray(`✅ ${attribute.key}`));
1208
- }
1209
- return needsUpdate;
1210
- });
1211
- if (attributesToProcess.length === 0) {
1212
- return true;
1213
- }
1214
- let remainingAttributes = [...attributesToProcess];
1215
- let overallRetryCount = 0;
1216
- const maxOverallRetries = 3;
1217
- while (remainingAttributes.length > 0 &&
1218
- overallRetryCount < maxOverallRetries) {
1219
- const attributesToProcessThisRound = [...remainingAttributes];
1220
- remainingAttributes = []; // Reset for next iteration
1221
- for (const attribute of attributesToProcessThisRound) {
1222
- const success = await createOrUpdateAttributeWithStatusCheck(db, dbId, currentCollection, attribute);
1223
- if (success) {
1224
- // Mark this specific attribute as processed
1225
- markAttributeProcessed(dbId, currentCollection.$id, attribute.key);
1226
- // Get updated collection data for next iteration
1227
- try {
1228
- currentCollection = isDatabaseAdapter(db)
1229
- ? (await db.getTable({ databaseId: dbId, tableId: collection.$id })).data
1230
- : await db.getCollection({
1231
- databaseId: dbId,
1232
- collectionId: collection.$id,
1233
- });
1234
- }
1235
- catch (error) {
1236
- MessageFormatter.info(chalk.yellow(`Warning: Could not refresh collection data: ${error}`));
1237
- }
1238
- // Add delay between successful attributes
1239
- await delay(1000);
1240
- }
1241
- else {
1242
- MessageFormatter.info(chalk.red(`❌ ${attribute.key}`));
1243
- remainingAttributes.push(attribute); // Add back to retry list
1244
- }
1245
- }
1246
- if (remainingAttributes.length === 0) {
1247
- return true;
1248
- }
1249
- overallRetryCount++;
1250
- if (overallRetryCount < maxOverallRetries) {
1251
- MessageFormatter.info(chalk.yellow(`⏳ Retrying ${remainingAttributes.length} failed attributes...`));
1252
- await delay(5000);
1253
- // Refresh collection data before retry
1254
- try {
1255
- currentCollection = isDatabaseAdapter(db)
1256
- ? (await db.getTable({ databaseId: dbId, tableId: collection.$id }))
1257
- .data
1258
- : await db.getCollection(dbId, collection.$id);
1259
- }
1260
- catch (error) {
1261
- // Silently continue if refresh fails
1262
- }
1263
- }
1264
- }
1265
- // If we get here, some attributes still failed after all retries
1266
- if (attributesToProcess.length > 0) {
1267
- MessageFormatter.info(chalk.red(`\n❌ Failed to create ${attributesToProcess.length} attributes after ${maxOverallRetries} attempts: ${attributesToProcess
1268
- .map((a) => a.key)
1269
- .join(", ")}`));
1270
- MessageFormatter.info(chalk.red(`This may indicate a fundamental issue with the attribute definitions or Appwrite instance`));
1271
- return false;
1272
- }
1273
- MessageFormatter.info(chalk.green(`\n✅ Successfully created all ${attributes.length} attributes for collection: ${collection.name}`));
1274
- return true;
1275
- };
1276
- export const createUpdateCollectionAttributes = async (db, dbId, collection, attributes) => {
1277
- MessageFormatter.info(chalk.green(`Creating/Updating attributes for collection: ${collection.name}`));
1278
- const existingAttributes = collection.attributes.map((attr) => parseAttribute(attr)) || [];
1279
- const attributesToRemove = existingAttributes.filter((attr) => !attributes.some((a) => a.key === attr.key));
1280
- const indexesToRemove = collection.indexes.filter((index) => attributesToRemove.some((attr) => index.attributes.includes(attr.key)));
1281
- if (attributesToRemove.length > 0) {
1282
- if (indexesToRemove.length > 0) {
1283
- MessageFormatter.info(chalk.red(`Removing indexes as they rely on an attribute that is being removed: ${indexesToRemove
1284
- .map((index) => index.key)
1285
- .join(", ")}`));
1286
- for (const index of indexesToRemove) {
1287
- await tryAwaitWithRetry(async () => {
1288
- if (isDatabaseAdapter(db)) {
1289
- await db.deleteIndex({
1290
- databaseId: dbId,
1291
- tableId: collection.$id,
1292
- key: index.key,
1293
- });
1294
- }
1295
- else {
1296
- await db.deleteIndex(dbId, collection.$id, index.key);
1297
- }
1298
- });
1299
- await delay(100);
1300
- }
1301
- }
1302
- for (const attr of attributesToRemove) {
1303
- MessageFormatter.info(chalk.red(`Removing attribute: ${attr.key} as it is no longer in the collection`));
1304
- await tryAwaitWithRetry(async () => {
1305
- if (isDatabaseAdapter(db)) {
1306
- await db.deleteAttribute({
1307
- databaseId: dbId,
1308
- tableId: collection.$id,
1309
- key: attr.key,
1310
- });
1311
- }
1312
- else {
1313
- await db.deleteAttribute(dbId, collection.$id, attr.key);
1314
- }
1315
- });
1316
- await delay(50);
1317
- }
1318
- }
1319
- const batchSize = 3;
1320
- for (let i = 0; i < attributes.length; i += batchSize) {
1321
- const batch = attributes.slice(i, i + batchSize);
1322
- const attributePromises = batch.map((attribute) => tryAwaitWithRetry(async () => await createOrUpdateAttribute(db, dbId, collection, attribute)));
1323
- const results = await Promise.allSettled(attributePromises);
1324
- results.forEach((result) => {
1325
- if (result.status === "rejected") {
1326
- MessageFormatter.error("An attribute promise was rejected:", result.reason);
1327
- }
1328
- });
1329
- // Add delay after each batch
1330
- await delay(200);
1331
- }
1332
- MessageFormatter.info(`Finished creating/updating attributes for collection: ${collection.name}`);
1333
- };