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.
- package/CONFIG_TODO.md +1189 -1189
- package/README.md +1004 -1004
- package/SELECTION_DIALOGS.md +145 -145
- package/SERVICE_IMPLEMENTATION_REPORT.md +462 -462
- package/package.json +6 -3
- package/scripts/copy-templates.ts +23 -23
- package/src/adapters/index.ts +11 -37
- package/src/backups/operations/bucketBackup.ts +277 -277
- package/src/backups/operations/collectionBackup.ts +310 -310
- package/src/backups/operations/comprehensiveBackup.ts +342 -342
- package/src/backups/schemas/bucketManifest.ts +78 -78
- package/src/backups/schemas/comprehensiveManifest.ts +76 -76
- package/src/backups/tracking/centralizedTracking.ts +352 -352
- package/src/cli/commands/configCommands.ts +265 -201
- package/src/cli/commands/databaseCommands.ts +931 -879
- package/src/cli/commands/functionCommands.ts +333 -332
- package/src/cli/commands/importFileCommands.ts +815 -0
- package/src/cli/commands/schemaCommands.ts +141 -141
- package/src/cli/commands/storageCommands.ts +2 -3
- package/src/cli/commands/transferCommands.ts +454 -457
- package/src/collections/attributes.ts.backup +1555 -1555
- package/src/collections/{attributes.ts → columns.ts} +15 -44
- package/src/collections/indexes.ts +350 -352
- package/src/collections/methods.ts +714 -815
- package/src/collections/tableOperations.ts +57 -21
- package/src/collections/transferOperations.ts +376 -377
- package/src/collections/wipeOperations.ts +449 -346
- package/src/databases/methods.ts +49 -49
- package/src/databases/setup.ts +77 -77
- package/src/examples/yamlTerminologyExample.ts +346 -346
- package/src/functions/deployments.ts +221 -220
- package/src/functions/fnConfigDiscovery.ts +2 -2
- package/src/functions/methods.ts +284 -284
- package/src/functions/templates/count-docs-in-collection/README.md +53 -53
- package/src/functions/templates/count-docs-in-collection/src/main.ts +159 -159
- package/src/functions/templates/count-docs-in-collection/src/request.ts +8 -8
- package/src/functions/templates/hono-typescript/README.md +285 -285
- package/src/functions/templates/hono-typescript/src/adapters/request.ts +73 -73
- package/src/functions/templates/hono-typescript/src/adapters/response.ts +105 -105
- package/src/functions/templates/hono-typescript/src/app.ts +179 -179
- package/src/functions/templates/hono-typescript/src/context.ts +102 -102
- package/src/functions/templates/hono-typescript/src/{index.ts → main.ts} +53 -53
- package/src/functions/templates/hono-typescript/src/middleware/appwrite.ts +118 -118
- package/src/functions/templates/typescript-node/README.md +31 -31
- package/src/functions/templates/typescript-node/src/context.ts +102 -102
- package/src/functions/templates/typescript-node/src/{index.ts → main.ts} +29 -29
- package/src/functions/templates/uv/README.md +30 -30
- package/src/functions/templates/uv/pyproject.toml +29 -29
- package/src/functions/templates/uv/src/context.py +124 -124
- package/src/functions/templates/uv/src/{index.py → main.py} +45 -45
- package/src/init.ts +62 -62
- package/src/interactiveCLI.ts +1095 -1030
- package/src/main.ts +1517 -1670
- package/src/migrations/afterImportActions.ts +579 -580
- package/src/migrations/appwriteToX.ts +634 -630
- package/src/migrations/comprehensiveTransfer.ts +2149 -2149
- package/src/migrations/dataLoader.ts +1729 -1702
- package/src/migrations/importController.ts +440 -428
- package/src/migrations/importDataActions.ts +315 -315
- package/src/migrations/relationships.ts +333 -334
- package/src/migrations/services/DataTransformationService.ts +195 -195
- package/src/migrations/services/FileHandlerService.ts +310 -310
- package/src/migrations/services/ImportOrchestrator.ts +674 -665
- package/src/migrations/services/RateLimitManager.ts +362 -362
- package/src/migrations/services/RelationshipResolver.ts +460 -460
- package/src/migrations/services/UserMappingService.ts +344 -344
- package/src/migrations/services/ValidationService.ts +333 -333
- package/src/migrations/transfer.ts +987 -942
- package/src/migrations/yaml/YamlImportConfigLoader.ts +438 -438
- package/src/migrations/yaml/YamlImportIntegration.ts +438 -438
- package/src/migrations/yaml/generateImportSchemas.ts +1347 -1347
- package/src/schemas/authUser.ts +23 -23
- package/src/setup.ts +8 -8
- package/src/setupCommands.ts +5 -6
- package/src/setupController.ts +42 -42
- package/src/shared/backupMetadataSchema.ts +93 -93
- package/src/shared/backupTracking.ts +211 -211
- package/src/shared/confirmationDialogs.ts +326 -326
- package/src/shared/migrationHelpers.ts +232 -232
- package/src/shared/operationLogger.ts +20 -20
- package/src/shared/operationQueue.ts +326 -327
- package/src/shared/operationsTable.ts +338 -338
- package/src/shared/operationsTableSchema.ts +60 -60
- package/src/shared/progressManager.ts +277 -277
- package/src/shared/relationshipExtractor.ts +214 -214
- package/src/shared/selectionDialogs.ts +775 -722
- package/src/storage/backupCompression.ts +88 -88
- package/src/storage/methods.ts +695 -682
- package/src/storage/schemas.ts +205 -205
- package/src/tables/indexManager.ts +409 -0
- package/src/types/node-appwrite-tablesdb.d.ts +43 -43
- package/src/types.ts +9 -9
- package/src/users/methods.ts +358 -359
- package/src/utils/configMigration.ts +347 -347
- package/src/utils/index.ts +2 -2
- package/src/utils/loadConfigs.ts +457 -449
- package/src/utils/setupFiles.ts +1236 -1238
- package/src/utilsController.ts +1263 -1213
- package/tests/README.md +496 -496
- package/tests/adapters/AdapterFactory.test.ts +276 -276
- package/tests/integration/syncOperations.test.ts +462 -462
- package/tests/jest.config.js +24 -24
- package/tests/migration/configMigration.test.ts +545 -545
- package/tests/setup.ts +61 -61
- package/tests/testUtils.ts +339 -339
- package/tests/utils/loadConfigs.test.ts +349 -349
- package/tests/validation/configValidation.test.ts +411 -411
- package/tsconfig.json +44 -44
- package/.appwrite/.yaml_schemas/appwrite-config.schema.json +0 -380
- package/.appwrite/.yaml_schemas/collection.schema.json +0 -255
- package/.appwrite/collections/Categories.yaml +0 -182
- package/.appwrite/collections/ExampleCollection.yaml +0 -36
- package/.appwrite/collections/Posts.yaml +0 -227
- package/.appwrite/collections/Users.yaml +0 -149
- package/.appwrite/config.yaml +0 -109
- package/.appwrite/import/README.md +0 -148
- package/.appwrite/import/categories-import.yaml +0 -129
- package/.appwrite/import/posts-import.yaml +0 -208
- package/.appwrite/import/users-import.yaml +0 -130
- package/.appwrite/importData/categories.json +0 -194
- package/.appwrite/importData/posts.json +0 -270
- package/.appwrite/importData/users.json +0 -220
- package/.appwrite/schemas/categories.json +0 -128
- package/.appwrite/schemas/exampleCollection.json +0 -52
- package/.appwrite/schemas/posts.json +0 -173
- package/.appwrite/schemas/users.json +0 -125
- package/dist/adapters/AdapterFactory.d.ts +0 -94
- package/dist/adapters/AdapterFactory.js +0 -405
- package/dist/adapters/DatabaseAdapter.d.ts +0 -242
- package/dist/adapters/DatabaseAdapter.js +0 -50
- package/dist/adapters/LegacyAdapter.d.ts +0 -50
- package/dist/adapters/LegacyAdapter.js +0 -612
- package/dist/adapters/TablesDBAdapter.d.ts +0 -45
- package/dist/adapters/TablesDBAdapter.js +0 -596
- package/dist/adapters/index.d.ts +0 -11
- package/dist/adapters/index.js +0 -12
- package/dist/backups/operations/bucketBackup.d.ts +0 -19
- package/dist/backups/operations/bucketBackup.js +0 -197
- package/dist/backups/operations/collectionBackup.d.ts +0 -30
- package/dist/backups/operations/collectionBackup.js +0 -201
- package/dist/backups/operations/comprehensiveBackup.d.ts +0 -25
- package/dist/backups/operations/comprehensiveBackup.js +0 -238
- package/dist/backups/schemas/bucketManifest.d.ts +0 -93
- package/dist/backups/schemas/bucketManifest.js +0 -33
- package/dist/backups/schemas/comprehensiveManifest.d.ts +0 -108
- package/dist/backups/schemas/comprehensiveManifest.js +0 -32
- package/dist/backups/tracking/centralizedTracking.d.ts +0 -34
- package/dist/backups/tracking/centralizedTracking.js +0 -274
- package/dist/cli/commands/configCommands.d.ts +0 -8
- package/dist/cli/commands/configCommands.js +0 -166
- package/dist/cli/commands/databaseCommands.d.ts +0 -14
- package/dist/cli/commands/databaseCommands.js +0 -644
- package/dist/cli/commands/functionCommands.d.ts +0 -7
- package/dist/cli/commands/functionCommands.js +0 -330
- package/dist/cli/commands/schemaCommands.d.ts +0 -7
- package/dist/cli/commands/schemaCommands.js +0 -169
- package/dist/cli/commands/storageCommands.d.ts +0 -5
- package/dist/cli/commands/storageCommands.js +0 -143
- package/dist/cli/commands/transferCommands.d.ts +0 -5
- package/dist/cli/commands/transferCommands.js +0 -384
- package/dist/collections/attributes.d.ts +0 -13
- package/dist/collections/attributes.js +0 -1364
- package/dist/collections/indexes.d.ts +0 -12
- package/dist/collections/indexes.js +0 -217
- package/dist/collections/methods.d.ts +0 -19
- package/dist/collections/methods.js +0 -734
- package/dist/collections/tableOperations.d.ts +0 -86
- package/dist/collections/tableOperations.js +0 -434
- package/dist/collections/transferOperations.d.ts +0 -8
- package/dist/collections/transferOperations.js +0 -412
- package/dist/collections/wipeOperations.d.ts +0 -16
- package/dist/collections/wipeOperations.js +0 -233
- package/dist/config/ConfigManager.d.ts +0 -450
- package/dist/config/ConfigManager.js +0 -625
- package/dist/config/configMigration.d.ts +0 -87
- package/dist/config/configMigration.js +0 -390
- package/dist/config/configValidation.d.ts +0 -66
- package/dist/config/configValidation.js +0 -358
- package/dist/config/index.d.ts +0 -8
- package/dist/config/index.js +0 -7
- package/dist/config/services/ConfigDiscoveryService.d.ts +0 -122
- package/dist/config/services/ConfigDiscoveryService.js +0 -322
- package/dist/config/services/ConfigLoaderService.d.ts +0 -129
- package/dist/config/services/ConfigLoaderService.js +0 -535
- package/dist/config/services/ConfigMergeService.d.ts +0 -208
- package/dist/config/services/ConfigMergeService.js +0 -308
- package/dist/config/services/ConfigValidationService.d.ts +0 -214
- package/dist/config/services/ConfigValidationService.js +0 -310
- package/dist/config/services/SessionAuthService.d.ts +0 -225
- package/dist/config/services/SessionAuthService.js +0 -456
- package/dist/config/services/__tests__/ConfigMergeService.test.d.ts +0 -1
- package/dist/config/services/__tests__/ConfigMergeService.test.js +0 -271
- package/dist/config/services/index.d.ts +0 -13
- package/dist/config/services/index.js +0 -10
- package/dist/config/yamlConfig.d.ts +0 -722
- package/dist/config/yamlConfig.js +0 -702
- package/dist/databases/methods.d.ts +0 -6
- package/dist/databases/methods.js +0 -35
- package/dist/databases/setup.d.ts +0 -5
- package/dist/databases/setup.js +0 -45
- package/dist/examples/yamlTerminologyExample.d.ts +0 -42
- package/dist/examples/yamlTerminologyExample.js +0 -272
- package/dist/functions/deployments.d.ts +0 -4
- package/dist/functions/deployments.js +0 -146
- package/dist/functions/fnConfigDiscovery.d.ts +0 -3
- package/dist/functions/fnConfigDiscovery.js +0 -108
- package/dist/functions/methods.d.ts +0 -16
- package/dist/functions/methods.js +0 -174
- package/dist/functions/pathResolution.d.ts +0 -37
- package/dist/functions/pathResolution.js +0 -185
- package/dist/functions/templates/count-docs-in-collection/README.md +0 -54
- package/dist/functions/templates/count-docs-in-collection/package.json +0 -25
- package/dist/functions/templates/count-docs-in-collection/src/main.ts +0 -159
- package/dist/functions/templates/count-docs-in-collection/src/request.ts +0 -9
- package/dist/functions/templates/count-docs-in-collection/tsconfig.json +0 -28
- package/dist/functions/templates/hono-typescript/README.md +0 -286
- package/dist/functions/templates/hono-typescript/package.json +0 -26
- package/dist/functions/templates/hono-typescript/src/adapters/request.ts +0 -74
- package/dist/functions/templates/hono-typescript/src/adapters/response.ts +0 -106
- package/dist/functions/templates/hono-typescript/src/app.ts +0 -180
- package/dist/functions/templates/hono-typescript/src/context.ts +0 -103
- package/dist/functions/templates/hono-typescript/src/index.ts +0 -54
- package/dist/functions/templates/hono-typescript/src/middleware/appwrite.ts +0 -119
- package/dist/functions/templates/hono-typescript/tsconfig.json +0 -20
- package/dist/functions/templates/typescript-node/README.md +0 -32
- package/dist/functions/templates/typescript-node/package.json +0 -25
- package/dist/functions/templates/typescript-node/src/context.ts +0 -103
- package/dist/functions/templates/typescript-node/src/index.ts +0 -29
- package/dist/functions/templates/typescript-node/tsconfig.json +0 -28
- package/dist/functions/templates/uv/README.md +0 -31
- package/dist/functions/templates/uv/pyproject.toml +0 -30
- package/dist/functions/templates/uv/src/__init__.py +0 -0
- package/dist/functions/templates/uv/src/context.py +0 -125
- package/dist/functions/templates/uv/src/index.py +0 -46
- package/dist/init.d.ts +0 -2
- package/dist/init.js +0 -57
- package/dist/interactiveCLI.d.ts +0 -31
- package/dist/interactiveCLI.js +0 -898
- package/dist/main.d.ts +0 -2
- package/dist/main.js +0 -1180
- package/dist/migrations/afterImportActions.d.ts +0 -17
- package/dist/migrations/afterImportActions.js +0 -306
- package/dist/migrations/appwriteToX.d.ts +0 -211
- package/dist/migrations/appwriteToX.js +0 -491
- package/dist/migrations/comprehensiveTransfer.d.ts +0 -147
- package/dist/migrations/comprehensiveTransfer.js +0 -1317
- package/dist/migrations/dataLoader.d.ts +0 -753
- package/dist/migrations/dataLoader.js +0 -1250
- package/dist/migrations/importController.d.ts +0 -23
- package/dist/migrations/importController.js +0 -268
- package/dist/migrations/importDataActions.d.ts +0 -50
- package/dist/migrations/importDataActions.js +0 -230
- package/dist/migrations/relationships.d.ts +0 -29
- package/dist/migrations/relationships.js +0 -204
- package/dist/migrations/services/DataTransformationService.d.ts +0 -55
- package/dist/migrations/services/DataTransformationService.js +0 -158
- package/dist/migrations/services/FileHandlerService.d.ts +0 -75
- package/dist/migrations/services/FileHandlerService.js +0 -236
- package/dist/migrations/services/ImportOrchestrator.d.ts +0 -97
- package/dist/migrations/services/ImportOrchestrator.js +0 -485
- package/dist/migrations/services/RateLimitManager.d.ts +0 -138
- package/dist/migrations/services/RateLimitManager.js +0 -279
- package/dist/migrations/services/RelationshipResolver.d.ts +0 -120
- package/dist/migrations/services/RelationshipResolver.js +0 -332
- package/dist/migrations/services/UserMappingService.d.ts +0 -109
- package/dist/migrations/services/UserMappingService.js +0 -277
- package/dist/migrations/services/ValidationService.d.ts +0 -74
- package/dist/migrations/services/ValidationService.js +0 -260
- package/dist/migrations/transfer.d.ts +0 -26
- package/dist/migrations/transfer.js +0 -608
- package/dist/migrations/yaml/YamlImportConfigLoader.d.ts +0 -131
- package/dist/migrations/yaml/YamlImportConfigLoader.js +0 -383
- package/dist/migrations/yaml/YamlImportIntegration.d.ts +0 -93
- package/dist/migrations/yaml/YamlImportIntegration.js +0 -341
- package/dist/migrations/yaml/generateImportSchemas.d.ts +0 -30
- package/dist/migrations/yaml/generateImportSchemas.js +0 -1327
- package/dist/schemas/authUser.d.ts +0 -24
- package/dist/schemas/authUser.js +0 -17
- package/dist/setup.d.ts +0 -2
- package/dist/setup.js +0 -5
- package/dist/setupCommands.d.ts +0 -58
- package/dist/setupCommands.js +0 -490
- package/dist/setupController.d.ts +0 -9
- package/dist/setupController.js +0 -34
- package/dist/shared/attributeMapper.d.ts +0 -20
- package/dist/shared/attributeMapper.js +0 -203
- package/dist/shared/backupMetadataSchema.d.ts +0 -94
- package/dist/shared/backupMetadataSchema.js +0 -38
- package/dist/shared/backupTracking.d.ts +0 -18
- package/dist/shared/backupTracking.js +0 -176
- package/dist/shared/confirmationDialogs.d.ts +0 -75
- package/dist/shared/confirmationDialogs.js +0 -236
- package/dist/shared/errorUtils.d.ts +0 -54
- package/dist/shared/errorUtils.js +0 -95
- package/dist/shared/functionManager.d.ts +0 -48
- package/dist/shared/functionManager.js +0 -348
- package/dist/shared/indexManager.d.ts +0 -24
- package/dist/shared/indexManager.js +0 -151
- package/dist/shared/jsonSchemaGenerator.d.ts +0 -50
- package/dist/shared/jsonSchemaGenerator.js +0 -290
- package/dist/shared/logging.d.ts +0 -61
- package/dist/shared/logging.js +0 -116
- package/dist/shared/messageFormatter.d.ts +0 -39
- package/dist/shared/messageFormatter.js +0 -162
- package/dist/shared/migrationHelpers.d.ts +0 -61
- package/dist/shared/migrationHelpers.js +0 -145
- package/dist/shared/operationLogger.d.ts +0 -10
- package/dist/shared/operationLogger.js +0 -12
- package/dist/shared/operationQueue.d.ts +0 -40
- package/dist/shared/operationQueue.js +0 -311
- package/dist/shared/operationsTable.d.ts +0 -26
- package/dist/shared/operationsTable.js +0 -286
- package/dist/shared/operationsTableSchema.d.ts +0 -48
- package/dist/shared/operationsTableSchema.js +0 -35
- package/dist/shared/progressManager.d.ts +0 -62
- package/dist/shared/progressManager.js +0 -215
- package/dist/shared/pydanticModelGenerator.d.ts +0 -17
- package/dist/shared/pydanticModelGenerator.js +0 -615
- package/dist/shared/relationshipExtractor.d.ts +0 -56
- package/dist/shared/relationshipExtractor.js +0 -138
- package/dist/shared/schemaGenerator.d.ts +0 -40
- package/dist/shared/schemaGenerator.js +0 -556
- package/dist/shared/selectionDialogs.d.ts +0 -214
- package/dist/shared/selectionDialogs.js +0 -544
- package/dist/storage/backupCompression.d.ts +0 -20
- package/dist/storage/backupCompression.js +0 -67
- package/dist/storage/methods.d.ts +0 -32
- package/dist/storage/methods.js +0 -472
- package/dist/storage/schemas.d.ts +0 -842
- package/dist/storage/schemas.js +0 -175
- package/dist/types.d.ts +0 -4
- package/dist/types.js +0 -3
- package/dist/users/methods.d.ts +0 -16
- package/dist/users/methods.js +0 -277
- package/dist/utils/ClientFactory.d.ts +0 -87
- package/dist/utils/ClientFactory.js +0 -212
- package/dist/utils/configDiscovery.d.ts +0 -78
- package/dist/utils/configDiscovery.js +0 -472
- package/dist/utils/configMigration.d.ts +0 -1
- package/dist/utils/configMigration.js +0 -261
- package/dist/utils/constantsGenerator.d.ts +0 -31
- package/dist/utils/constantsGenerator.js +0 -321
- package/dist/utils/dataConverters.d.ts +0 -46
- package/dist/utils/dataConverters.js +0 -139
- package/dist/utils/directoryUtils.d.ts +0 -22
- package/dist/utils/directoryUtils.js +0 -59
- package/dist/utils/getClientFromConfig.d.ts +0 -39
- package/dist/utils/getClientFromConfig.js +0 -199
- package/dist/utils/helperFunctions.d.ts +0 -63
- package/dist/utils/helperFunctions.js +0 -156
- package/dist/utils/index.d.ts +0 -2
- package/dist/utils/index.js +0 -2
- package/dist/utils/loadConfigs.d.ts +0 -50
- package/dist/utils/loadConfigs.js +0 -358
- package/dist/utils/pathResolvers.d.ts +0 -53
- package/dist/utils/pathResolvers.js +0 -72
- package/dist/utils/projectConfig.d.ts +0 -122
- package/dist/utils/projectConfig.js +0 -206
- package/dist/utils/retryFailedPromises.d.ts +0 -2
- package/dist/utils/retryFailedPromises.js +0 -23
- package/dist/utils/sessionAuth.d.ts +0 -48
- package/dist/utils/sessionAuth.js +0 -164
- package/dist/utils/setupFiles.d.ts +0 -4
- package/dist/utils/setupFiles.js +0 -1192
- package/dist/utils/typeGuards.d.ts +0 -35
- package/dist/utils/typeGuards.js +0 -57
- package/dist/utils/validationRules.d.ts +0 -43
- package/dist/utils/validationRules.js +0 -42
- package/dist/utils/versionDetection.d.ts +0 -58
- package/dist/utils/versionDetection.js +0 -251
- package/dist/utils/yamlConverter.d.ts +0 -100
- package/dist/utils/yamlConverter.js +0 -428
- package/dist/utils/yamlLoader.d.ts +0 -70
- package/dist/utils/yamlLoader.js +0 -267
- package/dist/utilsController.d.ts +0 -107
- package/dist/utilsController.js +0 -873
- package/src/adapters/AdapterFactory.ts +0 -510
- package/src/adapters/DatabaseAdapter.ts +0 -318
- package/src/adapters/LegacyAdapter.ts +0 -841
- package/src/adapters/TablesDBAdapter.ts +0 -815
- package/src/config/ConfigManager.ts +0 -817
- package/src/config/README.md +0 -274
- package/src/config/configMigration.ts +0 -575
- package/src/config/configValidation.ts +0 -445
- package/src/config/index.ts +0 -10
- package/src/config/services/ConfigDiscoveryService.ts +0 -410
- package/src/config/services/ConfigLoaderService.ts +0 -732
- package/src/config/services/ConfigMergeService.ts +0 -388
- package/src/config/services/ConfigValidationService.ts +0 -394
- package/src/config/services/SessionAuthService.ts +0 -565
- package/src/config/services/__tests__/ConfigMergeService.test.ts +0 -351
- package/src/config/services/index.ts +0 -29
- package/src/config/yamlConfig.ts +0 -761
- package/src/functions/pathResolution.ts +0 -227
- package/src/functions/templates/count-docs-in-collection/package.json +0 -25
- package/src/functions/templates/count-docs-in-collection/tsconfig.json +0 -28
- package/src/functions/templates/hono-typescript/package.json +0 -26
- package/src/functions/templates/hono-typescript/tsconfig.json +0 -20
- package/src/functions/templates/typescript-node/package.json +0 -25
- package/src/functions/templates/typescript-node/tsconfig.json +0 -28
- package/src/shared/attributeMapper.ts +0 -229
- package/src/shared/errorUtils.ts +0 -110
- package/src/shared/functionManager.ts +0 -537
- package/src/shared/indexManager.ts +0 -254
- package/src/shared/jsonSchemaGenerator.ts +0 -383
- package/src/shared/logging.ts +0 -149
- package/src/shared/messageFormatter.ts +0 -208
- package/src/shared/pydanticModelGenerator.ts +0 -618
- package/src/shared/schemaGenerator.ts +0 -644
- package/src/utils/ClientFactory.ts +0 -240
- package/src/utils/configDiscovery.ts +0 -557
- package/src/utils/constantsGenerator.ts +0 -369
- package/src/utils/dataConverters.ts +0 -159
- package/src/utils/directoryUtils.ts +0 -61
- package/src/utils/getClientFromConfig.ts +0 -257
- package/src/utils/helperFunctions.ts +0 -228
- package/src/utils/pathResolvers.ts +0 -81
- package/src/utils/projectConfig.ts +0 -340
- package/src/utils/retryFailedPromises.ts +0 -29
- package/src/utils/sessionAuth.ts +0 -230
- package/src/utils/typeGuards.ts +0 -65
- package/src/utils/validationRules.ts +0 -88
- package/src/utils/versionDetection.ts +0 -292
- package/src/utils/yamlConverter.ts +0 -542
- package/src/utils/yamlLoader.ts +0 -371
- package/tmp-sync-test/.appwrite/collections/TestCollection.yaml +0 -7
|
@@ -1,815 +1,714 @@
|
|
|
1
|
-
import {
|
|
2
|
-
Databases,
|
|
3
|
-
ID,
|
|
4
|
-
Permission,
|
|
5
|
-
Query,
|
|
6
|
-
type Models,
|
|
7
|
-
} from "node-appwrite";
|
|
8
|
-
import type { AppwriteConfig, CollectionCreate, Indexes, Attribute } from "appwrite-utils";
|
|
9
|
-
import type { DatabaseAdapter } from "
|
|
10
|
-
import { getAdapterFromConfig } from "
|
|
11
|
-
import {
|
|
12
|
-
nameToIdMapping,
|
|
13
|
-
processQueue,
|
|
14
|
-
queuedOperations,
|
|
15
|
-
clearProcessingState,
|
|
16
|
-
isCollectionProcessed,
|
|
17
|
-
markCollectionProcessed,
|
|
18
|
-
enqueueOperation
|
|
19
|
-
} from "../shared/operationQueue.js";
|
|
20
|
-
import { logger } from "
|
|
21
|
-
// Legacy attribute/index helpers removed in favor of unified adapter path
|
|
22
|
-
import {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
} from "
|
|
30
|
-
import {
|
|
31
|
-
import {
|
|
32
|
-
import {
|
|
33
|
-
import {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
db.
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
.
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
!
|
|
77
|
-
!
|
|
78
|
-
!
|
|
79
|
-
!
|
|
80
|
-
!
|
|
81
|
-
!
|
|
82
|
-
!
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
])
|
|
95
|
-
.
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
db.
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
await db.
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
return
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
//
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
case '
|
|
259
|
-
case '
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
)
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
//
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
const
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
const
|
|
345
|
-
const
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
const
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
if (
|
|
358
|
-
parts.push(
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
if (colResults.
|
|
368
|
-
MessageFormatter.
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
}
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
if (
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
if (relPlan.
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
}
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
if (attr.status === '
|
|
497
|
-
|
|
498
|
-
}
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
await delay(2000);
|
|
507
|
-
}
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
const
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
const
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
if (
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
}
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
}
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
}
|
|
716
|
-
|
|
717
|
-
// Process queued relationships once mapping likely populated
|
|
718
|
-
if (relQueue.length > 0) {
|
|
719
|
-
MessageFormatter.info(`🔧 Processing ${relQueue.length} queued relationship attributes for tables`, { prefix: "Tables" });
|
|
720
|
-
for (const { tableId, attr } of relQueue) {
|
|
721
|
-
const relNameOrId = (attr as any).relatedCollection as string | undefined;
|
|
722
|
-
if (!relNameOrId) continue;
|
|
723
|
-
const relId = nameToIdMapping.get(relNameOrId) || relNameOrId;
|
|
724
|
-
if (relId) {
|
|
725
|
-
(attr as any).relatedCollection = relId;
|
|
726
|
-
try {
|
|
727
|
-
await adapter.createAttribute({
|
|
728
|
-
databaseId,
|
|
729
|
-
tableId,
|
|
730
|
-
key: (attr as any).key,
|
|
731
|
-
type: (attr as any).type,
|
|
732
|
-
size: (attr as any).size,
|
|
733
|
-
required: !!(attr as any).required,
|
|
734
|
-
default: (attr as any).xdefault,
|
|
735
|
-
array: !!(attr as any).array,
|
|
736
|
-
min: (attr as any).min,
|
|
737
|
-
max: (attr as any).max,
|
|
738
|
-
elements: (attr as any).elements,
|
|
739
|
-
relatedCollection: relId,
|
|
740
|
-
relationType: (attr as any).relationType,
|
|
741
|
-
twoWay: (attr as any).twoWay,
|
|
742
|
-
twoWayKey: (attr as any).twoWayKey,
|
|
743
|
-
onDelete: (attr as any).onDelete,
|
|
744
|
-
side: (attr as any).side
|
|
745
|
-
});
|
|
746
|
-
await delay(150);
|
|
747
|
-
MessageFormatter.info(`✅ Successfully processed queued relationship: ${attr.key}`, { prefix: "Tables" });
|
|
748
|
-
} catch (e) {
|
|
749
|
-
MessageFormatter.error(`Failed queued relationship ${attr.key}`, e instanceof Error ? e : new Error(String(e)), { prefix: 'Attributes' });
|
|
750
|
-
}
|
|
751
|
-
} else {
|
|
752
|
-
MessageFormatter.warning(`Could not resolve relationship ${attr.key} -> ${relNameOrId}`, { prefix: "Tables" });
|
|
753
|
-
}
|
|
754
|
-
}
|
|
755
|
-
}
|
|
756
|
-
|
|
757
|
-
// Process any remaining queued operations to complete relationship sync
|
|
758
|
-
try {
|
|
759
|
-
MessageFormatter.info(`🔄 Processing final operation queue for database ${databaseId}`, { prefix: "Tables" });
|
|
760
|
-
await processQueue(adapter, databaseId);
|
|
761
|
-
MessageFormatter.info(`✅ Operation queue processing completed`, { prefix: "Tables" });
|
|
762
|
-
} catch (error) {
|
|
763
|
-
MessageFormatter.error(`Failed to process operation queue`, error instanceof Error ? error : new Error(String(error)), { prefix: 'Tables' });
|
|
764
|
-
}
|
|
765
|
-
};
|
|
766
|
-
|
|
767
|
-
export const generateMockData = async (
|
|
768
|
-
database: Databases,
|
|
769
|
-
databaseId: string,
|
|
770
|
-
configCollections: any[]
|
|
771
|
-
): Promise<void> => {
|
|
772
|
-
for (const { collection, mockFunction } of configCollections) {
|
|
773
|
-
if (mockFunction) {
|
|
774
|
-
MessageFormatter.progress(`Generating mock data for collection: ${collection.name}`, { prefix: "Mock Data" });
|
|
775
|
-
const mockData = mockFunction();
|
|
776
|
-
for (const data of mockData) {
|
|
777
|
-
await database.createDocument(
|
|
778
|
-
databaseId,
|
|
779
|
-
collection.$id,
|
|
780
|
-
ID.unique(),
|
|
781
|
-
data
|
|
782
|
-
);
|
|
783
|
-
}
|
|
784
|
-
}
|
|
785
|
-
}
|
|
786
|
-
};
|
|
787
|
-
|
|
788
|
-
export const fetchAllCollections = async (
|
|
789
|
-
dbId: string,
|
|
790
|
-
database: Databases
|
|
791
|
-
): Promise<Models.Collection[]> => {
|
|
792
|
-
MessageFormatter.progress(`Fetching all collections for database ID: ${dbId}`, { prefix: "Collections" });
|
|
793
|
-
let collections: Models.Collection[] = [];
|
|
794
|
-
let moreCollections = true;
|
|
795
|
-
let lastCollectionId: string | undefined;
|
|
796
|
-
|
|
797
|
-
while (moreCollections) {
|
|
798
|
-
const queries = [Query.limit(500)];
|
|
799
|
-
if (lastCollectionId) {
|
|
800
|
-
queries.push(Query.cursorAfter(lastCollectionId));
|
|
801
|
-
}
|
|
802
|
-
const response = await tryAwaitWithRetry(
|
|
803
|
-
async () => await database.listCollections(dbId, queries)
|
|
804
|
-
);
|
|
805
|
-
collections = collections.concat(response.collections);
|
|
806
|
-
moreCollections = response.collections.length === 500;
|
|
807
|
-
if (moreCollections) {
|
|
808
|
-
lastCollectionId =
|
|
809
|
-
response.collections[response.collections.length - 1].$id;
|
|
810
|
-
}
|
|
811
|
-
}
|
|
812
|
-
|
|
813
|
-
MessageFormatter.success(`Fetched a total of ${collections.length} collections`, { prefix: "Collections" });
|
|
814
|
-
return collections;
|
|
815
|
-
};
|
|
1
|
+
import {
|
|
2
|
+
Databases,
|
|
3
|
+
ID,
|
|
4
|
+
Permission,
|
|
5
|
+
Query,
|
|
6
|
+
type Models,
|
|
7
|
+
} from "node-appwrite";
|
|
8
|
+
import type { AppwriteConfig, CollectionCreate, Indexes, Attribute } from "appwrite-utils";
|
|
9
|
+
import type { DatabaseAdapter } from "appwrite-utils-helpers";
|
|
10
|
+
import { getAdapterFromConfig } from "appwrite-utils-helpers";
|
|
11
|
+
import {
|
|
12
|
+
nameToIdMapping,
|
|
13
|
+
processQueue,
|
|
14
|
+
queuedOperations,
|
|
15
|
+
clearProcessingState,
|
|
16
|
+
isCollectionProcessed,
|
|
17
|
+
markCollectionProcessed,
|
|
18
|
+
enqueueOperation
|
|
19
|
+
} from "../shared/operationQueue.js";
|
|
20
|
+
import { logger, SchemaGenerator } from "appwrite-utils-helpers";
|
|
21
|
+
// Legacy attribute/index helpers removed in favor of unified adapter path
|
|
22
|
+
import {
|
|
23
|
+
isNull,
|
|
24
|
+
isUndefined,
|
|
25
|
+
isNil,
|
|
26
|
+
isPlainObject,
|
|
27
|
+
isString,
|
|
28
|
+
} from "es-toolkit";
|
|
29
|
+
import { delay, tryAwaitWithRetry } from "appwrite-utils-helpers";
|
|
30
|
+
import { MessageFormatter, mapToCreateAttributeParams, mapToUpdateAttributeParams } from "appwrite-utils-helpers";
|
|
31
|
+
import { isLegacyDatabases } from "appwrite-utils-helpers";
|
|
32
|
+
import { diffTableColumns, isIndexEqualToIndex, diffColumnsDetailed, executeColumnOperations } from "./tableOperations.js";
|
|
33
|
+
import { createOrUpdateIndexesViaAdapter, deleteObsoleteIndexesViaAdapter } from "../tables/indexManager.js";
|
|
34
|
+
|
|
35
|
+
// Re-export wipe operations
|
|
36
|
+
export {
|
|
37
|
+
wipeDatabase,
|
|
38
|
+
wipeCollection,
|
|
39
|
+
wipeAllTables,
|
|
40
|
+
wipeTableRows,
|
|
41
|
+
} from "./wipeOperations.js";
|
|
42
|
+
|
|
43
|
+
// Re-export transfer operations
|
|
44
|
+
export {
|
|
45
|
+
transferDocumentsBetweenDbsLocalToLocal,
|
|
46
|
+
transferDocumentsBetweenDbsLocalToRemote,
|
|
47
|
+
} from "./transferOperations.js";
|
|
48
|
+
|
|
49
|
+
export const documentExists = async (
|
|
50
|
+
db: Databases | DatabaseAdapter,
|
|
51
|
+
dbId: string,
|
|
52
|
+
targetCollectionId: string,
|
|
53
|
+
toCreateObject: any
|
|
54
|
+
): Promise<Models.Document | null> => {
|
|
55
|
+
const collection = await (isLegacyDatabases(db) ?
|
|
56
|
+
db.getCollection(dbId, targetCollectionId) :
|
|
57
|
+
db.getTable({ databaseId: dbId, tableId: targetCollectionId }));
|
|
58
|
+
const attributes = (collection as any).attributes as any[];
|
|
59
|
+
let arrayTypeAttributes = attributes
|
|
60
|
+
.filter((attribute: any) => attribute.array === true)
|
|
61
|
+
.map((attribute: any) => attribute.key);
|
|
62
|
+
|
|
63
|
+
const isJsonString = (str: string) => {
|
|
64
|
+
try {
|
|
65
|
+
const json = JSON.parse(str);
|
|
66
|
+
return typeof json === "object" && json !== null;
|
|
67
|
+
} catch (e) {
|
|
68
|
+
return false;
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
// Convert object to entries and filter
|
|
73
|
+
const validEntries = Object.entries(toCreateObject).filter(
|
|
74
|
+
([key, value]) =>
|
|
75
|
+
!arrayTypeAttributes.includes(key) &&
|
|
76
|
+
!key.startsWith("$") &&
|
|
77
|
+
!isNull(value) &&
|
|
78
|
+
!isUndefined(value) &&
|
|
79
|
+
!isNil(value) &&
|
|
80
|
+
!isPlainObject(value) &&
|
|
81
|
+
!Array.isArray(value) &&
|
|
82
|
+
!(isString(value) && isJsonString(value)) &&
|
|
83
|
+
(isString(value) ? value.length < 4096 && value.length > 0 : true)
|
|
84
|
+
);
|
|
85
|
+
|
|
86
|
+
// Map and filter valid entries
|
|
87
|
+
const validMappedEntries = validEntries
|
|
88
|
+
.map(([key, value]) => [
|
|
89
|
+
key,
|
|
90
|
+
isString(value) || typeof value === "number" || typeof value === "boolean"
|
|
91
|
+
? value
|
|
92
|
+
: null,
|
|
93
|
+
])
|
|
94
|
+
.filter(([key, value]) => !isNull(value) && isString(key))
|
|
95
|
+
.slice(0, 25);
|
|
96
|
+
|
|
97
|
+
// Convert to Query parameters
|
|
98
|
+
const validQueryParams = validMappedEntries.map(([key, value]) =>
|
|
99
|
+
Query.equal(key as string, value as any)
|
|
100
|
+
);
|
|
101
|
+
|
|
102
|
+
// Execute the query with the validated and prepared parameters
|
|
103
|
+
const result = await (isLegacyDatabases(db) ?
|
|
104
|
+
db.listDocuments(dbId, targetCollectionId, validQueryParams) :
|
|
105
|
+
db.listRows({ databaseId: dbId, tableId: targetCollectionId, queries: validQueryParams }));
|
|
106
|
+
|
|
107
|
+
const items = isLegacyDatabases(db) ? result.documents : ((result as any).rows || result.documents);
|
|
108
|
+
return items?.[0] || null;
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
export const checkForCollection = async (
|
|
112
|
+
db: Databases | DatabaseAdapter,
|
|
113
|
+
dbId: string,
|
|
114
|
+
collection: Partial<CollectionCreate>
|
|
115
|
+
): Promise<Models.Collection | null> => {
|
|
116
|
+
try {
|
|
117
|
+
const isLegacy = isLegacyDatabases(db);
|
|
118
|
+
const entityType = isLegacy ? "Collection" : "Table";
|
|
119
|
+
MessageFormatter.progress(`Checking for ${entityType.toLowerCase()} with name: ${collection.name}`, { prefix: entityType + "s" });
|
|
120
|
+
const response = await tryAwaitWithRetry(
|
|
121
|
+
async () => isLegacy ?
|
|
122
|
+
await db.listCollections(dbId, [Query.equal("name", collection.name!)]) :
|
|
123
|
+
await db.listTables({ databaseId: dbId, queries: [Query.equal("name", collection.name!)] })
|
|
124
|
+
);
|
|
125
|
+
const items = isLegacy ? response.collections : ((response as any).tables || response.collections);
|
|
126
|
+
if (items && items.length > 0) {
|
|
127
|
+
MessageFormatter.info(`${entityType} found: ${items[0].$id}`, { prefix: entityType + "s" });
|
|
128
|
+
// Return remote collection for update operations (don't merge local config over it)
|
|
129
|
+
return items[0] as Models.Collection;
|
|
130
|
+
} else {
|
|
131
|
+
MessageFormatter.info(`No ${entityType.toLowerCase()} found with name: ${collection.name}`, { prefix: entityType + "s" });
|
|
132
|
+
return null;
|
|
133
|
+
}
|
|
134
|
+
} catch (error) {
|
|
135
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
136
|
+
MessageFormatter.error(`Error checking for collection: ${collection.name}`, error instanceof Error ? error : new Error(String(error)), { prefix: "Collections" });
|
|
137
|
+
logger.error('Collection check failed', {
|
|
138
|
+
collectionName: collection.name,
|
|
139
|
+
dbId,
|
|
140
|
+
error: errorMessage,
|
|
141
|
+
operation: 'checkForCollection'
|
|
142
|
+
});
|
|
143
|
+
return null;
|
|
144
|
+
}
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
// Helper function to fetch and cache collection by name
|
|
148
|
+
export const fetchAndCacheCollectionByName = async (
|
|
149
|
+
db: Databases | DatabaseAdapter,
|
|
150
|
+
dbId: string,
|
|
151
|
+
collectionName: string
|
|
152
|
+
): Promise<Models.Collection | undefined> => {
|
|
153
|
+
const isLegacy = isLegacyDatabases(db);
|
|
154
|
+
const entityType = isLegacy ? "Collection" : "Table";
|
|
155
|
+
if (nameToIdMapping.has(collectionName)) {
|
|
156
|
+
const collectionId = nameToIdMapping.get(collectionName);
|
|
157
|
+
MessageFormatter.debug(`${entityType} found in cache: ${collectionId}`, undefined, { prefix: entityType + "s" });
|
|
158
|
+
return await tryAwaitWithRetry(
|
|
159
|
+
async () => isLegacy ?
|
|
160
|
+
await db.getCollection(dbId, collectionId!) :
|
|
161
|
+
await db.getTable({ databaseId: dbId, tableId: collectionId! })
|
|
162
|
+
) as Models.Collection;
|
|
163
|
+
} else {
|
|
164
|
+
MessageFormatter.progress(`Fetching ${entityType.toLowerCase()} by name: ${collectionName}`, { prefix: entityType + "s" });
|
|
165
|
+
const collectionsPulled = await tryAwaitWithRetry(
|
|
166
|
+
async () => isLegacy ?
|
|
167
|
+
await db.listCollections(dbId, [Query.equal("name", collectionName)]) :
|
|
168
|
+
await db.listTables({ databaseId: dbId, queries: [Query.equal("name", collectionName)] })
|
|
169
|
+
);
|
|
170
|
+
const items = isLegacy ? collectionsPulled.collections : ((collectionsPulled as any).tables || collectionsPulled.collections);
|
|
171
|
+
if ((collectionsPulled.total || items?.length) > 0) {
|
|
172
|
+
const collection = items[0];
|
|
173
|
+
MessageFormatter.info(`${entityType} found: ${collection.$id}`, { prefix: entityType + "s" });
|
|
174
|
+
nameToIdMapping.set(collectionName, collection.$id);
|
|
175
|
+
return collection;
|
|
176
|
+
} else {
|
|
177
|
+
MessageFormatter.warning(`${entityType} not found by name: ${collectionName}`, { prefix: entityType + "s" });
|
|
178
|
+
return undefined;
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
};
|
|
182
|
+
|
|
183
|
+
export const generateSchemas = async (
|
|
184
|
+
config: AppwriteConfig,
|
|
185
|
+
appwriteFolderPath: string
|
|
186
|
+
): Promise<void> => {
|
|
187
|
+
const schemaGenerator = new SchemaGenerator(config, appwriteFolderPath);
|
|
188
|
+
await schemaGenerator.generateSchemas();
|
|
189
|
+
};
|
|
190
|
+
|
|
191
|
+
export const createOrUpdateCollections = async (
|
|
192
|
+
database: Databases,
|
|
193
|
+
databaseId: string,
|
|
194
|
+
config: AppwriteConfig,
|
|
195
|
+
deletedCollections?: { collectionId: string; collectionName: string }[],
|
|
196
|
+
selectedCollections: Models.Collection[] = []
|
|
197
|
+
): Promise<void> => {
|
|
198
|
+
// Clear processing state at the start of a new operation
|
|
199
|
+
clearProcessingState();
|
|
200
|
+
|
|
201
|
+
// Always use adapter path (LegacyAdapter translates when pre-1.8)
|
|
202
|
+
const { adapter } = await getAdapterFromConfig(config);
|
|
203
|
+
await createOrUpdateCollectionsViaAdapter(
|
|
204
|
+
adapter,
|
|
205
|
+
databaseId,
|
|
206
|
+
config,
|
|
207
|
+
deletedCollections,
|
|
208
|
+
selectedCollections
|
|
209
|
+
);
|
|
210
|
+
};
|
|
211
|
+
|
|
212
|
+
// New: Adapter-based implementation for TablesDB with state management
|
|
213
|
+
export const createOrUpdateCollectionsViaAdapter = async (
|
|
214
|
+
adapter: DatabaseAdapter,
|
|
215
|
+
databaseId: string,
|
|
216
|
+
config: AppwriteConfig,
|
|
217
|
+
deletedCollections?: { collectionId: string; collectionName: string }[],
|
|
218
|
+
selectedCollections: Models.Collection[] = []
|
|
219
|
+
): Promise<void> => {
|
|
220
|
+
const collectionsToProcess =
|
|
221
|
+
selectedCollections.length > 0 ? selectedCollections : (config.collections || []);
|
|
222
|
+
if (!collectionsToProcess || collectionsToProcess.length === 0) return;
|
|
223
|
+
|
|
224
|
+
const usedIds = new Set<string>();
|
|
225
|
+
MessageFormatter.info(`Processing ${collectionsToProcess.length} tables via adapter with intelligent state management`, { prefix: "Tables" });
|
|
226
|
+
|
|
227
|
+
// Helpers for attribute operations through adapter
|
|
228
|
+
const createAttr = async (tableId: string, attr: Attribute) => {
|
|
229
|
+
const params = mapToCreateAttributeParams(attr as any, { databaseId, tableId });
|
|
230
|
+
await adapter.createAttribute(params);
|
|
231
|
+
await delay(150);
|
|
232
|
+
};
|
|
233
|
+
const updateAttr = async (tableId: string, attr: Attribute) => {
|
|
234
|
+
const params = mapToUpdateAttributeParams(attr as any, { databaseId, tableId }) as any;
|
|
235
|
+
await adapter.updateAttribute(params);
|
|
236
|
+
await delay(150);
|
|
237
|
+
};
|
|
238
|
+
|
|
239
|
+
// Local queue for unresolved relationships
|
|
240
|
+
const relQueue: { tableId: string; attr: Attribute }[] = [];
|
|
241
|
+
|
|
242
|
+
for (const collection of collectionsToProcess) {
|
|
243
|
+
const { attributes, indexes, ...collectionData } = collection as any;
|
|
244
|
+
|
|
245
|
+
// Check if this table has already been processed in this session (per database)
|
|
246
|
+
if (collectionData.$id && isCollectionProcessed(collectionData.$id, databaseId)) {
|
|
247
|
+
MessageFormatter.info(`Table '${collectionData.name}' already processed, skipping`, { prefix: "Tables" });
|
|
248
|
+
continue;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
// Prepare permissions as strings (reuse Permission helper)
|
|
252
|
+
const permissions: string[] = [];
|
|
253
|
+
if (collection.$permissions && collection.$permissions.length > 0) {
|
|
254
|
+
for (const p of collection.$permissions as any[]) {
|
|
255
|
+
if (typeof p === 'string') permissions.push(p);
|
|
256
|
+
else {
|
|
257
|
+
switch (p.permission) {
|
|
258
|
+
case 'read': permissions.push(Permission.read(p.target)); break;
|
|
259
|
+
case 'create': permissions.push(Permission.create(p.target)); break;
|
|
260
|
+
case 'update': permissions.push(Permission.update(p.target)); break;
|
|
261
|
+
case 'delete': permissions.push(Permission.delete(p.target)); break;
|
|
262
|
+
case 'write': permissions.push(Permission.write(p.target)); break;
|
|
263
|
+
default: break;
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
// Find existing table — prefer lookup by ID (if provided), then by name
|
|
270
|
+
let table: any | undefined;
|
|
271
|
+
let tableId: string;
|
|
272
|
+
|
|
273
|
+
// 1) Try by explicit $id first (handles rename scenarios)
|
|
274
|
+
if (collectionData.$id) {
|
|
275
|
+
try {
|
|
276
|
+
const byId = await adapter.getTable({ databaseId, tableId: collectionData.$id });
|
|
277
|
+
table = (byId as any).data || (byId as any).tables?.[0];
|
|
278
|
+
if (table?.$id) {
|
|
279
|
+
MessageFormatter.info(`Found existing table by ID: ${table.$id}`, { prefix: 'Tables' });
|
|
280
|
+
}
|
|
281
|
+
} catch {
|
|
282
|
+
// Not found by ID; fall back to name lookup
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
// 2) If not found by ID, try by name
|
|
287
|
+
if (!table) {
|
|
288
|
+
const list = await adapter.listTables({ databaseId, queries: [Query.equal('name', collectionData.name)] });
|
|
289
|
+
const items: any[] = (list as any).tables || [];
|
|
290
|
+
table = items[0];
|
|
291
|
+
if (table?.$id) {
|
|
292
|
+
// If local has $id that differs from remote, prefer remote (IDs are immutable)
|
|
293
|
+
if (collectionData.$id && collectionData.$id !== table.$id) {
|
|
294
|
+
MessageFormatter.warning(`Config $id '${collectionData.$id}' differs from existing table ID '${table.$id}'. Using existing table.`, { prefix: 'Tables' });
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
if (!table) {
|
|
300
|
+
// Determine ID (prefer provided $id or re-use deleted one)
|
|
301
|
+
let foundColl = deletedCollections?.find(
|
|
302
|
+
(coll) => coll.collectionName.toLowerCase().trim().replace(" ", "") === collectionData.name.toLowerCase().trim().replace(" ", "")
|
|
303
|
+
);
|
|
304
|
+
if (collectionData.$id) tableId = collectionData.$id;
|
|
305
|
+
else if (foundColl && !usedIds.has(foundColl.collectionId)) tableId = foundColl.collectionId;
|
|
306
|
+
else tableId = ID.unique();
|
|
307
|
+
usedIds.add(tableId);
|
|
308
|
+
|
|
309
|
+
const res = await adapter.createTable({
|
|
310
|
+
databaseId,
|
|
311
|
+
id: tableId,
|
|
312
|
+
name: collectionData.name,
|
|
313
|
+
permissions,
|
|
314
|
+
documentSecurity: !!collectionData.documentSecurity,
|
|
315
|
+
enabled: collectionData.enabled !== false
|
|
316
|
+
});
|
|
317
|
+
table = (res as any).data || res;
|
|
318
|
+
nameToIdMapping.set(collectionData.name, tableId);
|
|
319
|
+
} else {
|
|
320
|
+
tableId = table.$id;
|
|
321
|
+
await adapter.updateTable({
|
|
322
|
+
databaseId,
|
|
323
|
+
id: tableId,
|
|
324
|
+
name: collectionData.name,
|
|
325
|
+
permissions,
|
|
326
|
+
documentSecurity: !!collectionData.documentSecurity,
|
|
327
|
+
enabled: collectionData.enabled !== false
|
|
328
|
+
});
|
|
329
|
+
// Cache the existing table ID
|
|
330
|
+
nameToIdMapping.set(collectionData.name, tableId);
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
// Add small delay after table create/update
|
|
334
|
+
await delay(250);
|
|
335
|
+
|
|
336
|
+
// Create/Update attributes: non-relationship first using enhanced planning
|
|
337
|
+
const nonRel = (attributes || []).filter((a: Attribute) => a.type !== 'relationship');
|
|
338
|
+
if (nonRel.length > 0) {
|
|
339
|
+
// Fetch existing columns once
|
|
340
|
+
const tableInfo = await adapter.getTable({ databaseId, tableId });
|
|
341
|
+
const existingCols: any[] = (tableInfo as any).data?.columns || (tableInfo as any).data?.attributes || [];
|
|
342
|
+
|
|
343
|
+
// Plan with icons
|
|
344
|
+
const plan = diffColumnsDetailed(nonRel as any, existingCols);
|
|
345
|
+
const plus = plan.toCreate.map((a: any) => a.key);
|
|
346
|
+
const plusminus = plan.toUpdate.map((u: any) => (u.attribute as any).key);
|
|
347
|
+
const minus = plan.toRecreate.map((r: any) => (r.newAttribute as any).key);
|
|
348
|
+
const skip = plan.unchanged;
|
|
349
|
+
|
|
350
|
+
// Compute deletions (remote extras not present locally)
|
|
351
|
+
const desiredKeysForDelete = new Set((attributes || []).map((a: any) => a.key));
|
|
352
|
+
const extraRemoteKeys = (existingCols || [])
|
|
353
|
+
.map((c: any) => c?.key)
|
|
354
|
+
.filter((k: any): k is string => !!k && !desiredKeysForDelete.has(k));
|
|
355
|
+
|
|
356
|
+
const parts: string[] = [];
|
|
357
|
+
if (plus.length) parts.push(`➕ ${plus.length} (${plus.join(', ')})`);
|
|
358
|
+
if (plusminus.length) parts.push(`🔧 ${plusminus.length} (${plusminus.join(', ')})`);
|
|
359
|
+
if (minus.length) parts.push(`♻️ ${minus.length} (${minus.join(', ')})`);
|
|
360
|
+
if (skip.length) parts.push(`⏭️ ${skip.length}`);
|
|
361
|
+
parts.push(`🗑️ ${extraRemoteKeys.length}${extraRemoteKeys.length ? ` (${extraRemoteKeys.join(', ')})` : ''}`);
|
|
362
|
+
MessageFormatter.info(`Plan → ${parts.join(' | ') || 'no changes'}`, { prefix: 'Attributes' });
|
|
363
|
+
|
|
364
|
+
// Execute
|
|
365
|
+
const colResults = await executeColumnOperations(adapter, databaseId, tableId, plan);
|
|
366
|
+
|
|
367
|
+
if (colResults.success.length > 0) {
|
|
368
|
+
MessageFormatter.success(`Processed ${colResults.success.length} ops`, { prefix: 'Attributes' });
|
|
369
|
+
}
|
|
370
|
+
if (colResults.errors.length > 0) {
|
|
371
|
+
MessageFormatter.error(`${colResults.errors.length} attribute operations failed:`, undefined, { prefix: 'Attributes' });
|
|
372
|
+
for (const err of colResults.errors) {
|
|
373
|
+
MessageFormatter.error(` ${err.column}: ${err.error}`, undefined, { prefix: 'Attributes' });
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
MessageFormatter.info(
|
|
377
|
+
`Summary → ➕ ${plan.toCreate.length} | 🔧 ${plan.toUpdate.length} | ♻️ ${plan.toRecreate.length} | ⏭️ ${plan.unchanged.length}`,
|
|
378
|
+
{ prefix: 'Attributes' }
|
|
379
|
+
);
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
// Relationship attributes — resolve relatedCollection to ID, then diff and create/update with recreate support
|
|
383
|
+
const relsAll = (attributes || []).filter((a: Attribute) => a.type === 'relationship') as any[];
|
|
384
|
+
if (relsAll.length > 0) {
|
|
385
|
+
const relsResolved: any[] = [];
|
|
386
|
+
const relsDeferred: any[] = [];
|
|
387
|
+
|
|
388
|
+
// Resolve related collections (names -> IDs) using cache or lookup.
|
|
389
|
+
// If not resolvable yet (target table created later in the same push), queue for later.
|
|
390
|
+
for (const attr of relsAll) {
|
|
391
|
+
const relNameOrId = attr.relatedCollection as string | undefined;
|
|
392
|
+
if (!relNameOrId) continue;
|
|
393
|
+
let relId = nameToIdMapping.get(relNameOrId) || relNameOrId;
|
|
394
|
+
let resolved = false;
|
|
395
|
+
if (nameToIdMapping.has(relNameOrId)) {
|
|
396
|
+
resolved = true;
|
|
397
|
+
} else {
|
|
398
|
+
// Try resolve by name
|
|
399
|
+
try {
|
|
400
|
+
const relList = await adapter.listTables({ databaseId, queries: [Query.equal('name', relNameOrId)] });
|
|
401
|
+
const relItems: any[] = (relList as any).tables || [];
|
|
402
|
+
if (relItems[0]?.$id) {
|
|
403
|
+
relId = relItems[0].$id;
|
|
404
|
+
nameToIdMapping.set(relNameOrId, relId);
|
|
405
|
+
resolved = true;
|
|
406
|
+
}
|
|
407
|
+
} catch {}
|
|
408
|
+
|
|
409
|
+
// If the relNameOrId looks like an ID but isn't resolved yet, attempt a direct get
|
|
410
|
+
if (!resolved && relNameOrId && relNameOrId.length >= 10) {
|
|
411
|
+
try {
|
|
412
|
+
const probe = await adapter.getTable({ databaseId, tableId: relNameOrId });
|
|
413
|
+
if ((probe as any).data?.$id) {
|
|
414
|
+
nameToIdMapping.set(relNameOrId, relNameOrId);
|
|
415
|
+
relId = relNameOrId;
|
|
416
|
+
resolved = true;
|
|
417
|
+
}
|
|
418
|
+
} catch {}
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
if (resolved && relId && typeof relId === 'string') {
|
|
423
|
+
attr.relatedCollection = relId;
|
|
424
|
+
relsResolved.push(attr);
|
|
425
|
+
} else {
|
|
426
|
+
// Defer until related table exists; queue a surgical operation
|
|
427
|
+
enqueueOperation({
|
|
428
|
+
type: 'attribute',
|
|
429
|
+
collectionId: tableId,
|
|
430
|
+
attribute: attr,
|
|
431
|
+
dependencies: [relNameOrId]
|
|
432
|
+
});
|
|
433
|
+
relsDeferred.push(attr);
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
// Compute a detailed plan for immediately resolvable relationships
|
|
438
|
+
const tableInfo2 = await adapter.getTable({ databaseId, tableId });
|
|
439
|
+
const existingCols2: any[] = (tableInfo2 as any).data?.columns || (tableInfo2 as any).data?.attributes || [];
|
|
440
|
+
const relPlan = diffColumnsDetailed(relsResolved as any, existingCols2);
|
|
441
|
+
|
|
442
|
+
// Relationship plan with icons (includes recreates)
|
|
443
|
+
{
|
|
444
|
+
const parts: string[] = [];
|
|
445
|
+
if (relPlan.toCreate.length) parts.push(`➕ ${relPlan.toCreate.length} (${relPlan.toCreate.map((a:any)=>a.key).join(', ')})`);
|
|
446
|
+
if (relPlan.toUpdate.length) parts.push(`🔧 ${relPlan.toUpdate.length} (${relPlan.toUpdate.map((u:any)=>u.attribute?.key ?? u.key).join(', ')})`);
|
|
447
|
+
if (relPlan.toRecreate.length) parts.push(`♻️ ${relPlan.toRecreate.length} (${relPlan.toRecreate.map((r:any)=>r.newAttribute?.key ?? r?.key).join(', ')})`);
|
|
448
|
+
if (relPlan.unchanged.length) parts.push(`⏭️ ${relPlan.unchanged.length}`);
|
|
449
|
+
MessageFormatter.info(`Plan → ${parts.join(' | ') || 'no changes'}`, { prefix: 'Relationships' });
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
// Execute plan using the same operation executor to properly handle deletes/recreates
|
|
453
|
+
const relResults = await executeColumnOperations(adapter, databaseId, tableId, relPlan);
|
|
454
|
+
if (relResults.success.length > 0) {
|
|
455
|
+
const totalRelationships = relPlan.toCreate.length + relPlan.toUpdate.length + relPlan.toRecreate.length + relPlan.unchanged.length;
|
|
456
|
+
const activeRelationships = relPlan.toCreate.length + relPlan.toUpdate.length + relPlan.toRecreate.length;
|
|
457
|
+
|
|
458
|
+
if (relResults.success.length !== activeRelationships) {
|
|
459
|
+
// Show both counts when they differ (usually due to recreations)
|
|
460
|
+
MessageFormatter.success(`Processed ${relResults.success.length} operations for ${activeRelationships} relationship${activeRelationships === 1 ? '' : 's'}`, { prefix: 'Relationships' });
|
|
461
|
+
} else {
|
|
462
|
+
MessageFormatter.success(`Processed ${relResults.success.length} relationship${relResults.success.length === 1 ? '' : 's'}`, { prefix: 'Relationships' });
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
if (relResults.errors.length > 0) {
|
|
466
|
+
MessageFormatter.error(`${relResults.errors.length} relationship operations failed:`, undefined, { prefix: 'Relationships' });
|
|
467
|
+
for (const err of relResults.errors) {
|
|
468
|
+
MessageFormatter.error(` ${err.column}: ${err.error}`, undefined, { prefix: 'Relationships' });
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
if (relsDeferred.length > 0) {
|
|
473
|
+
MessageFormatter.info(`Deferred ${relsDeferred.length} relationship(s) until related tables become available`, { prefix: 'Relationships' });
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
// Wait for all attributes to become available before creating indexes
|
|
478
|
+
const allAttrKeys = [
|
|
479
|
+
...nonRel.map((a: any) => a.key),
|
|
480
|
+
...relsAll.filter((a: any) => a.relatedCollection).map((a: any) => a.key)
|
|
481
|
+
];
|
|
482
|
+
|
|
483
|
+
if (allAttrKeys.length > 0) {
|
|
484
|
+
for (const attrKey of allAttrKeys) {
|
|
485
|
+
const maxWait = 60000; // 60 seconds
|
|
486
|
+
const startTime = Date.now();
|
|
487
|
+
let lastStatus = '';
|
|
488
|
+
|
|
489
|
+
while (Date.now() - startTime < maxWait) {
|
|
490
|
+
try {
|
|
491
|
+
const tableData = await adapter.getTable({ databaseId, tableId });
|
|
492
|
+
const attrs = (tableData as any).data?.columns || (tableData as any).data?.attributes || [];
|
|
493
|
+
const attr = attrs.find((a: any) => a.key === attrKey);
|
|
494
|
+
|
|
495
|
+
if (attr) {
|
|
496
|
+
if (attr.status === 'available') {
|
|
497
|
+
break; // Attribute is ready
|
|
498
|
+
}
|
|
499
|
+
if (attr.status === 'failed' || attr.status === 'stuck') {
|
|
500
|
+
throw new Error(`Attribute ${attrKey} failed to create: ${attr.error || 'unknown error'}`);
|
|
501
|
+
}
|
|
502
|
+
// Still processing, continue waiting
|
|
503
|
+
lastStatus = attr.status;
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
await delay(2000); // Check every 2 seconds
|
|
507
|
+
} catch (e) {
|
|
508
|
+
// If we can't check status, assume it's processing and continue
|
|
509
|
+
await delay(2000);
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
// Timeout check
|
|
514
|
+
if (Date.now() - startTime >= maxWait) {
|
|
515
|
+
MessageFormatter.warning(
|
|
516
|
+
`Attribute ${attrKey} did not become available within ${maxWait / 1000}s (last status: ${lastStatus}). Proceeding anyway.`,
|
|
517
|
+
{ prefix: 'Attributes' }
|
|
518
|
+
);
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
// Index management: create/update indexes using clean adapter-based system
|
|
524
|
+
const localTableConfig = config.collections?.find(
|
|
525
|
+
c => c.name === collectionData.name || c.$id === collectionData.$id
|
|
526
|
+
);
|
|
527
|
+
const idxs = (localTableConfig?.indexes ?? indexes ?? []) as any[];
|
|
528
|
+
|
|
529
|
+
// Create/update indexes with proper planning and execution
|
|
530
|
+
await createOrUpdateIndexesViaAdapter(adapter, databaseId, tableId, idxs, indexes);
|
|
531
|
+
|
|
532
|
+
// Handle obsolete index deletions
|
|
533
|
+
const desiredIndexKeys: Set<string> = new Set((indexes || []).map((i: any) => i.key as string));
|
|
534
|
+
await deleteObsoleteIndexesViaAdapter(adapter, databaseId, tableId, desiredIndexKeys);
|
|
535
|
+
|
|
536
|
+
// Deletions: remove columns/attributes that are present remotely but not in desired config
|
|
537
|
+
try {
|
|
538
|
+
const desiredKeys = new Set((attributes || []).map((a: any) => a.key));
|
|
539
|
+
// Also track case-insensitive keys to avoid double-deletion of renames (handled as recreates)
|
|
540
|
+
const desiredKeysLower = new Set((attributes || []).map((a: any) => a.key?.toLowerCase()));
|
|
541
|
+
const tableInfo3 = await adapter.getTable({ databaseId, tableId });
|
|
542
|
+
const existingCols3: any[] = (tableInfo3 as any).data?.columns || (tableInfo3 as any).data?.attributes || [];
|
|
543
|
+
const toDelete = existingCols3
|
|
544
|
+
.filter((col: any) => {
|
|
545
|
+
if (!col?.key) return false;
|
|
546
|
+
// Exact match - keep it
|
|
547
|
+
if (desiredKeys.has(col.key)) return false;
|
|
548
|
+
// Case-insensitive match (rename scenario) - already handled as recreate, don't delete again
|
|
549
|
+
if (desiredKeysLower.has(col.key?.toLowerCase())) return false;
|
|
550
|
+
// Don't delete child-side relationship attributes - they're auto-managed by Appwrite
|
|
551
|
+
// for two-way relationships and deleting them would break the parent relationship
|
|
552
|
+
if (col.type === 'relationship' && col.side === 'child') return false;
|
|
553
|
+
return true;
|
|
554
|
+
})
|
|
555
|
+
.map((col: any) => col.key as string);
|
|
556
|
+
|
|
557
|
+
if (toDelete.length > 0) {
|
|
558
|
+
MessageFormatter.info(`Plan → 🗑️ ${toDelete.length} (${toDelete.join(', ')})`, { prefix: 'Attributes' });
|
|
559
|
+
const deleted: string[] = [];
|
|
560
|
+
const errors: Array<{ key: string; error: string }> = [];
|
|
561
|
+
for (const key of toDelete) {
|
|
562
|
+
try {
|
|
563
|
+
// Drop any indexes that reference this attribute to avoid server errors
|
|
564
|
+
try {
|
|
565
|
+
const idxRes = await adapter.listIndexes({ databaseId, tableId });
|
|
566
|
+
const ilist: any[] = (idxRes as any).data || (idxRes as any).indexes || [];
|
|
567
|
+
for (const idx of ilist) {
|
|
568
|
+
const attrs: string[] = Array.isArray(idx.attributes)
|
|
569
|
+
? idx.attributes
|
|
570
|
+
: (Array.isArray((idx as any).columns) ? (idx as any).columns : []);
|
|
571
|
+
if (attrs.includes(key)) {
|
|
572
|
+
MessageFormatter.info(`🗑️ Deleting index '${idx.key}' referencing '${key}'`, { prefix: 'Indexes' });
|
|
573
|
+
await adapter.deleteIndex({ databaseId, tableId, key: idx.key });
|
|
574
|
+
await delay(500);
|
|
575
|
+
}
|
|
576
|
+
}
|
|
577
|
+
} catch {}
|
|
578
|
+
|
|
579
|
+
await adapter.deleteAttribute({ databaseId, tableId, key });
|
|
580
|
+
// Wait briefly for deletion to settle
|
|
581
|
+
const start = Date.now();
|
|
582
|
+
const maxWaitMs = 60000;
|
|
583
|
+
while (Date.now() - start < maxWaitMs) {
|
|
584
|
+
try {
|
|
585
|
+
const tinfo = await adapter.getTable({ databaseId, tableId });
|
|
586
|
+
const cols = (tinfo as any).data?.columns || (tinfo as any).data?.attributes || [];
|
|
587
|
+
const found = cols.find((c: any) => c.key === key);
|
|
588
|
+
if (!found) break;
|
|
589
|
+
if (found.status && found.status !== 'deleting') break;
|
|
590
|
+
} catch {}
|
|
591
|
+
await delay(1000);
|
|
592
|
+
}
|
|
593
|
+
deleted.push(key);
|
|
594
|
+
} catch (e: any) {
|
|
595
|
+
errors.push({ key, error: e?.message || String(e) });
|
|
596
|
+
}
|
|
597
|
+
}
|
|
598
|
+
if (deleted.length) {
|
|
599
|
+
MessageFormatter.success(`Deleted ${deleted.length} attributes: ${deleted.join(', ')}`, { prefix: 'Attributes' });
|
|
600
|
+
}
|
|
601
|
+
if (errors.length) {
|
|
602
|
+
MessageFormatter.error(`${errors.length} deletions failed`, undefined, { prefix: 'Attributes' });
|
|
603
|
+
errors.forEach(er => MessageFormatter.error(` ${er.key}: ${er.error}`, undefined, { prefix: 'Attributes' }));
|
|
604
|
+
}
|
|
605
|
+
} else {
|
|
606
|
+
MessageFormatter.info(`Plan → 🗑️ 0`, { prefix: 'Attributes' });
|
|
607
|
+
}
|
|
608
|
+
} catch (e) {
|
|
609
|
+
MessageFormatter.warning(`Could not evaluate deletions: ${(e as Error)?.message || e}`, { prefix: 'Attributes' });
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
// Mark this table as fully processed for this database to prevent re-processing in the same DB only
|
|
613
|
+
markCollectionProcessed(tableId, collectionData.name, databaseId);
|
|
614
|
+
}
|
|
615
|
+
|
|
616
|
+
// Process queued relationships once mapping likely populated
|
|
617
|
+
if (relQueue.length > 0) {
|
|
618
|
+
MessageFormatter.info(`🔧 Processing ${relQueue.length} queued relationship attributes for tables`, { prefix: "Tables" });
|
|
619
|
+
for (const { tableId, attr } of relQueue) {
|
|
620
|
+
const relNameOrId = (attr as any).relatedCollection as string | undefined;
|
|
621
|
+
if (!relNameOrId) continue;
|
|
622
|
+
const relId = nameToIdMapping.get(relNameOrId) || relNameOrId;
|
|
623
|
+
if (relId) {
|
|
624
|
+
(attr as any).relatedCollection = relId;
|
|
625
|
+
try {
|
|
626
|
+
await adapter.createAttribute({
|
|
627
|
+
databaseId,
|
|
628
|
+
tableId,
|
|
629
|
+
key: (attr as any).key,
|
|
630
|
+
type: (attr as any).type,
|
|
631
|
+
size: (attr as any).size,
|
|
632
|
+
required: !!(attr as any).required,
|
|
633
|
+
default: (attr as any).xdefault,
|
|
634
|
+
array: !!(attr as any).array,
|
|
635
|
+
min: (attr as any).min,
|
|
636
|
+
max: (attr as any).max,
|
|
637
|
+
elements: (attr as any).elements,
|
|
638
|
+
relatedCollection: relId,
|
|
639
|
+
relationType: (attr as any).relationType,
|
|
640
|
+
twoWay: (attr as any).twoWay,
|
|
641
|
+
twoWayKey: (attr as any).twoWayKey,
|
|
642
|
+
onDelete: (attr as any).onDelete,
|
|
643
|
+
side: (attr as any).side
|
|
644
|
+
});
|
|
645
|
+
await delay(150);
|
|
646
|
+
MessageFormatter.info(`✅ Successfully processed queued relationship: ${attr.key}`, { prefix: "Tables" });
|
|
647
|
+
} catch (e) {
|
|
648
|
+
MessageFormatter.error(`Failed queued relationship ${attr.key}`, e instanceof Error ? e : new Error(String(e)), { prefix: 'Attributes' });
|
|
649
|
+
}
|
|
650
|
+
} else {
|
|
651
|
+
MessageFormatter.warning(`Could not resolve relationship ${attr.key} -> ${relNameOrId}`, { prefix: "Tables" });
|
|
652
|
+
}
|
|
653
|
+
}
|
|
654
|
+
}
|
|
655
|
+
|
|
656
|
+
// Process any remaining queued operations to complete relationship sync
|
|
657
|
+
try {
|
|
658
|
+
MessageFormatter.info(`🔄 Processing final operation queue for database ${databaseId}`, { prefix: "Tables" });
|
|
659
|
+
await processQueue(adapter, databaseId);
|
|
660
|
+
MessageFormatter.info(`✅ Operation queue processing completed`, { prefix: "Tables" });
|
|
661
|
+
} catch (error) {
|
|
662
|
+
MessageFormatter.error(`Failed to process operation queue`, error instanceof Error ? error : new Error(String(error)), { prefix: 'Tables' });
|
|
663
|
+
}
|
|
664
|
+
};
|
|
665
|
+
|
|
666
|
+
export const generateMockData = async (
|
|
667
|
+
database: Databases,
|
|
668
|
+
databaseId: string,
|
|
669
|
+
configCollections: any[]
|
|
670
|
+
): Promise<void> => {
|
|
671
|
+
for (const { collection, mockFunction } of configCollections) {
|
|
672
|
+
if (mockFunction) {
|
|
673
|
+
MessageFormatter.progress(`Generating mock data for collection: ${collection.name}`, { prefix: "Mock Data" });
|
|
674
|
+
const mockData = mockFunction();
|
|
675
|
+
for (const data of mockData) {
|
|
676
|
+
await database.createDocument(
|
|
677
|
+
databaseId,
|
|
678
|
+
collection.$id,
|
|
679
|
+
ID.unique(),
|
|
680
|
+
data
|
|
681
|
+
);
|
|
682
|
+
}
|
|
683
|
+
}
|
|
684
|
+
}
|
|
685
|
+
};
|
|
686
|
+
|
|
687
|
+
export const fetchAllCollections = async (
|
|
688
|
+
dbId: string,
|
|
689
|
+
database: Databases
|
|
690
|
+
): Promise<Models.Collection[]> => {
|
|
691
|
+
MessageFormatter.progress(`Fetching all collections for database ID: ${dbId}`, { prefix: "Collections" });
|
|
692
|
+
let collections: Models.Collection[] = [];
|
|
693
|
+
let moreCollections = true;
|
|
694
|
+
let lastCollectionId: string | undefined;
|
|
695
|
+
|
|
696
|
+
while (moreCollections) {
|
|
697
|
+
const queries = [Query.limit(500)];
|
|
698
|
+
if (lastCollectionId) {
|
|
699
|
+
queries.push(Query.cursorAfter(lastCollectionId));
|
|
700
|
+
}
|
|
701
|
+
const response = await tryAwaitWithRetry(
|
|
702
|
+
async () => await database.listCollections(dbId, queries)
|
|
703
|
+
);
|
|
704
|
+
collections = collections.concat(response.collections);
|
|
705
|
+
moreCollections = response.collections.length === 500;
|
|
706
|
+
if (moreCollections) {
|
|
707
|
+
lastCollectionId =
|
|
708
|
+
response.collections[response.collections.length - 1].$id;
|
|
709
|
+
}
|
|
710
|
+
}
|
|
711
|
+
|
|
712
|
+
MessageFormatter.success(`Fetched a total of ${collections.length} collections`, { prefix: "Collections" });
|
|
713
|
+
return collections;
|
|
714
|
+
};
|