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,339 +1,377 @@
1
- import { converterFunctions, tryAwaitWithRetry } from "appwrite-utils";
2
- import {
3
- Client,
4
- Databases,
5
- IndexType,
6
- Query,
7
- Storage,
8
- Users,
9
- type Models,
10
- } from "node-appwrite";
11
- import { InputFile } from "node-appwrite/file";
12
- import { getAppwriteClient } from "../utils/helperFunctions.js";
1
+ import { converterFunctions, tryAwaitWithRetry } from "appwrite-utils";
2
+ import {
3
+ Client,
4
+ Databases,
5
+ IndexType,
6
+ Query,
7
+ Storage,
8
+ Users,
9
+ type Models,
10
+ } from "node-appwrite";
11
+ import { InputFile } from "node-appwrite/file";
12
+ import { getAppwriteClient } from "appwrite-utils-helpers";
13
13
  // Legacy attribute helpers retained only for local-to-local flows if needed
14
- import { parseAttribute } from "appwrite-utils";
15
- import chalk from "chalk";
16
- import { fetchAllCollections } from "../collections/methods.js";
17
- import { MessageFormatter } from "../shared/messageFormatter.js";
18
- import { LegacyAdapter } from "../adapters/LegacyAdapter.js";
19
- import { ProgressManager } from "../shared/progressManager.js";
20
- import { getClient, getAdapter } from "../utils/getClientFromConfig.js";
14
+ import { parseAttribute } from "appwrite-utils";
15
+ import chalk from "chalk";
16
+ import { fetchAllCollections } from "../collections/methods.js";
17
+ import { MessageFormatter, mapToCreateAttributeParams } from "appwrite-utils-helpers";
18
+ import { ProgressManager } from "../shared/progressManager.js";
19
+ import { getClient, getAdapter } from "appwrite-utils-helpers";
21
20
  import { diffTableColumns } from "../collections/tableOperations.js";
22
- import { mapToCreateAttributeParams } from "../shared/attributeMapper.js";
23
- import type { DatabaseAdapter } from "../adapters/DatabaseAdapter.js";
24
-
25
- export interface TransferOptions {
26
- fromDb: Models.Database | undefined;
27
- targetDb: Models.Database | undefined;
28
- isRemote: boolean;
29
- collections?: string[];
30
- transferEndpoint?: string;
31
- transferProject?: string;
32
- transferKey?: string;
33
- sourceBucket?: Models.Bucket;
34
- targetBucket?: Models.Bucket;
35
- transferUsers?: boolean;
36
- }
37
-
38
- export const transferStorageLocalToLocal = async (
39
- storage: Storage,
40
- fromBucketId: string,
41
- toBucketId: string
42
- ) => {
43
- MessageFormatter.info(
44
- `Transferring files from ${fromBucketId} to ${toBucketId}`,
45
- { prefix: "Transfer" }
46
- );
47
- let lastFileId: string | undefined;
48
- let fromFiles = await tryAwaitWithRetry(
49
- async () => await storage.listFiles(fromBucketId, [Query.limit(100)])
50
- );
51
- const allFromFiles = fromFiles.files;
52
- let numberOfFiles = 0;
53
-
54
- const downloadFileWithRetry = async (bucketId: string, fileId: string) => {
55
- let attempts = 3;
56
- while (attempts > 0) {
57
- try {
58
- return await storage.getFileDownload(bucketId, fileId);
59
- } catch (error) {
60
- MessageFormatter.error(
61
- `Error downloading file ${fileId}`,
62
- error instanceof Error ? error : new Error(String(error)),
63
- { prefix: "Transfer" }
64
- );
65
- attempts--;
66
- if (attempts === 0) throw error;
67
- }
68
- }
69
- };
70
-
71
- if (fromFiles.files.length < 100) {
72
- for (const file of allFromFiles) {
73
- const fileData = await tryAwaitWithRetry(
74
- async () => await downloadFileWithRetry(file.bucketId, file.$id)
75
- );
76
- if (!fileData) {
77
- MessageFormatter.error(
78
- `Error downloading file ${file.$id}`,
79
- undefined,
80
- { prefix: "Transfer" }
81
- );
82
- continue;
83
- }
84
- const fileToCreate = InputFile.fromBuffer(
85
- new Uint8Array(fileData),
86
- file.name
87
- );
88
- MessageFormatter.progress(`Creating file: ${file.name}`, {
89
- prefix: "Transfer",
90
- });
91
- try {
92
- await tryAwaitWithRetry(
93
- async () =>
94
- await storage.createFile(
95
- toBucketId,
96
- file.$id,
97
- fileToCreate,
98
- file.$permissions
99
- )
100
- );
101
- } catch (error: any) {
102
- // File already exists, so we can skip it
103
- continue;
104
- }
105
- numberOfFiles++;
106
- }
107
- } else {
108
- lastFileId = fromFiles.files[fromFiles.files.length - 1].$id;
109
- while (lastFileId) {
110
- const files = await tryAwaitWithRetry(
111
- async () =>
112
- await storage.listFiles(fromBucketId, [
113
- Query.limit(100),
114
- Query.cursorAfter(lastFileId!),
115
- ])
116
- );
117
- allFromFiles.push(...files.files);
118
- if (files.files.length < 100) {
119
- lastFileId = undefined;
120
- } else {
121
- lastFileId = files.files[files.files.length - 1].$id;
122
- }
123
- }
124
- for (const file of allFromFiles) {
125
- const fileData = await tryAwaitWithRetry(
126
- async () => await downloadFileWithRetry(file.bucketId, file.$id)
127
- );
128
- if (!fileData) {
129
- MessageFormatter.error(
130
- `Error downloading file ${file.$id}`,
131
- undefined,
132
- { prefix: "Transfer" }
133
- );
134
- continue;
135
- }
136
- const fileToCreate = InputFile.fromBuffer(
137
- new Uint8Array(fileData),
138
- file.name
139
- );
140
- try {
141
- await tryAwaitWithRetry(
142
- async () =>
143
- await storage.createFile(
144
- toBucketId,
145
- file.$id,
146
- fileToCreate,
147
- file.$permissions
148
- )
149
- );
150
- } catch (error: any) {
151
- // File already exists, so we can skip it
152
- MessageFormatter.warning(
153
- `File ${file.$id} already exists, skipping...`,
154
- { prefix: "Transfer" }
155
- );
156
- continue;
157
- }
158
- numberOfFiles++;
159
- }
160
- }
161
-
162
- MessageFormatter.success(
163
- `Transferred ${numberOfFiles} files from ${fromBucketId} to ${toBucketId}`,
164
- { prefix: "Transfer" }
165
- );
166
- };
167
-
168
- export const transferStorageLocalToRemote = async (
169
- localStorage: Storage,
170
- endpoint: string,
171
- projectId: string,
172
- apiKey: string,
173
- fromBucketId: string,
174
- toBucketId: string
175
- ) => {
176
- MessageFormatter.info(
177
- `Transferring files from current storage ${fromBucketId} to ${endpoint} bucket ${toBucketId}`,
178
- { prefix: "Transfer" }
179
- );
180
- const client = getAppwriteClient(endpoint, projectId, apiKey);
181
- const remoteStorage = new Storage(client);
182
- let numberOfFiles = 0;
183
- let lastFileId: string | undefined;
184
- let fromFiles = await tryAwaitWithRetry(
185
- async () => await localStorage.listFiles(fromBucketId, [Query.limit(100)])
186
- );
187
- const allFromFiles = fromFiles.files;
188
- if (fromFiles.files.length === 100) {
189
- lastFileId = fromFiles.files[fromFiles.files.length - 1].$id;
190
- while (lastFileId) {
191
- const files = await tryAwaitWithRetry(
192
- async () =>
193
- await localStorage.listFiles(fromBucketId, [
194
- Query.limit(100),
195
- Query.cursorAfter(lastFileId!),
196
- ])
197
- );
198
- allFromFiles.push(...files.files);
199
- if (files.files.length < 100) {
200
- break;
201
- }
202
- lastFileId = files.files[files.files.length - 1].$id;
203
- }
204
- }
205
-
206
- for (const file of allFromFiles) {
207
- const fileData = await tryAwaitWithRetry(
208
- async () => await localStorage.getFileDownload(file.bucketId, file.$id)
209
- );
210
- const fileToCreate = InputFile.fromBuffer(
211
- new Uint8Array(fileData),
212
- file.name
213
- );
214
- try {
215
- await tryAwaitWithRetry(
216
- async () =>
217
- await remoteStorage.createFile(
218
- toBucketId,
219
- file.$id,
220
- fileToCreate,
221
- file.$permissions
222
- )
223
- );
224
- } catch (error: any) {
225
- // File already exists, so we can skip it
226
- MessageFormatter.warning(`File ${file.$id} already exists, skipping...`, {
227
- prefix: "Transfer",
228
- });
229
- continue;
230
- }
231
- numberOfFiles++;
232
- }
233
- MessageFormatter.success(
234
- `Transferred ${numberOfFiles} files from ${fromBucketId} to ${toBucketId}`,
235
- { prefix: "Transfer" }
236
- );
237
- };
238
-
239
- // Document transfer functions moved to collections/methods.ts with enhanced UX
240
-
241
- // Remote document transfer functions moved to collections/methods.ts with enhanced UX
242
-
243
- /**
244
- * Transfers all collections and documents from one local database to another local database.
245
- *
246
- * @param {Databases} localDb - The local database instance.
247
- * @param {string} fromDbId - The ID of the source database.
248
- * @param {string} targetDbId - The ID of the target database.
249
- * @return {Promise<void>} A promise that resolves when the transfer is complete.
250
- */
251
- export const transferDatabaseLocalToLocal = async (
252
- localDb: Databases,
253
- fromDbId: string,
254
- targetDbId: string
255
- ) => {
256
- MessageFormatter.info(
257
- `Starting database transfer from ${fromDbId} to ${targetDbId}`,
258
- { prefix: "Transfer" }
259
- );
260
- // Get all collections from source database
261
- const sourceCollections = await fetchAllCollections(fromDbId, localDb);
262
- MessageFormatter.info(
263
- `Found ${sourceCollections.length} collections in source database`,
264
- { prefix: "Transfer" }
265
- );
266
-
267
- // Process each collection
268
- for (const collection of sourceCollections) {
269
- MessageFormatter.processing(
270
- `Processing collection: ${collection.name} (${collection.$id})`,
271
- { prefix: "Transfer" }
272
- );
273
-
274
- try {
275
- // Create or update collection in target
276
- let targetCollection: Models.Collection;
277
- const existingCollection = await tryAwaitWithRetry(async () =>
278
- localDb.listCollections(targetDbId, [
279
- Query.equal("$id", collection.$id),
280
- ])
281
- );
282
-
283
- if (existingCollection.collections.length > 0) {
284
- targetCollection = existingCollection.collections[0];
285
- MessageFormatter.info(
286
- `Collection ${collection.name} exists in target database`,
287
- { prefix: "Transfer" }
288
- );
289
-
290
- // Update collection if needed
291
- if (
292
- targetCollection.name !== collection.name ||
293
- targetCollection.$permissions !== collection.$permissions ||
294
- targetCollection.documentSecurity !== collection.documentSecurity ||
295
- targetCollection.enabled !== collection.enabled
296
- ) {
297
- targetCollection = await tryAwaitWithRetry(async () =>
298
- localDb.updateCollection(
299
- targetDbId,
300
- collection.$id,
301
- collection.name,
302
- collection.$permissions,
303
- collection.documentSecurity,
304
- collection.enabled
305
- )
306
- );
307
- MessageFormatter.success(
308
- `Collection ${collection.name} updated`,
309
- { prefix: "Transfer" }
310
- );
311
- }
312
- } else {
313
- MessageFormatter.progress(
314
- `Creating collection ${collection.name} in target database...`,
315
- { prefix: "Transfer" }
316
- );
317
- targetCollection = await tryAwaitWithRetry(async () =>
318
- localDb.createCollection(
319
- targetDbId,
320
- collection.$id,
321
- collection.name,
322
- collection.$permissions,
323
- collection.documentSecurity,
324
- collection.enabled
325
- )
326
- );
327
- }
328
-
329
- // Create attributes via local adapter (wrap the existing client)
330
- const localAdapter: DatabaseAdapter = new LegacyAdapter((localDb as any).client);
331
- MessageFormatter.info(`Creating attributes for ${collection.name} via adapter...`, { prefix: 'Transfer' });
332
- const uniformAttrs = collection.attributes.map((attr) => parseAttribute(attr as any));
21
+ import { type DatabaseAdapter } from "appwrite-utils-helpers";
22
+
23
+ export interface TransferOptions {
24
+ fromDb: Models.Database | undefined;
25
+ targetDb: Models.Database | undefined;
26
+ isRemote: boolean;
27
+ collections?: string[];
28
+ transferEndpoint?: string;
29
+ transferProject?: string;
30
+ transferKey?: string;
31
+ sourceBucket?: Models.Bucket;
32
+ targetBucket?: Models.Bucket;
33
+ transferUsers?: boolean;
34
+ }
35
+
36
+ export const transferStorageLocalToLocal = async (
37
+ storage: Storage,
38
+ fromBucketId: string,
39
+ toBucketId: string
40
+ ) => {
41
+ MessageFormatter.info(
42
+ `Transferring files from ${fromBucketId} to ${toBucketId}`,
43
+ { prefix: "Transfer" }
44
+ );
45
+ let lastFileId: string | undefined;
46
+ let fromFiles = await tryAwaitWithRetry(
47
+ async () => await storage.listFiles(fromBucketId, [Query.limit(100)])
48
+ );
49
+ const allFromFiles = fromFiles.files;
50
+ let numberOfFiles = 0;
51
+
52
+ const downloadFileWithRetry = async (bucketId: string, fileId: string) => {
53
+ let attempts = 3;
54
+ while (attempts > 0) {
55
+ try {
56
+ return await storage.getFileDownload(bucketId, fileId);
57
+ } catch (error) {
58
+ MessageFormatter.error(
59
+ `Error downloading file ${fileId}`,
60
+ error instanceof Error ? error : new Error(String(error)),
61
+ { prefix: "Transfer" }
62
+ );
63
+ attempts--;
64
+ if (attempts === 0) throw error;
65
+ }
66
+ }
67
+ };
68
+
69
+ if (fromFiles.files.length < 100) {
70
+ for (const file of allFromFiles) {
71
+ const fileData = await tryAwaitWithRetry(
72
+ async () => await downloadFileWithRetry(file.bucketId, file.$id)
73
+ );
74
+ if (!fileData) {
75
+ MessageFormatter.error(
76
+ `Error downloading file ${file.$id}`,
77
+ undefined,
78
+ { prefix: "Transfer" }
79
+ );
80
+ continue;
81
+ }
82
+ const fileToCreate = InputFile.fromBuffer(
83
+ new Uint8Array(fileData),
84
+ file.name
85
+ );
86
+ MessageFormatter.progress(`Creating file: ${file.name}`, {
87
+ prefix: "Transfer",
88
+ });
89
+ try {
90
+ await tryAwaitWithRetry(
91
+ async () =>
92
+ await storage.createFile(
93
+ toBucketId,
94
+ file.$id,
95
+ fileToCreate,
96
+ file.$permissions
97
+ )
98
+ );
99
+ } catch (error: any) {
100
+ // File already exists, so we can skip it
101
+ continue;
102
+ }
103
+ numberOfFiles++;
104
+ }
105
+ } else {
106
+ lastFileId = fromFiles.files[fromFiles.files.length - 1].$id;
107
+ while (lastFileId) {
108
+ const files = await tryAwaitWithRetry(
109
+ async () =>
110
+ await storage.listFiles(fromBucketId, [
111
+ Query.limit(100),
112
+ Query.cursorAfter(lastFileId!),
113
+ ])
114
+ );
115
+ allFromFiles.push(...files.files);
116
+ if (files.files.length < 100) {
117
+ lastFileId = undefined;
118
+ } else {
119
+ lastFileId = files.files[files.files.length - 1].$id;
120
+ }
121
+ }
122
+ for (const file of allFromFiles) {
123
+ const fileData = await tryAwaitWithRetry(
124
+ async () => await downloadFileWithRetry(file.bucketId, file.$id)
125
+ );
126
+ if (!fileData) {
127
+ MessageFormatter.error(
128
+ `Error downloading file ${file.$id}`,
129
+ undefined,
130
+ { prefix: "Transfer" }
131
+ );
132
+ continue;
133
+ }
134
+ const fileToCreate = InputFile.fromBuffer(
135
+ new Uint8Array(fileData),
136
+ file.name
137
+ );
138
+ try {
139
+ await tryAwaitWithRetry(
140
+ async () =>
141
+ await storage.createFile(
142
+ toBucketId,
143
+ file.$id,
144
+ fileToCreate,
145
+ file.$permissions
146
+ )
147
+ );
148
+ } catch (error: any) {
149
+ // File already exists, so we can skip it
150
+ MessageFormatter.warning(
151
+ `File ${file.$id} already exists, skipping...`,
152
+ { prefix: "Transfer" }
153
+ );
154
+ continue;
155
+ }
156
+ numberOfFiles++;
157
+ }
158
+ }
159
+
160
+ MessageFormatter.success(
161
+ `Transferred ${numberOfFiles} files from ${fromBucketId} to ${toBucketId}`,
162
+ { prefix: "Transfer" }
163
+ );
164
+ };
165
+
166
+ export const transferStorageLocalToRemote = async (
167
+ localStorage: Storage,
168
+ endpoint: string,
169
+ projectId: string,
170
+ apiKey: string,
171
+ fromBucketId: string,
172
+ toBucketId: string
173
+ ) => {
174
+ MessageFormatter.info(
175
+ `Transferring files from current storage ${fromBucketId} to ${endpoint} bucket ${toBucketId}`,
176
+ { prefix: "Transfer" }
177
+ );
178
+ const client = getAppwriteClient(endpoint, projectId, apiKey);
179
+ const remoteStorage = new Storage(client);
180
+ let numberOfFiles = 0;
181
+ let lastFileId: string | undefined;
182
+ let fromFiles = await tryAwaitWithRetry(
183
+ async () => await localStorage.listFiles(fromBucketId, [Query.limit(100)])
184
+ );
185
+ const allFromFiles = fromFiles.files;
186
+ if (fromFiles.files.length === 100) {
187
+ lastFileId = fromFiles.files[fromFiles.files.length - 1].$id;
188
+ while (lastFileId) {
189
+ const files = await tryAwaitWithRetry(
190
+ async () =>
191
+ await localStorage.listFiles(fromBucketId, [
192
+ Query.limit(100),
193
+ Query.cursorAfter(lastFileId!),
194
+ ])
195
+ );
196
+ allFromFiles.push(...files.files);
197
+ if (files.files.length < 100) {
198
+ break;
199
+ }
200
+ lastFileId = files.files[files.files.length - 1].$id;
201
+ }
202
+ }
203
+
204
+ for (const file of allFromFiles) {
205
+ const fileData = await tryAwaitWithRetry(
206
+ async () => await localStorage.getFileDownload(file.bucketId, file.$id)
207
+ );
208
+ const fileToCreate = InputFile.fromBuffer(
209
+ new Uint8Array(fileData),
210
+ file.name
211
+ );
212
+ try {
213
+ await tryAwaitWithRetry(
214
+ async () =>
215
+ await remoteStorage.createFile(
216
+ toBucketId,
217
+ file.$id,
218
+ fileToCreate,
219
+ file.$permissions
220
+ )
221
+ );
222
+ } catch (error: any) {
223
+ // File already exists, so we can skip it
224
+ MessageFormatter.warning(`File ${file.$id} already exists, skipping...`, {
225
+ prefix: "Transfer",
226
+ });
227
+ continue;
228
+ }
229
+ numberOfFiles++;
230
+ }
231
+ MessageFormatter.success(
232
+ `Transferred ${numberOfFiles} files from ${fromBucketId} to ${toBucketId}`,
233
+ { prefix: "Transfer" }
234
+ );
235
+ };
236
+
237
+ // Document transfer functions moved to collections/methods.ts with enhanced UX
238
+
239
+ // Remote document transfer functions moved to collections/methods.ts with enhanced UX
240
+
241
+ /**
242
+ * Transfers all tables/collections and documents from one local database to another local database.
243
+ * Uses the DatabaseAdapter for unified TablesDB / legacy support.
244
+ *
245
+ * @param {Databases} localDb - The local database instance (kept for signature compat).
246
+ * @param {string} fromDbId - The ID of the source database.
247
+ * @param {string} targetDbId - The ID of the target database.
248
+ * @param {string[]} collectionIds - Optional filter: specific table/collection IDs to transfer. undefined = all, empty array = none.
249
+ * @param {DatabaseAdapter} adapter - The database adapter (TablesDB or Legacy).
250
+ * @return {Promise<void>} A promise that resolves when the transfer is complete.
251
+ */
252
+ export const transferDatabaseLocalToLocal = async (
253
+ localDb: Databases,
254
+ fromDbId: string,
255
+ targetDbId: string,
256
+ collectionIds?: string[],
257
+ adapter?: DatabaseAdapter
258
+ ) => {
259
+ if (!adapter) {
260
+ throw new Error("DatabaseAdapter is required for transferDatabaseLocalToLocal");
261
+ }
262
+ const dbAdapter: DatabaseAdapter = adapter;
263
+
264
+ MessageFormatter.info(
265
+ `Starting database transfer from ${fromDbId} to ${targetDbId} (mode: ${dbAdapter.getApiMode()})`,
266
+ { prefix: "Transfer" }
267
+ );
268
+
269
+ // Get all tables/collections from source database via adapter
270
+ const sourceListRes = await dbAdapter.listTables({ databaseId: fromDbId, queries: [Query.limit(500)] });
271
+ let sourceTables: any[] = sourceListRes.tables || sourceListRes.collections || sourceListRes.data || [];
272
+
273
+ // Filter by collectionIds if provided (match by $id or by name)
274
+ if (collectionIds !== undefined) {
275
+ if (collectionIds.length === 0) {
276
+ MessageFormatter.info("No tables/collections selected for transfer, skipping.", { prefix: "Transfer" });
277
+ return;
278
+ }
279
+ const idSet = new Set(collectionIds);
280
+ sourceTables = sourceTables.filter((c: any) => idSet.has(c.$id) || idSet.has(c.name));
281
+ }
282
+
283
+ MessageFormatter.info(
284
+ `Found ${sourceTables.length} tables/collections in source database`,
285
+ { prefix: "Transfer" }
286
+ );
287
+
288
+ // Process each table/collection
289
+ for (const table of sourceTables) {
290
+ MessageFormatter.processing(
291
+ `Processing table: ${table.name} (${table.$id})`,
292
+ { prefix: "Transfer" }
293
+ );
294
+
295
+ try {
296
+ // Check if table exists in target via adapter
297
+ let targetTableId = table.$id;
298
+ let targetTableData: any;
299
+
300
+ try {
301
+ const existingRes = await dbAdapter.getTable({ databaseId: targetDbId, tableId: table.$id });
302
+ targetTableData = existingRes.data || (existingRes.tables && existingRes.tables[0]);
303
+
304
+ if (targetTableData) {
305
+ MessageFormatter.info(
306
+ `Table ${table.name} exists in target database`,
307
+ { prefix: "Transfer" }
308
+ );
309
+
310
+ // Update table if needed
311
+ const securityField = table.rowSecurity ?? table.documentSecurity ?? false;
312
+ const targetSecurity = targetTableData.rowSecurity ?? targetTableData.documentSecurity ?? false;
313
+ if (
314
+ targetTableData.name !== table.name ||
315
+ JSON.stringify(targetTableData.$permissions) !== JSON.stringify(table.$permissions) ||
316
+ targetSecurity !== securityField ||
317
+ targetTableData.enabled !== table.enabled
318
+ ) {
319
+ await tryAwaitWithRetry(async () =>
320
+ dbAdapter.updateTable({
321
+ databaseId: targetDbId,
322
+ id: table.$id,
323
+ name: table.name,
324
+ permissions: table.$permissions,
325
+ documentSecurity: table.documentSecurity,
326
+ rowSecurity: table.rowSecurity,
327
+ enabled: table.enabled,
328
+ })
329
+ );
330
+ MessageFormatter.success(
331
+ `Table ${table.name} updated`,
332
+ { prefix: "Transfer" }
333
+ );
334
+ }
335
+ }
336
+ } catch {
337
+ // Table does not exist in target, create it
338
+ targetTableData = null;
339
+ }
340
+
341
+ if (!targetTableData) {
342
+ MessageFormatter.progress(
343
+ `Creating table ${table.name} in target database...`,
344
+ { prefix: "Transfer" }
345
+ );
346
+ const createRes = await tryAwaitWithRetry(async () =>
347
+ dbAdapter.createTable({
348
+ databaseId: targetDbId,
349
+ id: table.$id,
350
+ name: table.name,
351
+ permissions: table.$permissions,
352
+ documentSecurity: table.documentSecurity,
353
+ rowSecurity: table.rowSecurity,
354
+ enabled: table.enabled,
355
+ })
356
+ );
357
+ targetTableData = createRes.data || (createRes.tables && createRes.tables[0]);
358
+ }
359
+
360
+ // Create attributes via adapter
361
+ MessageFormatter.info(`Creating attributes for ${table.name} via adapter...`, { prefix: 'Transfer' });
362
+
363
+ // Fetch existing attributes in target to skip already-created ones
364
+ const targetTableRes = await dbAdapter.getTable({ databaseId: targetDbId, tableId: targetTableId });
365
+ const targetInfo = targetTableRes.data || (targetTableRes.tables && targetTableRes.tables[0]);
366
+ const existingAttrs = targetInfo?.attributes || targetInfo?.columns || [];
367
+ const existingKeys = new Set(existingAttrs.map((a: any) => a.key || a.$id));
368
+
369
+ const uniformAttrs = (table.attributes || []).map((attr: any) => parseAttribute(attr as any));
333
370
  const nonRel = uniformAttrs.filter((a: any) => a.type !== 'relationship');
334
371
  for (const attr of nonRel) {
335
- const params = mapToCreateAttributeParams(attr as any, { databaseId: targetDbId, tableId: targetCollection.$id });
336
- await localAdapter.createAttribute(params);
372
+ if (existingKeys.has(attr.key)) continue;
373
+ const params = mapToCreateAttributeParams(attr as any, { databaseId: targetDbId, tableId: targetTableId });
374
+ await dbAdapter.createAttribute(params);
337
375
  await new Promise((r) => setTimeout(r, 150));
338
376
  }
339
377
 
@@ -343,8 +381,9 @@ export const transferDatabaseLocalToLocal = async (
343
381
  let lastStatus = '';
344
382
  while (Date.now() - start < maxWait) {
345
383
  try {
346
- const tableRes = await localAdapter.getTable({ databaseId: targetDbId, tableId: targetCollection.$id });
347
- const attrs = (tableRes as any).attributes || (tableRes as any).columns || [];
384
+ const tableRes = await dbAdapter.getTable({ databaseId: targetDbId, tableId: targetTableId });
385
+ const tInfo = tableRes.data || (tableRes.tables && tableRes.tables[0]);
386
+ const attrs = tInfo?.attributes || tInfo?.columns || [];
348
387
  const found = attrs.find((a: any) => a.key === attr.key);
349
388
  if (found) {
350
389
  if (found.status === 'available') break;
@@ -366,17 +405,18 @@ export const transferDatabaseLocalToLocal = async (
366
405
  // Relationship attributes
367
406
  const rels = uniformAttrs.filter((a: any) => a.type === 'relationship');
368
407
  for (const attr of rels) {
369
- const params = mapToCreateAttributeParams(attr as any, { databaseId: targetDbId, tableId: targetCollection.$id });
370
- await localAdapter.createAttribute(params);
408
+ if (existingKeys.has(attr.key)) continue;
409
+ const params = mapToCreateAttributeParams(attr as any, { databaseId: targetDbId, tableId: targetTableId });
410
+ await dbAdapter.createAttribute(params);
371
411
  await new Promise((r) => setTimeout(r, 150));
372
412
  }
373
-
413
+
374
414
  // Handle indexes via adapter (create or update)
375
- for (const idx of collection.indexes) {
415
+ for (const idx of (table.indexes || [])) {
376
416
  try {
377
- await localAdapter.createIndex({
417
+ await dbAdapter.createIndex({
378
418
  databaseId: targetDbId,
379
- tableId: targetCollection.$id,
419
+ tableId: targetTableId,
380
420
  key: (idx as any).key,
381
421
  type: (idx as any).type,
382
422
  attributes: (idx as any).attributes,
@@ -387,10 +427,10 @@ export const transferDatabaseLocalToLocal = async (
387
427
  } catch (e) {
388
428
  // Try update path by deleting and recreating if necessary
389
429
  try {
390
- await localAdapter.deleteIndex({ databaseId: targetDbId, tableId: targetCollection.$id, key: (idx as any).key });
391
- await localAdapter.createIndex({
430
+ await dbAdapter.deleteIndex({ databaseId: targetDbId, tableId: targetTableId, key: (idx as any).key });
431
+ await dbAdapter.createIndex({
392
432
  databaseId: targetDbId,
393
- tableId: targetCollection.$id,
433
+ tableId: targetTableId,
394
434
  key: (idx as any).key,
395
435
  type: (idx as any).type,
396
436
  attributes: (idx as any).attributes,
@@ -403,106 +443,111 @@ export const transferDatabaseLocalToLocal = async (
403
443
  }
404
444
  }
405
445
  }
406
-
407
- // Transfer documents
408
- const { transferDocumentsBetweenDbsLocalToLocal } = await import(
409
- "../collections/methods.js"
410
- );
411
- await transferDocumentsBetweenDbsLocalToLocal(
412
- localDb,
413
- fromDbId,
414
- targetDbId,
415
- collection.$id,
416
- targetCollection.$id
417
- );
418
- } catch (error) {
419
- MessageFormatter.error(
420
- `Error processing collection ${collection.name}`,
421
- error instanceof Error ? error : new Error(String(error)),
422
- { prefix: "Transfer" }
423
- );
424
- }
425
- }
426
- };
427
-
428
- export const transferDatabaseLocalToRemote = async (
429
- localDb: Databases,
430
- endpoint: string,
431
- projectId: string,
432
- apiKey: string,
433
- fromDbId: string,
434
- toDbId: string
435
- ) => {
436
- const client = getAppwriteClient(endpoint, projectId, apiKey);
437
- const remoteDb = new Databases(client);
438
-
439
- // Get all collections from source database
440
- const sourceCollections = await fetchAllCollections(fromDbId, localDb);
441
- MessageFormatter.info(
442
- `Found ${sourceCollections.length} collections in source database`,
443
- { prefix: "Transfer" }
444
- );
445
-
446
- // Process each collection
447
- for (const collection of sourceCollections) {
448
- MessageFormatter.processing(
449
- `Processing collection: ${collection.name} (${collection.$id})`,
450
- { prefix: "Transfer" }
451
- );
452
-
453
- try {
454
- // Create or update collection in target
455
- let targetCollection: Models.Collection;
456
- const existingCollection = await tryAwaitWithRetry(async () =>
457
- remoteDb.listCollections(toDbId, [Query.equal("$id", collection.$id)])
458
- );
459
-
460
- if (existingCollection.collections.length > 0) {
461
- targetCollection = existingCollection.collections[0];
462
- MessageFormatter.info(
463
- `Collection ${collection.name} exists in remote database`,
464
- { prefix: "Transfer" }
465
- );
466
-
467
- // Update collection if needed
468
- if (
469
- targetCollection.name !== collection.name ||
470
- targetCollection.$permissions !== collection.$permissions ||
471
- targetCollection.documentSecurity !== collection.documentSecurity ||
472
- targetCollection.enabled !== collection.enabled
473
- ) {
474
- targetCollection = await tryAwaitWithRetry(async () =>
475
- remoteDb.updateCollection(
476
- toDbId,
477
- collection.$id,
478
- collection.name,
479
- collection.$permissions,
480
- collection.documentSecurity,
481
- collection.enabled
482
- )
483
- );
484
- MessageFormatter.success(
485
- `Collection ${collection.name} updated`,
486
- { prefix: "Transfer" }
487
- );
488
- }
489
- } else {
490
- MessageFormatter.progress(
491
- `Creating collection ${collection.name} in remote database...`,
492
- { prefix: "Transfer" }
493
- );
494
- targetCollection = await tryAwaitWithRetry(async () =>
495
- remoteDb.createCollection(
496
- toDbId,
497
- collection.$id,
498
- collection.name,
499
- collection.$permissions,
500
- collection.documentSecurity,
501
- collection.enabled
502
- )
503
- );
504
- }
505
-
446
+
447
+ // Transfer documents/rows via adapter
448
+ const { transferDocumentsBetweenDbsLocalToLocal } = await import(
449
+ "../collections/transferOperations.js"
450
+ );
451
+ await transferDocumentsBetweenDbsLocalToLocal(
452
+ dbAdapter,
453
+ fromDbId,
454
+ targetDbId,
455
+ table.$id,
456
+ targetTableId
457
+ );
458
+ } catch (error) {
459
+ MessageFormatter.error(
460
+ `Error processing table ${table.name}`,
461
+ error instanceof Error ? error : new Error(String(error)),
462
+ { prefix: "Transfer" }
463
+ );
464
+ }
465
+ }
466
+ };
467
+
468
+ export const transferDatabaseLocalToRemote = async (
469
+ localDb: Databases,
470
+ endpoint: string,
471
+ projectId: string,
472
+ apiKey: string,
473
+ fromDbId: string,
474
+ toDbId: string,
475
+ collectionIds?: string[]
476
+ ) => {
477
+ const client = getAppwriteClient(endpoint, projectId, apiKey);
478
+ const remoteDb = new Databases(client);
479
+
480
+ // Get all collections from source database
481
+ let sourceCollections = await fetchAllCollections(fromDbId, localDb);
482
+ if (collectionIds && collectionIds.length > 0) {
483
+ const idSet = new Set(collectionIds);
484
+ sourceCollections = sourceCollections.filter((c) => idSet.has(c.$id));
485
+ }
486
+ MessageFormatter.info(
487
+ `Found ${sourceCollections.length} collections in source database`,
488
+ { prefix: "Transfer" }
489
+ );
490
+
491
+ // Process each collection
492
+ for (const collection of sourceCollections) {
493
+ MessageFormatter.processing(
494
+ `Processing collection: ${collection.name} (${collection.$id})`,
495
+ { prefix: "Transfer" }
496
+ );
497
+
498
+ try {
499
+ // Create or update collection in target
500
+ let targetCollection: Models.Collection;
501
+ const existingCollection = await tryAwaitWithRetry(async () =>
502
+ remoteDb.listCollections(toDbId, [Query.equal("$id", collection.$id)])
503
+ );
504
+
505
+ if (existingCollection.collections.length > 0) {
506
+ targetCollection = existingCollection.collections[0];
507
+ MessageFormatter.info(
508
+ `Collection ${collection.name} exists in remote database`,
509
+ { prefix: "Transfer" }
510
+ );
511
+
512
+ // Update collection if needed
513
+ if (
514
+ targetCollection.name !== collection.name ||
515
+ targetCollection.$permissions !== collection.$permissions ||
516
+ targetCollection.documentSecurity !== collection.documentSecurity ||
517
+ targetCollection.enabled !== collection.enabled
518
+ ) {
519
+ targetCollection = await tryAwaitWithRetry(async () =>
520
+ remoteDb.updateCollection(
521
+ toDbId,
522
+ collection.$id,
523
+ collection.name,
524
+ collection.$permissions,
525
+ collection.documentSecurity,
526
+ collection.enabled
527
+ )
528
+ );
529
+ MessageFormatter.success(
530
+ `Collection ${collection.name} updated`,
531
+ { prefix: "Transfer" }
532
+ );
533
+ }
534
+ } else {
535
+ MessageFormatter.progress(
536
+ `Creating collection ${collection.name} in remote database...`,
537
+ { prefix: "Transfer" }
538
+ );
539
+ targetCollection = await tryAwaitWithRetry(async () =>
540
+ remoteDb.createCollection(
541
+ toDbId,
542
+ collection.$id,
543
+ collection.name,
544
+ collection.$permissions,
545
+ collection.documentSecurity,
546
+ collection.enabled
547
+ )
548
+ );
549
+ }
550
+
506
551
  // Create/Update attributes via adapter (prefer adapter for remote)
507
552
  const { adapter: remoteAdapter } = await getAdapter(endpoint, projectId, apiKey, 'auto');
508
553
  MessageFormatter.info(`Creating attributes for ${collection.name} via adapter...`, { prefix: 'Transfer' });
@@ -551,13 +596,13 @@ export const transferDatabaseLocalToRemote = async (
551
596
  for (const a of rUpdate) { const p = mapToCreateAttributeParams(a as any, { databaseId: toDbId, tableId: collection.$id }); await (remoteAdapter as DatabaseAdapter).updateAttribute(p as any); await new Promise((r)=>setTimeout(r,150)); }
552
597
  for (const a of rCreate) { const p = mapToCreateAttributeParams(a as any, { databaseId: toDbId, tableId: collection.$id }); await (remoteAdapter as DatabaseAdapter).createAttribute(p); await new Promise((r)=>setTimeout(r,150)); }
553
598
  }
554
-
555
- // Handle indexes with enhanced status checking
556
- MessageFormatter.info(
557
- `Creating indexes for collection ${collection.name} with enhanced monitoring...`,
558
- { prefix: "Transfer" }
559
- );
560
-
599
+
600
+ // Handle indexes with enhanced status checking
601
+ MessageFormatter.info(
602
+ `Creating indexes for collection ${collection.name} with enhanced monitoring...`,
603
+ { prefix: "Transfer" }
604
+ );
605
+
561
606
  // Create indexes via adapter
562
607
  for (const idx of (collection.indexes as any[]) || []) {
563
608
  try {
@@ -574,495 +619,495 @@ export const transferDatabaseLocalToRemote = async (
574
619
  MessageFormatter.error(`Failed to create index ${idx.key}`, e instanceof Error ? e : new Error(String(e)), { prefix: 'Transfer' });
575
620
  }
576
621
  }
577
-
578
- // Transfer documents
579
- const { transferDocumentsBetweenDbsLocalToRemote } = await import(
580
- "../collections/methods.js"
581
- );
582
- await transferDocumentsBetweenDbsLocalToRemote(
583
- localDb,
584
- endpoint,
585
- projectId,
586
- apiKey,
587
- fromDbId,
588
- toDbId,
589
- collection.$id,
590
- targetCollection.$id
591
- );
592
- } catch (error) {
593
- MessageFormatter.error(
594
- `Error processing collection ${collection.name}`,
595
- error instanceof Error ? error : new Error(String(error)),
596
- { prefix: "Transfer" }
597
- );
598
- }
599
- }
600
- };
601
-
602
- export const transferUsersLocalToRemote = async (
603
- localUsers: Users,
604
- endpoint: string,
605
- projectId: string,
606
- apiKey: string
607
- ) => {
608
- MessageFormatter.info(
609
- "Starting user transfer to remote instance...",
610
- { prefix: "Transfer" }
611
- );
612
-
613
- const client = getClient(endpoint, projectId, apiKey);
614
- const remoteUsers = new Users(client);
615
-
616
- let totalTransferred = 0;
617
- let lastId: string | undefined;
618
-
619
- while (true) {
620
- const queries = [Query.limit(100)];
621
- if (lastId) {
622
- queries.push(Query.cursorAfter(lastId));
623
- }
624
-
625
- const usersList = await tryAwaitWithRetry(async () =>
626
- localUsers.list(queries)
627
- );
628
-
629
- if (usersList.users.length === 0) {
630
- break;
631
- }
632
-
633
- for (const user of usersList.users) {
634
- try {
635
- // Check if user already exists in remote
636
- let remoteUser: Models.User<Models.Preferences> | undefined;
637
- try {
638
- remoteUser = await tryAwaitWithRetry(async () =>
639
- remoteUsers.get(user.$id)
640
- );
641
-
642
- // If user exists, update only the differences
643
- if (remoteUser) {
644
- MessageFormatter.info(
645
- `User ${user.$id} exists, checking for updates...`,
646
- { prefix: "Transfer" }
647
- );
648
- let hasUpdates = false;
649
-
650
- // Update name if different
651
- if (remoteUser.name !== user.name) {
652
- await tryAwaitWithRetry(async () =>
653
- remoteUsers.updateName(user.$id, user.name)
654
- );
655
- MessageFormatter.success(
656
- `Updated name for user ${user.$id}`,
657
- { prefix: "Transfer" }
658
- );
659
- hasUpdates = true;
660
- }
661
-
662
- // Update email if different
663
- if (remoteUser.email !== user.email) {
664
- await tryAwaitWithRetry(async () =>
665
- remoteUsers.updateEmail(user.$id, user.email)
666
- );
667
- MessageFormatter.success(
668
- `Updated email for user ${user.$id}`,
669
- { prefix: "Transfer" }
670
- );
671
- hasUpdates = true;
672
- }
673
-
674
- // Update phone if different
675
- const normalizedLocalPhone = user.phone
676
- ? converterFunctions.convertPhoneStringToUSInternational(user.phone)
677
- : undefined;
678
- if (remoteUser.phone !== normalizedLocalPhone) {
679
- if (normalizedLocalPhone) {
680
- await tryAwaitWithRetry(async () =>
681
- remoteUsers.updatePhone(user.$id, normalizedLocalPhone)
682
- );
683
- }
684
- MessageFormatter.success(
685
- `Updated phone for user ${user.$id}`,
686
- { prefix: "Transfer" }
687
- );
688
- hasUpdates = true;
689
- }
690
-
691
- // Update preferences if different
692
- if (JSON.stringify(remoteUser.prefs) !== JSON.stringify(user.prefs)) {
693
- await tryAwaitWithRetry(async () =>
694
- remoteUsers.updatePrefs(user.$id, user.prefs)
695
- );
696
- MessageFormatter.success(
697
- `Updated preferences for user ${user.$id}`,
698
- { prefix: "Transfer" }
699
- );
700
- hasUpdates = true;
701
- }
702
-
703
- // Update labels if different
704
- if (JSON.stringify(remoteUser.labels) !== JSON.stringify(user.labels)) {
705
- await tryAwaitWithRetry(async () =>
706
- remoteUsers.updateLabels(user.$id, user.labels)
707
- );
708
- MessageFormatter.success(
709
- `Updated labels for user ${user.$id}`,
710
- { prefix: "Transfer" }
711
- );
712
- hasUpdates = true;
713
- }
714
-
715
- // Update email verification if different
716
- if (remoteUser.emailVerification !== user.emailVerification) {
717
- await tryAwaitWithRetry(async () =>
718
- remoteUsers.updateEmailVerification(user.$id, user.emailVerification)
719
- );
720
- MessageFormatter.success(
721
- `Updated email verification for user ${user.$id}`,
722
- { prefix: "Transfer" }
723
- );
724
- hasUpdates = true;
725
- }
726
-
727
- // Update phone verification if different
728
- if (remoteUser.phoneVerification !== user.phoneVerification) {
729
- await tryAwaitWithRetry(async () =>
730
- remoteUsers.updatePhoneVerification(user.$id, user.phoneVerification)
731
- );
732
- MessageFormatter.success(
733
- `Updated phone verification for user ${user.$id}`,
734
- { prefix: "Transfer" }
735
- );
736
- hasUpdates = true;
737
- }
738
-
739
- // Update status if different
740
- if (remoteUser.status !== user.status) {
741
- await tryAwaitWithRetry(async () =>
742
- remoteUsers.updateStatus(user.$id, user.status)
743
- );
744
- MessageFormatter.success(
745
- `Updated status for user ${user.$id}`,
746
- { prefix: "Transfer" }
747
- );
748
- hasUpdates = true;
749
- }
750
-
751
- if (!hasUpdates) {
752
- MessageFormatter.info(
753
- `User ${user.$id} is already up to date, skipping...`,
754
- { prefix: "Transfer" }
755
- );
756
- } else {
757
- totalTransferred++;
758
- MessageFormatter.success(
759
- `Updated user ${user.$id}`,
760
- { prefix: "Transfer" }
761
- );
762
- }
763
- continue;
764
- }
765
- } catch (error: any) {
766
- // User doesn't exist, proceed with creation
767
- }
768
-
769
- const phone = user.phone
770
- ? converterFunctions.convertPhoneStringToUSInternational(user.phone)
771
- : undefined;
772
-
773
- // Handle user creation based on hash type
774
- if (user.hash && user.password) {
775
- // User has a hashed password - recreate with proper hash method
776
- const hashType = user.hash.toLowerCase();
777
- const hashedPassword = user.password; // This is already hashed
778
- const hashOptions = (user.hashOptions as Record<string, any>) || {};
779
-
780
- try {
781
- switch (hashType) {
782
- case "argon2":
783
- await tryAwaitWithRetry(async () =>
784
- remoteUsers.createArgon2User(
785
- user.$id,
786
- user.email,
787
- hashedPassword,
788
- user.name
789
- )
790
- );
791
- break;
792
-
793
- case "bcrypt":
794
- await tryAwaitWithRetry(async () =>
795
- remoteUsers.createBcryptUser(
796
- user.$id,
797
- user.email,
798
- hashedPassword,
799
- user.name
800
- )
801
- );
802
- break;
803
-
804
- case "scrypt":
805
- // Scrypt requires additional parameters from hashOptions
806
- const salt =
807
- typeof hashOptions.salt === "string" ? hashOptions.salt : "";
808
- const costCpu =
809
- typeof hashOptions.costCpu === "number"
810
- ? hashOptions.costCpu
811
- : 32768;
812
- const costMemory =
813
- typeof hashOptions.costMemory === "number"
814
- ? hashOptions.costMemory
815
- : 14;
816
- const costParallel =
817
- typeof hashOptions.costParallel === "number"
818
- ? hashOptions.costParallel
819
- : 1;
820
- const length =
821
- typeof hashOptions.length === "number"
822
- ? hashOptions.length
823
- : 64;
824
-
825
- // Warn if using default values due to missing hash options
826
- if (
827
- !hashOptions.salt ||
828
- typeof hashOptions.costCpu !== "number"
829
- ) {
830
- MessageFormatter.warning(
831
- `User ${user.$id}: Using default Scrypt parameters due to missing hashOptions`,
832
- { prefix: "Transfer" }
833
- );
834
- }
835
-
836
- await tryAwaitWithRetry(async () =>
837
- remoteUsers.createScryptUser(
838
- user.$id,
839
- user.email,
840
- hashedPassword,
841
- salt,
842
- costCpu,
843
- costMemory,
844
- costParallel,
845
- length,
846
- user.name
847
- )
848
- );
849
- break;
850
-
851
- case "scryptmodified":
852
- // Scrypt Modified (Firebase) requires salt, separator, and signer key
853
- const modSalt =
854
- typeof hashOptions.salt === "string" ? hashOptions.salt : "";
855
- const saltSeparator =
856
- typeof hashOptions.saltSeparator === "string"
857
- ? hashOptions.saltSeparator
858
- : "";
859
- const signerKey =
860
- typeof hashOptions.signerKey === "string"
861
- ? hashOptions.signerKey
862
- : "";
863
-
864
- // Warn if critical parameters are missing
865
- if (
866
- !hashOptions.salt ||
867
- !hashOptions.saltSeparator ||
868
- !hashOptions.signerKey
869
- ) {
870
- MessageFormatter.warning(
871
- `User ${user.$id}: Missing critical Scrypt Modified parameters in hashOptions`,
872
- { prefix: "Transfer" }
873
- );
874
- }
875
-
876
- await tryAwaitWithRetry(async () =>
877
- remoteUsers.createScryptModifiedUser(
878
- user.$id,
879
- user.email,
880
- hashedPassword,
881
- modSalt,
882
- saltSeparator,
883
- signerKey,
884
- user.name
885
- )
886
- );
887
- break;
888
-
889
- case "md5":
890
- await tryAwaitWithRetry(async () =>
891
- remoteUsers.createMD5User(
892
- user.$id,
893
- user.email,
894
- hashedPassword,
895
- user.name
896
- )
897
- );
898
- break;
899
-
900
- case "sha":
901
- case "sha1":
902
- case "sha256":
903
- case "sha512":
904
- // SHA variants - determine version from hash type
905
- const getPasswordHashVersion = (hash: string) => {
906
- switch (hash.toLowerCase()) {
907
- case "sha1":
908
- return "sha1" as any;
909
- case "sha256":
910
- return "sha256" as any;
911
- case "sha512":
912
- return "sha512" as any;
913
- default:
914
- return "sha256" as any; // Default to SHA256
915
- }
916
- };
917
-
918
- await tryAwaitWithRetry(async () =>
919
- remoteUsers.createSHAUser(
920
- user.$id,
921
- user.email,
922
- hashedPassword,
923
- getPasswordHashVersion(hashType),
924
- user.name
925
- )
926
- );
927
- break;
928
-
929
- case "phpass":
930
- await tryAwaitWithRetry(async () =>
931
- remoteUsers.createPHPassUser(
932
- user.$id,
933
- user.email,
934
- hashedPassword,
935
- user.name
936
- )
937
- );
938
- break;
939
-
940
- default:
941
- MessageFormatter.warning(
942
- `Unknown hash type '${hashType}' for user ${user.$id}, falling back to Argon2`,
943
- { prefix: "Transfer" }
944
- );
945
- await tryAwaitWithRetry(async () =>
946
- remoteUsers.createArgon2User(
947
- user.$id,
948
- user.email,
949
- hashedPassword,
950
- user.name
951
- )
952
- );
953
- break;
954
- }
955
-
956
- MessageFormatter.success(
957
- `User ${user.$id} created with preserved ${hashType} password`,
958
- { prefix: "Transfer" }
959
- );
960
- } catch (error) {
961
- MessageFormatter.warning(
962
- `Failed to create user ${user.$id} with ${hashType} hash, trying with temporary password`,
963
- { prefix: "Transfer" }
964
- );
965
-
966
- // Fallback to creating user with temporary password
967
- await tryAwaitWithRetry(async () =>
968
- remoteUsers.create(
969
- user.$id,
970
- user.email,
971
- phone,
972
- `changeMe${user.email}`,
973
- user.name
974
- )
975
- );
976
-
977
- MessageFormatter.warning(
978
- `User ${user.$id} created with temporary password - password reset required`,
979
- { prefix: "Transfer" }
980
- );
981
- }
982
- } else {
983
- // No hash or password - create with temporary password
984
- const tempPassword = user.password || `changeMe${user.email}`;
985
-
986
- await tryAwaitWithRetry(async () =>
987
- remoteUsers.create(
988
- user.$id,
989
- user.email,
990
- phone,
991
- tempPassword,
992
- user.name
993
- )
994
- );
995
-
996
- if (!user.password) {
997
- MessageFormatter.warning(
998
- `User ${user.$id} created with temporary password - password reset required`,
999
- { prefix: "Transfer" }
1000
- );
1001
- }
1002
- }
1003
-
1004
- // Update phone, labels, and other attributes for newly created users
1005
- if (phone) {
1006
- await tryAwaitWithRetry(async () =>
1007
- remoteUsers.updatePhone(user.$id, phone)
1008
- );
1009
- }
1010
-
1011
- if (user.labels && user.labels.length > 0) {
1012
- await tryAwaitWithRetry(async () =>
1013
- remoteUsers.updateLabels(user.$id, user.labels)
1014
- );
1015
- }
1016
-
1017
- // Update user preferences and status for newly created users
1018
- await tryAwaitWithRetry(async () =>
1019
- remoteUsers.updatePrefs(user.$id, user.prefs)
1020
- );
1021
-
1022
- if (user.emailVerification) {
1023
- await tryAwaitWithRetry(async () =>
1024
- remoteUsers.updateEmailVerification(user.$id, true)
1025
- );
1026
- } else {
1027
- await tryAwaitWithRetry(async () =>
1028
- remoteUsers.updateEmailVerification(user.$id, false)
1029
- );
1030
- }
1031
-
1032
- if (user.phoneVerification) {
1033
- await tryAwaitWithRetry(async () =>
1034
- remoteUsers.updatePhoneVerification(user.$id, true)
1035
- );
1036
- }
1037
-
1038
- if (user.status === false) {
1039
- await tryAwaitWithRetry(async () =>
1040
- remoteUsers.updateStatus(user.$id, false)
1041
- );
1042
- }
1043
-
1044
- totalTransferred++;
1045
- MessageFormatter.success(
1046
- `Transferred user ${user.$id}`,
1047
- { prefix: "Transfer" }
1048
- );
1049
- } catch (error) {
1050
- MessageFormatter.error(
1051
- `Failed to transfer user ${user.$id}`,
1052
- error instanceof Error ? error : new Error(String(error)),
1053
- { prefix: "Transfer" }
1054
- );
1055
- }
1056
- }
1057
-
1058
- if (usersList.users.length < 100) {
1059
- break;
1060
- }
1061
- lastId = usersList.users[usersList.users.length - 1].$id;
1062
- }
1063
-
1064
- MessageFormatter.success(
1065
- `Successfully transferred ${totalTransferred} users`,
1066
- { prefix: "Transfer" }
1067
- );
1068
- };
622
+
623
+ // Transfer documents
624
+ const { transferDocumentsBetweenDbsLocalToRemote } = await import(
625
+ "../collections/methods.js"
626
+ );
627
+ await transferDocumentsBetweenDbsLocalToRemote(
628
+ localDb,
629
+ endpoint,
630
+ projectId,
631
+ apiKey,
632
+ fromDbId,
633
+ toDbId,
634
+ collection.$id,
635
+ targetCollection.$id
636
+ );
637
+ } catch (error) {
638
+ MessageFormatter.error(
639
+ `Error processing collection ${collection.name}`,
640
+ error instanceof Error ? error : new Error(String(error)),
641
+ { prefix: "Transfer" }
642
+ );
643
+ }
644
+ }
645
+ };
646
+
647
+ export const transferUsersLocalToRemote = async (
648
+ localUsers: Users,
649
+ endpoint: string,
650
+ projectId: string,
651
+ apiKey: string
652
+ ) => {
653
+ MessageFormatter.info(
654
+ "Starting user transfer to remote instance...",
655
+ { prefix: "Transfer" }
656
+ );
657
+
658
+ const client = getClient(endpoint, projectId, apiKey);
659
+ const remoteUsers = new Users(client);
660
+
661
+ let totalTransferred = 0;
662
+ let lastId: string | undefined;
663
+
664
+ while (true) {
665
+ const queries = [Query.limit(100)];
666
+ if (lastId) {
667
+ queries.push(Query.cursorAfter(lastId));
668
+ }
669
+
670
+ const usersList = await tryAwaitWithRetry(async () =>
671
+ localUsers.list(queries)
672
+ );
673
+
674
+ if (usersList.users.length === 0) {
675
+ break;
676
+ }
677
+
678
+ for (const user of usersList.users) {
679
+ try {
680
+ // Check if user already exists in remote
681
+ let remoteUser: Models.User<Models.Preferences> | undefined;
682
+ try {
683
+ remoteUser = await tryAwaitWithRetry(async () =>
684
+ remoteUsers.get(user.$id)
685
+ );
686
+
687
+ // If user exists, update only the differences
688
+ if (remoteUser) {
689
+ MessageFormatter.info(
690
+ `User ${user.$id} exists, checking for updates...`,
691
+ { prefix: "Transfer" }
692
+ );
693
+ let hasUpdates = false;
694
+
695
+ // Update name if different
696
+ if (remoteUser.name !== user.name) {
697
+ await tryAwaitWithRetry(async () =>
698
+ remoteUsers.updateName(user.$id, user.name)
699
+ );
700
+ MessageFormatter.success(
701
+ `Updated name for user ${user.$id}`,
702
+ { prefix: "Transfer" }
703
+ );
704
+ hasUpdates = true;
705
+ }
706
+
707
+ // Update email if different
708
+ if (remoteUser.email !== user.email) {
709
+ await tryAwaitWithRetry(async () =>
710
+ remoteUsers.updateEmail(user.$id, user.email)
711
+ );
712
+ MessageFormatter.success(
713
+ `Updated email for user ${user.$id}`,
714
+ { prefix: "Transfer" }
715
+ );
716
+ hasUpdates = true;
717
+ }
718
+
719
+ // Update phone if different
720
+ const normalizedLocalPhone = user.phone
721
+ ? converterFunctions.convertPhoneStringToUSInternational(user.phone)
722
+ : undefined;
723
+ if (remoteUser.phone !== normalizedLocalPhone) {
724
+ if (normalizedLocalPhone) {
725
+ await tryAwaitWithRetry(async () =>
726
+ remoteUsers.updatePhone(user.$id, normalizedLocalPhone)
727
+ );
728
+ }
729
+ MessageFormatter.success(
730
+ `Updated phone for user ${user.$id}`,
731
+ { prefix: "Transfer" }
732
+ );
733
+ hasUpdates = true;
734
+ }
735
+
736
+ // Update preferences if different
737
+ if (JSON.stringify(remoteUser.prefs) !== JSON.stringify(user.prefs)) {
738
+ await tryAwaitWithRetry(async () =>
739
+ remoteUsers.updatePrefs(user.$id, user.prefs)
740
+ );
741
+ MessageFormatter.success(
742
+ `Updated preferences for user ${user.$id}`,
743
+ { prefix: "Transfer" }
744
+ );
745
+ hasUpdates = true;
746
+ }
747
+
748
+ // Update labels if different
749
+ if (JSON.stringify(remoteUser.labels) !== JSON.stringify(user.labels)) {
750
+ await tryAwaitWithRetry(async () =>
751
+ remoteUsers.updateLabels(user.$id, user.labels)
752
+ );
753
+ MessageFormatter.success(
754
+ `Updated labels for user ${user.$id}`,
755
+ { prefix: "Transfer" }
756
+ );
757
+ hasUpdates = true;
758
+ }
759
+
760
+ // Update email verification if different
761
+ if (remoteUser.emailVerification !== user.emailVerification) {
762
+ await tryAwaitWithRetry(async () =>
763
+ remoteUsers.updateEmailVerification(user.$id, user.emailVerification)
764
+ );
765
+ MessageFormatter.success(
766
+ `Updated email verification for user ${user.$id}`,
767
+ { prefix: "Transfer" }
768
+ );
769
+ hasUpdates = true;
770
+ }
771
+
772
+ // Update phone verification if different
773
+ if (remoteUser.phoneVerification !== user.phoneVerification) {
774
+ await tryAwaitWithRetry(async () =>
775
+ remoteUsers.updatePhoneVerification(user.$id, user.phoneVerification)
776
+ );
777
+ MessageFormatter.success(
778
+ `Updated phone verification for user ${user.$id}`,
779
+ { prefix: "Transfer" }
780
+ );
781
+ hasUpdates = true;
782
+ }
783
+
784
+ // Update status if different
785
+ if (remoteUser.status !== user.status) {
786
+ await tryAwaitWithRetry(async () =>
787
+ remoteUsers.updateStatus(user.$id, user.status)
788
+ );
789
+ MessageFormatter.success(
790
+ `Updated status for user ${user.$id}`,
791
+ { prefix: "Transfer" }
792
+ );
793
+ hasUpdates = true;
794
+ }
795
+
796
+ if (!hasUpdates) {
797
+ MessageFormatter.info(
798
+ `User ${user.$id} is already up to date, skipping...`,
799
+ { prefix: "Transfer" }
800
+ );
801
+ } else {
802
+ totalTransferred++;
803
+ MessageFormatter.success(
804
+ `Updated user ${user.$id}`,
805
+ { prefix: "Transfer" }
806
+ );
807
+ }
808
+ continue;
809
+ }
810
+ } catch (error: any) {
811
+ // User doesn't exist, proceed with creation
812
+ }
813
+
814
+ const phone = user.phone
815
+ ? converterFunctions.convertPhoneStringToUSInternational(user.phone)
816
+ : undefined;
817
+
818
+ // Handle user creation based on hash type
819
+ if (user.hash && user.password) {
820
+ // User has a hashed password - recreate with proper hash method
821
+ const hashType = user.hash.toLowerCase();
822
+ const hashedPassword = user.password; // This is already hashed
823
+ const hashOptions = (user.hashOptions as Record<string, any>) || {};
824
+
825
+ try {
826
+ switch (hashType) {
827
+ case "argon2":
828
+ await tryAwaitWithRetry(async () =>
829
+ remoteUsers.createArgon2User(
830
+ user.$id,
831
+ user.email,
832
+ hashedPassword,
833
+ user.name
834
+ )
835
+ );
836
+ break;
837
+
838
+ case "bcrypt":
839
+ await tryAwaitWithRetry(async () =>
840
+ remoteUsers.createBcryptUser(
841
+ user.$id,
842
+ user.email,
843
+ hashedPassword,
844
+ user.name
845
+ )
846
+ );
847
+ break;
848
+
849
+ case "scrypt":
850
+ // Scrypt requires additional parameters from hashOptions
851
+ const salt =
852
+ typeof hashOptions.salt === "string" ? hashOptions.salt : "";
853
+ const costCpu =
854
+ typeof hashOptions.costCpu === "number"
855
+ ? hashOptions.costCpu
856
+ : 32768;
857
+ const costMemory =
858
+ typeof hashOptions.costMemory === "number"
859
+ ? hashOptions.costMemory
860
+ : 14;
861
+ const costParallel =
862
+ typeof hashOptions.costParallel === "number"
863
+ ? hashOptions.costParallel
864
+ : 1;
865
+ const length =
866
+ typeof hashOptions.length === "number"
867
+ ? hashOptions.length
868
+ : 64;
869
+
870
+ // Warn if using default values due to missing hash options
871
+ if (
872
+ !hashOptions.salt ||
873
+ typeof hashOptions.costCpu !== "number"
874
+ ) {
875
+ MessageFormatter.warning(
876
+ `User ${user.$id}: Using default Scrypt parameters due to missing hashOptions`,
877
+ { prefix: "Transfer" }
878
+ );
879
+ }
880
+
881
+ await tryAwaitWithRetry(async () =>
882
+ remoteUsers.createScryptUser(
883
+ user.$id,
884
+ user.email,
885
+ hashedPassword,
886
+ salt,
887
+ costCpu,
888
+ costMemory,
889
+ costParallel,
890
+ length,
891
+ user.name
892
+ )
893
+ );
894
+ break;
895
+
896
+ case "scryptmodified":
897
+ // Scrypt Modified (Firebase) requires salt, separator, and signer key
898
+ const modSalt =
899
+ typeof hashOptions.salt === "string" ? hashOptions.salt : "";
900
+ const saltSeparator =
901
+ typeof hashOptions.saltSeparator === "string"
902
+ ? hashOptions.saltSeparator
903
+ : "";
904
+ const signerKey =
905
+ typeof hashOptions.signerKey === "string"
906
+ ? hashOptions.signerKey
907
+ : "";
908
+
909
+ // Warn if critical parameters are missing
910
+ if (
911
+ !hashOptions.salt ||
912
+ !hashOptions.saltSeparator ||
913
+ !hashOptions.signerKey
914
+ ) {
915
+ MessageFormatter.warning(
916
+ `User ${user.$id}: Missing critical Scrypt Modified parameters in hashOptions`,
917
+ { prefix: "Transfer" }
918
+ );
919
+ }
920
+
921
+ await tryAwaitWithRetry(async () =>
922
+ remoteUsers.createScryptModifiedUser(
923
+ user.$id,
924
+ user.email,
925
+ hashedPassword,
926
+ modSalt,
927
+ saltSeparator,
928
+ signerKey,
929
+ user.name
930
+ )
931
+ );
932
+ break;
933
+
934
+ case "md5":
935
+ await tryAwaitWithRetry(async () =>
936
+ remoteUsers.createMD5User(
937
+ user.$id,
938
+ user.email,
939
+ hashedPassword,
940
+ user.name
941
+ )
942
+ );
943
+ break;
944
+
945
+ case "sha":
946
+ case "sha1":
947
+ case "sha256":
948
+ case "sha512":
949
+ // SHA variants - determine version from hash type
950
+ const getPasswordHashVersion = (hash: string) => {
951
+ switch (hash.toLowerCase()) {
952
+ case "sha1":
953
+ return "sha1" as any;
954
+ case "sha256":
955
+ return "sha256" as any;
956
+ case "sha512":
957
+ return "sha512" as any;
958
+ default:
959
+ return "sha256" as any; // Default to SHA256
960
+ }
961
+ };
962
+
963
+ await tryAwaitWithRetry(async () =>
964
+ remoteUsers.createSHAUser(
965
+ user.$id,
966
+ user.email,
967
+ hashedPassword,
968
+ getPasswordHashVersion(hashType),
969
+ user.name
970
+ )
971
+ );
972
+ break;
973
+
974
+ case "phpass":
975
+ await tryAwaitWithRetry(async () =>
976
+ remoteUsers.createPHPassUser(
977
+ user.$id,
978
+ user.email,
979
+ hashedPassword,
980
+ user.name
981
+ )
982
+ );
983
+ break;
984
+
985
+ default:
986
+ MessageFormatter.warning(
987
+ `Unknown hash type '${hashType}' for user ${user.$id}, falling back to Argon2`,
988
+ { prefix: "Transfer" }
989
+ );
990
+ await tryAwaitWithRetry(async () =>
991
+ remoteUsers.createArgon2User(
992
+ user.$id,
993
+ user.email,
994
+ hashedPassword,
995
+ user.name
996
+ )
997
+ );
998
+ break;
999
+ }
1000
+
1001
+ MessageFormatter.success(
1002
+ `User ${user.$id} created with preserved ${hashType} password`,
1003
+ { prefix: "Transfer" }
1004
+ );
1005
+ } catch (error) {
1006
+ MessageFormatter.warning(
1007
+ `Failed to create user ${user.$id} with ${hashType} hash, trying with temporary password`,
1008
+ { prefix: "Transfer" }
1009
+ );
1010
+
1011
+ // Fallback to creating user with temporary password
1012
+ await tryAwaitWithRetry(async () =>
1013
+ remoteUsers.create(
1014
+ user.$id,
1015
+ user.email,
1016
+ phone,
1017
+ `changeMe${user.email}`,
1018
+ user.name
1019
+ )
1020
+ );
1021
+
1022
+ MessageFormatter.warning(
1023
+ `User ${user.$id} created with temporary password - password reset required`,
1024
+ { prefix: "Transfer" }
1025
+ );
1026
+ }
1027
+ } else {
1028
+ // No hash or password - create with temporary password
1029
+ const tempPassword = user.password || `changeMe${user.email}`;
1030
+
1031
+ await tryAwaitWithRetry(async () =>
1032
+ remoteUsers.create(
1033
+ user.$id,
1034
+ user.email,
1035
+ phone,
1036
+ tempPassword,
1037
+ user.name
1038
+ )
1039
+ );
1040
+
1041
+ if (!user.password) {
1042
+ MessageFormatter.warning(
1043
+ `User ${user.$id} created with temporary password - password reset required`,
1044
+ { prefix: "Transfer" }
1045
+ );
1046
+ }
1047
+ }
1048
+
1049
+ // Update phone, labels, and other attributes for newly created users
1050
+ if (phone) {
1051
+ await tryAwaitWithRetry(async () =>
1052
+ remoteUsers.updatePhone(user.$id, phone)
1053
+ );
1054
+ }
1055
+
1056
+ if (user.labels && user.labels.length > 0) {
1057
+ await tryAwaitWithRetry(async () =>
1058
+ remoteUsers.updateLabels(user.$id, user.labels)
1059
+ );
1060
+ }
1061
+
1062
+ // Update user preferences and status for newly created users
1063
+ await tryAwaitWithRetry(async () =>
1064
+ remoteUsers.updatePrefs(user.$id, user.prefs)
1065
+ );
1066
+
1067
+ if (user.emailVerification) {
1068
+ await tryAwaitWithRetry(async () =>
1069
+ remoteUsers.updateEmailVerification(user.$id, true)
1070
+ );
1071
+ } else {
1072
+ await tryAwaitWithRetry(async () =>
1073
+ remoteUsers.updateEmailVerification(user.$id, false)
1074
+ );
1075
+ }
1076
+
1077
+ if (user.phoneVerification) {
1078
+ await tryAwaitWithRetry(async () =>
1079
+ remoteUsers.updatePhoneVerification(user.$id, true)
1080
+ );
1081
+ }
1082
+
1083
+ if (user.status === false) {
1084
+ await tryAwaitWithRetry(async () =>
1085
+ remoteUsers.updateStatus(user.$id, false)
1086
+ );
1087
+ }
1088
+
1089
+ totalTransferred++;
1090
+ MessageFormatter.success(
1091
+ `Transferred user ${user.$id}`,
1092
+ { prefix: "Transfer" }
1093
+ );
1094
+ } catch (error) {
1095
+ MessageFormatter.error(
1096
+ `Failed to transfer user ${user.$id}`,
1097
+ error instanceof Error ? error : new Error(String(error)),
1098
+ { prefix: "Transfer" }
1099
+ );
1100
+ }
1101
+ }
1102
+
1103
+ if (usersList.users.length < 100) {
1104
+ break;
1105
+ }
1106
+ lastId = usersList.users[usersList.users.length - 1].$id;
1107
+ }
1108
+
1109
+ MessageFormatter.success(
1110
+ `Successfully transferred ${totalTransferred} users`,
1111
+ { prefix: "Transfer" }
1112
+ );
1113
+ };