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,1702 +1,1729 @@
1
- import type { ImportDataActions } from "./importDataActions.js";
2
- import {
3
- AttributeMappingsSchema,
4
- CollectionCreateSchema,
5
- importDefSchema,
6
- type AppwriteConfig,
7
- type AttributeMappings,
8
- type CollectionCreate,
9
- type ConfigDatabase,
10
- type IdMapping,
11
- type ImportDef,
12
- type ImportDefs,
13
- type RelationshipAttribute,
14
- } from "appwrite-utils";
15
- import path from "path";
16
- import fs from "fs";
17
- import { convertObjectByAttributeMappings } from "../utils/dataConverters.js";
18
- import { z } from "zod";
19
- import { checkForCollection } from "../collections/methods.js";
20
- import { ID, Users, type Databases } from "node-appwrite";
21
- import { logger } from "../shared/logging.js";
22
- import { findOrCreateOperation, updateOperation } from "../shared/migrationHelpers.js";
23
- import { AuthUserCreateSchema } from "../schemas/authUser.js";
24
- import { LegacyAdapter } from "../adapters/LegacyAdapter.js";
25
- import { UsersController } from "../users/methods.js";
26
- import { finalizeByAttributeMap } from "../utils/helperFunctions.js";
27
- import { isEmpty } from "es-toolkit/compat";
28
- import { MessageFormatter } from "../shared/messageFormatter.js";
29
-
30
- // Define a schema for the structure of collection import data using Zod for validation
31
- export const CollectionImportDataSchema = z.object({
32
- // Optional collection creation schema
33
- collection: CollectionCreateSchema.optional(),
34
- // Array of data objects each containing rawData, finalData, context, and an import definition
35
- data: z.array(
36
- z.object({
37
- rawData: z.any(), // The initial raw data
38
- finalData: z.any(), // The transformed data ready for import
39
- context: z.any(), // Additional context for the data transformation
40
- importDef: importDefSchema.optional(), // The import definition schema
41
- })
42
- ),
43
- });
44
-
45
- // Infer the TypeScript type from the Zod schema
46
- export type CollectionImportData = z.infer<typeof CollectionImportDataSchema>;
47
-
48
- // DataLoader class to handle the loading of data into collections
49
- export class DataLoader {
50
- // Private member variables to hold configuration and state
51
- private appwriteFolderPath: string;
52
- private importDataActions: ImportDataActions;
53
- private database: Databases;
54
- private usersController: UsersController;
55
- private config: AppwriteConfig;
56
- // Map to hold the import data for each collection by name
57
- importMap = new Map<string, CollectionImportData>();
58
- // Map to track old to new ID mappings for each collection, if applicable
59
- private oldIdToNewIdPerCollectionMap = new Map<string, Map<string, string>>();
60
- // Map to hold the import operation ID for each collection
61
- collectionImportOperations = new Map<string, string>();
62
- // Map to hold the merged user map for relationship resolution
63
- // Will hold an array of the old user ID's that are mapped to the same new user ID
64
- // For example, if there are two users with the same email, they will both be mapped to the same new user ID
65
- // Prevents duplicate users with the other two maps below it and allows me to keep the old ID's
66
- private mergedUserMap = new Map<string, string[]>();
67
- // Maps to hold email and phone to user ID mappings for unique-ness in User Accounts
68
- private emailToUserIdMap = new Map<string, string>();
69
- private phoneToUserIdMap = new Map<string, string>();
70
- private userIdSet = new Set<string>();
71
- userExistsMap = new Map<string, boolean>();
72
- private shouldWriteFile = false;
73
-
74
- // Constructor to initialize the DataLoader with necessary configurations
75
- constructor(
76
- appwriteFolderPath: string,
77
- importDataActions: ImportDataActions,
78
- database: Databases,
79
- config: AppwriteConfig,
80
- shouldWriteFile?: boolean
81
- ) {
82
- this.appwriteFolderPath = appwriteFolderPath;
83
- this.importDataActions = importDataActions;
84
- this.database = database;
85
- this.usersController = new UsersController(config, database);
86
- this.config = config;
87
- this.shouldWriteFile = shouldWriteFile || false;
88
- }
89
-
90
- // Helper method to generate a consistent key for collections
91
- getCollectionKey(name: string) {
92
- return name.toLowerCase().replace(" ", "");
93
- }
94
-
95
- /**
96
- * Merges two objects by updating the source object with the target object's values.
97
- * It iterates through the target object's keys and updates the source object if:
98
- * - The source object has the key.
99
- * - The target object's value for that key is not null, undefined, or an empty string.
100
- * - If the target object has an array value, it concatenates the values and removes duplicates.
101
- *
102
- * @param source - The source object to be updated.
103
- * @param target - The target object with values to update the source object.
104
- * @returns The updated source object.
105
- */
106
- mergeObjects(source: any, update: any): any {
107
- // Create a new object to hold the merged result
108
- const result = { ...source };
109
-
110
- // Loop through the keys of the object we care about
111
- for (const [key, value] of Object.entries(source)) {
112
- // Check if the key exists in the target object
113
- if (!Object.hasOwn(update, key)) {
114
- // If the key doesn't exist, we can just skip it like bad cheese
115
- continue;
116
- }
117
- if (update[key] === value) {
118
- continue;
119
- }
120
- // If the value ain't here, we can just do whatever man
121
- if (value === undefined || value === null || value === "") {
122
- // If the update key is defined
123
- if (
124
- update[key] !== undefined &&
125
- update[key] !== null &&
126
- update[key] !== ""
127
- ) {
128
- // might as well use it eh?
129
- result[key] = update[key];
130
- }
131
- // ELSE if the value is an array, because it would then not be === to those things above
132
- } else if (Array.isArray(value)) {
133
- // Get the update value
134
- const updateValue = update[key];
135
- // If the update value is an array, concatenate and remove duplicates
136
- // and poopy data
137
- if (Array.isArray(updateValue)) {
138
- result[key] = [...new Set([...value, ...updateValue])].filter(
139
- (item) => item !== null && item !== undefined && item !== ""
140
- );
141
- } else {
142
- // If the update value is not an array, just use it
143
- result[key] = [...value, updateValue].filter(
144
- (item) => item !== null && item !== undefined && item !== ""
145
- );
146
- }
147
- } else if (typeof value === "object" && !Array.isArray(value)) {
148
- // If the value is an object, we need to merge it
149
- if (typeof update[key] === "object" && !Array.isArray(update[key])) {
150
- result[key] = this.mergeObjects(value, update[key]);
151
- }
152
- } else {
153
- // Finally, the source value is defined, and not an array, so we don't care about the update value
154
- continue;
155
- }
156
- }
157
- // Because the objects should technically always be validated FIRST, we can assume the update keys are also defined on the source object
158
- for (const [key, value] of Object.entries(update)) {
159
- if (value === undefined || value === null || value === "") {
160
- continue;
161
- } else if (!Object.hasOwn(source, key)) {
162
- result[key] = value;
163
- } else if (
164
- typeof source[key] === "object" &&
165
- typeof value === "object" &&
166
- !Array.isArray(source[key]) &&
167
- !Array.isArray(value)
168
- ) {
169
- result[key] = this.mergeObjects(source[key], value);
170
- } else if (Array.isArray(source[key]) && Array.isArray(value)) {
171
- result[key] = [...new Set([...source[key], ...value])].filter(
172
- (item) => item !== null && item !== undefined && item !== ""
173
- );
174
- } else if (
175
- source[key] === undefined ||
176
- source[key] === null ||
177
- source[key] === ""
178
- ) {
179
- result[key] = value;
180
- }
181
- }
182
-
183
- return result;
184
- }
185
-
186
- // Method to load data from a file specified in the import definition
187
- loadData(importDef: ImportDef): any[] {
188
- // Simply join appwriteFolderPath with the importDef.filePath
189
- const filePath = path.resolve(this.appwriteFolderPath, importDef.filePath);
190
- MessageFormatter.info(`Loading data from: ${filePath}`, { prefix: "Data" });
191
- if (!fs.existsSync(filePath)) {
192
- MessageFormatter.error(`File not found: ${filePath}`, undefined, { prefix: "Data" });
193
- return [];
194
- }
195
-
196
- // Read the file and parse the JSON data
197
- const rawData = fs.readFileSync(filePath, "utf8");
198
- const parsedData = importDef.basePath
199
- ? JSON.parse(rawData)[importDef.basePath]
200
- : JSON.parse(rawData);
201
-
202
- MessageFormatter.success(`Loaded ${parsedData?.length || 0} items from ${filePath}`, { prefix: "Data" });
203
- return parsedData;
204
- }
205
-
206
- // Helper method to check if a new ID already exists in the old-to-new ID map
207
- checkMapValuesForId(newId: string, collectionName: string) {
208
- const oldIdMap = this.oldIdToNewIdPerCollectionMap.get(collectionName);
209
- for (const [key, value] of oldIdMap?.entries() || []) {
210
- if (value === newId) {
211
- return key;
212
- }
213
- }
214
- return false;
215
- }
216
-
217
- // Method to generate a unique ID that doesn't conflict with existing IDs
218
- getTrueUniqueId(collectionName: string) {
219
- let newId = ID.unique();
220
- let condition =
221
- this.checkMapValuesForId(newId, collectionName) ||
222
- this.userExistsMap.has(newId) ||
223
- this.userIdSet.has(newId) ||
224
- this.importMap
225
- .get(this.getCollectionKey("users"))
226
- ?.data.some(
227
- (user) =>
228
- user.finalData.docId === newId || user.finalData.userId === newId
229
- );
230
- while (condition) {
231
- newId = ID.unique();
232
- condition =
233
- this.checkMapValuesForId(newId, collectionName) ||
234
- this.userExistsMap.has(newId) ||
235
- this.userIdSet.has(newId) ||
236
- this.importMap
237
- .get(this.getCollectionKey("users"))
238
- ?.data.some(
239
- (user) =>
240
- user.finalData.docId === newId || user.finalData.userId === newId
241
- );
242
- }
243
- return newId;
244
- }
245
-
246
- // Method to create a context object for data transformation
247
- createContext(
248
- db: ConfigDatabase,
249
- collection: CollectionCreate,
250
- item: any,
251
- docId: string
252
- ) {
253
- return {
254
- ...item, // Spread the item data for easy access to its properties
255
- dbId: db.$id,
256
- dbName: db.name,
257
- collId: collection.$id,
258
- collName: collection.name,
259
- docId: docId,
260
- createdDoc: {}, // Initially null, to be updated when the document is created
261
- };
262
- }
263
-
264
- /**
265
- * Transforms the given item based on the provided attribute mappings.
266
- * This method applies conversion rules to the item's attributes as defined in the attribute mappings.
267
- *
268
- * @param item - The item to be transformed.
269
- * @param attributeMappings - The mappings that define how each attribute should be transformed.
270
- * @returns The transformed item.
271
- */
272
- transformData(item: any, attributeMappings: AttributeMappings): any {
273
- // Convert the item using the attribute mappings provided
274
- const convertedItem = convertObjectByAttributeMappings(
275
- item,
276
- attributeMappings
277
- );
278
- // Run additional converter functions on the converted item, if any
279
- return this.importDataActions.runConverterFunctions(
280
- convertedItem,
281
- attributeMappings
282
- );
283
- }
284
-
285
- async setupMaps(dbId: string) {
286
- // Initialize the users collection in the import map
287
- this.importMap.set(this.getCollectionKey("users"), {
288
- data: [],
289
- });
290
- for (const db of this.config.databases) {
291
- if (db.$id !== dbId) {
292
- continue;
293
- }
294
- if (!this.config.collections) {
295
- continue;
296
- }
297
- for (let index = 0; index < this.config.collections.length; index++) {
298
- const collectionConfig = this.config.collections[index];
299
- let collection = CollectionCreateSchema.parse(collectionConfig);
300
- // Check if the collection exists in the database
301
- const collectionExists = await checkForCollection(
302
- this.database,
303
- db.$id,
304
- collection
305
- );
306
- if (!collectionExists) {
307
- logger.error(`No collection found for ${collection.name}`);
308
- continue;
309
- } else if (!collection.name) {
310
- logger.error(`Collection ${collection.name} has no name`);
311
- continue;
312
- }
313
- // Update the collection ID with the existing one
314
- collectionConfig.$id = collectionExists.$id;
315
- collection.$id = collectionExists.$id;
316
- this.config.collections[index] = collectionConfig;
317
- // Find or create an import operation for the collection
318
- const adapter = new LegacyAdapter(this.database.client);
319
- const collectionImportOperation = await findOrCreateOperation(
320
- adapter,
321
- dbId,
322
- "importData",
323
- collection.$id!
324
- );
325
- // Store the operation ID in the map
326
- this.collectionImportOperations.set(
327
- this.getCollectionKey(collection.name),
328
- collectionImportOperation.$id
329
- );
330
- // Initialize the collection in the import map
331
- this.importMap.set(this.getCollectionKey(collection.name), {
332
- collection: collection,
333
- data: [],
334
- });
335
- }
336
- }
337
- }
338
-
339
- async getAllUsers() {
340
- const users = new UsersController(this.config, this.database);
341
- const allUsers = await users.getAllUsers();
342
- // Iterate over the users and setup our maps ahead of time for email and phone
343
- for (const user of allUsers) {
344
- if (user.email) {
345
- this.emailToUserIdMap.set(user.email.toLowerCase(), user.$id);
346
- }
347
- if (user.phone) {
348
- this.phoneToUserIdMap.set(user.phone, user.$id);
349
- }
350
- this.userExistsMap.set(user.$id, true);
351
- this.userIdSet.add(user.$id);
352
- let importData = this.importMap.get(this.getCollectionKey("users"));
353
- if (!importData) {
354
- importData = {
355
- data: [],
356
- };
357
- }
358
- importData.data.push({
359
- finalData: {
360
- ...user,
361
- email: user.email?.toLowerCase(),
362
- userId: user.$id,
363
- docId: user.$id,
364
- },
365
- context: {
366
- ...user,
367
- email: user.email?.toLowerCase(),
368
- userId: user.$id,
369
- docId: user.$id,
370
- },
371
- rawData: user,
372
- });
373
- this.importMap.set(this.getCollectionKey("users"), importData);
374
- }
375
- return allUsers;
376
- }
377
-
378
- // Main method to start the data loading process for a given database ID
379
- async start(dbId: string) {
380
- MessageFormatter.divider();
381
- MessageFormatter.info(`Starting data setup for database: ${dbId}`, { prefix: "Data" });
382
- MessageFormatter.divider();
383
- await this.setupMaps(dbId);
384
- const allUsers = await this.getAllUsers();
385
- MessageFormatter.info(
386
- `Fetched ${allUsers.length} users, waiting a few seconds to let the program catch up...`,
387
- { prefix: "Data" }
388
- );
389
- await new Promise((resolve) => setTimeout(resolve, 5000));
390
- // Iterate over the configured databases to find the matching one
391
- for (const db of this.config.databases) {
392
- if (db.$id !== dbId) {
393
- continue;
394
- }
395
- if (!this.config.collections) {
396
- continue;
397
- }
398
- // Iterate over the configured collections to process each
399
- for (const collectionConfig of this.config.collections) {
400
- const collection = collectionConfig;
401
- // Determine if this is the users collection
402
- let isUsersCollection =
403
- this.getCollectionKey(this.config.usersCollectionName) ===
404
- this.getCollectionKey(collection.name);
405
- const collectionDefs = collection.importDefs;
406
- if (!collectionDefs || !collectionDefs.length) {
407
- continue;
408
- }
409
- // Process create and update definitions for the collection
410
- const createDefs = collection.importDefs.filter(
411
- (def: ImportDef) => def.type === "create" || !def.type
412
- );
413
- const updateDefs = collection.importDefs.filter(
414
- (def: ImportDef) => def.type === "update"
415
- );
416
- for (const createDef of createDefs) {
417
- if (!isUsersCollection || !createDef.createUsers) {
418
- await this.prepareCreateData(db, collection, createDef);
419
- } else {
420
- // Special handling for users collection if needed
421
- await this.prepareUserCollectionCreateData(
422
- db,
423
- collection,
424
- createDef
425
- );
426
- }
427
- }
428
- for (const updateDef of updateDefs) {
429
- if (!this.importMap.has(this.getCollectionKey(collection.name))) {
430
- logger.error(
431
- `No data found for collection ${collection.name} for updateDef but it says it's supposed to have one...`
432
- );
433
- continue;
434
- }
435
- // Prepare the update data for the collection
436
- this.prepareUpdateData(db, collection, updateDef);
437
- }
438
- }
439
- MessageFormatter.info("Running update references", { prefix: "Data" });
440
- // this.dealWithMergedUsers();
441
- this.updateOldReferencesForNew();
442
- MessageFormatter.success("Done running update references", { prefix: "Data" });
443
- }
444
- // for (const collection of this.config.collections) {
445
- // this.resolveDataItemRelationships(collection);
446
- // }
447
- MessageFormatter.divider();
448
- MessageFormatter.success(`Data setup for database: ${dbId} completed`, { prefix: "Data" });
449
- MessageFormatter.divider();
450
- if (this.shouldWriteFile) {
451
- this.writeMapsToJsonFile();
452
- }
453
- }
454
-
455
- /**
456
- * Deals with merged users by iterating through all collections in the configuration.
457
- * We have merged users if there are duplicate emails or phones in the import data.
458
- * This function will iterate through all collections that are the same name as the
459
- * users collection and pull out their primaryKeyField's. It will then loop through
460
- * each collection and find any documents that have a
461
- *
462
- * @return {void} This function does not return anything.
463
- */
464
- // dealWithMergedUsers() {
465
- // const usersCollectionKey = this.getCollectionKey(
466
- // this.config.usersCollectionName
467
- // );
468
- // const usersCollectionData = this.importMap.get(usersCollectionKey);
469
-
470
- // if (!this.config.collections) {
471
- // console.log("No collections found in configuration.");
472
- // return;
473
- // }
474
-
475
- // let needsUpdate = false;
476
- // let numUpdates = 0;
477
-
478
- // for (const collectionConfig of this.config.collections) {
479
- // const collectionKey = this.getCollectionKey(collectionConfig.name);
480
- // const collectionData = this.importMap.get(collectionKey);
481
- // const collectionImportDefs = collectionConfig.importDefs;
482
- // const collectionIdMappings = collectionImportDefs
483
- // .map((importDef) => importDef.idMappings)
484
- // .flat()
485
- // .filter((idMapping) => idMapping !== undefined && idMapping !== null);
486
- // if (!collectionData || !collectionData.data) continue;
487
- // for (const dataItem of collectionData.data) {
488
- // for (const idMapping of collectionIdMappings) {
489
- // // We know it's the users collection here
490
- // if (this.getCollectionKey(idMapping.targetCollection) === usersCollectionKey) {
491
- // const targetFieldKey = idMapping.targetFieldToMatch || idMapping.targetField;
492
- // if (targetFieldKey === )
493
- // const targetValue = dataItem.finalData[targetFieldKey];
494
- // const targetCollectionData = this.importMap.get(this.getCollectionKey(idMapping.targetCollection));
495
- // if (!targetCollectionData || !targetCollectionData.data) continue;
496
- // const foundData = targetCollectionData.data.filter(({ context }) => {
497
- // const targetValue = context[targetFieldKey];
498
- // const isMatch = `${targetValue}` === `${valueToMatch}`;
499
- // return isMatch && targetValue !== undefined && targetValue !== null;
500
- // });
501
- // }
502
- // }
503
- // }
504
- // }
505
- // }
506
-
507
- /**
508
- * Gets the value to match for a given key in the final data or context.
509
- * @param finalData - The final data object.
510
- * @param context - The context object.
511
- * @param key - The key to get the value for.
512
- * @returns The value to match for from finalData or Context
513
- */
514
- getValueFromData(finalData: any, context: any, key: string) {
515
- if (
516
- context[key] !== undefined &&
517
- context[key] !== null &&
518
- context[key] !== ""
519
- ) {
520
- return context[key];
521
- }
522
- return finalData[key];
523
- }
524
-
525
- updateOldReferencesForNew() {
526
- if (!this.config.collections) {
527
- return;
528
- }
529
-
530
- for (const collectionConfig of this.config.collections) {
531
- const collectionKey = this.getCollectionKey(collectionConfig.name);
532
- const collectionData = this.importMap.get(collectionKey);
533
-
534
- if (!collectionData || !collectionData.data) continue;
535
-
536
- MessageFormatter.processing(
537
- `Updating references for collection: ${collectionConfig.name}`,
538
- { prefix: "Data" }
539
- );
540
-
541
- let needsUpdate = false;
542
-
543
- // Iterate over each data item in the current collection
544
- for (let i = 0; i < collectionData.data.length; i++) {
545
- if (collectionConfig.importDefs) {
546
- for (const importDef of collectionConfig.importDefs) {
547
- if (importDef.idMappings) {
548
- for (const idMapping of importDef.idMappings) {
549
- const targetCollectionKey = this.getCollectionKey(
550
- idMapping.targetCollection
551
- );
552
- const fieldToSetKey =
553
- idMapping.fieldToSet || idMapping.sourceField;
554
- const targetFieldKey =
555
- idMapping.targetFieldToMatch || idMapping.targetField;
556
- const sourceValue = this.getValueFromData(
557
- collectionData.data[i].finalData,
558
- collectionData.data[i].context,
559
- idMapping.sourceField
560
- );
561
-
562
- // Skip if value to match is missing or empty
563
- if (
564
- !sourceValue ||
565
- isEmpty(sourceValue) ||
566
- sourceValue === null
567
- )
568
- continue;
569
-
570
- const isFieldToSetArray = collectionConfig.attributes.find(
571
- (attribute) => attribute.key === fieldToSetKey
572
- )?.array;
573
-
574
- const targetCollectionData =
575
- this.importMap.get(targetCollectionKey);
576
- if (!targetCollectionData || !targetCollectionData.data)
577
- continue;
578
-
579
- // Handle cases where sourceValue is an array
580
- const sourceValues = Array.isArray(sourceValue)
581
- ? sourceValue.map((sourceValue) => `${sourceValue}`)
582
- : [`${sourceValue}`];
583
- let newData = [];
584
-
585
- for (const valueToMatch of sourceValues) {
586
- // Find matching data in the target collection
587
- const foundData = targetCollectionData.data.filter(
588
- ({ context, finalData }) => {
589
- const targetValue = this.getValueFromData(
590
- finalData,
591
- context,
592
- targetFieldKey
593
- );
594
- const isMatch = `${targetValue}` === `${valueToMatch}`;
595
- // Ensure the targetValue is defined and not null
596
- return (
597
- isMatch &&
598
- targetValue !== undefined &&
599
- targetValue !== null
600
- );
601
- }
602
- );
603
-
604
- if (foundData.length) {
605
- newData.push(
606
- ...foundData.map((data) => {
607
- const newValue = this.getValueFromData(
608
- data.finalData,
609
- data.context,
610
- idMapping.targetField
611
- );
612
- return newValue;
613
- })
614
- );
615
- } else {
616
- logger.info(
617
- `No data found for collection: ${targetCollectionKey} with value: ${valueToMatch} for field: ${fieldToSetKey} -- idMapping: ${JSON.stringify(
618
- idMapping,
619
- null,
620
- 2
621
- )}`
622
- );
623
- }
624
- continue;
625
- }
626
-
627
- const getCurrentDataFiltered = (currentData: any) => {
628
- if (Array.isArray(currentData.finalData[fieldToSetKey])) {
629
- return currentData.finalData[fieldToSetKey].filter(
630
- (data: any) => !sourceValues.includes(`${data}`)
631
- );
632
- }
633
- return currentData.finalData[fieldToSetKey];
634
- };
635
-
636
- // Get the current data to be updated
637
- const currentDataFiltered = getCurrentDataFiltered(
638
- collectionData.data[i]
639
- );
640
-
641
- if (newData.length) {
642
- needsUpdate = true;
643
-
644
- // Handle cases where current data is an array
645
- if (isFieldToSetArray) {
646
- if (!currentDataFiltered) {
647
- // Set new data if current data is undefined
648
- collectionData.data[i].finalData[fieldToSetKey] =
649
- Array.isArray(newData) ? newData : [newData];
650
- } else {
651
- if (Array.isArray(currentDataFiltered)) {
652
- // Convert current data to array and merge if new data is non-empty array
653
- collectionData.data[i].finalData[fieldToSetKey] = [
654
- ...new Set(
655
- [...currentDataFiltered, ...newData].filter(
656
- (value: any) =>
657
- value !== null &&
658
- value !== undefined &&
659
- value !== ""
660
- )
661
- ),
662
- ];
663
- } else {
664
- // Merge arrays if new data is non-empty array and filter for uniqueness
665
- collectionData.data[i].finalData[fieldToSetKey] = [
666
- ...new Set(
667
- [
668
- ...(Array.isArray(currentDataFiltered)
669
- ? currentDataFiltered
670
- : [currentDataFiltered]),
671
- ...newData,
672
- ].filter(
673
- (value: any) =>
674
- value !== null &&
675
- value !== undefined &&
676
- value !== "" &&
677
- !sourceValues.includes(`${value}`)
678
- )
679
- ),
680
- ];
681
- }
682
- }
683
- } else {
684
- if (!currentDataFiltered) {
685
- // Set new data if current data is undefined
686
- collectionData.data[i].finalData[fieldToSetKey] =
687
- Array.isArray(newData) ? newData[0] : newData;
688
- } else if (Array.isArray(newData) && newData.length > 0) {
689
- // Convert current data to array and merge if new data is non-empty array, then filter for uniqueness
690
- // and take the first value, because it's an array and the attribute is not an array
691
- collectionData.data[i].finalData[fieldToSetKey] = [
692
- ...new Set(
693
- [currentDataFiltered, ...newData].filter(
694
- (value: any) =>
695
- value !== null &&
696
- value !== undefined &&
697
- value !== "" &&
698
- !sourceValues.includes(`${value}`)
699
- )
700
- ),
701
- ].slice(0, 1)[0];
702
- } else if (
703
- !Array.isArray(newData) &&
704
- newData !== undefined
705
- ) {
706
- // Simply update the field if new data is not an array and defined
707
- collectionData.data[i].finalData[fieldToSetKey] = newData;
708
- }
709
- }
710
- }
711
- }
712
- }
713
- }
714
- }
715
- }
716
-
717
- // Update the import map if any changes were made
718
- if (needsUpdate) {
719
- this.importMap.set(collectionKey, collectionData);
720
- }
721
- }
722
- }
723
-
724
- private writeMapsToJsonFile() {
725
- const outputDir = path.resolve(process.cwd(), "zlogs");
726
-
727
- // Ensure the logs directory exists
728
- if (!fs.existsSync(outputDir)) {
729
- fs.mkdirSync(outputDir);
730
- }
731
-
732
- // Helper function to write data to a file
733
- const writeToFile = (fileName: string, data: any) => {
734
- const outputFile = path.join(outputDir, fileName);
735
- fs.writeFile(outputFile, JSON.stringify(data, null, 2), "utf8", (err) => {
736
- if (err) {
737
- MessageFormatter.error(`Error writing data to ${fileName}`, err instanceof Error ? err : new Error(String(err)), { prefix: "Data" });
738
- return;
739
- }
740
- MessageFormatter.success(`Data successfully written to ${fileName}`, { prefix: "Data" });
741
- });
742
- };
743
-
744
- // Convert Maps to arrays of entries for serialization
745
- const oldIdToNewIdPerCollectionMap = Array.from(
746
- this.oldIdToNewIdPerCollectionMap.entries()
747
- ).map(([key, value]) => {
748
- return {
749
- collection: key,
750
- data: Array.from(value.entries()),
751
- };
752
- });
753
-
754
- const mergedUserMap = Array.from(this.mergedUserMap.entries());
755
-
756
- // Write each part to a separate file
757
- writeToFile(
758
- "oldIdToNewIdPerCollectionMap.json",
759
- oldIdToNewIdPerCollectionMap
760
- );
761
- writeToFile("mergedUserMap.json", mergedUserMap);
762
-
763
- // Write each collection's data to a separate file
764
- this.importMap.forEach((value, key) => {
765
- const data = {
766
- collection: key,
767
- data: value.data.map((item: any) => {
768
- return {
769
- finalData: item.finalData,
770
- context: item.context,
771
- };
772
- }),
773
- };
774
- writeToFile(`${key}.json`, data);
775
- });
776
- }
777
-
778
- /**
779
- * Prepares user data by checking for duplicates based on email or phone, adding to a duplicate map if found,
780
- * and then returning the transformed item without user-specific keys.
781
- *
782
- * @param item - The raw item to be processed.
783
- * @param attributeMappings - The attribute mappings for the item.
784
- * @returns The transformed item with user-specific keys removed.
785
- */
786
- prepareUserData(
787
- item: any,
788
- attributeMappings: AttributeMappings,
789
- primaryKeyField: string,
790
- newId: string
791
- ): {
792
- transformedItem: any;
793
- existingId: string | undefined;
794
- userData: {
795
- rawData: any;
796
- finalData: z.infer<typeof AuthUserCreateSchema>;
797
- };
798
- } {
799
- if (
800
- this.userIdSet.has(newId) ||
801
- this.userExistsMap.has(newId) ||
802
- Array.from(this.emailToUserIdMap.values()).includes(newId) ||
803
- Array.from(this.phoneToUserIdMap.values()).includes(newId)
804
- ) {
805
- newId = this.getTrueUniqueId(this.getCollectionKey("users"));
806
- }
807
- let transformedItem = this.transformData(item, attributeMappings);
808
- let userData = AuthUserCreateSchema.safeParse(transformedItem);
809
- if (userData.data?.email) {
810
- userData.data.email = userData.data.email.toLowerCase();
811
- }
812
- if (!userData.success || !(userData.data.email || userData.data.phone)) {
813
- logger.error(
814
- `Invalid user data: ${JSON.stringify(
815
- userData.error?.issues,
816
- undefined,
817
- 2
818
- )} or missing email/phone`
819
- );
820
-
821
- const userKeys = ["email", "phone", "name", "labels", "prefs"];
822
- userKeys.forEach((key) => {
823
- if (transformedItem.hasOwnProperty(key)) {
824
- delete transformedItem[key];
825
- }
826
- });
827
- return {
828
- transformedItem,
829
- existingId: undefined,
830
- userData: {
831
- rawData: item,
832
- finalData: transformedItem,
833
- },
834
- };
835
- }
836
- const email = userData.data.email?.toLowerCase();
837
- const phone = userData.data.phone;
838
- let existingId: string | undefined;
839
-
840
- // Check for duplicate email and phone
841
- if (email && this.emailToUserIdMap.has(email)) {
842
- existingId = this.emailToUserIdMap.get(email);
843
- if (phone && !this.phoneToUserIdMap.has(phone)) {
844
- this.phoneToUserIdMap.set(phone, newId);
845
- }
846
- } else if (phone && this.phoneToUserIdMap.has(phone)) {
847
- existingId = this.phoneToUserIdMap.get(phone);
848
- if (email && !this.emailToUserIdMap.has(email)) {
849
- this.emailToUserIdMap.set(email, newId);
850
- }
851
- } else {
852
- if (email) this.emailToUserIdMap.set(email, newId);
853
- if (phone) this.phoneToUserIdMap.set(phone, newId);
854
- }
855
-
856
- if (existingId) {
857
- userData.data.userId = existingId;
858
- const mergedUsers = this.mergedUserMap.get(existingId) || [];
859
- mergedUsers.push(`${item[primaryKeyField]}`);
860
- this.mergedUserMap.set(existingId, mergedUsers);
861
- const userFound = this.importMap
862
- .get(this.getCollectionKey("users"))
863
- ?.data.find((userDataExisting) => {
864
- let userIdToMatch: string | undefined;
865
- if (userDataExisting?.finalData?.userId) {
866
- userIdToMatch = userDataExisting?.finalData?.userId;
867
- } else if (userDataExisting?.finalData?.docId) {
868
- userIdToMatch = userDataExisting?.finalData?.docId;
869
- } else if (userDataExisting?.context?.userId) {
870
- userIdToMatch = userDataExisting.context.userId;
871
- } else if (userDataExisting?.context?.docId) {
872
- userIdToMatch = userDataExisting.context.docId;
873
- }
874
- return userIdToMatch === existingId;
875
- });
876
- if (userFound) {
877
- userFound.finalData.userId = existingId;
878
- userFound.finalData.docId = existingId;
879
- this.userIdSet.add(existingId);
880
- transformedItem = {
881
- ...transformedItem,
882
- userId: existingId,
883
- docId: existingId,
884
- };
885
- }
886
-
887
- const userKeys = ["email", "phone", "name", "labels", "prefs"];
888
- userKeys.forEach((key) => {
889
- if (transformedItem.hasOwnProperty(key)) {
890
- delete transformedItem[key];
891
- }
892
- });
893
- return {
894
- transformedItem,
895
- existingId,
896
- userData: {
897
- rawData: userFound?.rawData,
898
- finalData: userFound?.finalData,
899
- },
900
- };
901
- } else {
902
- existingId = newId;
903
- userData.data.userId = existingId;
904
- }
905
-
906
- const userKeys = ["email", "phone", "name", "labels", "prefs"];
907
- userKeys.forEach((key) => {
908
- if (transformedItem.hasOwnProperty(key)) {
909
- delete transformedItem[key];
910
- }
911
- });
912
-
913
- const usersMap = this.importMap.get(this.getCollectionKey("users"));
914
- const userDataToAdd = {
915
- rawData: item,
916
- finalData: userData.data,
917
- context: {},
918
- };
919
- this.importMap.set(this.getCollectionKey("users"), {
920
- data: [...(usersMap?.data || []), userDataToAdd],
921
- });
922
- this.userIdSet.add(existingId);
923
-
924
- return {
925
- transformedItem,
926
- existingId,
927
- userData: userDataToAdd,
928
- };
929
- }
930
-
931
- /**
932
- * Prepares the data for creating user collection documents.
933
- * This involves loading the data, transforming it according to the import definition,
934
- * and handling the creation of new unique IDs for each item.
935
- *
936
- * @param db - The database configuration.
937
- * @param collection - The collection configuration.
938
- * @param importDef - The import definition containing the attribute mappings and other relevant info.
939
- */
940
- async prepareUserCollectionCreateData(
941
- db: ConfigDatabase,
942
- collection: CollectionCreate,
943
- importDef: ImportDef
944
- ): Promise<void> {
945
- // Load the raw data based on the import definition
946
- const rawData = this.loadData(importDef);
947
- let operationId = this.collectionImportOperations.get(
948
- this.getCollectionKey(collection.name)
949
- );
950
- // Initialize a new map for old ID to new ID mappings
951
- const oldIdToNewIdMap = new Map<string, string>();
952
- // Retrieve or initialize the collection-specific old ID to new ID map
953
- const collectionOldIdToNewIdMap =
954
- this.oldIdToNewIdPerCollectionMap.get(
955
- this.getCollectionKey(collection.name)
956
- ) ||
957
- this.oldIdToNewIdPerCollectionMap
958
- .set(this.getCollectionKey(collection.name), oldIdToNewIdMap)
959
- .get(this.getCollectionKey(collection.name));
960
- const adapter = new LegacyAdapter(this.database.client);
961
- if (!operationId) {
962
- const collectionImportOperation = await findOrCreateOperation(
963
- adapter,
964
- db.$id,
965
- "importData",
966
- collection.$id!
967
- );
968
- // Store the operation ID in the map
969
- this.collectionImportOperations.set(
970
- this.getCollectionKey(collection.name),
971
- collectionImportOperation.$id
972
- );
973
- operationId = collectionImportOperation.$id;
974
- }
975
- if (operationId) {
976
- await updateOperation(adapter, db.$id, operationId, {
977
- status: "ready",
978
- total: rawData.length,
979
- });
980
- }
981
- // Retrieve the current user data and the current collection data from the import map
982
- const currentUserData = this.importMap.get(this.getCollectionKey("users"));
983
- const currentData = this.importMap.get(
984
- this.getCollectionKey(collection.name)
985
- );
986
- // Log errors if the necessary data is not found in the import map
987
- if (!currentUserData) {
988
- logger.error(
989
- `No data found for collection ${"users"} for createDef but it says it's supposed to have one...`
990
- );
991
- return;
992
- } else if (!currentData) {
993
- logger.error(
994
- `No data found for collection ${collection.name} for createDef but it says it's supposed to have one...`
995
- );
996
- return;
997
- }
998
- // Iterate through each item in the raw data
999
- for (const item of rawData) {
1000
- // Prepare user data, check for duplicates, and remove user-specific keys
1001
- let { transformedItem, existingId, userData } = this.prepareUserData(
1002
- item,
1003
- importDef.attributeMappings,
1004
- importDef.primaryKeyField,
1005
- this.getTrueUniqueId(this.getCollectionKey("users"))
1006
- );
1007
-
1008
- logger.info(
1009
- `In create user -- transformedItem: ${JSON.stringify(
1010
- transformedItem,
1011
- null,
1012
- 2
1013
- )}`
1014
- );
1015
-
1016
- // Generate a new unique ID for the item or use existing ID
1017
- if (!existingId && !userData.finalData?.userId) {
1018
- // No existing user ID, generate a new unique ID
1019
- existingId = this.getTrueUniqueId(
1020
- this.getCollectionKey(collection.name)
1021
- );
1022
- transformedItem = {
1023
- ...transformedItem,
1024
- userId: existingId,
1025
- docId: existingId,
1026
- };
1027
- } else if (!existingId && userData.finalData?.userId) {
1028
- // Existing user ID, use it as the new ID
1029
- existingId = userData.finalData.userId;
1030
- transformedItem = {
1031
- ...transformedItem,
1032
- userId: existingId,
1033
- docId: existingId,
1034
- };
1035
- }
1036
-
1037
- // Create a context object for the item, including the new ID
1038
- let context = this.createContext(db, collection, item, existingId!);
1039
-
1040
- // Merge the transformed data into the context
1041
- context = { ...context, ...transformedItem, ...userData.finalData };
1042
-
1043
- // If a primary key field is defined, handle the ID mapping and check for duplicates
1044
- if (importDef.primaryKeyField) {
1045
- const oldId = item[importDef.primaryKeyField];
1046
-
1047
- // Check if the oldId already exists to handle potential duplicates
1048
- if (
1049
- this.oldIdToNewIdPerCollectionMap
1050
- .get(this.getCollectionKey(collection.name))
1051
- ?.has(`${oldId}`)
1052
- ) {
1053
- // Found a duplicate oldId, now decide how to merge or handle these duplicates
1054
- for (const data of currentData.data) {
1055
- if (
1056
- data.finalData.docId === oldId ||
1057
- data.finalData.userId === oldId ||
1058
- data.context.docId === oldId ||
1059
- data.context.userId === oldId
1060
- ) {
1061
- transformedItem = this.mergeObjects(
1062
- data.finalData,
1063
- transformedItem
1064
- );
1065
- }
1066
- }
1067
- } else {
1068
- // No duplicate found, simply map the oldId to the new itemId
1069
- collectionOldIdToNewIdMap?.set(`${oldId}`, `${existingId}`);
1070
- }
1071
- }
1072
-
1073
- // Handle merging for currentUserData
1074
- for (let i = 0; i < currentUserData.data.length; i++) {
1075
- const currentUserDataItem = currentUserData.data[i];
1076
- const samePhones =
1077
- currentUserDataItem.finalData.phone &&
1078
- transformedItem.phone &&
1079
- currentUserDataItem.finalData.phone === transformedItem.phone;
1080
- const sameEmails =
1081
- currentUserDataItem.finalData.email &&
1082
- transformedItem.email &&
1083
- currentUserDataItem.finalData.email === transformedItem.email;
1084
- if (
1085
- (currentUserDataItem.finalData.docId === existingId ||
1086
- currentUserDataItem.finalData.userId === existingId) &&
1087
- (samePhones || sameEmails) &&
1088
- currentUserDataItem.finalData &&
1089
- userData.finalData
1090
- ) {
1091
- const userDataMerged = this.mergeObjects(
1092
- currentUserData.data[i].finalData,
1093
- userData.finalData
1094
- );
1095
- currentUserData.data[i].finalData = userDataMerged;
1096
- this.importMap.set(this.getCollectionKey("users"), currentUserData);
1097
- }
1098
- }
1099
- // Update the attribute mappings with any actions that need to be performed post-import
1100
- // We added the basePath to get the folder from the filePath
1101
- const mappingsWithActions = this.getAttributeMappingsWithActions(
1102
- importDef.attributeMappings,
1103
- context,
1104
- transformedItem
1105
- );
1106
- // Update the import definition with the new attribute mappings
1107
- const newImportDef = {
1108
- ...importDef,
1109
- attributeMappings: mappingsWithActions,
1110
- };
1111
-
1112
- const updatedData = this.importMap.get(
1113
- this.getCollectionKey(collection.name)
1114
- )!;
1115
-
1116
- let foundData = false;
1117
- for (let i = 0; i < updatedData.data.length; i++) {
1118
- if (
1119
- updatedData.data[i].finalData.docId === existingId ||
1120
- updatedData.data[i].finalData.userId === existingId ||
1121
- updatedData.data[i].context.docId === existingId ||
1122
- updatedData.data[i].context.userId === existingId
1123
- ) {
1124
- updatedData.data[i].finalData = this.mergeObjects(
1125
- updatedData.data[i].finalData,
1126
- transformedItem
1127
- );
1128
- updatedData.data[i].context = this.mergeObjects(
1129
- updatedData.data[i].context,
1130
- context
1131
- );
1132
- const mergedImportDef = {
1133
- ...updatedData.data[i].importDef,
1134
- idMappings: [
1135
- ...(updatedData.data[i].importDef?.idMappings || []),
1136
- ...(newImportDef.idMappings || []),
1137
- ],
1138
- attributeMappings: [
1139
- ...(updatedData.data[i].importDef?.attributeMappings || []),
1140
- ...(newImportDef.attributeMappings || []),
1141
- ],
1142
- };
1143
- updatedData.data[i].importDef = mergedImportDef as ImportDef;
1144
- this.importMap.set(
1145
- this.getCollectionKey(collection.name),
1146
- updatedData
1147
- );
1148
- this.oldIdToNewIdPerCollectionMap.set(
1149
- this.getCollectionKey(collection.name),
1150
- collectionOldIdToNewIdMap!
1151
- );
1152
-
1153
- foundData = true;
1154
- }
1155
- }
1156
- if (!foundData) {
1157
- // Add new data to the associated collection
1158
- updatedData.data.push({
1159
- rawData: item,
1160
- context: context,
1161
- importDef: newImportDef,
1162
- finalData: transformedItem,
1163
- });
1164
- this.importMap.set(this.getCollectionKey(collection.name), updatedData);
1165
- this.oldIdToNewIdPerCollectionMap.set(
1166
- this.getCollectionKey(collection.name),
1167
- collectionOldIdToNewIdMap!
1168
- );
1169
- }
1170
- }
1171
- }
1172
-
1173
- /**
1174
- * Prepares the data for creating documents in a collection.
1175
- * This involves loading the data, transforming it, and handling ID mappings.
1176
- *
1177
- * @param db - The database configuration.
1178
- * @param collection - The collection configuration.
1179
- * @param importDef - The import definition containing the attribute mappings and other relevant info.
1180
- */
1181
- async prepareCreateData(
1182
- db: ConfigDatabase,
1183
- collection: CollectionCreate,
1184
- importDef: ImportDef
1185
- ): Promise<void> {
1186
- // Load the raw data based on the import definition
1187
- const rawData = this.loadData(importDef);
1188
- let operationId = this.collectionImportOperations.get(
1189
- this.getCollectionKey(collection.name)
1190
- );
1191
- const adapter = new LegacyAdapter(this.database.client);
1192
- if (!operationId) {
1193
- const collectionImportOperation = await findOrCreateOperation(
1194
- adapter,
1195
- db.$id,
1196
- "importData",
1197
- collection.$id!
1198
- );
1199
- // Store the operation ID in the map
1200
- this.collectionImportOperations.set(
1201
- this.getCollectionKey(collection.name),
1202
- collectionImportOperation.$id
1203
- );
1204
- operationId = collectionImportOperation.$id;
1205
- }
1206
- if (operationId) {
1207
- await updateOperation(adapter, db.$id, operationId, {
1208
- status: "ready",
1209
- total: rawData.length,
1210
- });
1211
- }
1212
- // Initialize a new map for old ID to new ID mappings
1213
- const oldIdToNewIdMapNew = new Map<string, string>();
1214
- // Retrieve or initialize the collection-specific old ID to new ID map
1215
- const collectionOldIdToNewIdMap =
1216
- this.oldIdToNewIdPerCollectionMap.get(
1217
- this.getCollectionKey(collection.name)
1218
- ) ||
1219
- this.oldIdToNewIdPerCollectionMap
1220
- .set(this.getCollectionKey(collection.name), oldIdToNewIdMapNew)
1221
- .get(this.getCollectionKey(collection.name));
1222
- const isRegions = collection.name.toLowerCase() === "regions";
1223
- // Iterate through each item in the raw data
1224
- for (const item of rawData) {
1225
- // Generate a new unique ID for the item
1226
- const itemIdNew = this.getTrueUniqueId(
1227
- this.getCollectionKey(collection.name)
1228
- );
1229
- if (isRegions) {
1230
- logger.info(`Creating region: ${JSON.stringify(item, null, 2)}`);
1231
- }
1232
- // Retrieve the current collection data from the import map
1233
- const currentData = this.importMap.get(
1234
- this.getCollectionKey(collection.name)
1235
- );
1236
- // Create a context object for the item, including the new ID
1237
- let context = this.createContext(db, collection, item, itemIdNew);
1238
- // Transform the item data based on the attribute mappings
1239
- let transformedData = this.transformData(
1240
- item,
1241
- importDef.attributeMappings
1242
- );
1243
- // If a primary key field is defined, handle the ID mapping and check for duplicates
1244
- if (importDef.primaryKeyField) {
1245
- const oldId = item[importDef.primaryKeyField];
1246
- if (collectionOldIdToNewIdMap?.has(`${oldId}`)) {
1247
- logger.error(
1248
- `Collection ${collection.name} has multiple documents with the same primary key ${oldId}`
1249
- );
1250
- continue;
1251
- }
1252
- collectionOldIdToNewIdMap?.set(`${oldId}`, `${itemIdNew}`);
1253
- }
1254
- // Merge the transformed data into the context
1255
- context = { ...context, ...transformedData };
1256
- // Validate the item before proceeding
1257
- const isValid = this.importDataActions.validateItem(
1258
- transformedData,
1259
- importDef.attributeMappings,
1260
- context
1261
- );
1262
- if (!isValid) {
1263
- continue;
1264
- }
1265
- // Update the attribute mappings with any actions that need to be performed post-import
1266
- // We added the basePath to get the folder from the filePath
1267
- const mappingsWithActions = this.getAttributeMappingsWithActions(
1268
- importDef.attributeMappings,
1269
- context,
1270
- transformedData
1271
- );
1272
- // Update the import definition with the new attribute mappings
1273
- const newImportDef = {
1274
- ...importDef,
1275
- attributeMappings: mappingsWithActions,
1276
- };
1277
- // If the current collection data exists, add the item with its context and final data
1278
- if (currentData && currentData.data) {
1279
- currentData.data.push({
1280
- rawData: item,
1281
- context: context,
1282
- importDef: newImportDef,
1283
- finalData: transformedData,
1284
- });
1285
- this.importMap.set(this.getCollectionKey(collection.name), currentData);
1286
- this.oldIdToNewIdPerCollectionMap.set(
1287
- this.getCollectionKey(collection.name),
1288
- collectionOldIdToNewIdMap!
1289
- );
1290
- } else {
1291
- logger.error(
1292
- `No data found for collection ${collection.name} for createDef but it says it's supposed to have one...`
1293
- );
1294
- continue;
1295
- }
1296
- }
1297
- }
1298
-
1299
- /**
1300
- * Prepares the data for updating documents within a collection.
1301
- * This method loads the raw data based on the import definition, transforms it according to the attribute mappings,
1302
- * finds the new ID for each item based on the primary key or update mapping, and then validates the transformed data.
1303
- * If the data is valid, it updates the import definition with any post-import actions and adds the item to the current collection data.
1304
- *
1305
- * @param db - The database configuration.
1306
- * @param collection - The collection configuration.
1307
- * @param importDef - The import definition containing the attribute mappings and other relevant info.
1308
- */
1309
- async prepareUpdateData(
1310
- db: ConfigDatabase,
1311
- collection: CollectionCreate,
1312
- importDef: ImportDef
1313
- ) {
1314
- const currentData = this.importMap.get(
1315
- this.getCollectionKey(collection.name)
1316
- );
1317
- const oldIdToNewIdMap = this.oldIdToNewIdPerCollectionMap.get(
1318
- this.getCollectionKey(collection.name)
1319
- );
1320
-
1321
- if (
1322
- !(currentData?.data && currentData?.data.length > 0) &&
1323
- !oldIdToNewIdMap
1324
- ) {
1325
- logger.error(
1326
- `No data found for collection ${collection.name} for updateDef but it says it's supposed to have one...`
1327
- );
1328
- return;
1329
- }
1330
-
1331
- const rawData = this.loadData(importDef);
1332
- const operationId = this.collectionImportOperations.get(
1333
- this.getCollectionKey(collection.name)
1334
- );
1335
- if (!operationId) {
1336
- throw new Error(
1337
- `No import operation found for collection ${collection.name}`
1338
- );
1339
- }
1340
-
1341
- for (const item of rawData) {
1342
- let transformedData = this.transformData(
1343
- item,
1344
- importDef.attributeMappings
1345
- );
1346
- let newId: string | undefined;
1347
- let oldId: string | undefined;
1348
- let itemDataToUpdate: CollectionImportData["data"][number] | undefined;
1349
-
1350
- // Try to find itemDataToUpdate using updateMapping
1351
- if (importDef.updateMapping) {
1352
- oldId =
1353
- item[importDef.updateMapping.originalIdField] ||
1354
- transformedData[importDef.updateMapping.originalIdField];
1355
- if (oldId) {
1356
- itemDataToUpdate = currentData?.data.find(
1357
- ({ context, finalData }) => {
1358
- const targetField =
1359
- importDef.updateMapping!.targetField ??
1360
- importDef.updateMapping!.originalIdField;
1361
-
1362
- return (
1363
- `${context[targetField]}` === `${oldId}` ||
1364
- `${finalData[targetField]}` === `${oldId}`
1365
- );
1366
- }
1367
- );
1368
-
1369
- if (itemDataToUpdate) {
1370
- newId = itemDataToUpdate.context.docId;
1371
- }
1372
- }
1373
- }
1374
-
1375
- // If updateMapping is not defined or did not find the item, use primaryKeyField
1376
- if (!itemDataToUpdate && importDef.primaryKeyField) {
1377
- oldId =
1378
- item[importDef.primaryKeyField] ||
1379
- transformedData[importDef.primaryKeyField];
1380
- if (oldId && oldId.length > 0) {
1381
- newId = oldIdToNewIdMap?.get(`${oldId}`);
1382
- if (
1383
- !newId &&
1384
- this.getCollectionKey(this.config.usersCollectionName) ===
1385
- this.getCollectionKey(collection.name)
1386
- ) {
1387
- for (const [key, value] of this.mergedUserMap.entries()) {
1388
- if (value.includes(`${oldId}`)) {
1389
- newId = key;
1390
- break;
1391
- }
1392
- }
1393
- }
1394
- }
1395
-
1396
- if (oldId && !itemDataToUpdate) {
1397
- itemDataToUpdate = currentData?.data.find(
1398
- (data) =>
1399
- `${data.context[importDef.primaryKeyField]}` === `${oldId}`
1400
- );
1401
- }
1402
- }
1403
-
1404
- if (!oldId) {
1405
- logger.error(
1406
- `No old ID found (to update another document with) in prepareUpdateData for ${
1407
- collection.name
1408
- }, ${JSON.stringify(item, null, 2)}`
1409
- );
1410
- continue;
1411
- }
1412
-
1413
- if (!newId && !itemDataToUpdate) {
1414
- logger.error(
1415
- `No new id && no data found for collection ${
1416
- collection.name
1417
- } for updateDef ${JSON.stringify(
1418
- item,
1419
- null,
1420
- 2
1421
- )} but it says it's supposed to have one...`
1422
- );
1423
- continue;
1424
- } else if (itemDataToUpdate) {
1425
- newId =
1426
- itemDataToUpdate.finalData.docId || itemDataToUpdate.context.docId;
1427
- if (!newId) {
1428
- logger.error(
1429
- `No new id found for collection ${
1430
- collection.name
1431
- } for updateDef ${JSON.stringify(
1432
- item,
1433
- null,
1434
- 2
1435
- )} but has itemDataToUpdate ${JSON.stringify(
1436
- itemDataToUpdate,
1437
- null,
1438
- 2
1439
- )} but it says it's supposed to have one...`
1440
- );
1441
- continue;
1442
- }
1443
- }
1444
-
1445
- if (!itemDataToUpdate || !newId) {
1446
- logger.error(
1447
- `No data or ID (docId) found for collection ${
1448
- collection.name
1449
- } for updateDef ${JSON.stringify(
1450
- item,
1451
- null,
1452
- 2
1453
- )} but it says it's supposed to have one...`
1454
- );
1455
- continue;
1456
- }
1457
-
1458
- transformedData = this.mergeObjects(
1459
- itemDataToUpdate.finalData,
1460
- transformedData
1461
- );
1462
-
1463
- // Create a context object for the item, including the new ID and transformed data
1464
- let context = itemDataToUpdate.context;
1465
- context = this.mergeObjects(context, transformedData);
1466
-
1467
- // Validate the item before proceeding
1468
- const isValid = this.importDataActions.validateItem(
1469
- item,
1470
- importDef.attributeMappings,
1471
- context
1472
- );
1473
-
1474
- if (!isValid) {
1475
- logger.info(
1476
- `Skipping item: ${JSON.stringify(item, null, 2)} because it's invalid`
1477
- );
1478
- continue;
1479
- }
1480
-
1481
- // Update the attribute mappings with any actions that need to be performed post-import
1482
- // We added the basePath to get the folder from the filePath
1483
- const mappingsWithActions = this.getAttributeMappingsWithActions(
1484
- importDef.attributeMappings,
1485
- context,
1486
- transformedData
1487
- );
1488
-
1489
- // Update the import definition with the new attribute mappings
1490
- const newImportDef = {
1491
- ...importDef,
1492
- attributeMappings: mappingsWithActions,
1493
- };
1494
-
1495
- if (itemDataToUpdate) {
1496
- itemDataToUpdate.finalData = this.mergeObjects(
1497
- itemDataToUpdate.finalData,
1498
- transformedData
1499
- );
1500
- itemDataToUpdate.context = context;
1501
-
1502
- // Fix: Ensure we properly merge the attribute mappings and their actions
1503
- const mergedAttributeMappings = newImportDef.attributeMappings.map(
1504
- (newMapping) => {
1505
- const existingMapping =
1506
- itemDataToUpdate.importDef?.attributeMappings.find(
1507
- (m) => m.targetKey === newMapping.targetKey
1508
- );
1509
-
1510
- return {
1511
- ...newMapping,
1512
- postImportActions: [
1513
- ...(existingMapping?.postImportActions || []),
1514
- ...(newMapping.postImportActions || []),
1515
- ],
1516
- };
1517
- }
1518
- );
1519
-
1520
- itemDataToUpdate.importDef = {
1521
- ...newImportDef,
1522
- attributeMappings: mergedAttributeMappings,
1523
- };
1524
-
1525
- // Debug logging
1526
- if (
1527
- mergedAttributeMappings.some((m) => m.postImportActions?.length > 0)
1528
- ) {
1529
- logger.info(
1530
- `Post-import actions for ${collection.name}: ${JSON.stringify(
1531
- mergedAttributeMappings
1532
- .filter((m) => m.postImportActions?.length > 0)
1533
- .map((m) => ({
1534
- targetKey: m.targetKey,
1535
- actions: m.postImportActions,
1536
- })),
1537
- null,
1538
- 2
1539
- )}`
1540
- );
1541
- }
1542
- } else {
1543
- currentData!.data.push({
1544
- rawData: item,
1545
- context: context,
1546
- importDef: newImportDef,
1547
- finalData: transformedData,
1548
- });
1549
- }
1550
-
1551
- // Since we're modifying currentData in place, we ensure no duplicates are added
1552
- this.importMap.set(this.getCollectionKey(collection.name), currentData!);
1553
- }
1554
- }
1555
-
1556
- private updateReferencesBasedOnAttributeMappings() {
1557
- if (!this.config.collections) {
1558
- return;
1559
- }
1560
- this.config.collections.forEach((collectionConfig) => {
1561
- const collectionName = collectionConfig.name;
1562
- const collectionData = this.importMap.get(
1563
- this.getCollectionKey(collectionName)
1564
- );
1565
-
1566
- if (!collectionData) {
1567
- logger.error(`No data found for collection ${collectionName}`);
1568
- return;
1569
- }
1570
-
1571
- collectionData.data.forEach((dataItem) => {
1572
- collectionConfig.importDefs.forEach((importDef) => {
1573
- if (!importDef.idMappings) return; // Skip collections without idMappings
1574
- importDef.idMappings.forEach((mapping) => {
1575
- if (mapping && mapping.targetField) {
1576
- const idsToUpdate = Array.isArray(
1577
- dataItem[mapping.targetField as keyof typeof dataItem]
1578
- )
1579
- ? dataItem[mapping.targetField as keyof typeof dataItem]
1580
- : [dataItem[mapping.targetField as keyof typeof dataItem]];
1581
- const updatedIds = idsToUpdate.map((id: string) =>
1582
- this.getMergedId(id, mapping.targetCollection)
1583
- );
1584
-
1585
- // Update the dataItem with the new IDs
1586
- dataItem[mapping.targetField as keyof typeof dataItem] =
1587
- Array.isArray(
1588
- dataItem[mapping.targetField as keyof typeof dataItem]
1589
- )
1590
- ? updatedIds
1591
- : updatedIds[0];
1592
- }
1593
- });
1594
- });
1595
- });
1596
- });
1597
- }
1598
-
1599
- private getMergedId(oldId: string, relatedCollectionName: string): string {
1600
- // Retrieve the old to new ID map for the related collection
1601
- const oldToNewIdMap = this.oldIdToNewIdPerCollectionMap.get(
1602
- this.getCollectionKey(relatedCollectionName)
1603
- );
1604
-
1605
- // If there's a mapping for the old ID, return the new ID
1606
- if (oldToNewIdMap && oldToNewIdMap.has(`${oldId}`)) {
1607
- return oldToNewIdMap.get(`${oldId}`)!; // The non-null assertion (!) is used because we checked if the map has the key
1608
- }
1609
-
1610
- // If no mapping is found, return the old ID as a fallback
1611
- return oldId;
1612
- }
1613
-
1614
- /**
1615
- * Generates attribute mappings with post-import actions based on the provided attribute mappings.
1616
- * This method checks each mapping for a fileData attribute and adds a post-import action to create a file
1617
- * and update the field with the file's ID if necessary.
1618
- *
1619
- * @param attributeMappings - The attribute mappings from the import definition.
1620
- * @param context - The context object containing information about the database, collection, and document.
1621
- * @param item - The item being imported, used for resolving template paths in fileData mappings.
1622
- * @returns The attribute mappings updated with any necessary post-import actions.
1623
- */
1624
- getAttributeMappingsWithActions(
1625
- attributeMappings: AttributeMappings,
1626
- context: any,
1627
- item: any
1628
- ) {
1629
- // Iterate over each attribute mapping to check for fileData attributes
1630
- return attributeMappings.map((mapping) => {
1631
- if (mapping.fileData) {
1632
- // Resolve the file path using the provided template, context, and item
1633
- let mappingFilePath = this.importDataActions.resolveTemplate(
1634
- mapping.fileData.path,
1635
- context,
1636
- item
1637
- );
1638
- // Ensure the file path is absolute if it doesn't start with "http"
1639
- if (!mappingFilePath.toLowerCase().startsWith("http")) {
1640
- // First try the direct path
1641
- let fullPath = path.resolve(this.appwriteFolderPath, mappingFilePath);
1642
-
1643
- // If file doesn't exist, search in subdirectories
1644
- if (!fs.existsSync(fullPath)) {
1645
- const findFileInDir = (dir: string): string | null => {
1646
- const files = fs.readdirSync(dir);
1647
-
1648
- for (const file of files) {
1649
- const filePath = path.join(dir, file);
1650
- const stat = fs.statSync(filePath);
1651
-
1652
- if (stat.isDirectory()) {
1653
- // Recursively search subdirectories
1654
- const found = findFileInDir(filePath);
1655
- if (found) return found;
1656
- } else if (file === path.basename(mappingFilePath)) {
1657
- return filePath;
1658
- }
1659
- }
1660
- return null;
1661
- };
1662
-
1663
- const foundPath = findFileInDir(this.appwriteFolderPath);
1664
- if (foundPath) {
1665
- mappingFilePath = foundPath;
1666
- } else {
1667
- logger.warn(
1668
- `File not found in any subdirectory: ${mappingFilePath}`
1669
- );
1670
- // Keep the original resolved path as fallback
1671
- mappingFilePath = fullPath;
1672
- }
1673
- } else {
1674
- mappingFilePath = fullPath;
1675
- }
1676
- }
1677
- // Define the after-import action to create a file and update the field
1678
- const afterImportAction = {
1679
- action: "createFileAndUpdateField",
1680
- params: [
1681
- "{dbId}",
1682
- "{collId}",
1683
- "{docId}",
1684
- mapping.targetKey,
1685
- `${this.config!.documentBucketId}_${context.dbName
1686
- .toLowerCase()
1687
- .replace(" ", "")}`, // Assuming 'images' is your bucket ID
1688
- mappingFilePath,
1689
- mapping.fileData.name,
1690
- ],
1691
- };
1692
- // Add the after-import action to the mapping's postImportActions array
1693
- const postImportActions = mapping.postImportActions
1694
- ? [...mapping.postImportActions, afterImportAction]
1695
- : [afterImportAction];
1696
- return { ...mapping, postImportActions };
1697
- }
1698
- // Return the mapping unchanged if no fileData attribute is found
1699
- return mapping;
1700
- });
1701
- }
1702
- }
1
+ import type { ImportDataActions } from "./importDataActions.js";
2
+ import {
3
+ AttributeMappingsSchema,
4
+ CollectionCreateSchema,
5
+ importDefSchema,
6
+ type AppwriteConfig,
7
+ type AttributeMappings,
8
+ type CollectionCreate,
9
+ type ConfigDatabase,
10
+ type IdMapping,
11
+ type ImportDef,
12
+ type ImportDefs,
13
+ type RelationshipAttribute,
14
+ } from "appwrite-utils";
15
+ import path from "path";
16
+ import fs from "fs";
17
+ import { convertObjectByAttributeMappings } from "appwrite-utils-helpers";
18
+ import { z } from "zod";
19
+ import { checkForCollection } from "../collections/methods.js";
20
+ import { ID, Users, type Databases } from "node-appwrite";
21
+ import { logger, AdapterFactory, type DatabaseAdapter, MessageFormatter } from "appwrite-utils-helpers";
22
+ import { findOrCreateOperation, updateOperation } from "../shared/migrationHelpers.js";
23
+ import { AuthUserCreateSchema } from "../schemas/authUser.js";
24
+ import { UsersController } from "../users/methods.js";
25
+ import { isEmpty } from "es-toolkit/compat";
26
+
27
+ // Define a schema for the structure of collection import data using Zod for validation
28
+ export const CollectionImportDataSchema = z.object({
29
+ // Optional collection creation schema
30
+ collection: CollectionCreateSchema.optional(),
31
+ // Array of data objects each containing rawData, finalData, context, and an import definition
32
+ data: z.array(
33
+ z.object({
34
+ rawData: z.any(), // The initial raw data
35
+ finalData: z.any(), // The transformed data ready for import
36
+ context: z.any(), // Additional context for the data transformation
37
+ importDef: importDefSchema.optional(), // The import definition schema
38
+ })
39
+ ),
40
+ });
41
+
42
+ // Infer the TypeScript type from the Zod schema
43
+ export type CollectionImportData = z.infer<typeof CollectionImportDataSchema>;
44
+
45
+ // DataLoader class to handle the loading of data into collections
46
+ export class DataLoader {
47
+ // Private member variables to hold configuration and state
48
+ private appwriteFolderPath: string;
49
+ private importDataActions: ImportDataActions;
50
+ private database: Databases;
51
+ private usersController: UsersController;
52
+ private config: AppwriteConfig;
53
+ // Map to hold the import data for each collection by name
54
+ importMap = new Map<string, CollectionImportData>();
55
+ // Map to track old to new ID mappings for each collection, if applicable
56
+ private oldIdToNewIdPerCollectionMap = new Map<string, Map<string, string>>();
57
+ // Map to hold the import operation ID for each collection
58
+ collectionImportOperations = new Map<string, string>();
59
+ // Map to hold the merged user map for relationship resolution
60
+ // Will hold an array of the old user ID's that are mapped to the same new user ID
61
+ // For example, if there are two users with the same email, they will both be mapped to the same new user ID
62
+ // Prevents duplicate users with the other two maps below it and allows me to keep the old ID's
63
+ private mergedUserMap = new Map<string, string[]>();
64
+ // Maps to hold email and phone to user ID mappings for unique-ness in User Accounts
65
+ private emailToUserIdMap = new Map<string, string>();
66
+ private phoneToUserIdMap = new Map<string, string>();
67
+ private userIdSet = new Set<string>();
68
+ userExistsMap = new Map<string, boolean>();
69
+ private shouldWriteFile = false;
70
+ private _adapter: DatabaseAdapter | null = null;
71
+
72
+ private async getAdapter(): Promise<DatabaseAdapter> {
73
+ if (!this._adapter) {
74
+ const { adapter } = await AdapterFactory.createFromConfig(this.config);
75
+ this._adapter = adapter;
76
+ }
77
+ return this._adapter;
78
+ }
79
+
80
+ // Constructor to initialize the DataLoader with necessary configurations
81
+ constructor(
82
+ appwriteFolderPath: string,
83
+ importDataActions: ImportDataActions,
84
+ database: Databases,
85
+ config: AppwriteConfig,
86
+ shouldWriteFile?: boolean
87
+ ) {
88
+ this.appwriteFolderPath = appwriteFolderPath;
89
+ this.importDataActions = importDataActions;
90
+ this.database = database;
91
+ this.usersController = new UsersController(config, database);
92
+ this.config = config;
93
+ this.shouldWriteFile = shouldWriteFile || false;
94
+ }
95
+
96
+ // Helper method to generate a consistent key for collections
97
+ getCollectionKey(name: string) {
98
+ return name.toLowerCase().replace(" ", "");
99
+ }
100
+
101
+ /**
102
+ * Merges two objects by updating the source object with the target object's values.
103
+ * It iterates through the target object's keys and updates the source object if:
104
+ * - The source object has the key.
105
+ * - The target object's value for that key is not null, undefined, or an empty string.
106
+ * - If the target object has an array value, it concatenates the values and removes duplicates.
107
+ *
108
+ * @param source - The source object to be updated.
109
+ * @param target - The target object with values to update the source object.
110
+ * @returns The updated source object.
111
+ */
112
+ mergeObjects(source: any, update: any): any {
113
+ // Create a new object to hold the merged result
114
+ const result = { ...source };
115
+
116
+ // Loop through the keys of the object we care about
117
+ for (const [key, value] of Object.entries(source)) {
118
+ // Check if the key exists in the target object
119
+ if (!Object.hasOwn(update, key)) {
120
+ // If the key doesn't exist, we can just skip it like bad cheese
121
+ continue;
122
+ }
123
+ if (update[key] === value) {
124
+ continue;
125
+ }
126
+ // If the value ain't here, we can just do whatever man
127
+ if (value === undefined || value === null || value === "") {
128
+ // If the update key is defined
129
+ if (
130
+ update[key] !== undefined &&
131
+ update[key] !== null &&
132
+ update[key] !== ""
133
+ ) {
134
+ // might as well use it eh?
135
+ result[key] = update[key];
136
+ }
137
+ // ELSE if the value is an array, because it would then not be === to those things above
138
+ } else if (Array.isArray(value)) {
139
+ // Get the update value
140
+ const updateValue = update[key];
141
+ // If the update value is an array, concatenate and remove duplicates
142
+ // and poopy data
143
+ if (Array.isArray(updateValue)) {
144
+ result[key] = [...new Set([...value, ...updateValue])].filter(
145
+ (item) => item !== null && item !== undefined && item !== ""
146
+ );
147
+ } else {
148
+ // If the update value is not an array, just use it
149
+ result[key] = [...value, updateValue].filter(
150
+ (item) => item !== null && item !== undefined && item !== ""
151
+ );
152
+ }
153
+ } else if (typeof value === "object" && !Array.isArray(value)) {
154
+ // If the value is an object, we need to merge it
155
+ if (typeof update[key] === "object" && !Array.isArray(update[key])) {
156
+ result[key] = this.mergeObjects(value, update[key]);
157
+ }
158
+ } else {
159
+ // Finally, the source value is defined, and not an array, so we don't care about the update value
160
+ continue;
161
+ }
162
+ }
163
+ // Because the objects should technically always be validated FIRST, we can assume the update keys are also defined on the source object
164
+ for (const [key, value] of Object.entries(update)) {
165
+ if (value === undefined || value === null || value === "") {
166
+ continue;
167
+ } else if (!Object.hasOwn(source, key)) {
168
+ result[key] = value;
169
+ } else if (
170
+ typeof source[key] === "object" &&
171
+ typeof value === "object" &&
172
+ !Array.isArray(source[key]) &&
173
+ !Array.isArray(value)
174
+ ) {
175
+ result[key] = this.mergeObjects(source[key], value);
176
+ } else if (Array.isArray(source[key]) && Array.isArray(value)) {
177
+ result[key] = [...new Set([...source[key], ...value])].filter(
178
+ (item) => item !== null && item !== undefined && item !== ""
179
+ );
180
+ } else if (
181
+ source[key] === undefined ||
182
+ source[key] === null ||
183
+ source[key] === ""
184
+ ) {
185
+ result[key] = value;
186
+ }
187
+ }
188
+
189
+ return result;
190
+ }
191
+
192
+ // Method to load data from a file specified in the import definition
193
+ loadData(importDef: ImportDef): any[] {
194
+ // Simply join appwriteFolderPath with the importDef.filePath
195
+ const filePath = path.resolve(this.appwriteFolderPath, importDef.filePath);
196
+ MessageFormatter.info(`Loading data from: ${filePath}`, { prefix: "Data" });
197
+ if (!fs.existsSync(filePath)) {
198
+ MessageFormatter.error(`File not found: ${filePath}`, undefined, { prefix: "Data" });
199
+ return [];
200
+ }
201
+
202
+ // Read the file and parse the JSON data
203
+ const rawData = fs.readFileSync(filePath, "utf8");
204
+ const parsedData = importDef.basePath
205
+ ? JSON.parse(rawData)[importDef.basePath]
206
+ : JSON.parse(rawData);
207
+
208
+ MessageFormatter.success(`Loaded ${parsedData?.length || 0} items from ${filePath}`, { prefix: "Data" });
209
+ return parsedData;
210
+ }
211
+
212
+ // Helper method to check if a new ID already exists in the old-to-new ID map
213
+ checkMapValuesForId(newId: string, collectionName: string) {
214
+ const oldIdMap = this.oldIdToNewIdPerCollectionMap.get(collectionName);
215
+ for (const [key, value] of oldIdMap?.entries() || []) {
216
+ if (value === newId) {
217
+ return key;
218
+ }
219
+ }
220
+ return false;
221
+ }
222
+
223
+ // Method to generate a unique ID that doesn't conflict with existing IDs
224
+ getTrueUniqueId(collectionName: string) {
225
+ let newId = ID.unique();
226
+ let condition =
227
+ this.checkMapValuesForId(newId, collectionName) ||
228
+ this.userExistsMap.has(newId) ||
229
+ this.userIdSet.has(newId) ||
230
+ this.importMap
231
+ .get(this.getCollectionKey("users"))
232
+ ?.data.some(
233
+ (user) =>
234
+ user.finalData.docId === newId || user.finalData.userId === newId
235
+ );
236
+ while (condition) {
237
+ newId = ID.unique();
238
+ condition =
239
+ this.checkMapValuesForId(newId, collectionName) ||
240
+ this.userExistsMap.has(newId) ||
241
+ this.userIdSet.has(newId) ||
242
+ this.importMap
243
+ .get(this.getCollectionKey("users"))
244
+ ?.data.some(
245
+ (user) =>
246
+ user.finalData.docId === newId || user.finalData.userId === newId
247
+ );
248
+ }
249
+ return newId;
250
+ }
251
+
252
+ // Method to create a context object for data transformation
253
+ createContext(
254
+ db: ConfigDatabase,
255
+ collection: CollectionCreate,
256
+ item: any,
257
+ docId: string
258
+ ) {
259
+ return {
260
+ ...item, // Spread the item data for easy access to its properties
261
+ dbId: db.$id,
262
+ dbName: db.name,
263
+ collId: collection.$id,
264
+ collName: collection.name,
265
+ docId: docId,
266
+ createdDoc: {}, // Initially null, to be updated when the document is created
267
+ };
268
+ }
269
+
270
+ /**
271
+ * Transforms the given item based on the provided attribute mappings.
272
+ * This method applies conversion rules to the item's attributes as defined in the attribute mappings.
273
+ *
274
+ * @param item - The item to be transformed.
275
+ * @param attributeMappings - The mappings that define how each attribute should be transformed.
276
+ * @returns The transformed item.
277
+ */
278
+ transformData(item: any, attributeMappings: AttributeMappings): any {
279
+ // Convert the item using the attribute mappings provided
280
+ const convertedItem = convertObjectByAttributeMappings(
281
+ item,
282
+ attributeMappings
283
+ );
284
+ // Run additional converter functions on the converted item, if any
285
+ return this.importDataActions.runConverterFunctions(
286
+ convertedItem,
287
+ attributeMappings
288
+ );
289
+ }
290
+
291
+ async setupMaps(dbId: string, specificCollections?: string[]) {
292
+ // Initialize the users collection in the import map
293
+ this.importMap.set(this.getCollectionKey("users"), {
294
+ data: [],
295
+ });
296
+ for (const db of this.config.databases) {
297
+ if (db.$id !== dbId) {
298
+ continue;
299
+ }
300
+ if (!this.config.collections) {
301
+ continue;
302
+ }
303
+ for (let index = 0; index < this.config.collections.length; index++) {
304
+ const collectionConfig = this.config.collections[index];
305
+ let collection = CollectionCreateSchema.parse(collectionConfig);
306
+ // Skip collections not in the specific list if one was provided
307
+ if (specificCollections && specificCollections.length > 0 &&
308
+ !specificCollections.includes(collection.name)) {
309
+ continue;
310
+ }
311
+ // Check if the collection exists in the database
312
+ const collectionExists = await checkForCollection(
313
+ this.database,
314
+ db.$id,
315
+ collection
316
+ );
317
+ if (!collectionExists) {
318
+ logger.error(`No collection found for ${collection.name}`);
319
+ continue;
320
+ } else if (!collection.name) {
321
+ logger.error(`Collection ${collection.name} has no name`);
322
+ continue;
323
+ }
324
+ // Update the collection ID with the existing one
325
+ collectionConfig.$id = collectionExists.$id;
326
+ collection.$id = collectionExists.$id;
327
+ this.config.collections[index] = collectionConfig;
328
+ // Find or create an import operation for the collection (non-fatal)
329
+ try {
330
+ const adapter = await this.getAdapter();
331
+ const collectionImportOperation = await findOrCreateOperation(
332
+ adapter,
333
+ dbId,
334
+ "importData",
335
+ collection.$id!
336
+ );
337
+ this.collectionImportOperations.set(
338
+ this.getCollectionKey(collection.name),
339
+ collectionImportOperation.$id
340
+ );
341
+ } catch (error) {
342
+ MessageFormatter.warning(
343
+ `Operations tracking unavailable for ${collection.name}, import will proceed without it`,
344
+ { prefix: "Import" }
345
+ );
346
+ }
347
+ // Initialize the collection in the import map
348
+ this.importMap.set(this.getCollectionKey(collection.name), {
349
+ collection: collection,
350
+ data: [],
351
+ });
352
+ }
353
+ }
354
+ }
355
+
356
+ async getAllUsers() {
357
+ const users = new UsersController(this.config, this.database);
358
+ const allUsers = await users.getAllUsers();
359
+ // Iterate over the users and setup our maps ahead of time for email and phone
360
+ for (const user of allUsers) {
361
+ if (user.email) {
362
+ this.emailToUserIdMap.set(user.email.toLowerCase(), user.$id);
363
+ }
364
+ if (user.phone) {
365
+ this.phoneToUserIdMap.set(user.phone, user.$id);
366
+ }
367
+ this.userExistsMap.set(user.$id, true);
368
+ this.userIdSet.add(user.$id);
369
+ let importData = this.importMap.get(this.getCollectionKey("users"));
370
+ if (!importData) {
371
+ importData = {
372
+ data: [],
373
+ };
374
+ }
375
+ importData.data.push({
376
+ finalData: {
377
+ ...user,
378
+ email: user.email?.toLowerCase(),
379
+ userId: user.$id,
380
+ docId: user.$id,
381
+ },
382
+ context: {
383
+ ...user,
384
+ email: user.email?.toLowerCase(),
385
+ userId: user.$id,
386
+ docId: user.$id,
387
+ },
388
+ rawData: user,
389
+ });
390
+ this.importMap.set(this.getCollectionKey("users"), importData);
391
+ }
392
+ return allUsers;
393
+ }
394
+
395
+ // Main method to start the data loading process for a given database ID
396
+ async start(dbId: string, specificCollections?: string[]) {
397
+ MessageFormatter.divider();
398
+ MessageFormatter.info(`Starting data setup for database: ${dbId}`, { prefix: "Data" });
399
+ MessageFormatter.divider();
400
+ // Only fetch users if we're importing users or no specific collections specified
401
+ const needsUsers = !specificCollections || specificCollections.length === 0 ||
402
+ specificCollections.some(c =>
403
+ this.getCollectionKey(c) === this.getCollectionKey(this.config.usersCollectionName)
404
+ );
405
+ if (needsUsers) {
406
+ const allUsers = await this.getAllUsers();
407
+ MessageFormatter.info(
408
+ `Fetched ${allUsers.length} users`,
409
+ { prefix: "Data" }
410
+ );
411
+ }
412
+ // Iterate over the configured databases to find the matching one
413
+ for (const db of this.config.databases) {
414
+ if (db.$id !== dbId) {
415
+ continue;
416
+ }
417
+ if (!this.config.collections) {
418
+ continue;
419
+ }
420
+ // Iterate over the configured collections to process each
421
+ for (const collectionConfig of this.config.collections) {
422
+ const collection = collectionConfig;
423
+ // Skip collections not in the specific list
424
+ if (specificCollections && specificCollections.length > 0 &&
425
+ !specificCollections.includes(collection.name)) {
426
+ continue;
427
+ }
428
+ // Determine if this is the users collection
429
+ let isUsersCollection =
430
+ this.getCollectionKey(this.config.usersCollectionName) ===
431
+ this.getCollectionKey(collection.name);
432
+ const collectionDefs = collection.importDefs;
433
+ if (!collectionDefs || !collectionDefs.length) {
434
+ continue;
435
+ }
436
+ // Process create and update definitions for the collection
437
+ const createDefs = collection.importDefs.filter(
438
+ (def: ImportDef) => def.type === "create" || !def.type
439
+ );
440
+ const updateDefs = collection.importDefs.filter(
441
+ (def: ImportDef) => def.type === "update"
442
+ );
443
+ for (const createDef of createDefs) {
444
+ if (!isUsersCollection || !createDef.createUsers) {
445
+ await this.prepareCreateData(db, collection, createDef);
446
+ } else {
447
+ // Special handling for users collection if needed
448
+ await this.prepareUserCollectionCreateData(
449
+ db,
450
+ collection,
451
+ createDef
452
+ );
453
+ }
454
+ }
455
+ for (const updateDef of updateDefs) {
456
+ if (!this.importMap.has(this.getCollectionKey(collection.name))) {
457
+ logger.error(
458
+ `No data found for collection ${collection.name} for updateDef but it says it's supposed to have one...`
459
+ );
460
+ continue;
461
+ }
462
+ // Prepare the update data for the collection
463
+ this.prepareUpdateData(db, collection, updateDef);
464
+ }
465
+ }
466
+ MessageFormatter.info("Running update references", { prefix: "Data" });
467
+ // this.dealWithMergedUsers();
468
+ this.updateOldReferencesForNew();
469
+ MessageFormatter.success("Done running update references", { prefix: "Data" });
470
+ }
471
+ // for (const collection of this.config.collections) {
472
+ // this.resolveDataItemRelationships(collection);
473
+ // }
474
+ MessageFormatter.divider();
475
+ MessageFormatter.success(`Data setup for database: ${dbId} completed`, { prefix: "Data" });
476
+ MessageFormatter.divider();
477
+ if (this.shouldWriteFile) {
478
+ this.writeMapsToJsonFile();
479
+ }
480
+ }
481
+
482
+ /**
483
+ * Deals with merged users by iterating through all collections in the configuration.
484
+ * We have merged users if there are duplicate emails or phones in the import data.
485
+ * This function will iterate through all collections that are the same name as the
486
+ * users collection and pull out their primaryKeyField's. It will then loop through
487
+ * each collection and find any documents that have a
488
+ *
489
+ * @return {void} This function does not return anything.
490
+ */
491
+ // dealWithMergedUsers() {
492
+ // const usersCollectionKey = this.getCollectionKey(
493
+ // this.config.usersCollectionName
494
+ // );
495
+ // const usersCollectionData = this.importMap.get(usersCollectionKey);
496
+
497
+ // if (!this.config.collections) {
498
+ // console.log("No collections found in configuration.");
499
+ // return;
500
+ // }
501
+
502
+ // let needsUpdate = false;
503
+ // let numUpdates = 0;
504
+
505
+ // for (const collectionConfig of this.config.collections) {
506
+ // const collectionKey = this.getCollectionKey(collectionConfig.name);
507
+ // const collectionData = this.importMap.get(collectionKey);
508
+ // const collectionImportDefs = collectionConfig.importDefs;
509
+ // const collectionIdMappings = collectionImportDefs
510
+ // .map((importDef) => importDef.idMappings)
511
+ // .flat()
512
+ // .filter((idMapping) => idMapping !== undefined && idMapping !== null);
513
+ // if (!collectionData || !collectionData.data) continue;
514
+ // for (const dataItem of collectionData.data) {
515
+ // for (const idMapping of collectionIdMappings) {
516
+ // // We know it's the users collection here
517
+ // if (this.getCollectionKey(idMapping.targetCollection) === usersCollectionKey) {
518
+ // const targetFieldKey = idMapping.targetFieldToMatch || idMapping.targetField;
519
+ // if (targetFieldKey === )
520
+ // const targetValue = dataItem.finalData[targetFieldKey];
521
+ // const targetCollectionData = this.importMap.get(this.getCollectionKey(idMapping.targetCollection));
522
+ // if (!targetCollectionData || !targetCollectionData.data) continue;
523
+ // const foundData = targetCollectionData.data.filter(({ context }) => {
524
+ // const targetValue = context[targetFieldKey];
525
+ // const isMatch = `${targetValue}` === `${valueToMatch}`;
526
+ // return isMatch && targetValue !== undefined && targetValue !== null;
527
+ // });
528
+ // }
529
+ // }
530
+ // }
531
+ // }
532
+ // }
533
+
534
+ /**
535
+ * Gets the value to match for a given key in the final data or context.
536
+ * @param finalData - The final data object.
537
+ * @param context - The context object.
538
+ * @param key - The key to get the value for.
539
+ * @returns The value to match for from finalData or Context
540
+ */
541
+ getValueFromData(finalData: any, context: any, key: string) {
542
+ if (
543
+ context[key] !== undefined &&
544
+ context[key] !== null &&
545
+ context[key] !== ""
546
+ ) {
547
+ return context[key];
548
+ }
549
+ return finalData[key];
550
+ }
551
+
552
+ updateOldReferencesForNew() {
553
+ if (!this.config.collections) {
554
+ return;
555
+ }
556
+
557
+ for (const collectionConfig of this.config.collections) {
558
+ const collectionKey = this.getCollectionKey(collectionConfig.name);
559
+ const collectionData = this.importMap.get(collectionKey);
560
+
561
+ if (!collectionData || !collectionData.data) continue;
562
+
563
+ MessageFormatter.processing(
564
+ `Updating references for collection: ${collectionConfig.name}`,
565
+ { prefix: "Data" }
566
+ );
567
+
568
+ let needsUpdate = false;
569
+
570
+ // Iterate over each data item in the current collection
571
+ for (let i = 0; i < collectionData.data.length; i++) {
572
+ if (collectionConfig.importDefs) {
573
+ for (const importDef of collectionConfig.importDefs) {
574
+ if (importDef.idMappings) {
575
+ for (const idMapping of importDef.idMappings) {
576
+ const targetCollectionKey = this.getCollectionKey(
577
+ idMapping.targetCollection
578
+ );
579
+ const fieldToSetKey =
580
+ idMapping.fieldToSet || idMapping.sourceField;
581
+ const targetFieldKey =
582
+ idMapping.targetFieldToMatch || idMapping.targetField;
583
+ const sourceValue = this.getValueFromData(
584
+ collectionData.data[i].finalData,
585
+ collectionData.data[i].context,
586
+ idMapping.sourceField
587
+ );
588
+
589
+ // Skip if value to match is missing or empty
590
+ if (
591
+ !sourceValue ||
592
+ isEmpty(sourceValue) ||
593
+ sourceValue === null
594
+ )
595
+ continue;
596
+
597
+ const isFieldToSetArray = collectionConfig.attributes.find(
598
+ (attribute) => attribute.key === fieldToSetKey
599
+ )?.array;
600
+
601
+ const targetCollectionData =
602
+ this.importMap.get(targetCollectionKey);
603
+ if (!targetCollectionData || !targetCollectionData.data)
604
+ continue;
605
+
606
+ // Handle cases where sourceValue is an array
607
+ const sourceValues = Array.isArray(sourceValue)
608
+ ? sourceValue.map((sourceValue) => `${sourceValue}`)
609
+ : [`${sourceValue}`];
610
+ let newData = [];
611
+
612
+ for (const valueToMatch of sourceValues) {
613
+ // Find matching data in the target collection
614
+ const foundData = targetCollectionData.data.filter(
615
+ ({ context, finalData }) => {
616
+ const targetValue = this.getValueFromData(
617
+ finalData,
618
+ context,
619
+ targetFieldKey
620
+ );
621
+ const isMatch = `${targetValue}` === `${valueToMatch}`;
622
+ // Ensure the targetValue is defined and not null
623
+ return (
624
+ isMatch &&
625
+ targetValue !== undefined &&
626
+ targetValue !== null
627
+ );
628
+ }
629
+ );
630
+
631
+ if (foundData.length) {
632
+ newData.push(
633
+ ...foundData.map((data) => {
634
+ const newValue = this.getValueFromData(
635
+ data.finalData,
636
+ data.context,
637
+ idMapping.targetField
638
+ );
639
+ return newValue;
640
+ })
641
+ );
642
+ } else {
643
+ logger.info(
644
+ `No data found for collection: ${targetCollectionKey} with value: ${valueToMatch} for field: ${fieldToSetKey} -- idMapping: ${JSON.stringify(
645
+ idMapping,
646
+ null,
647
+ 2
648
+ )}`
649
+ );
650
+ }
651
+ continue;
652
+ }
653
+
654
+ const getCurrentDataFiltered = (currentData: any) => {
655
+ if (Array.isArray(currentData.finalData[fieldToSetKey])) {
656
+ return currentData.finalData[fieldToSetKey].filter(
657
+ (data: any) => !sourceValues.includes(`${data}`)
658
+ );
659
+ }
660
+ return currentData.finalData[fieldToSetKey];
661
+ };
662
+
663
+ // Get the current data to be updated
664
+ const currentDataFiltered = getCurrentDataFiltered(
665
+ collectionData.data[i]
666
+ );
667
+
668
+ if (newData.length) {
669
+ needsUpdate = true;
670
+
671
+ // Handle cases where current data is an array
672
+ if (isFieldToSetArray) {
673
+ if (!currentDataFiltered) {
674
+ // Set new data if current data is undefined
675
+ collectionData.data[i].finalData[fieldToSetKey] =
676
+ Array.isArray(newData) ? newData : [newData];
677
+ } else {
678
+ if (Array.isArray(currentDataFiltered)) {
679
+ // Convert current data to array and merge if new data is non-empty array
680
+ collectionData.data[i].finalData[fieldToSetKey] = [
681
+ ...new Set(
682
+ [...currentDataFiltered, ...newData].filter(
683
+ (value: any) =>
684
+ value !== null &&
685
+ value !== undefined &&
686
+ value !== ""
687
+ )
688
+ ),
689
+ ];
690
+ } else {
691
+ // Merge arrays if new data is non-empty array and filter for uniqueness
692
+ collectionData.data[i].finalData[fieldToSetKey] = [
693
+ ...new Set(
694
+ [
695
+ ...(Array.isArray(currentDataFiltered)
696
+ ? currentDataFiltered
697
+ : [currentDataFiltered]),
698
+ ...newData,
699
+ ].filter(
700
+ (value: any) =>
701
+ value !== null &&
702
+ value !== undefined &&
703
+ value !== "" &&
704
+ !sourceValues.includes(`${value}`)
705
+ )
706
+ ),
707
+ ];
708
+ }
709
+ }
710
+ } else {
711
+ if (!currentDataFiltered) {
712
+ // Set new data if current data is undefined
713
+ collectionData.data[i].finalData[fieldToSetKey] =
714
+ Array.isArray(newData) ? newData[0] : newData;
715
+ } else if (Array.isArray(newData) && newData.length > 0) {
716
+ // Convert current data to array and merge if new data is non-empty array, then filter for uniqueness
717
+ // and take the first value, because it's an array and the attribute is not an array
718
+ collectionData.data[i].finalData[fieldToSetKey] = [
719
+ ...new Set(
720
+ [currentDataFiltered, ...newData].filter(
721
+ (value: any) =>
722
+ value !== null &&
723
+ value !== undefined &&
724
+ value !== "" &&
725
+ !sourceValues.includes(`${value}`)
726
+ )
727
+ ),
728
+ ].slice(0, 1)[0];
729
+ } else if (
730
+ !Array.isArray(newData) &&
731
+ newData !== undefined
732
+ ) {
733
+ // Simply update the field if new data is not an array and defined
734
+ collectionData.data[i].finalData[fieldToSetKey] = newData;
735
+ }
736
+ }
737
+ }
738
+ }
739
+ }
740
+ }
741
+ }
742
+ }
743
+
744
+ // Update the import map if any changes were made
745
+ if (needsUpdate) {
746
+ this.importMap.set(collectionKey, collectionData);
747
+ }
748
+ }
749
+ }
750
+
751
+ private writeMapsToJsonFile() {
752
+ const outputDir = path.resolve(process.cwd(), "zlogs");
753
+
754
+ // Ensure the logs directory exists
755
+ if (!fs.existsSync(outputDir)) {
756
+ fs.mkdirSync(outputDir);
757
+ }
758
+
759
+ // Helper function to write data to a file
760
+ const writeToFile = (fileName: string, data: any) => {
761
+ const outputFile = path.join(outputDir, fileName);
762
+ fs.writeFile(outputFile, JSON.stringify(data, null, 2), "utf8", (err) => {
763
+ if (err) {
764
+ MessageFormatter.error(`Error writing data to ${fileName}`, err instanceof Error ? err : new Error(String(err)), { prefix: "Data" });
765
+ return;
766
+ }
767
+ MessageFormatter.success(`Data successfully written to ${fileName}`, { prefix: "Data" });
768
+ });
769
+ };
770
+
771
+ // Convert Maps to arrays of entries for serialization
772
+ const oldIdToNewIdPerCollectionMap = Array.from(
773
+ this.oldIdToNewIdPerCollectionMap.entries()
774
+ ).map(([key, value]) => {
775
+ return {
776
+ collection: key,
777
+ data: Array.from(value.entries()),
778
+ };
779
+ });
780
+
781
+ const mergedUserMap = Array.from(this.mergedUserMap.entries());
782
+
783
+ // Write each part to a separate file
784
+ writeToFile(
785
+ "oldIdToNewIdPerCollectionMap.json",
786
+ oldIdToNewIdPerCollectionMap
787
+ );
788
+ writeToFile("mergedUserMap.json", mergedUserMap);
789
+
790
+ // Write each collection's data to a separate file
791
+ this.importMap.forEach((value, key) => {
792
+ const data = {
793
+ collection: key,
794
+ data: value.data.map((item: any) => {
795
+ return {
796
+ finalData: item.finalData,
797
+ context: item.context,
798
+ };
799
+ }),
800
+ };
801
+ writeToFile(`${key}.json`, data);
802
+ });
803
+ }
804
+
805
+ /**
806
+ * Prepares user data by checking for duplicates based on email or phone, adding to a duplicate map if found,
807
+ * and then returning the transformed item without user-specific keys.
808
+ *
809
+ * @param item - The raw item to be processed.
810
+ * @param attributeMappings - The attribute mappings for the item.
811
+ * @returns The transformed item with user-specific keys removed.
812
+ */
813
+ prepareUserData(
814
+ item: any,
815
+ attributeMappings: AttributeMappings,
816
+ primaryKeyField: string,
817
+ newId: string
818
+ ): {
819
+ transformedItem: any;
820
+ existingId: string | undefined;
821
+ userData: {
822
+ rawData: any;
823
+ finalData: z.infer<typeof AuthUserCreateSchema>;
824
+ };
825
+ } {
826
+ if (
827
+ this.userIdSet.has(newId) ||
828
+ this.userExistsMap.has(newId) ||
829
+ Array.from(this.emailToUserIdMap.values()).includes(newId) ||
830
+ Array.from(this.phoneToUserIdMap.values()).includes(newId)
831
+ ) {
832
+ newId = this.getTrueUniqueId(this.getCollectionKey("users"));
833
+ }
834
+ let transformedItem = this.transformData(item, attributeMappings);
835
+ let userData = AuthUserCreateSchema.safeParse(transformedItem);
836
+ if (userData.data?.email) {
837
+ userData.data.email = userData.data.email.toLowerCase();
838
+ }
839
+ if (!userData.success || !(userData.data.email || userData.data.phone)) {
840
+ logger.error(
841
+ `Invalid user data: ${JSON.stringify(
842
+ userData.error?.issues,
843
+ undefined,
844
+ 2
845
+ )} or missing email/phone`
846
+ );
847
+
848
+ const userKeys = ["email", "phone", "name", "labels", "prefs"];
849
+ userKeys.forEach((key) => {
850
+ if (transformedItem.hasOwnProperty(key)) {
851
+ delete transformedItem[key];
852
+ }
853
+ });
854
+ return {
855
+ transformedItem,
856
+ existingId: undefined,
857
+ userData: {
858
+ rawData: item,
859
+ finalData: transformedItem,
860
+ },
861
+ };
862
+ }
863
+ const email = userData.data.email?.toLowerCase();
864
+ const phone = userData.data.phone;
865
+ let existingId: string | undefined;
866
+
867
+ // Check for duplicate email and phone
868
+ if (email && this.emailToUserIdMap.has(email)) {
869
+ existingId = this.emailToUserIdMap.get(email);
870
+ if (phone && !this.phoneToUserIdMap.has(phone)) {
871
+ this.phoneToUserIdMap.set(phone, newId);
872
+ }
873
+ } else if (phone && this.phoneToUserIdMap.has(phone)) {
874
+ existingId = this.phoneToUserIdMap.get(phone);
875
+ if (email && !this.emailToUserIdMap.has(email)) {
876
+ this.emailToUserIdMap.set(email, newId);
877
+ }
878
+ } else {
879
+ if (email) this.emailToUserIdMap.set(email, newId);
880
+ if (phone) this.phoneToUserIdMap.set(phone, newId);
881
+ }
882
+
883
+ if (existingId) {
884
+ userData.data.userId = existingId;
885
+ const mergedUsers = this.mergedUserMap.get(existingId) || [];
886
+ mergedUsers.push(`${item[primaryKeyField]}`);
887
+ this.mergedUserMap.set(existingId, mergedUsers);
888
+ const userFound = this.importMap
889
+ .get(this.getCollectionKey("users"))
890
+ ?.data.find((userDataExisting) => {
891
+ let userIdToMatch: string | undefined;
892
+ if (userDataExisting?.finalData?.userId) {
893
+ userIdToMatch = userDataExisting?.finalData?.userId;
894
+ } else if (userDataExisting?.finalData?.docId) {
895
+ userIdToMatch = userDataExisting?.finalData?.docId;
896
+ } else if (userDataExisting?.context?.userId) {
897
+ userIdToMatch = userDataExisting.context.userId;
898
+ } else if (userDataExisting?.context?.docId) {
899
+ userIdToMatch = userDataExisting.context.docId;
900
+ }
901
+ return userIdToMatch === existingId;
902
+ });
903
+ if (userFound) {
904
+ userFound.finalData.userId = existingId;
905
+ userFound.finalData.docId = existingId;
906
+ this.userIdSet.add(existingId);
907
+ transformedItem = {
908
+ ...transformedItem,
909
+ userId: existingId,
910
+ docId: existingId,
911
+ };
912
+ }
913
+
914
+ const userKeys = ["email", "phone", "name", "labels", "prefs"];
915
+ userKeys.forEach((key) => {
916
+ if (transformedItem.hasOwnProperty(key)) {
917
+ delete transformedItem[key];
918
+ }
919
+ });
920
+ return {
921
+ transformedItem,
922
+ existingId,
923
+ userData: {
924
+ rawData: userFound?.rawData,
925
+ finalData: userFound?.finalData,
926
+ },
927
+ };
928
+ } else {
929
+ existingId = newId;
930
+ userData.data.userId = existingId;
931
+ }
932
+
933
+ const userKeys = ["email", "phone", "name", "labels", "prefs"];
934
+ userKeys.forEach((key) => {
935
+ if (transformedItem.hasOwnProperty(key)) {
936
+ delete transformedItem[key];
937
+ }
938
+ });
939
+
940
+ const usersMap = this.importMap.get(this.getCollectionKey("users"));
941
+ const userDataToAdd = {
942
+ rawData: item,
943
+ finalData: userData.data,
944
+ context: {},
945
+ };
946
+ this.importMap.set(this.getCollectionKey("users"), {
947
+ data: [...(usersMap?.data || []), userDataToAdd],
948
+ });
949
+ this.userIdSet.add(existingId);
950
+
951
+ return {
952
+ transformedItem,
953
+ existingId,
954
+ userData: userDataToAdd,
955
+ };
956
+ }
957
+
958
+ /**
959
+ * Prepares the data for creating user collection documents.
960
+ * This involves loading the data, transforming it according to the import definition,
961
+ * and handling the creation of new unique IDs for each item.
962
+ *
963
+ * @param db - The database configuration.
964
+ * @param collection - The collection configuration.
965
+ * @param importDef - The import definition containing the attribute mappings and other relevant info.
966
+ */
967
+ async prepareUserCollectionCreateData(
968
+ db: ConfigDatabase,
969
+ collection: CollectionCreate,
970
+ importDef: ImportDef
971
+ ): Promise<void> {
972
+ // Load the raw data based on the import definition
973
+ const rawData = this.loadData(importDef);
974
+ let operationId = this.collectionImportOperations.get(
975
+ this.getCollectionKey(collection.name)
976
+ );
977
+ // Initialize a new map for old ID to new ID mappings
978
+ const oldIdToNewIdMap = new Map<string, string>();
979
+ // Retrieve or initialize the collection-specific old ID to new ID map
980
+ const collectionOldIdToNewIdMap =
981
+ this.oldIdToNewIdPerCollectionMap.get(
982
+ this.getCollectionKey(collection.name)
983
+ ) ||
984
+ this.oldIdToNewIdPerCollectionMap
985
+ .set(this.getCollectionKey(collection.name), oldIdToNewIdMap)
986
+ .get(this.getCollectionKey(collection.name));
987
+ const adapter = await this.getAdapter();
988
+ if (!operationId) {
989
+ const collectionImportOperation = await findOrCreateOperation(
990
+ adapter,
991
+ db.$id,
992
+ "importData",
993
+ collection.$id!
994
+ );
995
+ // Store the operation ID in the map
996
+ this.collectionImportOperations.set(
997
+ this.getCollectionKey(collection.name),
998
+ collectionImportOperation.$id
999
+ );
1000
+ operationId = collectionImportOperation.$id;
1001
+ }
1002
+ if (operationId) {
1003
+ await updateOperation(adapter, db.$id, operationId, {
1004
+ status: "in_progress",
1005
+ total: rawData.length,
1006
+ });
1007
+ }
1008
+ // Retrieve the current user data and the current collection data from the import map
1009
+ const currentUserData = this.importMap.get(this.getCollectionKey("users"));
1010
+ const currentData = this.importMap.get(
1011
+ this.getCollectionKey(collection.name)
1012
+ );
1013
+ // Log errors if the necessary data is not found in the import map
1014
+ if (!currentUserData) {
1015
+ logger.error(
1016
+ `No data found for collection ${"users"} for createDef but it says it's supposed to have one...`
1017
+ );
1018
+ return;
1019
+ } else if (!currentData) {
1020
+ logger.error(
1021
+ `No data found for collection ${collection.name} for createDef but it says it's supposed to have one...`
1022
+ );
1023
+ return;
1024
+ }
1025
+ // Iterate through each item in the raw data
1026
+ for (const item of rawData) {
1027
+ // Prepare user data, check for duplicates, and remove user-specific keys
1028
+ let { transformedItem, existingId, userData } = this.prepareUserData(
1029
+ item,
1030
+ importDef.attributeMappings,
1031
+ importDef.primaryKeyField,
1032
+ this.getTrueUniqueId(this.getCollectionKey("users"))
1033
+ );
1034
+
1035
+ logger.info(
1036
+ `In create user -- transformedItem: ${JSON.stringify(
1037
+ transformedItem,
1038
+ null,
1039
+ 2
1040
+ )}`
1041
+ );
1042
+
1043
+ // Generate a new unique ID for the item or use existing ID
1044
+ if (!existingId && !userData.finalData?.userId) {
1045
+ // No existing user ID, generate a new unique ID
1046
+ existingId = this.getTrueUniqueId(
1047
+ this.getCollectionKey(collection.name)
1048
+ );
1049
+ transformedItem = {
1050
+ ...transformedItem,
1051
+ userId: existingId,
1052
+ docId: existingId,
1053
+ };
1054
+ } else if (!existingId && userData.finalData?.userId) {
1055
+ // Existing user ID, use it as the new ID
1056
+ existingId = userData.finalData.userId;
1057
+ transformedItem = {
1058
+ ...transformedItem,
1059
+ userId: existingId,
1060
+ docId: existingId,
1061
+ };
1062
+ }
1063
+
1064
+ // Create a context object for the item, including the new ID
1065
+ let context = this.createContext(db, collection, item, existingId!);
1066
+
1067
+ // Merge the transformed data into the context
1068
+ context = { ...context, ...transformedItem, ...userData.finalData };
1069
+
1070
+ // If a primary key field is defined, handle the ID mapping and check for duplicates
1071
+ if (importDef.primaryKeyField) {
1072
+ const oldId = item[importDef.primaryKeyField];
1073
+
1074
+ // Check if the oldId already exists to handle potential duplicates
1075
+ if (
1076
+ this.oldIdToNewIdPerCollectionMap
1077
+ .get(this.getCollectionKey(collection.name))
1078
+ ?.has(`${oldId}`)
1079
+ ) {
1080
+ // Found a duplicate oldId, now decide how to merge or handle these duplicates
1081
+ for (const data of currentData.data) {
1082
+ if (
1083
+ data.finalData.docId === oldId ||
1084
+ data.finalData.userId === oldId ||
1085
+ data.context.docId === oldId ||
1086
+ data.context.userId === oldId
1087
+ ) {
1088
+ transformedItem = this.mergeObjects(
1089
+ data.finalData,
1090
+ transformedItem
1091
+ );
1092
+ }
1093
+ }
1094
+ } else {
1095
+ // No duplicate found, simply map the oldId to the new itemId
1096
+ collectionOldIdToNewIdMap?.set(`${oldId}`, `${existingId}`);
1097
+ }
1098
+ }
1099
+
1100
+ // Handle merging for currentUserData
1101
+ for (let i = 0; i < currentUserData.data.length; i++) {
1102
+ const currentUserDataItem = currentUserData.data[i];
1103
+ const samePhones =
1104
+ currentUserDataItem.finalData.phone &&
1105
+ transformedItem.phone &&
1106
+ currentUserDataItem.finalData.phone === transformedItem.phone;
1107
+ const sameEmails =
1108
+ currentUserDataItem.finalData.email &&
1109
+ transformedItem.email &&
1110
+ currentUserDataItem.finalData.email === transformedItem.email;
1111
+ if (
1112
+ (currentUserDataItem.finalData.docId === existingId ||
1113
+ currentUserDataItem.finalData.userId === existingId) &&
1114
+ (samePhones || sameEmails) &&
1115
+ currentUserDataItem.finalData &&
1116
+ userData.finalData
1117
+ ) {
1118
+ const userDataMerged = this.mergeObjects(
1119
+ currentUserData.data[i].finalData,
1120
+ userData.finalData
1121
+ );
1122
+ currentUserData.data[i].finalData = userDataMerged;
1123
+ this.importMap.set(this.getCollectionKey("users"), currentUserData);
1124
+ }
1125
+ }
1126
+ // Update the attribute mappings with any actions that need to be performed post-import
1127
+ // We added the basePath to get the folder from the filePath
1128
+ const mappingsWithActions = this.getAttributeMappingsWithActions(
1129
+ importDef.attributeMappings,
1130
+ context,
1131
+ transformedItem
1132
+ );
1133
+ // Update the import definition with the new attribute mappings
1134
+ const newImportDef = {
1135
+ ...importDef,
1136
+ attributeMappings: mappingsWithActions,
1137
+ };
1138
+
1139
+ const updatedData = this.importMap.get(
1140
+ this.getCollectionKey(collection.name)
1141
+ )!;
1142
+
1143
+ let foundData = false;
1144
+ for (let i = 0; i < updatedData.data.length; i++) {
1145
+ if (
1146
+ updatedData.data[i].finalData.docId === existingId ||
1147
+ updatedData.data[i].finalData.userId === existingId ||
1148
+ updatedData.data[i].context.docId === existingId ||
1149
+ updatedData.data[i].context.userId === existingId
1150
+ ) {
1151
+ updatedData.data[i].finalData = this.mergeObjects(
1152
+ updatedData.data[i].finalData,
1153
+ transformedItem
1154
+ );
1155
+ updatedData.data[i].context = this.mergeObjects(
1156
+ updatedData.data[i].context,
1157
+ context
1158
+ );
1159
+ const mergedImportDef = {
1160
+ ...updatedData.data[i].importDef,
1161
+ idMappings: [
1162
+ ...(updatedData.data[i].importDef?.idMappings || []),
1163
+ ...(newImportDef.idMappings || []),
1164
+ ],
1165
+ attributeMappings: [
1166
+ ...(updatedData.data[i].importDef?.attributeMappings || []),
1167
+ ...(newImportDef.attributeMappings || []),
1168
+ ],
1169
+ };
1170
+ updatedData.data[i].importDef = mergedImportDef as ImportDef;
1171
+ this.importMap.set(
1172
+ this.getCollectionKey(collection.name),
1173
+ updatedData
1174
+ );
1175
+ this.oldIdToNewIdPerCollectionMap.set(
1176
+ this.getCollectionKey(collection.name),
1177
+ collectionOldIdToNewIdMap!
1178
+ );
1179
+
1180
+ foundData = true;
1181
+ }
1182
+ }
1183
+ if (!foundData) {
1184
+ // Add new data to the associated collection
1185
+ updatedData.data.push({
1186
+ rawData: item,
1187
+ context: context,
1188
+ importDef: newImportDef,
1189
+ finalData: transformedItem,
1190
+ });
1191
+ this.importMap.set(this.getCollectionKey(collection.name), updatedData);
1192
+ this.oldIdToNewIdPerCollectionMap.set(
1193
+ this.getCollectionKey(collection.name),
1194
+ collectionOldIdToNewIdMap!
1195
+ );
1196
+ }
1197
+ }
1198
+ }
1199
+
1200
+ /**
1201
+ * Prepares the data for creating documents in a collection.
1202
+ * This involves loading the data, transforming it, and handling ID mappings.
1203
+ *
1204
+ * @param db - The database configuration.
1205
+ * @param collection - The collection configuration.
1206
+ * @param importDef - The import definition containing the attribute mappings and other relevant info.
1207
+ */
1208
+ async prepareCreateData(
1209
+ db: ConfigDatabase,
1210
+ collection: CollectionCreate,
1211
+ importDef: ImportDef
1212
+ ): Promise<void> {
1213
+ // Load the raw data based on the import definition
1214
+ const rawData = this.loadData(importDef);
1215
+ let operationId = this.collectionImportOperations.get(
1216
+ this.getCollectionKey(collection.name)
1217
+ );
1218
+ const adapter = await this.getAdapter();
1219
+ if (!operationId) {
1220
+ const collectionImportOperation = await findOrCreateOperation(
1221
+ adapter,
1222
+ db.$id,
1223
+ "importData",
1224
+ collection.$id!
1225
+ );
1226
+ // Store the operation ID in the map
1227
+ this.collectionImportOperations.set(
1228
+ this.getCollectionKey(collection.name),
1229
+ collectionImportOperation.$id
1230
+ );
1231
+ operationId = collectionImportOperation.$id;
1232
+ }
1233
+ if (operationId) {
1234
+ await updateOperation(adapter, db.$id, operationId, {
1235
+ status: "in_progress",
1236
+ total: rawData.length,
1237
+ });
1238
+ }
1239
+ // Initialize a new map for old ID to new ID mappings
1240
+ const oldIdToNewIdMapNew = new Map<string, string>();
1241
+ // Retrieve or initialize the collection-specific old ID to new ID map
1242
+ const collectionOldIdToNewIdMap =
1243
+ this.oldIdToNewIdPerCollectionMap.get(
1244
+ this.getCollectionKey(collection.name)
1245
+ ) ||
1246
+ this.oldIdToNewIdPerCollectionMap
1247
+ .set(this.getCollectionKey(collection.name), oldIdToNewIdMapNew)
1248
+ .get(this.getCollectionKey(collection.name));
1249
+ const isRegions = collection.name.toLowerCase() === "regions";
1250
+ // Iterate through each item in the raw data
1251
+ for (const item of rawData) {
1252
+ // Generate a new unique ID for the item
1253
+ const itemIdNew = this.getTrueUniqueId(
1254
+ this.getCollectionKey(collection.name)
1255
+ );
1256
+ if (isRegions) {
1257
+ logger.info(`Creating region: ${JSON.stringify(item, null, 2)}`);
1258
+ }
1259
+ // Retrieve the current collection data from the import map
1260
+ const currentData = this.importMap.get(
1261
+ this.getCollectionKey(collection.name)
1262
+ );
1263
+ // Create a context object for the item, including the new ID
1264
+ let context = this.createContext(db, collection, item, itemIdNew);
1265
+ // Transform the item data based on the attribute mappings
1266
+ let transformedData = this.transformData(
1267
+ item,
1268
+ importDef.attributeMappings
1269
+ );
1270
+ // If a primary key field is defined, handle the ID mapping and check for duplicates
1271
+ if (importDef.primaryKeyField) {
1272
+ const oldId = item[importDef.primaryKeyField];
1273
+ if (collectionOldIdToNewIdMap?.has(`${oldId}`)) {
1274
+ logger.error(
1275
+ `Collection ${collection.name} has multiple documents with the same primary key ${oldId}`
1276
+ );
1277
+ continue;
1278
+ }
1279
+ collectionOldIdToNewIdMap?.set(`${oldId}`, `${itemIdNew}`);
1280
+ }
1281
+ // Merge the transformed data into the context
1282
+ context = { ...context, ...transformedData };
1283
+ // Validate the item before proceeding
1284
+ const isValid = this.importDataActions.validateItem(
1285
+ transformedData,
1286
+ importDef.attributeMappings,
1287
+ context
1288
+ );
1289
+ if (!isValid) {
1290
+ continue;
1291
+ }
1292
+ // Update the attribute mappings with any actions that need to be performed post-import
1293
+ // We added the basePath to get the folder from the filePath
1294
+ const mappingsWithActions = this.getAttributeMappingsWithActions(
1295
+ importDef.attributeMappings,
1296
+ context,
1297
+ transformedData
1298
+ );
1299
+ // Update the import definition with the new attribute mappings
1300
+ const newImportDef = {
1301
+ ...importDef,
1302
+ attributeMappings: mappingsWithActions,
1303
+ };
1304
+ // If the current collection data exists, add the item with its context and final data
1305
+ if (currentData && currentData.data) {
1306
+ currentData.data.push({
1307
+ rawData: item,
1308
+ context: context,
1309
+ importDef: newImportDef,
1310
+ finalData: transformedData,
1311
+ });
1312
+ this.importMap.set(this.getCollectionKey(collection.name), currentData);
1313
+ this.oldIdToNewIdPerCollectionMap.set(
1314
+ this.getCollectionKey(collection.name),
1315
+ collectionOldIdToNewIdMap!
1316
+ );
1317
+ } else {
1318
+ logger.error(
1319
+ `No data found for collection ${collection.name} for createDef but it says it's supposed to have one...`
1320
+ );
1321
+ continue;
1322
+ }
1323
+ }
1324
+ }
1325
+
1326
+ /**
1327
+ * Prepares the data for updating documents within a collection.
1328
+ * This method loads the raw data based on the import definition, transforms it according to the attribute mappings,
1329
+ * finds the new ID for each item based on the primary key or update mapping, and then validates the transformed data.
1330
+ * If the data is valid, it updates the import definition with any post-import actions and adds the item to the current collection data.
1331
+ *
1332
+ * @param db - The database configuration.
1333
+ * @param collection - The collection configuration.
1334
+ * @param importDef - The import definition containing the attribute mappings and other relevant info.
1335
+ */
1336
+ async prepareUpdateData(
1337
+ db: ConfigDatabase,
1338
+ collection: CollectionCreate,
1339
+ importDef: ImportDef
1340
+ ) {
1341
+ const currentData = this.importMap.get(
1342
+ this.getCollectionKey(collection.name)
1343
+ );
1344
+ const oldIdToNewIdMap = this.oldIdToNewIdPerCollectionMap.get(
1345
+ this.getCollectionKey(collection.name)
1346
+ );
1347
+
1348
+ if (
1349
+ !(currentData?.data && currentData?.data.length > 0) &&
1350
+ !oldIdToNewIdMap
1351
+ ) {
1352
+ logger.error(
1353
+ `No data found for collection ${collection.name} for updateDef but it says it's supposed to have one...`
1354
+ );
1355
+ return;
1356
+ }
1357
+
1358
+ const rawData = this.loadData(importDef);
1359
+ const operationId = this.collectionImportOperations.get(
1360
+ this.getCollectionKey(collection.name)
1361
+ );
1362
+ if (!operationId) {
1363
+ throw new Error(
1364
+ `No import operation found for collection ${collection.name}`
1365
+ );
1366
+ }
1367
+
1368
+ for (const item of rawData) {
1369
+ let transformedData = this.transformData(
1370
+ item,
1371
+ importDef.attributeMappings
1372
+ );
1373
+ let newId: string | undefined;
1374
+ let oldId: string | undefined;
1375
+ let itemDataToUpdate: CollectionImportData["data"][number] | undefined;
1376
+
1377
+ // Try to find itemDataToUpdate using updateMapping
1378
+ if (importDef.updateMapping) {
1379
+ oldId =
1380
+ item[importDef.updateMapping.originalIdField] ||
1381
+ transformedData[importDef.updateMapping.originalIdField];
1382
+ if (oldId) {
1383
+ itemDataToUpdate = currentData?.data.find(
1384
+ ({ context, finalData }) => {
1385
+ const targetField =
1386
+ importDef.updateMapping!.targetField ??
1387
+ importDef.updateMapping!.originalIdField;
1388
+
1389
+ return (
1390
+ `${context[targetField]}` === `${oldId}` ||
1391
+ `${finalData[targetField]}` === `${oldId}`
1392
+ );
1393
+ }
1394
+ );
1395
+
1396
+ if (itemDataToUpdate) {
1397
+ newId = itemDataToUpdate.context.docId;
1398
+ }
1399
+ }
1400
+ }
1401
+
1402
+ // If updateMapping is not defined or did not find the item, use primaryKeyField
1403
+ if (!itemDataToUpdate && importDef.primaryKeyField) {
1404
+ oldId =
1405
+ item[importDef.primaryKeyField] ||
1406
+ transformedData[importDef.primaryKeyField];
1407
+ if (oldId && oldId.length > 0) {
1408
+ newId = oldIdToNewIdMap?.get(`${oldId}`);
1409
+ if (
1410
+ !newId &&
1411
+ this.getCollectionKey(this.config.usersCollectionName) ===
1412
+ this.getCollectionKey(collection.name)
1413
+ ) {
1414
+ for (const [key, value] of this.mergedUserMap.entries()) {
1415
+ if (value.includes(`${oldId}`)) {
1416
+ newId = key;
1417
+ break;
1418
+ }
1419
+ }
1420
+ }
1421
+ }
1422
+
1423
+ if (oldId && !itemDataToUpdate) {
1424
+ itemDataToUpdate = currentData?.data.find(
1425
+ (data) =>
1426
+ `${data.context[importDef.primaryKeyField]}` === `${oldId}`
1427
+ );
1428
+ }
1429
+ }
1430
+
1431
+ if (!oldId) {
1432
+ logger.error(
1433
+ `No old ID found (to update another document with) in prepareUpdateData for ${
1434
+ collection.name
1435
+ }, ${JSON.stringify(item, null, 2)}`
1436
+ );
1437
+ continue;
1438
+ }
1439
+
1440
+ if (!newId && !itemDataToUpdate) {
1441
+ logger.error(
1442
+ `No new id && no data found for collection ${
1443
+ collection.name
1444
+ } for updateDef ${JSON.stringify(
1445
+ item,
1446
+ null,
1447
+ 2
1448
+ )} but it says it's supposed to have one...`
1449
+ );
1450
+ continue;
1451
+ } else if (itemDataToUpdate) {
1452
+ newId =
1453
+ itemDataToUpdate.finalData.docId || itemDataToUpdate.context.docId;
1454
+ if (!newId) {
1455
+ logger.error(
1456
+ `No new id found for collection ${
1457
+ collection.name
1458
+ } for updateDef ${JSON.stringify(
1459
+ item,
1460
+ null,
1461
+ 2
1462
+ )} but has itemDataToUpdate ${JSON.stringify(
1463
+ itemDataToUpdate,
1464
+ null,
1465
+ 2
1466
+ )} but it says it's supposed to have one...`
1467
+ );
1468
+ continue;
1469
+ }
1470
+ }
1471
+
1472
+ if (!itemDataToUpdate || !newId) {
1473
+ logger.error(
1474
+ `No data or ID (docId) found for collection ${
1475
+ collection.name
1476
+ } for updateDef ${JSON.stringify(
1477
+ item,
1478
+ null,
1479
+ 2
1480
+ )} but it says it's supposed to have one...`
1481
+ );
1482
+ continue;
1483
+ }
1484
+
1485
+ transformedData = this.mergeObjects(
1486
+ itemDataToUpdate.finalData,
1487
+ transformedData
1488
+ );
1489
+
1490
+ // Create a context object for the item, including the new ID and transformed data
1491
+ let context = itemDataToUpdate.context;
1492
+ context = this.mergeObjects(context, transformedData);
1493
+
1494
+ // Validate the item before proceeding
1495
+ const isValid = this.importDataActions.validateItem(
1496
+ item,
1497
+ importDef.attributeMappings,
1498
+ context
1499
+ );
1500
+
1501
+ if (!isValid) {
1502
+ logger.info(
1503
+ `Skipping item: ${JSON.stringify(item, null, 2)} because it's invalid`
1504
+ );
1505
+ continue;
1506
+ }
1507
+
1508
+ // Update the attribute mappings with any actions that need to be performed post-import
1509
+ // We added the basePath to get the folder from the filePath
1510
+ const mappingsWithActions = this.getAttributeMappingsWithActions(
1511
+ importDef.attributeMappings,
1512
+ context,
1513
+ transformedData
1514
+ );
1515
+
1516
+ // Update the import definition with the new attribute mappings
1517
+ const newImportDef = {
1518
+ ...importDef,
1519
+ attributeMappings: mappingsWithActions,
1520
+ };
1521
+
1522
+ if (itemDataToUpdate) {
1523
+ itemDataToUpdate.finalData = this.mergeObjects(
1524
+ itemDataToUpdate.finalData,
1525
+ transformedData
1526
+ );
1527
+ itemDataToUpdate.context = context;
1528
+
1529
+ // Fix: Ensure we properly merge the attribute mappings and their actions
1530
+ const mergedAttributeMappings = newImportDef.attributeMappings.map(
1531
+ (newMapping) => {
1532
+ const existingMapping =
1533
+ itemDataToUpdate.importDef?.attributeMappings.find(
1534
+ (m) => m.targetKey === newMapping.targetKey
1535
+ );
1536
+
1537
+ return {
1538
+ ...newMapping,
1539
+ postImportActions: [
1540
+ ...(existingMapping?.postImportActions || []),
1541
+ ...(newMapping.postImportActions || []),
1542
+ ],
1543
+ };
1544
+ }
1545
+ );
1546
+
1547
+ itemDataToUpdate.importDef = {
1548
+ ...newImportDef,
1549
+ attributeMappings: mergedAttributeMappings,
1550
+ };
1551
+
1552
+ // Debug logging
1553
+ if (
1554
+ mergedAttributeMappings.some((m) => m.postImportActions?.length > 0)
1555
+ ) {
1556
+ logger.info(
1557
+ `Post-import actions for ${collection.name}: ${JSON.stringify(
1558
+ mergedAttributeMappings
1559
+ .filter((m) => m.postImportActions?.length > 0)
1560
+ .map((m) => ({
1561
+ targetKey: m.targetKey,
1562
+ actions: m.postImportActions,
1563
+ })),
1564
+ null,
1565
+ 2
1566
+ )}`
1567
+ );
1568
+ }
1569
+ } else {
1570
+ currentData!.data.push({
1571
+ rawData: item,
1572
+ context: context,
1573
+ importDef: newImportDef,
1574
+ finalData: transformedData,
1575
+ });
1576
+ }
1577
+
1578
+ // Since we're modifying currentData in place, we ensure no duplicates are added
1579
+ this.importMap.set(this.getCollectionKey(collection.name), currentData!);
1580
+ }
1581
+ }
1582
+
1583
+ private updateReferencesBasedOnAttributeMappings() {
1584
+ if (!this.config.collections) {
1585
+ return;
1586
+ }
1587
+ this.config.collections.forEach((collectionConfig) => {
1588
+ const collectionName = collectionConfig.name;
1589
+ const collectionData = this.importMap.get(
1590
+ this.getCollectionKey(collectionName)
1591
+ );
1592
+
1593
+ if (!collectionData) {
1594
+ logger.error(`No data found for collection ${collectionName}`);
1595
+ return;
1596
+ }
1597
+
1598
+ collectionData.data.forEach((dataItem) => {
1599
+ collectionConfig.importDefs.forEach((importDef) => {
1600
+ if (!importDef.idMappings) return; // Skip collections without idMappings
1601
+ importDef.idMappings.forEach((mapping) => {
1602
+ if (mapping && mapping.targetField) {
1603
+ const idsToUpdate = Array.isArray(
1604
+ dataItem[mapping.targetField as keyof typeof dataItem]
1605
+ )
1606
+ ? dataItem[mapping.targetField as keyof typeof dataItem]
1607
+ : [dataItem[mapping.targetField as keyof typeof dataItem]];
1608
+ const updatedIds = idsToUpdate.map((id: string) =>
1609
+ this.getMergedId(id, mapping.targetCollection)
1610
+ );
1611
+
1612
+ // Update the dataItem with the new IDs
1613
+ dataItem[mapping.targetField as keyof typeof dataItem] =
1614
+ Array.isArray(
1615
+ dataItem[mapping.targetField as keyof typeof dataItem]
1616
+ )
1617
+ ? updatedIds
1618
+ : updatedIds[0];
1619
+ }
1620
+ });
1621
+ });
1622
+ });
1623
+ });
1624
+ }
1625
+
1626
+ private getMergedId(oldId: string, relatedCollectionName: string): string {
1627
+ // Retrieve the old to new ID map for the related collection
1628
+ const oldToNewIdMap = this.oldIdToNewIdPerCollectionMap.get(
1629
+ this.getCollectionKey(relatedCollectionName)
1630
+ );
1631
+
1632
+ // If there's a mapping for the old ID, return the new ID
1633
+ if (oldToNewIdMap && oldToNewIdMap.has(`${oldId}`)) {
1634
+ return oldToNewIdMap.get(`${oldId}`)!; // The non-null assertion (!) is used because we checked if the map has the key
1635
+ }
1636
+
1637
+ // If no mapping is found, return the old ID as a fallback
1638
+ return oldId;
1639
+ }
1640
+
1641
+ /**
1642
+ * Generates attribute mappings with post-import actions based on the provided attribute mappings.
1643
+ * This method checks each mapping for a fileData attribute and adds a post-import action to create a file
1644
+ * and update the field with the file's ID if necessary.
1645
+ *
1646
+ * @param attributeMappings - The attribute mappings from the import definition.
1647
+ * @param context - The context object containing information about the database, collection, and document.
1648
+ * @param item - The item being imported, used for resolving template paths in fileData mappings.
1649
+ * @returns The attribute mappings updated with any necessary post-import actions.
1650
+ */
1651
+ getAttributeMappingsWithActions(
1652
+ attributeMappings: AttributeMappings,
1653
+ context: any,
1654
+ item: any
1655
+ ) {
1656
+ // Iterate over each attribute mapping to check for fileData attributes
1657
+ return attributeMappings.map((mapping) => {
1658
+ if (mapping.fileData) {
1659
+ // Resolve the file path using the provided template, context, and item
1660
+ let mappingFilePath = this.importDataActions.resolveTemplate(
1661
+ mapping.fileData.path,
1662
+ context,
1663
+ item
1664
+ );
1665
+ // Ensure the file path is absolute if it doesn't start with "http"
1666
+ if (!mappingFilePath.toLowerCase().startsWith("http")) {
1667
+ // First try the direct path
1668
+ let fullPath = path.resolve(this.appwriteFolderPath, mappingFilePath);
1669
+
1670
+ // If file doesn't exist, search in subdirectories
1671
+ if (!fs.existsSync(fullPath)) {
1672
+ const findFileInDir = (dir: string): string | null => {
1673
+ const files = fs.readdirSync(dir);
1674
+
1675
+ for (const file of files) {
1676
+ const filePath = path.join(dir, file);
1677
+ const stat = fs.statSync(filePath);
1678
+
1679
+ if (stat.isDirectory()) {
1680
+ // Recursively search subdirectories
1681
+ const found = findFileInDir(filePath);
1682
+ if (found) return found;
1683
+ } else if (file === path.basename(mappingFilePath)) {
1684
+ return filePath;
1685
+ }
1686
+ }
1687
+ return null;
1688
+ };
1689
+
1690
+ const foundPath = findFileInDir(this.appwriteFolderPath);
1691
+ if (foundPath) {
1692
+ mappingFilePath = foundPath;
1693
+ } else {
1694
+ logger.warn(
1695
+ `File not found in any subdirectory: ${mappingFilePath}`
1696
+ );
1697
+ // Keep the original resolved path as fallback
1698
+ mappingFilePath = fullPath;
1699
+ }
1700
+ } else {
1701
+ mappingFilePath = fullPath;
1702
+ }
1703
+ }
1704
+ // Define the after-import action to create a file and update the field
1705
+ const afterImportAction = {
1706
+ action: "createFileAndUpdateField",
1707
+ params: [
1708
+ "{dbId}",
1709
+ "{collId}",
1710
+ "{docId}",
1711
+ mapping.targetKey,
1712
+ `${this.config!.documentBucketId}_${context.dbName
1713
+ .toLowerCase()
1714
+ .replace(" ", "")}`, // Assuming 'images' is your bucket ID
1715
+ mappingFilePath,
1716
+ mapping.fileData.name,
1717
+ ],
1718
+ };
1719
+ // Add the after-import action to the mapping's postImportActions array
1720
+ const postImportActions = mapping.postImportActions
1721
+ ? [...mapping.postImportActions, afterImportAction]
1722
+ : [afterImportAction];
1723
+ return { ...mapping, postImportActions };
1724
+ }
1725
+ // Return the mapping unchanged if no fileData attribute is found
1726
+ return mapping;
1727
+ });
1728
+ }
1729
+ }