@njdamstra/appwrite-utils-cli 1.8.9

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 (392) hide show
  1. package/CHANGELOG.md +19 -0
  2. package/README.md +1133 -0
  3. package/dist/adapters/AdapterFactory.d.ts +94 -0
  4. package/dist/adapters/AdapterFactory.js +405 -0
  5. package/dist/adapters/DatabaseAdapter.d.ts +233 -0
  6. package/dist/adapters/DatabaseAdapter.js +50 -0
  7. package/dist/adapters/LegacyAdapter.d.ts +50 -0
  8. package/dist/adapters/LegacyAdapter.js +612 -0
  9. package/dist/adapters/TablesDBAdapter.d.ts +45 -0
  10. package/dist/adapters/TablesDBAdapter.js +571 -0
  11. package/dist/adapters/index.d.ts +11 -0
  12. package/dist/adapters/index.js +12 -0
  13. package/dist/backups/operations/bucketBackup.d.ts +19 -0
  14. package/dist/backups/operations/bucketBackup.js +197 -0
  15. package/dist/backups/operations/collectionBackup.d.ts +30 -0
  16. package/dist/backups/operations/collectionBackup.js +201 -0
  17. package/dist/backups/operations/comprehensiveBackup.d.ts +25 -0
  18. package/dist/backups/operations/comprehensiveBackup.js +238 -0
  19. package/dist/backups/schemas/bucketManifest.d.ts +93 -0
  20. package/dist/backups/schemas/bucketManifest.js +33 -0
  21. package/dist/backups/schemas/comprehensiveManifest.d.ts +108 -0
  22. package/dist/backups/schemas/comprehensiveManifest.js +32 -0
  23. package/dist/backups/tracking/centralizedTracking.d.ts +34 -0
  24. package/dist/backups/tracking/centralizedTracking.js +274 -0
  25. package/dist/cli/commands/configCommands.d.ts +8 -0
  26. package/dist/cli/commands/configCommands.js +166 -0
  27. package/dist/cli/commands/databaseCommands.d.ts +13 -0
  28. package/dist/cli/commands/databaseCommands.js +554 -0
  29. package/dist/cli/commands/functionCommands.d.ts +7 -0
  30. package/dist/cli/commands/functionCommands.js +330 -0
  31. package/dist/cli/commands/schemaCommands.d.ts +7 -0
  32. package/dist/cli/commands/schemaCommands.js +169 -0
  33. package/dist/cli/commands/storageCommands.d.ts +5 -0
  34. package/dist/cli/commands/storageCommands.js +143 -0
  35. package/dist/cli/commands/transferCommands.d.ts +5 -0
  36. package/dist/cli/commands/transferCommands.js +384 -0
  37. package/dist/collections/attributes.d.ts +13 -0
  38. package/dist/collections/attributes.js +1364 -0
  39. package/dist/collections/indexes.d.ts +12 -0
  40. package/dist/collections/indexes.js +217 -0
  41. package/dist/collections/methods.d.ts +19 -0
  42. package/dist/collections/methods.js +682 -0
  43. package/dist/collections/tableOperations.d.ts +86 -0
  44. package/dist/collections/tableOperations.js +434 -0
  45. package/dist/collections/transferOperations.d.ts +8 -0
  46. package/dist/collections/transferOperations.js +412 -0
  47. package/dist/collections/wipeOperations.d.ts +16 -0
  48. package/dist/collections/wipeOperations.js +233 -0
  49. package/dist/config/ConfigManager.d.ts +445 -0
  50. package/dist/config/ConfigManager.js +625 -0
  51. package/dist/config/configMigration.d.ts +87 -0
  52. package/dist/config/configMigration.js +390 -0
  53. package/dist/config/configValidation.d.ts +66 -0
  54. package/dist/config/configValidation.js +358 -0
  55. package/dist/config/index.d.ts +8 -0
  56. package/dist/config/index.js +7 -0
  57. package/dist/config/services/ConfigDiscoveryService.d.ts +126 -0
  58. package/dist/config/services/ConfigDiscoveryService.js +374 -0
  59. package/dist/config/services/ConfigLoaderService.d.ts +129 -0
  60. package/dist/config/services/ConfigLoaderService.js +540 -0
  61. package/dist/config/services/ConfigMergeService.d.ts +208 -0
  62. package/dist/config/services/ConfigMergeService.js +308 -0
  63. package/dist/config/services/ConfigValidationService.d.ts +214 -0
  64. package/dist/config/services/ConfigValidationService.js +310 -0
  65. package/dist/config/services/SessionAuthService.d.ts +225 -0
  66. package/dist/config/services/SessionAuthService.js +456 -0
  67. package/dist/config/services/__tests__/ConfigMergeService.test.d.ts +1 -0
  68. package/dist/config/services/__tests__/ConfigMergeService.test.js +271 -0
  69. package/dist/config/services/index.d.ts +13 -0
  70. package/dist/config/services/index.js +10 -0
  71. package/dist/config/yamlConfig.d.ts +722 -0
  72. package/dist/config/yamlConfig.js +702 -0
  73. package/dist/databases/methods.d.ts +6 -0
  74. package/dist/databases/methods.js +35 -0
  75. package/dist/databases/setup.d.ts +5 -0
  76. package/dist/databases/setup.js +45 -0
  77. package/dist/examples/yamlTerminologyExample.d.ts +42 -0
  78. package/dist/examples/yamlTerminologyExample.js +272 -0
  79. package/dist/functions/deployments.d.ts +4 -0
  80. package/dist/functions/deployments.js +146 -0
  81. package/dist/functions/fnConfigDiscovery.d.ts +3 -0
  82. package/dist/functions/fnConfigDiscovery.js +108 -0
  83. package/dist/functions/methods.d.ts +16 -0
  84. package/dist/functions/methods.js +162 -0
  85. package/dist/functions/pathResolution.d.ts +37 -0
  86. package/dist/functions/pathResolution.js +185 -0
  87. package/dist/functions/templates/count-docs-in-collection/README.md +54 -0
  88. package/dist/functions/templates/count-docs-in-collection/src/main.ts +159 -0
  89. package/dist/functions/templates/count-docs-in-collection/src/request.ts +9 -0
  90. package/dist/functions/templates/hono-typescript/README.md +286 -0
  91. package/dist/functions/templates/hono-typescript/src/adapters/request.ts +74 -0
  92. package/dist/functions/templates/hono-typescript/src/adapters/response.ts +106 -0
  93. package/dist/functions/templates/hono-typescript/src/app.ts +180 -0
  94. package/dist/functions/templates/hono-typescript/src/context.ts +103 -0
  95. package/dist/functions/templates/hono-typescript/src/index.ts +54 -0
  96. package/dist/functions/templates/hono-typescript/src/middleware/appwrite.ts +119 -0
  97. package/dist/functions/templates/typescript-node/README.md +32 -0
  98. package/dist/functions/templates/typescript-node/src/context.ts +103 -0
  99. package/dist/functions/templates/typescript-node/src/index.ts +29 -0
  100. package/dist/functions/templates/uv/README.md +31 -0
  101. package/dist/functions/templates/uv/pyproject.toml +30 -0
  102. package/dist/functions/templates/uv/src/__init__.py +0 -0
  103. package/dist/functions/templates/uv/src/context.py +125 -0
  104. package/dist/functions/templates/uv/src/index.py +46 -0
  105. package/dist/init.d.ts +2 -0
  106. package/dist/init.js +57 -0
  107. package/dist/interactiveCLI.d.ts +31 -0
  108. package/dist/interactiveCLI.js +898 -0
  109. package/dist/main.d.ts +2 -0
  110. package/dist/main.js +1172 -0
  111. package/dist/migrations/afterImportActions.d.ts +17 -0
  112. package/dist/migrations/afterImportActions.js +306 -0
  113. package/dist/migrations/appwriteToX.d.ts +211 -0
  114. package/dist/migrations/appwriteToX.js +491 -0
  115. package/dist/migrations/comprehensiveTransfer.d.ts +147 -0
  116. package/dist/migrations/comprehensiveTransfer.js +1317 -0
  117. package/dist/migrations/dataLoader.d.ts +753 -0
  118. package/dist/migrations/dataLoader.js +1250 -0
  119. package/dist/migrations/importController.d.ts +23 -0
  120. package/dist/migrations/importController.js +268 -0
  121. package/dist/migrations/importDataActions.d.ts +50 -0
  122. package/dist/migrations/importDataActions.js +230 -0
  123. package/dist/migrations/relationships.d.ts +29 -0
  124. package/dist/migrations/relationships.js +204 -0
  125. package/dist/migrations/services/DataTransformationService.d.ts +55 -0
  126. package/dist/migrations/services/DataTransformationService.js +158 -0
  127. package/dist/migrations/services/FileHandlerService.d.ts +75 -0
  128. package/dist/migrations/services/FileHandlerService.js +236 -0
  129. package/dist/migrations/services/ImportOrchestrator.d.ts +97 -0
  130. package/dist/migrations/services/ImportOrchestrator.js +485 -0
  131. package/dist/migrations/services/RateLimitManager.d.ts +138 -0
  132. package/dist/migrations/services/RateLimitManager.js +279 -0
  133. package/dist/migrations/services/RelationshipResolver.d.ts +120 -0
  134. package/dist/migrations/services/RelationshipResolver.js +332 -0
  135. package/dist/migrations/services/UserMappingService.d.ts +109 -0
  136. package/dist/migrations/services/UserMappingService.js +277 -0
  137. package/dist/migrations/services/ValidationService.d.ts +74 -0
  138. package/dist/migrations/services/ValidationService.js +260 -0
  139. package/dist/migrations/transfer.d.ts +26 -0
  140. package/dist/migrations/transfer.js +608 -0
  141. package/dist/migrations/yaml/YamlImportConfigLoader.d.ts +131 -0
  142. package/dist/migrations/yaml/YamlImportConfigLoader.js +383 -0
  143. package/dist/migrations/yaml/YamlImportIntegration.d.ts +93 -0
  144. package/dist/migrations/yaml/YamlImportIntegration.js +341 -0
  145. package/dist/migrations/yaml/generateImportSchemas.d.ts +30 -0
  146. package/dist/migrations/yaml/generateImportSchemas.js +1327 -0
  147. package/dist/schemas/authUser.d.ts +24 -0
  148. package/dist/schemas/authUser.js +17 -0
  149. package/dist/setup.d.ts +2 -0
  150. package/dist/setup.js +5 -0
  151. package/dist/setupCommands.d.ts +58 -0
  152. package/dist/setupCommands.js +490 -0
  153. package/dist/setupController.d.ts +9 -0
  154. package/dist/setupController.js +34 -0
  155. package/dist/shared/attributeMapper.d.ts +20 -0
  156. package/dist/shared/attributeMapper.js +203 -0
  157. package/dist/shared/backupMetadataSchema.d.ts +94 -0
  158. package/dist/shared/backupMetadataSchema.js +38 -0
  159. package/dist/shared/backupTracking.d.ts +18 -0
  160. package/dist/shared/backupTracking.js +176 -0
  161. package/dist/shared/confirmationDialogs.d.ts +75 -0
  162. package/dist/shared/confirmationDialogs.js +236 -0
  163. package/dist/shared/errorUtils.d.ts +54 -0
  164. package/dist/shared/errorUtils.js +95 -0
  165. package/dist/shared/functionManager.d.ts +48 -0
  166. package/dist/shared/functionManager.js +336 -0
  167. package/dist/shared/indexManager.d.ts +24 -0
  168. package/dist/shared/indexManager.js +151 -0
  169. package/dist/shared/jsonSchemaGenerator.d.ts +50 -0
  170. package/dist/shared/jsonSchemaGenerator.js +290 -0
  171. package/dist/shared/logging.d.ts +61 -0
  172. package/dist/shared/logging.js +116 -0
  173. package/dist/shared/messageFormatter.d.ts +39 -0
  174. package/dist/shared/messageFormatter.js +162 -0
  175. package/dist/shared/migrationHelpers.d.ts +61 -0
  176. package/dist/shared/migrationHelpers.js +145 -0
  177. package/dist/shared/operationLogger.d.ts +10 -0
  178. package/dist/shared/operationLogger.js +12 -0
  179. package/dist/shared/operationQueue.d.ts +40 -0
  180. package/dist/shared/operationQueue.js +311 -0
  181. package/dist/shared/operationsTable.d.ts +26 -0
  182. package/dist/shared/operationsTable.js +286 -0
  183. package/dist/shared/operationsTableSchema.d.ts +48 -0
  184. package/dist/shared/operationsTableSchema.js +35 -0
  185. package/dist/shared/progressManager.d.ts +62 -0
  186. package/dist/shared/progressManager.js +215 -0
  187. package/dist/shared/pydanticModelGenerator.d.ts +17 -0
  188. package/dist/shared/pydanticModelGenerator.js +615 -0
  189. package/dist/shared/relationshipExtractor.d.ts +56 -0
  190. package/dist/shared/relationshipExtractor.js +138 -0
  191. package/dist/shared/schemaGenerator.d.ts +40 -0
  192. package/dist/shared/schemaGenerator.js +556 -0
  193. package/dist/shared/selectionDialogs.d.ts +214 -0
  194. package/dist/shared/selectionDialogs.js +544 -0
  195. package/dist/storage/backupCompression.d.ts +20 -0
  196. package/dist/storage/backupCompression.js +67 -0
  197. package/dist/storage/methods.d.ts +32 -0
  198. package/dist/storage/methods.js +472 -0
  199. package/dist/storage/schemas.d.ts +842 -0
  200. package/dist/storage/schemas.js +175 -0
  201. package/dist/types.d.ts +4 -0
  202. package/dist/types.js +3 -0
  203. package/dist/users/methods.d.ts +16 -0
  204. package/dist/users/methods.js +277 -0
  205. package/dist/utils/ClientFactory.d.ts +87 -0
  206. package/dist/utils/ClientFactory.js +212 -0
  207. package/dist/utils/configDiscovery.d.ts +78 -0
  208. package/dist/utils/configDiscovery.js +472 -0
  209. package/dist/utils/configMigration.d.ts +1 -0
  210. package/dist/utils/configMigration.js +261 -0
  211. package/dist/utils/constantsGenerator.d.ts +31 -0
  212. package/dist/utils/constantsGenerator.js +321 -0
  213. package/dist/utils/dataConverters.d.ts +46 -0
  214. package/dist/utils/dataConverters.js +139 -0
  215. package/dist/utils/directoryUtils.d.ts +22 -0
  216. package/dist/utils/directoryUtils.js +59 -0
  217. package/dist/utils/getClientFromConfig.d.ts +39 -0
  218. package/dist/utils/getClientFromConfig.js +199 -0
  219. package/dist/utils/helperFunctions.d.ts +63 -0
  220. package/dist/utils/helperFunctions.js +156 -0
  221. package/dist/utils/index.d.ts +2 -0
  222. package/dist/utils/index.js +2 -0
  223. package/dist/utils/loadConfigs.d.ts +50 -0
  224. package/dist/utils/loadConfigs.js +358 -0
  225. package/dist/utils/pathResolvers.d.ts +53 -0
  226. package/dist/utils/pathResolvers.js +72 -0
  227. package/dist/utils/projectConfig.d.ts +119 -0
  228. package/dist/utils/projectConfig.js +171 -0
  229. package/dist/utils/retryFailedPromises.d.ts +2 -0
  230. package/dist/utils/retryFailedPromises.js +23 -0
  231. package/dist/utils/sessionAuth.d.ts +48 -0
  232. package/dist/utils/sessionAuth.js +164 -0
  233. package/dist/utils/setupFiles.d.ts +4 -0
  234. package/dist/utils/setupFiles.js +1192 -0
  235. package/dist/utils/typeGuards.d.ts +35 -0
  236. package/dist/utils/typeGuards.js +57 -0
  237. package/dist/utils/validationRules.d.ts +43 -0
  238. package/dist/utils/validationRules.js +42 -0
  239. package/dist/utils/versionDetection.d.ts +58 -0
  240. package/dist/utils/versionDetection.js +251 -0
  241. package/dist/utils/yamlConverter.d.ts +100 -0
  242. package/dist/utils/yamlConverter.js +428 -0
  243. package/dist/utils/yamlLoader.d.ts +70 -0
  244. package/dist/utils/yamlLoader.js +267 -0
  245. package/dist/utilsController.d.ts +106 -0
  246. package/dist/utilsController.js +863 -0
  247. package/package.json +75 -0
  248. package/scripts/copy-templates.ts +23 -0
  249. package/src/adapters/AdapterFactory.ts +510 -0
  250. package/src/adapters/DatabaseAdapter.ts +306 -0
  251. package/src/adapters/LegacyAdapter.ts +841 -0
  252. package/src/adapters/TablesDBAdapter.ts +773 -0
  253. package/src/adapters/index.ts +37 -0
  254. package/src/backups/operations/bucketBackup.ts +277 -0
  255. package/src/backups/operations/collectionBackup.ts +310 -0
  256. package/src/backups/operations/comprehensiveBackup.ts +342 -0
  257. package/src/backups/schemas/bucketManifest.ts +78 -0
  258. package/src/backups/schemas/comprehensiveManifest.ts +76 -0
  259. package/src/backups/tracking/centralizedTracking.ts +352 -0
  260. package/src/cli/commands/configCommands.ts +201 -0
  261. package/src/cli/commands/databaseCommands.ts +749 -0
  262. package/src/cli/commands/functionCommands.ts +418 -0
  263. package/src/cli/commands/schemaCommands.ts +200 -0
  264. package/src/cli/commands/storageCommands.ts +152 -0
  265. package/src/cli/commands/transferCommands.ts +457 -0
  266. package/src/collections/attributes.ts +2054 -0
  267. package/src/collections/attributes.ts.backup +1555 -0
  268. package/src/collections/indexes.ts +352 -0
  269. package/src/collections/methods.ts +745 -0
  270. package/src/collections/tableOperations.ts +506 -0
  271. package/src/collections/transferOperations.ts +590 -0
  272. package/src/collections/wipeOperations.ts +346 -0
  273. package/src/config/ConfigManager.ts +808 -0
  274. package/src/config/README.md +274 -0
  275. package/src/config/configMigration.ts +575 -0
  276. package/src/config/configValidation.ts +445 -0
  277. package/src/config/index.ts +10 -0
  278. package/src/config/services/ConfigDiscoveryService.ts +463 -0
  279. package/src/config/services/ConfigLoaderService.ts +740 -0
  280. package/src/config/services/ConfigMergeService.ts +388 -0
  281. package/src/config/services/ConfigValidationService.ts +394 -0
  282. package/src/config/services/SessionAuthService.ts +565 -0
  283. package/src/config/services/__tests__/ConfigMergeService.test.ts +351 -0
  284. package/src/config/services/index.ts +29 -0
  285. package/src/config/yamlConfig.ts +761 -0
  286. package/src/databases/methods.ts +49 -0
  287. package/src/databases/setup.ts +77 -0
  288. package/src/examples/yamlTerminologyExample.ts +346 -0
  289. package/src/functions/deployments.ts +220 -0
  290. package/src/functions/fnConfigDiscovery.ts +103 -0
  291. package/src/functions/methods.ts +271 -0
  292. package/src/functions/pathResolution.ts +227 -0
  293. package/src/functions/templates/count-docs-in-collection/README.md +54 -0
  294. package/src/functions/templates/count-docs-in-collection/src/main.ts +159 -0
  295. package/src/functions/templates/count-docs-in-collection/src/request.ts +9 -0
  296. package/src/functions/templates/hono-typescript/README.md +286 -0
  297. package/src/functions/templates/hono-typescript/src/adapters/request.ts +74 -0
  298. package/src/functions/templates/hono-typescript/src/adapters/response.ts +106 -0
  299. package/src/functions/templates/hono-typescript/src/app.ts +180 -0
  300. package/src/functions/templates/hono-typescript/src/context.ts +103 -0
  301. package/src/functions/templates/hono-typescript/src/index.ts +54 -0
  302. package/src/functions/templates/hono-typescript/src/middleware/appwrite.ts +119 -0
  303. package/src/functions/templates/typescript-node/README.md +32 -0
  304. package/src/functions/templates/typescript-node/src/context.ts +103 -0
  305. package/src/functions/templates/typescript-node/src/index.ts +29 -0
  306. package/src/functions/templates/uv/README.md +31 -0
  307. package/src/functions/templates/uv/pyproject.toml +30 -0
  308. package/src/functions/templates/uv/src/__init__.py +0 -0
  309. package/src/functions/templates/uv/src/context.py +125 -0
  310. package/src/functions/templates/uv/src/index.py +46 -0
  311. package/src/init.ts +62 -0
  312. package/src/interactiveCLI.ts +1136 -0
  313. package/src/main.ts +1661 -0
  314. package/src/migrations/afterImportActions.ts +580 -0
  315. package/src/migrations/appwriteToX.ts +664 -0
  316. package/src/migrations/comprehensiveTransfer.ts +2285 -0
  317. package/src/migrations/dataLoader.ts +1702 -0
  318. package/src/migrations/importController.ts +428 -0
  319. package/src/migrations/importDataActions.ts +315 -0
  320. package/src/migrations/relationships.ts +334 -0
  321. package/src/migrations/services/DataTransformationService.ts +196 -0
  322. package/src/migrations/services/FileHandlerService.ts +311 -0
  323. package/src/migrations/services/ImportOrchestrator.ts +666 -0
  324. package/src/migrations/services/RateLimitManager.ts +363 -0
  325. package/src/migrations/services/RelationshipResolver.ts +461 -0
  326. package/src/migrations/services/UserMappingService.ts +345 -0
  327. package/src/migrations/services/ValidationService.ts +349 -0
  328. package/src/migrations/transfer.ts +1068 -0
  329. package/src/migrations/yaml/YamlImportConfigLoader.ts +439 -0
  330. package/src/migrations/yaml/YamlImportIntegration.ts +446 -0
  331. package/src/migrations/yaml/generateImportSchemas.ts +1354 -0
  332. package/src/schemas/authUser.ts +23 -0
  333. package/src/setup.ts +8 -0
  334. package/src/setupCommands.ts +603 -0
  335. package/src/setupController.ts +43 -0
  336. package/src/shared/attributeMapper.ts +229 -0
  337. package/src/shared/backupMetadataSchema.ts +93 -0
  338. package/src/shared/backupTracking.ts +211 -0
  339. package/src/shared/confirmationDialogs.ts +327 -0
  340. package/src/shared/errorUtils.ts +110 -0
  341. package/src/shared/functionManager.ts +525 -0
  342. package/src/shared/indexManager.ts +254 -0
  343. package/src/shared/jsonSchemaGenerator.ts +383 -0
  344. package/src/shared/logging.ts +149 -0
  345. package/src/shared/messageFormatter.ts +208 -0
  346. package/src/shared/migrationHelpers.ts +232 -0
  347. package/src/shared/operationLogger.ts +20 -0
  348. package/src/shared/operationQueue.ts +377 -0
  349. package/src/shared/operationsTable.ts +338 -0
  350. package/src/shared/operationsTableSchema.ts +60 -0
  351. package/src/shared/progressManager.ts +278 -0
  352. package/src/shared/pydanticModelGenerator.ts +618 -0
  353. package/src/shared/relationshipExtractor.ts +214 -0
  354. package/src/shared/schemaGenerator.ts +644 -0
  355. package/src/shared/selectionDialogs.ts +749 -0
  356. package/src/storage/backupCompression.ts +88 -0
  357. package/src/storage/methods.ts +698 -0
  358. package/src/storage/schemas.ts +205 -0
  359. package/src/types/node-appwrite-tablesdb.d.ts +44 -0
  360. package/src/types.ts +9 -0
  361. package/src/users/methods.ts +359 -0
  362. package/src/utils/ClientFactory.ts +240 -0
  363. package/src/utils/configDiscovery.ts +557 -0
  364. package/src/utils/configMigration.ts +348 -0
  365. package/src/utils/constantsGenerator.ts +369 -0
  366. package/src/utils/dataConverters.ts +159 -0
  367. package/src/utils/directoryUtils.ts +61 -0
  368. package/src/utils/getClientFromConfig.ts +257 -0
  369. package/src/utils/helperFunctions.ts +228 -0
  370. package/src/utils/index.ts +2 -0
  371. package/src/utils/loadConfigs.ts +449 -0
  372. package/src/utils/pathResolvers.ts +81 -0
  373. package/src/utils/projectConfig.ts +299 -0
  374. package/src/utils/retryFailedPromises.ts +29 -0
  375. package/src/utils/sessionAuth.ts +230 -0
  376. package/src/utils/setupFiles.ts +1238 -0
  377. package/src/utils/typeGuards.ts +65 -0
  378. package/src/utils/validationRules.ts +88 -0
  379. package/src/utils/versionDetection.ts +292 -0
  380. package/src/utils/yamlConverter.ts +542 -0
  381. package/src/utils/yamlLoader.ts +371 -0
  382. package/src/utilsController.ts +1203 -0
  383. package/tests/README.md +497 -0
  384. package/tests/adapters/AdapterFactory.test.ts +277 -0
  385. package/tests/integration/syncOperations.test.ts +463 -0
  386. package/tests/jest.config.js +25 -0
  387. package/tests/migration/configMigration.test.ts +546 -0
  388. package/tests/setup.ts +62 -0
  389. package/tests/testUtils.ts +340 -0
  390. package/tests/utils/loadConfigs.test.ts +350 -0
  391. package/tests/validation/configValidation.test.ts +412 -0
  392. package/tsconfig.json +44 -0
@@ -0,0 +1,1364 @@
1
+ import { Query } from "node-appwrite";
2
+ import { attributeSchema, parseAttribute, } from "@njdamstra/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
+ };