appwrite-utils-cli 1.9.6 → 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 (426) 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 -44
  23. package/src/collections/indexes.ts +350 -352
  24. package/src/collections/methods.ts +714 -815
  25. package/src/collections/tableOperations.ts +57 -21
  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 +409 -0
  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 -405
  129. package/dist/adapters/DatabaseAdapter.d.ts +0 -242
  130. package/dist/adapters/DatabaseAdapter.js +0 -50
  131. package/dist/adapters/LegacyAdapter.d.ts +0 -50
  132. package/dist/adapters/LegacyAdapter.js +0 -612
  133. package/dist/adapters/TablesDBAdapter.d.ts +0 -45
  134. package/dist/adapters/TablesDBAdapter.js +0 -596
  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 -1364
  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 -734
  167. package/dist/collections/tableOperations.d.ts +0 -86
  168. package/dist/collections/tableOperations.js +0 -434
  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 -625
  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/indexManager.d.ts +0 -24
  298. package/dist/shared/indexManager.js +0 -151
  299. package/dist/shared/jsonSchemaGenerator.d.ts +0 -50
  300. package/dist/shared/jsonSchemaGenerator.js +0 -290
  301. package/dist/shared/logging.d.ts +0 -61
  302. package/dist/shared/logging.js +0 -116
  303. package/dist/shared/messageFormatter.d.ts +0 -39
  304. package/dist/shared/messageFormatter.js +0 -162
  305. package/dist/shared/migrationHelpers.d.ts +0 -61
  306. package/dist/shared/migrationHelpers.js +0 -145
  307. package/dist/shared/operationLogger.d.ts +0 -10
  308. package/dist/shared/operationLogger.js +0 -12
  309. package/dist/shared/operationQueue.d.ts +0 -40
  310. package/dist/shared/operationQueue.js +0 -311
  311. package/dist/shared/operationsTable.d.ts +0 -26
  312. package/dist/shared/operationsTable.js +0 -286
  313. package/dist/shared/operationsTableSchema.d.ts +0 -48
  314. package/dist/shared/operationsTableSchema.js +0 -35
  315. package/dist/shared/progressManager.d.ts +0 -62
  316. package/dist/shared/progressManager.js +0 -215
  317. package/dist/shared/pydanticModelGenerator.d.ts +0 -17
  318. package/dist/shared/pydanticModelGenerator.js +0 -615
  319. package/dist/shared/relationshipExtractor.d.ts +0 -56
  320. package/dist/shared/relationshipExtractor.js +0 -138
  321. package/dist/shared/schemaGenerator.d.ts +0 -40
  322. package/dist/shared/schemaGenerator.js +0 -556
  323. package/dist/shared/selectionDialogs.d.ts +0 -214
  324. package/dist/shared/selectionDialogs.js +0 -544
  325. package/dist/storage/backupCompression.d.ts +0 -20
  326. package/dist/storage/backupCompression.js +0 -67
  327. package/dist/storage/methods.d.ts +0 -32
  328. package/dist/storage/methods.js +0 -472
  329. package/dist/storage/schemas.d.ts +0 -842
  330. package/dist/storage/schemas.js +0 -175
  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 -510
  378. package/src/adapters/DatabaseAdapter.ts +0 -318
  379. package/src/adapters/LegacyAdapter.ts +0 -841
  380. package/src/adapters/TablesDBAdapter.ts +0 -815
  381. package/src/config/ConfigManager.ts +0 -817
  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/indexManager.ts +0 -254
  405. package/src/shared/jsonSchemaGenerator.ts +0 -383
  406. package/src/shared/logging.ts +0 -149
  407. package/src/shared/messageFormatter.ts +0 -208
  408. package/src/shared/pydanticModelGenerator.ts +0 -618
  409. package/src/shared/schemaGenerator.ts +0 -644
  410. package/src/utils/ClientFactory.ts +0 -240
  411. package/src/utils/configDiscovery.ts +0 -557
  412. package/src/utils/constantsGenerator.ts +0 -369
  413. package/src/utils/dataConverters.ts +0 -159
  414. package/src/utils/directoryUtils.ts +0 -61
  415. package/src/utils/getClientFromConfig.ts +0 -257
  416. package/src/utils/helperFunctions.ts +0 -228
  417. package/src/utils/pathResolvers.ts +0 -81
  418. package/src/utils/projectConfig.ts +0 -340
  419. package/src/utils/retryFailedPromises.ts +0 -29
  420. package/src/utils/sessionAuth.ts +0 -230
  421. package/src/utils/typeGuards.ts +0 -65
  422. package/src/utils/validationRules.ts +0 -88
  423. package/src/utils/versionDetection.ts +0 -292
  424. package/src/utils/yamlConverter.ts +0 -542
  425. package/src/utils/yamlLoader.ts +0 -371
  426. package/tmp-sync-test/.appwrite/collections/TestCollection.yaml +0 -7
@@ -1,1364 +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
- console.log(`DEBUG updateLegacyAttribute before normalizeMinMaxValues:`, {
478
- key: attribute.key,
479
- type: attribute.type,
480
- min: attribute.min,
481
- max: attribute.max
482
- });
483
- const { min: normalizedMin, max: normalizedMax } = normalizeMinMaxValues(attribute);
484
- switch (attribute.type) {
485
- case "string":
486
- await db.updateStringAttribute(dbId, collectionId, attribute.key, attribute.required || false, !attribute.required && attribute.xdefault !== undefined
487
- ? attribute.xdefault
488
- : null, attribute.size);
489
- break;
490
- case "integer":
491
- await db.updateIntegerAttribute(dbId, collectionId, attribute.key, attribute.required || false, !attribute.required && attribute.xdefault !== undefined
492
- ? attribute.xdefault
493
- : null, normalizedMin !== undefined
494
- ? parseInt(String(normalizedMin))
495
- : undefined, normalizedMax !== undefined
496
- ? parseInt(String(normalizedMax))
497
- : undefined);
498
- break;
499
- case "double":
500
- case "float":
501
- const minParam = normalizedMin !== undefined ? Number(normalizedMin) : undefined;
502
- const maxParam = normalizedMax !== undefined ? Number(normalizedMax) : undefined;
503
- await db.updateFloatAttribute(dbId, collectionId, attribute.key, attribute.required || false, minParam, maxParam, !attribute.required && attribute.xdefault !== undefined
504
- ? attribute.xdefault
505
- : null);
506
- break;
507
- case "boolean":
508
- await db.updateBooleanAttribute(dbId, collectionId, attribute.key, attribute.required || false, !attribute.required && attribute.xdefault !== undefined
509
- ? attribute.xdefault
510
- : null);
511
- break;
512
- case "datetime":
513
- await db.updateDatetimeAttribute(dbId, collectionId, attribute.key, attribute.required || false, !attribute.required && attribute.xdefault !== undefined
514
- ? attribute.xdefault
515
- : null);
516
- break;
517
- case "email":
518
- await db.updateEmailAttribute(dbId, collectionId, attribute.key, attribute.required || false, !attribute.required && attribute.xdefault !== undefined
519
- ? attribute.xdefault
520
- : null);
521
- break;
522
- case "ip":
523
- await db.updateIpAttribute(dbId, collectionId, attribute.key, attribute.required || false, !attribute.required && attribute.xdefault !== undefined
524
- ? attribute.xdefault
525
- : null);
526
- break;
527
- case "url":
528
- await db.updateUrlAttribute(dbId, collectionId, attribute.key, attribute.required || false, !attribute.required && attribute.xdefault !== undefined
529
- ? attribute.xdefault
530
- : null);
531
- break;
532
- case "enum":
533
- await db.updateEnumAttribute(dbId, collectionId, attribute.key, attribute.elements || [], attribute.required || false, !attribute.required && attribute.xdefault !== undefined
534
- ? attribute.xdefault
535
- : null);
536
- break;
537
- case "relationship":
538
- await db.updateRelationshipAttribute(dbId, collectionId, attribute.key, attribute.onDelete);
539
- break;
540
- default:
541
- throw new Error(`Unsupported attribute type for update: ${attribute.type}`);
542
- }
543
- };
544
- /**
545
- * Wait for attribute to become available, with retry logic for stuck attributes and exponential backoff
546
- */
547
- const waitForAttributeAvailable = async (db, dbId, collectionId, attributeKey, maxWaitTime = 60000, // 1 minute
548
- retryCount = 0, maxRetries = 5) => {
549
- const startTime = Date.now();
550
- let checkInterval = 2000; // Start with 2 seconds
551
- logger.info(`Waiting for attribute '${attributeKey}' to become available`, {
552
- dbId,
553
- collectionId,
554
- maxWaitTime,
555
- retryCount,
556
- maxRetries,
557
- operation: "waitForAttributeAvailable",
558
- });
559
- // Calculate exponential backoff: 2s, 4s, 8s, 16s, 30s (capped at 30s)
560
- if (retryCount > 0) {
561
- const exponentialDelay = calculateExponentialBackoff(retryCount);
562
- await delay(exponentialDelay);
563
- }
564
- while (Date.now() - startTime < maxWaitTime) {
565
- try {
566
- const collection = isDatabaseAdapter(db)
567
- ? (await db.getTable({ databaseId: dbId, tableId: collectionId })).data
568
- : await db.getCollection(dbId, collectionId);
569
- const attribute = collection.attributes.find((attr) => attr.key === attributeKey);
570
- if (!attribute) {
571
- MessageFormatter.error(`Attribute '${attributeKey}' not found`);
572
- return false;
573
- }
574
- const statusInfo = {
575
- attributeKey,
576
- status: attribute.status,
577
- error: attribute.error,
578
- dbId,
579
- collectionId,
580
- waitTime: Date.now() - startTime,
581
- operation: "waitForAttributeAvailable",
582
- };
583
- switch (attribute.status) {
584
- case "available":
585
- logger.info(`Attribute '${attributeKey}' became available`, statusInfo);
586
- return true;
587
- case "failed":
588
- logger.error(`Attribute '${attributeKey}' failed`, statusInfo);
589
- return false;
590
- case "stuck":
591
- logger.warn(`Attribute '${attributeKey}' is stuck`, statusInfo);
592
- return false;
593
- case "processing":
594
- // Continue waiting
595
- logger.debug(`Attribute '${attributeKey}' still processing`, statusInfo);
596
- break;
597
- case "deleting":
598
- MessageFormatter.info(chalk.yellow(`Attribute '${attributeKey}' is being deleted`));
599
- logger.warn(`Attribute '${attributeKey}' is being deleted`, statusInfo);
600
- break;
601
- default:
602
- MessageFormatter.info(chalk.yellow(`Unknown status '${attribute.status}' for attribute '${attributeKey}'`));
603
- logger.warn(`Unknown status for attribute '${attributeKey}'`, statusInfo);
604
- break;
605
- }
606
- await delay(checkInterval);
607
- }
608
- catch (error) {
609
- const errorMessage = error instanceof Error ? error.message : String(error);
610
- MessageFormatter.error(`Error checking attribute status: ${errorMessage}`);
611
- logger.error("Error checking attribute status", {
612
- attributeKey,
613
- dbId,
614
- collectionId,
615
- error: errorMessage,
616
- waitTime: Date.now() - startTime,
617
- operation: "waitForAttributeAvailable",
618
- });
619
- return false;
620
- }
621
- }
622
- // Timeout reached
623
- MessageFormatter.info(chalk.yellow(`⏰ Timeout waiting for attribute '${attributeKey}' (${maxWaitTime}ms)`));
624
- // If we have retries left and this isn't the last retry, try recreating
625
- if (retryCount < maxRetries) {
626
- MessageFormatter.info(chalk.yellow(`🔄 Retrying attribute creation (attempt ${retryCount + 1}/${maxRetries})`));
627
- return false; // Signal that we need to retry
628
- }
629
- return false;
630
- };
631
- /**
632
- * Wait for all attributes in a collection to become available
633
- */
634
- const waitForAllAttributesAvailable = async (db, dbId, collectionId, attributeKeys, maxWaitTime = 60000) => {
635
- MessageFormatter.info(chalk.blue(`Waiting for ${attributeKeys.length} attributes to become available...`));
636
- const failedAttributes = [];
637
- for (const attributeKey of attributeKeys) {
638
- const success = await waitForAttributeAvailable(db, dbId, collectionId, attributeKey, maxWaitTime);
639
- if (!success) {
640
- failedAttributes.push(attributeKey);
641
- }
642
- }
643
- return failedAttributes;
644
- };
645
- /**
646
- * Delete collection and recreate with retry logic
647
- */
648
- const deleteAndRecreateCollection = async (db, dbId, collection, retryCount) => {
649
- try {
650
- MessageFormatter.info(chalk.yellow(`🗑️ Deleting collection '${collection.name}' for retry ${retryCount}`));
651
- // Delete the collection
652
- if (isDatabaseAdapter(db)) {
653
- await db.deleteTable({ databaseId: dbId, tableId: collection.$id });
654
- }
655
- else {
656
- await db.deleteCollection(dbId, collection.$id);
657
- }
658
- MessageFormatter.warning(`Deleted collection '${collection.name}'`);
659
- // Wait a bit before recreating
660
- await delay(2000);
661
- // Recreate the collection
662
- MessageFormatter.info(`🔄 Recreating collection '${collection.name}'`);
663
- const newCollection = isDatabaseAdapter(db)
664
- ? (await db.createTable({
665
- databaseId: dbId,
666
- id: collection.$id,
667
- name: collection.name,
668
- permissions: collection.$permissions,
669
- documentSecurity: collection.documentSecurity,
670
- enabled: collection.enabled,
671
- })).data
672
- : await db.createCollection(dbId, collection.$id, collection.name, collection.$permissions, collection.documentSecurity, collection.enabled);
673
- MessageFormatter.success(`✅ Recreated collection '${collection.name}'`);
674
- return newCollection;
675
- }
676
- catch (error) {
677
- MessageFormatter.info(chalk.red(`Failed to delete/recreate collection '${collection.name}': ${error}`));
678
- return null;
679
- }
680
- };
681
- /**
682
- * Get the fields that should be compared for a specific attribute type
683
- * Only returns fields that are valid for the given type to avoid false positives
684
- */
685
- const getComparableFields = (type) => {
686
- const baseFields = ["key", "type", "array", "required", "xdefault"];
687
- switch (type) {
688
- case "string":
689
- return [...baseFields, "size", "encrypt"];
690
- case "integer":
691
- case "double":
692
- case "float":
693
- return [...baseFields, "min", "max"];
694
- case "enum":
695
- return [...baseFields, "elements"];
696
- case "relationship":
697
- return [
698
- ...baseFields,
699
- "relationType",
700
- "twoWay",
701
- "twoWayKey",
702
- "onDelete",
703
- "relatedCollection",
704
- ];
705
- case "boolean":
706
- case "datetime":
707
- case "email":
708
- case "ip":
709
- case "url":
710
- return baseFields;
711
- default:
712
- // Fallback to all fields for unknown types
713
- return [
714
- "key",
715
- "type",
716
- "array",
717
- "encrypt",
718
- "required",
719
- "size",
720
- "min",
721
- "max",
722
- "xdefault",
723
- "elements",
724
- "relationType",
725
- "twoWay",
726
- "twoWayKey",
727
- "onDelete",
728
- "relatedCollection",
729
- ];
730
- }
731
- };
732
- const attributesSame = (databaseAttribute, configAttribute) => {
733
- // Normalize both attributes for comparison (handle extreme database values)
734
- const normalizedDbAttr = normalizeAttributeForComparison(databaseAttribute);
735
- const normalizedConfigAttr = normalizeAttributeForComparison(configAttribute);
736
- // Use type-specific field list to avoid false positives from irrelevant fields
737
- const attributesToCheck = getComparableFields(normalizedConfigAttr.type);
738
- const fieldsToCheck = attributesToCheck.filter((attr) => {
739
- if (attr !== "xdefault") {
740
- return true;
741
- }
742
- const dbRequired = Boolean(normalizedDbAttr.required);
743
- const configRequired = Boolean(normalizedConfigAttr.required);
744
- return !(dbRequired || configRequired);
745
- });
746
- const differences = [];
747
- const result = fieldsToCheck.every((attr) => {
748
- // Check if both objects have the attribute
749
- const dbHasAttr = attr in normalizedDbAttr;
750
- const configHasAttr = attr in normalizedConfigAttr;
751
- // If both have the attribute, compare values
752
- if (dbHasAttr && configHasAttr) {
753
- const dbValue = normalizedDbAttr[attr];
754
- const configValue = normalizedConfigAttr[attr];
755
- // Consider undefined and null as equivalent
756
- if ((dbValue === undefined || dbValue === null) &&
757
- (configValue === undefined || configValue === null)) {
758
- return true;
759
- }
760
- // Normalize booleans: treat undefined and false as equivalent
761
- if (typeof dbValue === "boolean" || typeof configValue === "boolean") {
762
- const boolMatch = Boolean(dbValue) === Boolean(configValue);
763
- if (!boolMatch) {
764
- differences.push(`${attr}: db=${dbValue} config=${configValue}`);
765
- }
766
- return boolMatch;
767
- }
768
- // For numeric comparisons, compare numbers if both are numeric-like
769
- if ((typeof dbValue === "number" ||
770
- (typeof dbValue === "string" &&
771
- dbValue !== "" &&
772
- !isNaN(Number(dbValue)))) &&
773
- (typeof configValue === "number" ||
774
- (typeof configValue === "string" &&
775
- configValue !== "" &&
776
- !isNaN(Number(configValue))))) {
777
- const numMatch = Number(dbValue) === Number(configValue);
778
- if (!numMatch) {
779
- differences.push(`${attr}: db=${dbValue} config=${configValue}`);
780
- }
781
- return numMatch;
782
- }
783
- // For array comparisons (e.g., enum elements), use order-independent equality
784
- if (Array.isArray(dbValue) && Array.isArray(configValue)) {
785
- const arrayMatch = dbValue.length === configValue.length &&
786
- dbValue.every((val) => configValue.includes(val));
787
- if (!arrayMatch) {
788
- differences.push(`${attr}: db=${JSON.stringify(dbValue)} config=${JSON.stringify(configValue)}`);
789
- }
790
- return arrayMatch;
791
- }
792
- const match = dbValue === configValue;
793
- if (!match) {
794
- differences.push(`${attr}: db=${JSON.stringify(dbValue)} config=${JSON.stringify(configValue)}`);
795
- }
796
- return match;
797
- }
798
- // If neither has the attribute, consider it the same
799
- if (!dbHasAttr && !configHasAttr) {
800
- return true;
801
- }
802
- // If one has the attribute and the other doesn't, check if it's undefined or null
803
- if (dbHasAttr && !configHasAttr) {
804
- const dbValue = normalizedDbAttr[attr];
805
- // Consider default-false booleans as equal to missing in config
806
- if (typeof dbValue === "boolean") {
807
- const match = dbValue === false; // missing in config equals false in db
808
- if (!match) {
809
- differences.push(`${attr}: db=${dbValue} config=<missing>`);
810
- }
811
- return match;
812
- }
813
- const match = dbValue === undefined || dbValue === null;
814
- if (!match) {
815
- differences.push(`${attr}: db=${JSON.stringify(dbValue)} config=<missing>`);
816
- }
817
- return match;
818
- }
819
- if (!dbHasAttr && configHasAttr) {
820
- const configValue = normalizedConfigAttr[attr];
821
- // Consider default-false booleans as equal to missing in db
822
- if (typeof configValue === "boolean") {
823
- const match = configValue === false; // missing in db equals false in config
824
- if (!match) {
825
- differences.push(`${attr}: db=<missing> config=${configValue}`);
826
- }
827
- return match;
828
- }
829
- const match = configValue === undefined || configValue === null;
830
- if (!match) {
831
- differences.push(`${attr}: db=<missing> config=${JSON.stringify(configValue)}`);
832
- }
833
- return match;
834
- }
835
- // If we reach here, the attributes are different
836
- differences.push(`${attr}: unexpected comparison state`);
837
- return false;
838
- });
839
- if (!result && differences.length > 0) {
840
- logger.debug(`Attribute mismatch detected for '${normalizedConfigAttr.key}'`, {
841
- differences,
842
- dbAttribute: normalizedDbAttr,
843
- configAttribute: normalizedConfigAttr,
844
- operation: "attributesSame",
845
- });
846
- }
847
- return result;
848
- };
849
- /**
850
- * Enhanced attribute creation with proper status monitoring and retry logic
851
- */
852
- export const createOrUpdateAttributeWithStatusCheck = async (db, dbId, collection, attribute, retryCount = 0, maxRetries = 5) => {
853
- try {
854
- // First, try to create/update the attribute using existing logic
855
- const result = await createOrUpdateAttribute(db, dbId, collection, attribute);
856
- // If the attribute was queued (relationship dependency unresolved),
857
- // skip status polling and retry logic — the queue will handle it later.
858
- if (result === "queued") {
859
- MessageFormatter.info(chalk.yellow(`⏭️ Deferred relationship attribute '${attribute.key}' — queued for later once dependencies are available`));
860
- return true;
861
- }
862
- // If collection creation failed, return false to indicate failure
863
- if (result === "error") {
864
- MessageFormatter.error(`Failed to create collection for attribute '${attribute.key}'`);
865
- return false;
866
- }
867
- // Now wait for the attribute to become available
868
- const success = await waitForAttributeAvailable(db, dbId, collection.$id, attribute.key, 60000, // 1 minute timeout
869
- retryCount, maxRetries);
870
- if (success) {
871
- return true;
872
- }
873
- // If not successful and we have retries left, delete specific attribute and try again
874
- if (retryCount < maxRetries) {
875
- MessageFormatter.info(chalk.yellow(`Attribute '${attribute.key}' failed/stuck, deleting and retrying...`));
876
- // Try to delete the specific stuck attribute instead of the entire collection
877
- try {
878
- if (isDatabaseAdapter(db)) {
879
- await db.deleteAttribute({
880
- databaseId: dbId,
881
- tableId: collection.$id,
882
- key: attribute.key,
883
- });
884
- }
885
- else {
886
- await db.deleteAttribute(dbId, collection.$id, attribute.key);
887
- }
888
- MessageFormatter.info(chalk.yellow(`Deleted stuck attribute '${attribute.key}', will retry creation`));
889
- // Wait a bit before retry
890
- await delay(3000);
891
- // Get fresh collection data
892
- const freshCollection = isDatabaseAdapter(db)
893
- ? (await db.getTable({ databaseId: dbId, tableId: collection.$id }))
894
- .data
895
- : await db.getCollection(dbId, collection.$id);
896
- // Retry with the same collection (attribute should be gone now)
897
- return await createOrUpdateAttributeWithStatusCheck(db, dbId, freshCollection, attribute, retryCount + 1, maxRetries);
898
- }
899
- catch (deleteError) {
900
- MessageFormatter.info(chalk.red(`Failed to delete stuck attribute '${attribute.key}': ${deleteError}`));
901
- // If attribute deletion fails, only then try collection recreation as last resort
902
- if (retryCount >= maxRetries - 1) {
903
- MessageFormatter.info(chalk.yellow(`Last resort: Recreating collection for attribute '${attribute.key}'`));
904
- // Get fresh collection data
905
- const freshCollection = isDatabaseAdapter(db)
906
- ? (await db.getTable({ databaseId: dbId, tableId: collection.$id }))
907
- .data
908
- : await db.getCollection(dbId, collection.$id);
909
- // Delete and recreate collection
910
- const newCollection = await deleteAndRecreateCollection(db, dbId, freshCollection, retryCount + 1);
911
- if (newCollection) {
912
- // Retry with the new collection
913
- return await createOrUpdateAttributeWithStatusCheck(db, dbId, newCollection, attribute, retryCount + 1, maxRetries);
914
- }
915
- }
916
- else {
917
- // Continue to next retry without collection recreation
918
- return await createOrUpdateAttributeWithStatusCheck(db, dbId, collection, attribute, retryCount + 1, maxRetries);
919
- }
920
- }
921
- }
922
- MessageFormatter.info(chalk.red(`❌ Failed to create attribute '${attribute.key}' after ${maxRetries + 1} attempts`));
923
- return false;
924
- }
925
- catch (error) {
926
- MessageFormatter.info(chalk.red(`Error creating attribute '${attribute.key}': ${error}`));
927
- if (retryCount < maxRetries) {
928
- MessageFormatter.info(chalk.yellow(`Retrying attribute '${attribute.key}' due to error...`));
929
- // Wait a bit before retry
930
- await delay(2000);
931
- return await createOrUpdateAttributeWithStatusCheck(db, dbId, collection, attribute, retryCount + 1, maxRetries);
932
- }
933
- return false;
934
- }
935
- };
936
- export const createOrUpdateAttribute = async (db, dbId, collection, attribute) => {
937
- let action = "create";
938
- let foundAttribute;
939
- const updateEnabled = true;
940
- let finalAttribute = attribute;
941
- try {
942
- const collectionAttr = collection.attributes.find((attr) => attr.key === attribute.key);
943
- foundAttribute = parseAttribute(collectionAttr);
944
- }
945
- catch (error) {
946
- foundAttribute = undefined;
947
- }
948
- // If attribute exists but type changed, delete it so we can recreate with new type
949
- if (foundAttribute &&
950
- foundAttribute.type !== attribute.type) {
951
- MessageFormatter.info(chalk.yellow(`Attribute '${attribute.key}' type changed from '${foundAttribute.type}' to '${attribute.type}'. Recreating attribute.`));
952
- try {
953
- if (isDatabaseAdapter(db)) {
954
- await db.deleteAttribute({
955
- databaseId: dbId,
956
- tableId: collection.$id,
957
- key: attribute.key
958
- });
959
- }
960
- else {
961
- await db.deleteAttribute(dbId, collection.$id, attribute.key);
962
- }
963
- // Remove from local collection metadata so downstream logic treats it as new
964
- collection.attributes = collection.attributes.filter((attr) => attr.key !== attribute.key);
965
- foundAttribute = undefined;
966
- }
967
- catch (deleteError) {
968
- MessageFormatter.error(`Failed to delete attribute '${attribute.key}' before recreation: ${deleteError}`);
969
- return "error";
970
- }
971
- }
972
- if (foundAttribute &&
973
- attributesSame(foundAttribute, attribute) &&
974
- updateEnabled) {
975
- // No need to do anything, they are the same
976
- return "processed";
977
- }
978
- else if (foundAttribute &&
979
- !attributesSame(foundAttribute, attribute) &&
980
- updateEnabled) {
981
- // MessageFormatter.info(
982
- // `Updating attribute with same key ${attribute.key} but different values`
983
- // );
984
- // DEBUG: Log before object merge to detect corruption
985
- if ((attribute.key === 'conversationType' || attribute.key === 'messageStreakCount')) {
986
- console.log(`[DEBUG] MERGE - key="${attribute.key}"`, {
987
- found: {
988
- elements: foundAttribute?.elements,
989
- min: foundAttribute?.min,
990
- max: foundAttribute?.max
991
- },
992
- desired: {
993
- elements: attribute?.elements,
994
- min: attribute?.min,
995
- max: attribute?.max
996
- }
997
- });
998
- }
999
- finalAttribute = {
1000
- ...foundAttribute,
1001
- ...attribute,
1002
- };
1003
- // DEBUG: Log after object merge to detect corruption
1004
- if ((finalAttribute.key === 'conversationType' || finalAttribute.key === 'messageStreakCount')) {
1005
- console.log(`[DEBUG] AFTER_MERGE - key="${finalAttribute.key}"`, {
1006
- merged: {
1007
- elements: finalAttribute?.elements,
1008
- min: finalAttribute?.min,
1009
- max: finalAttribute?.max
1010
- }
1011
- });
1012
- }
1013
- action = "update";
1014
- }
1015
- else if (!updateEnabled &&
1016
- foundAttribute &&
1017
- !attributesSame(foundAttribute, attribute)) {
1018
- if (isDatabaseAdapter(db)) {
1019
- await db.deleteAttribute({
1020
- databaseId: dbId,
1021
- tableId: collection.$id,
1022
- key: attribute.key,
1023
- });
1024
- }
1025
- else {
1026
- await db.deleteAttribute(dbId, collection.$id, attribute.key);
1027
- }
1028
- MessageFormatter.info(`Deleted attribute: ${attribute.key} to recreate it because they diff (update disabled temporarily)`);
1029
- return "processed";
1030
- }
1031
- // Relationship attribute logic with adjustments
1032
- let collectionFoundViaRelatedCollection;
1033
- let relatedCollectionId;
1034
- if (finalAttribute.type === "relationship" &&
1035
- finalAttribute.relatedCollection) {
1036
- // First try treating relatedCollection as an ID directly
1037
- try {
1038
- const byIdCollection = isDatabaseAdapter(db)
1039
- ? (await db.getTable({
1040
- databaseId: dbId,
1041
- tableId: finalAttribute.relatedCollection,
1042
- })).data
1043
- : await db.getCollection(dbId, finalAttribute.relatedCollection);
1044
- collectionFoundViaRelatedCollection = byIdCollection;
1045
- relatedCollectionId = byIdCollection.$id;
1046
- // Cache by name for subsequent lookups
1047
- nameToIdMapping.set(byIdCollection.name, byIdCollection.$id);
1048
- }
1049
- catch (_) {
1050
- // Not an ID or not found — fall back to name-based resolution below
1051
- }
1052
- if (!collectionFoundViaRelatedCollection &&
1053
- nameToIdMapping.has(finalAttribute.relatedCollection)) {
1054
- relatedCollectionId = nameToIdMapping.get(finalAttribute.relatedCollection);
1055
- try {
1056
- collectionFoundViaRelatedCollection = isDatabaseAdapter(db)
1057
- ? (await db.getTable({
1058
- databaseId: dbId,
1059
- tableId: relatedCollectionId,
1060
- })).data
1061
- : await db.getCollection(dbId, relatedCollectionId);
1062
- }
1063
- catch (e) {
1064
- // MessageFormatter.info(
1065
- // `Collection not found: ${finalAttribute.relatedCollection} when nameToIdMapping was set`
1066
- // );
1067
- collectionFoundViaRelatedCollection = undefined;
1068
- }
1069
- }
1070
- else if (!collectionFoundViaRelatedCollection) {
1071
- const collectionsPulled = isDatabaseAdapter(db)
1072
- ? await db.listTables({
1073
- databaseId: dbId,
1074
- queries: [Query.equal("name", finalAttribute.relatedCollection)],
1075
- })
1076
- : await db.listCollections(dbId, [
1077
- Query.equal("name", finalAttribute.relatedCollection),
1078
- ]);
1079
- if (collectionsPulled.total && collectionsPulled.total > 0) {
1080
- collectionFoundViaRelatedCollection = isDatabaseAdapter(db)
1081
- ? collectionsPulled.tables?.[0]
1082
- : collectionsPulled.collections?.[0];
1083
- relatedCollectionId = collectionFoundViaRelatedCollection?.$id;
1084
- if (relatedCollectionId) {
1085
- nameToIdMapping.set(finalAttribute.relatedCollection, relatedCollectionId);
1086
- }
1087
- }
1088
- }
1089
- // ONLY queue relationship attributes that have actual unresolved dependencies
1090
- if (!(relatedCollectionId && collectionFoundViaRelatedCollection)) {
1091
- MessageFormatter.info(chalk.yellow(`⏳ Queueing relationship attribute '${finalAttribute.key}' - related collection '${finalAttribute.relatedCollection}' not found yet`));
1092
- enqueueOperation({
1093
- type: "attribute",
1094
- collectionId: collection.$id,
1095
- collection: collection,
1096
- attribute,
1097
- dependencies: [finalAttribute.relatedCollection],
1098
- });
1099
- return "queued";
1100
- }
1101
- }
1102
- finalAttribute = parseAttribute(finalAttribute);
1103
- // Ensure collection/table exists - create it if it doesn't
1104
- try {
1105
- await (isDatabaseAdapter(db)
1106
- ? db.getTable({ databaseId: dbId, tableId: collection.$id })
1107
- : db.getCollection(dbId, collection.$id));
1108
- }
1109
- catch (error) {
1110
- // Collection doesn't exist - create it
1111
- if (error.code === 404 ||
1112
- (error instanceof Error &&
1113
- (error.message.includes("collection_not_found") ||
1114
- error.message.includes("Collection with the requested ID could not be found")))) {
1115
- MessageFormatter.info(`Collection '${collection.name}' doesn't exist, creating it first...`);
1116
- try {
1117
- if (isDatabaseAdapter(db)) {
1118
- await db.createTable({
1119
- databaseId: dbId,
1120
- id: collection.$id,
1121
- name: collection.name,
1122
- permissions: collection.$permissions || [],
1123
- documentSecurity: collection.documentSecurity ?? false,
1124
- enabled: collection.enabled ?? true,
1125
- });
1126
- }
1127
- else {
1128
- await db.createCollection(dbId, collection.$id, collection.name, collection.$permissions || [], collection.documentSecurity ?? false, collection.enabled ?? true);
1129
- }
1130
- MessageFormatter.success(`Created collection '${collection.name}'`);
1131
- await delay(500); // Wait for collection to be ready
1132
- }
1133
- catch (createError) {
1134
- MessageFormatter.error(`Failed to create collection '${collection.name}'`, createError instanceof Error
1135
- ? createError
1136
- : new Error(String(createError)));
1137
- return "error";
1138
- }
1139
- }
1140
- else {
1141
- // Other error - re-throw
1142
- throw error;
1143
- }
1144
- }
1145
- // Use adapter-based attribute creation/update
1146
- if (action === "create") {
1147
- await tryAwaitWithRetry(async () => await createAttributeViaAdapter(db, dbId, collection.$id, finalAttribute));
1148
- }
1149
- else {
1150
- console.log(`Updating attribute '${finalAttribute.key}'...`);
1151
- if (finalAttribute.type === "double" || finalAttribute.type === "integer") {
1152
- console.log("finalAttribute:", finalAttribute);
1153
- }
1154
- await tryAwaitWithRetry(async () => await updateAttributeViaAdapter(db, dbId, collection.$id, finalAttribute));
1155
- }
1156
- return "processed";
1157
- };
1158
- /**
1159
- * Enhanced collection attribute creation with proper status monitoring
1160
- */
1161
- export const createUpdateCollectionAttributesWithStatusCheck = async (db, dbId, collection, attributes) => {
1162
- const existingAttributes = collection.attributes.map((attr) => parseAttribute(attr)) || [];
1163
- const attributesToRemove = existingAttributes.filter((attr) => !attributes.some((a) => a.key === attr.key));
1164
- const indexesToRemove = collection.indexes.filter((index) => attributesToRemove.some((attr) => index.attributes.includes(attr.key)));
1165
- // Handle attribute removal first
1166
- if (attributesToRemove.length > 0) {
1167
- if (indexesToRemove.length > 0) {
1168
- MessageFormatter.info(chalk.red(`Removing indexes as they rely on an attribute that is being removed: ${indexesToRemove
1169
- .map((index) => index.key)
1170
- .join(", ")}`));
1171
- for (const index of indexesToRemove) {
1172
- await tryAwaitWithRetry(async () => {
1173
- if (isDatabaseAdapter(db)) {
1174
- await db.deleteIndex({
1175
- databaseId: dbId,
1176
- tableId: collection.$id,
1177
- key: index.key,
1178
- });
1179
- }
1180
- else {
1181
- await db.deleteIndex(dbId, collection.$id, index.key);
1182
- }
1183
- });
1184
- await delay(500); // Longer delay for deletions
1185
- }
1186
- }
1187
- for (const attr of attributesToRemove) {
1188
- MessageFormatter.info(chalk.red(`Removing attribute: ${attr.key} as it is no longer in the collection`));
1189
- await tryAwaitWithRetry(async () => {
1190
- if (isDatabaseAdapter(db)) {
1191
- await db.deleteAttribute({
1192
- databaseId: dbId,
1193
- tableId: collection.$id,
1194
- key: attr.key,
1195
- });
1196
- }
1197
- else {
1198
- await db.deleteAttribute(dbId, collection.$id, attr.key);
1199
- }
1200
- });
1201
- await delay(500); // Longer delay for deletions
1202
- }
1203
- }
1204
- // First, get fresh collection data and determine which attributes actually need processing
1205
- let currentCollection = collection;
1206
- try {
1207
- currentCollection = isDatabaseAdapter(db)
1208
- ? (await db.getTable({ databaseId: dbId, tableId: collection.$id })).data
1209
- : await db.getCollection(dbId, collection.$id);
1210
- }
1211
- catch (error) {
1212
- MessageFormatter.info(chalk.yellow(`Warning: Could not refresh collection data: ${error}`));
1213
- }
1214
- const existingAttributesMap = new Map();
1215
- try {
1216
- const parsedAttributes = currentCollection.attributes.map((attr) => parseAttribute(attr));
1217
- parsedAttributes.forEach((attr) => existingAttributesMap.set(attr.key, attr));
1218
- }
1219
- catch (error) {
1220
- MessageFormatter.info(chalk.yellow(`Warning: Could not parse existing attributes: ${error}`));
1221
- }
1222
- // Filter to only attributes that need processing (new, changed, or not yet processed)
1223
- const attributesToProcess = attributes.filter((attribute) => {
1224
- // Skip if already processed in this session
1225
- if (isAttributeProcessed(dbId, currentCollection.$id, attribute.key)) {
1226
- return false;
1227
- }
1228
- const existing = existingAttributesMap.get(attribute.key);
1229
- if (!existing) {
1230
- MessageFormatter.info(`➕ ${attribute.key}`);
1231
- return true;
1232
- }
1233
- const needsUpdate = !attributesSame(existing, parseAttribute(attribute));
1234
- if (needsUpdate) {
1235
- MessageFormatter.info(`🔄 ${attribute.key}`);
1236
- }
1237
- else {
1238
- MessageFormatter.info(chalk.gray(`✅ ${attribute.key}`));
1239
- }
1240
- return needsUpdate;
1241
- });
1242
- if (attributesToProcess.length === 0) {
1243
- return true;
1244
- }
1245
- let remainingAttributes = [...attributesToProcess];
1246
- let overallRetryCount = 0;
1247
- const maxOverallRetries = 3;
1248
- while (remainingAttributes.length > 0 &&
1249
- overallRetryCount < maxOverallRetries) {
1250
- const attributesToProcessThisRound = [...remainingAttributes];
1251
- remainingAttributes = []; // Reset for next iteration
1252
- for (const attribute of attributesToProcessThisRound) {
1253
- const success = await createOrUpdateAttributeWithStatusCheck(db, dbId, currentCollection, attribute);
1254
- if (success) {
1255
- // Mark this specific attribute as processed
1256
- markAttributeProcessed(dbId, currentCollection.$id, attribute.key);
1257
- // Get updated collection data for next iteration
1258
- try {
1259
- currentCollection = isDatabaseAdapter(db)
1260
- ? (await db.getTable({ databaseId: dbId, tableId: collection.$id })).data
1261
- : await db.getCollection({
1262
- databaseId: dbId,
1263
- collectionId: collection.$id,
1264
- });
1265
- }
1266
- catch (error) {
1267
- MessageFormatter.info(chalk.yellow(`Warning: Could not refresh collection data: ${error}`));
1268
- }
1269
- // Add delay between successful attributes
1270
- await delay(1000);
1271
- }
1272
- else {
1273
- MessageFormatter.info(chalk.red(`❌ ${attribute.key}`));
1274
- remainingAttributes.push(attribute); // Add back to retry list
1275
- }
1276
- }
1277
- if (remainingAttributes.length === 0) {
1278
- return true;
1279
- }
1280
- overallRetryCount++;
1281
- if (overallRetryCount < maxOverallRetries) {
1282
- MessageFormatter.info(chalk.yellow(`⏳ Retrying ${remainingAttributes.length} failed attributes...`));
1283
- await delay(5000);
1284
- // Refresh collection data before retry
1285
- try {
1286
- currentCollection = isDatabaseAdapter(db)
1287
- ? (await db.getTable({ databaseId: dbId, tableId: collection.$id }))
1288
- .data
1289
- : await db.getCollection(dbId, collection.$id);
1290
- }
1291
- catch (error) {
1292
- // Silently continue if refresh fails
1293
- }
1294
- }
1295
- }
1296
- // If we get here, some attributes still failed after all retries
1297
- if (attributesToProcess.length > 0) {
1298
- MessageFormatter.info(chalk.red(`\n❌ Failed to create ${attributesToProcess.length} attributes after ${maxOverallRetries} attempts: ${attributesToProcess
1299
- .map((a) => a.key)
1300
- .join(", ")}`));
1301
- MessageFormatter.info(chalk.red(`This may indicate a fundamental issue with the attribute definitions or Appwrite instance`));
1302
- return false;
1303
- }
1304
- MessageFormatter.info(chalk.green(`\n✅ Successfully created all ${attributes.length} attributes for collection: ${collection.name}`));
1305
- return true;
1306
- };
1307
- export const createUpdateCollectionAttributes = async (db, dbId, collection, attributes) => {
1308
- MessageFormatter.info(chalk.green(`Creating/Updating attributes for collection: ${collection.name}`));
1309
- const existingAttributes = collection.attributes.map((attr) => parseAttribute(attr)) || [];
1310
- const attributesToRemove = existingAttributes.filter((attr) => !attributes.some((a) => a.key === attr.key));
1311
- const indexesToRemove = collection.indexes.filter((index) => attributesToRemove.some((attr) => index.attributes.includes(attr.key)));
1312
- if (attributesToRemove.length > 0) {
1313
- if (indexesToRemove.length > 0) {
1314
- MessageFormatter.info(chalk.red(`Removing indexes as they rely on an attribute that is being removed: ${indexesToRemove
1315
- .map((index) => index.key)
1316
- .join(", ")}`));
1317
- for (const index of indexesToRemove) {
1318
- await tryAwaitWithRetry(async () => {
1319
- if (isDatabaseAdapter(db)) {
1320
- await db.deleteIndex({
1321
- databaseId: dbId,
1322
- tableId: collection.$id,
1323
- key: index.key,
1324
- });
1325
- }
1326
- else {
1327
- await db.deleteIndex(dbId, collection.$id, index.key);
1328
- }
1329
- });
1330
- await delay(100);
1331
- }
1332
- }
1333
- for (const attr of attributesToRemove) {
1334
- MessageFormatter.info(chalk.red(`Removing attribute: ${attr.key} as it is no longer in the collection`));
1335
- await tryAwaitWithRetry(async () => {
1336
- if (isDatabaseAdapter(db)) {
1337
- await db.deleteAttribute({
1338
- databaseId: dbId,
1339
- tableId: collection.$id,
1340
- key: attr.key,
1341
- });
1342
- }
1343
- else {
1344
- await db.deleteAttribute(dbId, collection.$id, attr.key);
1345
- }
1346
- });
1347
- await delay(50);
1348
- }
1349
- }
1350
- const batchSize = 3;
1351
- for (let i = 0; i < attributes.length; i += batchSize) {
1352
- const batch = attributes.slice(i, i + batchSize);
1353
- const attributePromises = batch.map((attribute) => tryAwaitWithRetry(async () => await createOrUpdateAttribute(db, dbId, collection, attribute)));
1354
- const results = await Promise.allSettled(attributePromises);
1355
- results.forEach((result) => {
1356
- if (result.status === "rejected") {
1357
- MessageFormatter.error("An attribute promise was rejected:", result.reason);
1358
- }
1359
- });
1360
- // Add delay after each batch
1361
- await delay(200);
1362
- }
1363
- MessageFormatter.info(`Finished creating/updating attributes for collection: ${collection.name}`);
1364
- };