appwrite-utils-cli 1.11.0 → 1.12.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 (250) hide show
  1. package/{src/adapters/index.ts → dist/adapters/index.d.ts} +0 -1
  2. package/dist/adapters/index.js +10 -0
  3. package/dist/backups/operations/bucketBackup.d.ts +19 -0
  4. package/dist/backups/operations/bucketBackup.js +197 -0
  5. package/dist/backups/operations/collectionBackup.d.ts +30 -0
  6. package/dist/backups/operations/collectionBackup.js +201 -0
  7. package/dist/backups/operations/comprehensiveBackup.d.ts +25 -0
  8. package/dist/backups/operations/comprehensiveBackup.js +238 -0
  9. package/dist/backups/schemas/bucketManifest.d.ts +93 -0
  10. package/dist/backups/schemas/bucketManifest.js +33 -0
  11. package/dist/backups/schemas/comprehensiveManifest.d.ts +108 -0
  12. package/dist/backups/schemas/comprehensiveManifest.js +32 -0
  13. package/dist/backups/tracking/centralizedTracking.d.ts +34 -0
  14. package/dist/backups/tracking/centralizedTracking.js +274 -0
  15. package/dist/cli/commands/configCommands.d.ts +8 -0
  16. package/dist/cli/commands/configCommands.js +210 -0
  17. package/dist/cli/commands/databaseCommands.d.ts +14 -0
  18. package/dist/cli/commands/databaseCommands.js +696 -0
  19. package/dist/cli/commands/functionCommands.d.ts +7 -0
  20. package/dist/cli/commands/functionCommands.js +330 -0
  21. package/dist/cli/commands/importFileCommands.d.ts +7 -0
  22. package/dist/cli/commands/importFileCommands.js +674 -0
  23. package/dist/cli/commands/schemaCommands.d.ts +7 -0
  24. package/dist/cli/commands/schemaCommands.js +169 -0
  25. package/dist/cli/commands/storageCommands.d.ts +5 -0
  26. package/dist/cli/commands/storageCommands.js +142 -0
  27. package/dist/cli/commands/transferCommands.d.ts +5 -0
  28. package/dist/cli/commands/transferCommands.js +382 -0
  29. package/dist/collections/columns.d.ts +13 -0
  30. package/dist/collections/columns.js +1339 -0
  31. package/dist/collections/indexes.d.ts +12 -0
  32. package/dist/collections/indexes.js +215 -0
  33. package/dist/collections/methods.d.ts +19 -0
  34. package/dist/collections/methods.js +605 -0
  35. package/dist/collections/tableOperations.d.ts +87 -0
  36. package/dist/collections/tableOperations.js +466 -0
  37. package/dist/collections/transferOperations.d.ts +8 -0
  38. package/dist/collections/transferOperations.js +411 -0
  39. package/dist/collections/wipeOperations.d.ts +17 -0
  40. package/dist/collections/wipeOperations.js +306 -0
  41. package/dist/databases/methods.d.ts +6 -0
  42. package/dist/databases/methods.js +35 -0
  43. package/dist/databases/setup.d.ts +5 -0
  44. package/dist/databases/setup.js +45 -0
  45. package/dist/examples/yamlTerminologyExample.d.ts +42 -0
  46. package/dist/examples/yamlTerminologyExample.js +272 -0
  47. package/dist/functions/deployments.d.ts +4 -0
  48. package/dist/functions/deployments.js +146 -0
  49. package/dist/functions/fnConfigDiscovery.d.ts +3 -0
  50. package/dist/functions/fnConfigDiscovery.js +108 -0
  51. package/dist/functions/methods.d.ts +16 -0
  52. package/dist/functions/methods.js +174 -0
  53. package/dist/init.d.ts +2 -0
  54. package/dist/init.js +57 -0
  55. package/dist/interactiveCLI.d.ts +36 -0
  56. package/dist/interactiveCLI.js +952 -0
  57. package/dist/main.d.ts +2 -0
  58. package/dist/main.js +1125 -0
  59. package/dist/migrations/afterImportActions.d.ts +17 -0
  60. package/dist/migrations/afterImportActions.js +305 -0
  61. package/dist/migrations/appwriteToX.d.ts +211 -0
  62. package/dist/migrations/appwriteToX.js +493 -0
  63. package/dist/migrations/comprehensiveTransfer.d.ts +147 -0
  64. package/dist/migrations/comprehensiveTransfer.js +1315 -0
  65. package/dist/migrations/dataLoader.d.ts +755 -0
  66. package/dist/migrations/dataLoader.js +1272 -0
  67. package/dist/migrations/importController.d.ts +25 -0
  68. package/dist/migrations/importController.js +283 -0
  69. package/dist/migrations/importDataActions.d.ts +50 -0
  70. package/dist/migrations/importDataActions.js +230 -0
  71. package/dist/migrations/relationships.d.ts +29 -0
  72. package/dist/migrations/relationships.js +203 -0
  73. package/dist/migrations/services/DataTransformationService.d.ts +55 -0
  74. package/dist/migrations/services/DataTransformationService.js +158 -0
  75. package/dist/migrations/services/FileHandlerService.d.ts +75 -0
  76. package/dist/migrations/services/FileHandlerService.js +236 -0
  77. package/dist/migrations/services/ImportOrchestrator.d.ts +99 -0
  78. package/dist/migrations/services/ImportOrchestrator.js +493 -0
  79. package/dist/migrations/services/RateLimitManager.d.ts +138 -0
  80. package/dist/migrations/services/RateLimitManager.js +279 -0
  81. package/dist/migrations/services/RelationshipResolver.d.ts +120 -0
  82. package/dist/migrations/services/RelationshipResolver.js +332 -0
  83. package/dist/migrations/services/UserMappingService.d.ts +109 -0
  84. package/dist/migrations/services/UserMappingService.js +277 -0
  85. package/dist/migrations/services/ValidationService.d.ts +74 -0
  86. package/dist/migrations/services/ValidationService.js +260 -0
  87. package/dist/migrations/transfer.d.ts +30 -0
  88. package/dist/migrations/transfer.js +661 -0
  89. package/dist/migrations/yaml/YamlImportConfigLoader.d.ts +131 -0
  90. package/dist/migrations/yaml/YamlImportConfigLoader.js +383 -0
  91. package/dist/migrations/yaml/YamlImportIntegration.d.ts +93 -0
  92. package/dist/migrations/yaml/YamlImportIntegration.js +341 -0
  93. package/dist/migrations/yaml/generateImportSchemas.d.ts +30 -0
  94. package/dist/migrations/yaml/generateImportSchemas.js +1327 -0
  95. package/dist/schemas/authUser.d.ts +24 -0
  96. package/dist/schemas/authUser.js +17 -0
  97. package/dist/setup.d.ts +2 -0
  98. package/{src/setup.ts → dist/setup.js} +0 -3
  99. package/dist/setupCommands.d.ts +58 -0
  100. package/dist/setupCommands.js +489 -0
  101. package/dist/setupController.d.ts +9 -0
  102. package/dist/setupController.js +34 -0
  103. package/dist/shared/backupMetadataSchema.d.ts +94 -0
  104. package/dist/shared/backupMetadataSchema.js +38 -0
  105. package/dist/shared/backupTracking.d.ts +18 -0
  106. package/dist/shared/backupTracking.js +176 -0
  107. package/dist/shared/confirmationDialogs.d.ts +75 -0
  108. package/dist/shared/confirmationDialogs.js +236 -0
  109. package/dist/shared/migrationHelpers.d.ts +61 -0
  110. package/dist/shared/migrationHelpers.js +145 -0
  111. package/{src/shared/operationLogger.ts → dist/shared/operationLogger.d.ts} +1 -11
  112. package/dist/shared/operationLogger.js +12 -0
  113. package/dist/shared/operationQueue.d.ts +40 -0
  114. package/dist/shared/operationQueue.js +310 -0
  115. package/dist/shared/operationsTable.d.ts +26 -0
  116. package/dist/shared/operationsTable.js +287 -0
  117. package/dist/shared/operationsTableSchema.d.ts +48 -0
  118. package/dist/shared/operationsTableSchema.js +35 -0
  119. package/dist/shared/progressManager.d.ts +62 -0
  120. package/dist/shared/progressManager.js +215 -0
  121. package/dist/shared/relationshipExtractor.d.ts +56 -0
  122. package/dist/shared/relationshipExtractor.js +138 -0
  123. package/dist/shared/selectionDialogs.d.ts +220 -0
  124. package/dist/shared/selectionDialogs.js +588 -0
  125. package/dist/storage/backupCompression.d.ts +20 -0
  126. package/dist/storage/backupCompression.js +67 -0
  127. package/dist/storage/methods.d.ts +44 -0
  128. package/dist/storage/methods.js +475 -0
  129. package/dist/storage/schemas.d.ts +842 -0
  130. package/dist/storage/schemas.js +175 -0
  131. package/dist/tables/indexManager.d.ts +65 -0
  132. package/dist/tables/indexManager.js +294 -0
  133. package/{src/types.ts → dist/types.d.ts} +1 -6
  134. package/dist/types.js +3 -0
  135. package/dist/users/methods.d.ts +16 -0
  136. package/dist/users/methods.js +276 -0
  137. package/dist/utils/configMigration.d.ts +1 -0
  138. package/dist/utils/configMigration.js +261 -0
  139. package/dist/utils/index.js +2 -0
  140. package/dist/utils/loadConfigs.d.ts +50 -0
  141. package/dist/utils/loadConfigs.js +357 -0
  142. package/dist/utils/setupFiles.d.ts +4 -0
  143. package/dist/utils/setupFiles.js +1190 -0
  144. package/dist/utilsController.d.ts +114 -0
  145. package/dist/utilsController.js +898 -0
  146. package/package.json +6 -3
  147. package/CHANGELOG.md +0 -35
  148. package/CONFIG_TODO.md +0 -1189
  149. package/SELECTION_DIALOGS.md +0 -146
  150. package/SERVICE_IMPLEMENTATION_REPORT.md +0 -462
  151. package/scripts/copy-templates.ts +0 -23
  152. package/src/backups/operations/bucketBackup.ts +0 -277
  153. package/src/backups/operations/collectionBackup.ts +0 -310
  154. package/src/backups/operations/comprehensiveBackup.ts +0 -342
  155. package/src/backups/schemas/bucketManifest.ts +0 -78
  156. package/src/backups/schemas/comprehensiveManifest.ts +0 -76
  157. package/src/backups/tracking/centralizedTracking.ts +0 -352
  158. package/src/cli/commands/configCommands.ts +0 -265
  159. package/src/cli/commands/databaseCommands.ts +0 -931
  160. package/src/cli/commands/functionCommands.ts +0 -419
  161. package/src/cli/commands/importFileCommands.ts +0 -815
  162. package/src/cli/commands/schemaCommands.ts +0 -200
  163. package/src/cli/commands/storageCommands.ts +0 -151
  164. package/src/cli/commands/transferCommands.ts +0 -454
  165. package/src/collections/attributes.ts.backup +0 -1555
  166. package/src/collections/columns.ts +0 -2025
  167. package/src/collections/indexes.ts +0 -350
  168. package/src/collections/methods.ts +0 -714
  169. package/src/collections/tableOperations.ts +0 -542
  170. package/src/collections/transferOperations.ts +0 -589
  171. package/src/collections/wipeOperations.ts +0 -449
  172. package/src/databases/methods.ts +0 -49
  173. package/src/databases/setup.ts +0 -77
  174. package/src/examples/yamlTerminologyExample.ts +0 -346
  175. package/src/functions/deployments.ts +0 -221
  176. package/src/functions/fnConfigDiscovery.ts +0 -103
  177. package/src/functions/methods.ts +0 -284
  178. package/src/init.ts +0 -62
  179. package/src/interactiveCLI.ts +0 -1201
  180. package/src/main.ts +0 -1517
  181. package/src/migrations/afterImportActions.ts +0 -579
  182. package/src/migrations/appwriteToX.ts +0 -668
  183. package/src/migrations/comprehensiveTransfer.ts +0 -2285
  184. package/src/migrations/dataLoader.ts +0 -1729
  185. package/src/migrations/importController.ts +0 -440
  186. package/src/migrations/importDataActions.ts +0 -315
  187. package/src/migrations/relationships.ts +0 -333
  188. package/src/migrations/services/DataTransformationService.ts +0 -196
  189. package/src/migrations/services/FileHandlerService.ts +0 -311
  190. package/src/migrations/services/ImportOrchestrator.ts +0 -675
  191. package/src/migrations/services/RateLimitManager.ts +0 -363
  192. package/src/migrations/services/RelationshipResolver.ts +0 -461
  193. package/src/migrations/services/UserMappingService.ts +0 -345
  194. package/src/migrations/services/ValidationService.ts +0 -349
  195. package/src/migrations/transfer.ts +0 -1113
  196. package/src/migrations/yaml/YamlImportConfigLoader.ts +0 -439
  197. package/src/migrations/yaml/YamlImportIntegration.ts +0 -446
  198. package/src/migrations/yaml/generateImportSchemas.ts +0 -1354
  199. package/src/schemas/authUser.ts +0 -23
  200. package/src/setupCommands.ts +0 -602
  201. package/src/setupController.ts +0 -43
  202. package/src/shared/backupMetadataSchema.ts +0 -93
  203. package/src/shared/backupTracking.ts +0 -211
  204. package/src/shared/confirmationDialogs.ts +0 -327
  205. package/src/shared/migrationHelpers.ts +0 -232
  206. package/src/shared/operationQueue.ts +0 -376
  207. package/src/shared/operationsTable.ts +0 -338
  208. package/src/shared/operationsTableSchema.ts +0 -60
  209. package/src/shared/progressManager.ts +0 -278
  210. package/src/shared/relationshipExtractor.ts +0 -214
  211. package/src/shared/selectionDialogs.ts +0 -802
  212. package/src/storage/backupCompression.ts +0 -88
  213. package/src/storage/methods.ts +0 -711
  214. package/src/storage/schemas.ts +0 -205
  215. package/src/tables/indexManager.ts +0 -409
  216. package/src/types/node-appwrite-tablesdb.d.ts +0 -44
  217. package/src/users/methods.ts +0 -358
  218. package/src/utils/configMigration.ts +0 -348
  219. package/src/utils/loadConfigs.ts +0 -457
  220. package/src/utils/setupFiles.ts +0 -1236
  221. package/src/utilsController.ts +0 -1263
  222. package/tests/README.md +0 -497
  223. package/tests/adapters/AdapterFactory.test.ts +0 -277
  224. package/tests/integration/syncOperations.test.ts +0 -463
  225. package/tests/jest.config.js +0 -25
  226. package/tests/migration/configMigration.test.ts +0 -546
  227. package/tests/setup.ts +0 -62
  228. package/tests/testUtils.ts +0 -340
  229. package/tests/utils/loadConfigs.test.ts +0 -350
  230. package/tests/validation/configValidation.test.ts +0 -412
  231. package/tsconfig.json +0 -44
  232. /package/{src → dist}/functions/templates/count-docs-in-collection/README.md +0 -0
  233. /package/{src → dist}/functions/templates/count-docs-in-collection/src/main.ts +0 -0
  234. /package/{src → dist}/functions/templates/count-docs-in-collection/src/request.ts +0 -0
  235. /package/{src → dist}/functions/templates/hono-typescript/README.md +0 -0
  236. /package/{src → dist}/functions/templates/hono-typescript/src/adapters/request.ts +0 -0
  237. /package/{src → dist}/functions/templates/hono-typescript/src/adapters/response.ts +0 -0
  238. /package/{src → dist}/functions/templates/hono-typescript/src/app.ts +0 -0
  239. /package/{src → dist}/functions/templates/hono-typescript/src/context.ts +0 -0
  240. /package/{src → dist}/functions/templates/hono-typescript/src/main.ts +0 -0
  241. /package/{src → dist}/functions/templates/hono-typescript/src/middleware/appwrite.ts +0 -0
  242. /package/{src → dist}/functions/templates/typescript-node/README.md +0 -0
  243. /package/{src → dist}/functions/templates/typescript-node/src/context.ts +0 -0
  244. /package/{src → dist}/functions/templates/typescript-node/src/main.ts +0 -0
  245. /package/{src → dist}/functions/templates/uv/README.md +0 -0
  246. /package/{src → dist}/functions/templates/uv/pyproject.toml +0 -0
  247. /package/{src → dist}/functions/templates/uv/src/__init__.py +0 -0
  248. /package/{src → dist}/functions/templates/uv/src/context.py +0 -0
  249. /package/{src → dist}/functions/templates/uv/src/main.py +0 -0
  250. /package/{src/utils/index.ts → dist/utils/index.d.ts} +0 -0
package/src/main.ts DELETED
@@ -1,1517 +0,0 @@
1
- #!/usr/bin/env node
2
- import yargs from "yargs";
3
- import { type ArgumentsCamelCase } from "yargs";
4
- import { hideBin } from "yargs/helpers";
5
- import { InteractiveCLI } from "./interactiveCLI.js";
6
- import { UtilsController, type SetupOptions } from "./utilsController.js";
7
- import type { TransferOptions } from "./migrations/transfer.js";
8
- import { Databases, Storage, type Models } from "node-appwrite";
9
- import { getClient } from "appwrite-utils-helpers";
10
- import { fetchAllDatabases } from "./databases/methods.js";
11
- import { setupDirsFiles } from "./utils/setupFiles.js";
12
- import { fetchAllCollections } from "./collections/methods.js";
13
- import type { Specification } from "appwrite-utils";
14
- import chalk from "chalk";
15
- import { listSpecifications } from "./functions/methods.js";
16
- import { MessageFormatter, logger, AuthenticationError } from "appwrite-utils-helpers";
17
- import { ConfirmationDialogs } from "./shared/confirmationDialogs.js";
18
- import { SelectionDialogs } from "./shared/selectionDialogs.js";
19
- import type { SyncSelectionSummary, DatabaseSelection, BucketSelection } from "./shared/selectionDialogs.js";
20
- import path from "path";
21
- import fs from "fs";
22
- import { createRequire } from "node:module";
23
-
24
- const require = createRequire(import.meta.url);
25
- if (!(globalThis as any).require) {
26
- (globalThis as any).require = require;
27
- }
28
-
29
- interface CliOptions {
30
- config?: string;
31
- appwriteConfig?: boolean;
32
- it?: boolean;
33
- dbIds?: string;
34
- collectionIds?: string;
35
- bucketIds?: string;
36
- wipe?: "all" | "storage" | "docs" | "users";
37
- wipeCollections?: boolean;
38
- generate?: boolean;
39
- import?: boolean;
40
- backup?: boolean;
41
- backupFormat?: "json" | "zip";
42
- comprehensiveBackup?: boolean;
43
- trackingDatabaseId?: string;
44
- parallelDownloads?: number;
45
- writeData?: boolean;
46
- push?: boolean;
47
- sync?: boolean;
48
- endpoint?: string;
49
- projectId?: string;
50
- apiKey?: string;
51
- transfer?: boolean;
52
- transferUsers?: boolean;
53
- fromDbId?: string;
54
- toDbId?: string;
55
- fromCollectionId?: string;
56
- toCollectionId?: string;
57
- fromBucketId?: string;
58
- toBucketId?: string;
59
- remoteEndpoint?: string;
60
- remoteProjectId?: string;
61
- remoteApiKey?: string;
62
- setup?: boolean;
63
- updateFunctionSpec?: boolean;
64
- functionId?: string;
65
- specification?: string;
66
- migrateConfig?: boolean;
67
- generateConstants?: boolean;
68
- constantsLanguages?: string;
69
- constantsOutput?: string;
70
- migrateCollectionsToTables?: boolean;
71
- useSession?: boolean;
72
- sessionCookie?: string;
73
- listBackups?: boolean;
74
- autoSync?: boolean;
75
- selectBuckets?: boolean;
76
- // New schema/constant CLI flags
77
- generateSchemas?: boolean;
78
- schemaFormat?: 'zod' | 'json' | 'pydantic' | 'both' | 'all';
79
- schemaOutDir?: string;
80
- constantsInclude?: string;
81
- // Direct file import
82
- importFile?: string;
83
- targetDb?: string;
84
- targetTable?: string;
85
- }
86
-
87
- type ParsedArgv = ArgumentsCamelCase<CliOptions>;
88
-
89
- /**
90
- * Enhanced sync function with intelligent configuration detection and selection dialogs
91
- */
92
- async function performEnhancedSync(
93
- controller: UtilsController,
94
- parsedArgv: ParsedArgv
95
- ): Promise<SyncSelectionSummary | null> {
96
- try {
97
- MessageFormatter.banner("Enhanced Sync", "Intelligent configuration detection and selection");
98
-
99
- if (!controller.config) {
100
- MessageFormatter.error("No Appwrite configuration found", undefined, { prefix: "Sync" });
101
- return null;
102
- }
103
-
104
- // Get all available databases from remote
105
- const availableDatabases = await fetchAllDatabases(controller.database!);
106
- if (availableDatabases.length === 0) {
107
- MessageFormatter.warning("No databases found in remote project", { prefix: "Sync" });
108
- return null;
109
- }
110
-
111
- // Get existing configuration
112
- const configuredDatabases = controller.config.databases || [];
113
- const configuredBuckets = controller.config.buckets || [];
114
-
115
- // Check if we have existing configuration
116
- const hasExistingConfig = configuredDatabases.length > 0 || configuredBuckets.length > 0;
117
-
118
- let syncExisting = false;
119
- let modifyConfiguration = true;
120
-
121
- if (hasExistingConfig) {
122
- // Prompt about existing configuration
123
- const response = await SelectionDialogs.promptForExistingConfig([
124
- ...configuredDatabases,
125
- ...configuredBuckets
126
- ]);
127
- syncExisting = response.syncExisting;
128
- modifyConfiguration = response.modifyConfiguration;
129
-
130
- if (syncExisting && !modifyConfiguration) {
131
- // Just sync existing configuration without changes
132
- MessageFormatter.info("Syncing existing configuration without modifications", { prefix: "Sync" });
133
-
134
- // Convert configured databases to DatabaseSelection format
135
- const databaseSelections: DatabaseSelection[] = configuredDatabases.map(db => ({
136
- databaseId: db.$id,
137
- databaseName: db.name,
138
- tableIds: [], // Tables will be populated from collections config
139
- tableNames: [],
140
- isNew: false
141
- }));
142
-
143
- // Convert configured buckets to BucketSelection format
144
- const bucketSelections: BucketSelection[] = configuredBuckets.map(bucket => ({
145
- bucketId: bucket.$id,
146
- bucketName: bucket.name,
147
- databaseId: undefined,
148
- databaseName: undefined,
149
- isNew: false
150
- }));
151
-
152
- const selectionSummary = SelectionDialogs.createSyncSelectionSummary(
153
- databaseSelections,
154
- bucketSelections
155
- );
156
-
157
- const confirmed = await SelectionDialogs.confirmSyncSelection(selectionSummary, 'pull');
158
- if (!confirmed) {
159
- MessageFormatter.info("Pull operation cancelled by user", { prefix: "Sync" });
160
- return null;
161
- }
162
-
163
- // Perform sync with existing configuration (pull from remote)
164
- await controller.selectivePull(databaseSelections, bucketSelections);
165
- return selectionSummary;
166
- }
167
- }
168
-
169
- if (!modifyConfiguration) {
170
- MessageFormatter.info("No configuration changes requested", { prefix: "Sync" });
171
- return null;
172
- }
173
-
174
- // Allow new items selection based on user choice
175
- const allowNewOnly = !syncExisting;
176
-
177
- // Select databases
178
- const selectedDatabaseIds = await SelectionDialogs.selectDatabases(
179
- availableDatabases,
180
- configuredDatabases,
181
- {
182
- showSelectAll: false,
183
- allowNewOnly,
184
- defaultSelected: []
185
- }
186
- );
187
-
188
- if (selectedDatabaseIds.length === 0) {
189
- MessageFormatter.warning("No databases selected for sync", { prefix: "Sync" });
190
- return null;
191
- }
192
-
193
- // For each selected database, get available tables and select them
194
- const tableSelectionsMap = new Map<string, string[]>();
195
- const availableTablesMap = new Map<string, any[]>();
196
-
197
- for (const databaseId of selectedDatabaseIds) {
198
- const database = availableDatabases.find(db => db.$id === databaseId)!;
199
-
200
- SelectionDialogs.showProgress(`Fetching tables for database: ${database.name}`);
201
-
202
- // Get available tables from remote
203
- const availableTables = await fetchAllCollections(databaseId, controller.database!);
204
- availableTablesMap.set(databaseId, availableTables);
205
-
206
- // Get configured tables for this database
207
- // Note: Collections are stored globally in the config, not per database
208
- const configuredTables = controller.config.collections || [];
209
-
210
- // Select tables for this database
211
- const selectedTableIds = await SelectionDialogs.selectTablesForDatabase(
212
- databaseId,
213
- database.name,
214
- availableTables,
215
- configuredTables,
216
- {
217
- showSelectAll: false,
218
- allowNewOnly,
219
- defaultSelected: []
220
- }
221
- );
222
-
223
- tableSelectionsMap.set(databaseId, selectedTableIds);
224
-
225
- if (selectedTableIds.length === 0) {
226
- MessageFormatter.warning(`No tables selected for database: ${database.name}`, { prefix: "Sync" });
227
- }
228
- }
229
-
230
- // Select buckets
231
- let selectedBucketIds: string[] = [];
232
-
233
- // Get available buckets from remote
234
- if (controller.storage) {
235
- try {
236
- // Note: We need to implement fetchAllBuckets or use storage.listBuckets
237
- // For now, we'll use configured buckets as available
238
- SelectionDialogs.showProgress("Fetching storage buckets...");
239
-
240
- // Create a mock availableBuckets array - in real implementation,
241
- // you'd fetch this from the Appwrite API
242
- const availableBuckets = configuredBuckets; // Placeholder
243
-
244
- selectedBucketIds = await SelectionDialogs.selectBucketsForDatabases(
245
- selectedDatabaseIds,
246
- availableBuckets,
247
- configuredBuckets,
248
- {
249
- showSelectAll: false,
250
- allowNewOnly: parsedArgv.selectBuckets ? false : allowNewOnly,
251
- groupByDatabase: true,
252
- defaultSelected: []
253
- }
254
- );
255
- } catch (error) {
256
- MessageFormatter.warning("Could not fetch storage buckets", { prefix: "Sync" });
257
- logger.warn("Failed to fetch buckets during sync", { error });
258
- }
259
- }
260
-
261
- // Create selection objects
262
- const databaseSelections = SelectionDialogs.createDatabaseSelection(
263
- selectedDatabaseIds,
264
- availableDatabases,
265
- tableSelectionsMap,
266
- configuredDatabases,
267
- availableTablesMap
268
- );
269
-
270
- const bucketSelections = SelectionDialogs.createBucketSelection(
271
- selectedBucketIds,
272
- [], // availableBuckets - would be populated from API
273
- configuredBuckets,
274
- availableDatabases
275
- );
276
-
277
- // Show final confirmation
278
- const selectionSummary = SelectionDialogs.createSyncSelectionSummary(
279
- databaseSelections,
280
- bucketSelections
281
- );
282
-
283
- const confirmed = await SelectionDialogs.confirmSyncSelection(selectionSummary, 'pull');
284
- if (!confirmed) {
285
- MessageFormatter.info("Pull operation cancelled by user", { prefix: "Sync" });
286
- return null;
287
- }
288
-
289
- // Perform the selective sync (pull from remote)
290
- await controller.selectivePull(databaseSelections, bucketSelections);
291
-
292
- MessageFormatter.success("Enhanced sync completed successfully", { prefix: "Sync" });
293
- return selectionSummary;
294
-
295
- } catch (error) {
296
- SelectionDialogs.showError("Enhanced sync failed", error instanceof Error ? error : new Error(String(error)));
297
- return null;
298
- }
299
- }
300
-
301
- /**
302
- * Performs selective sync with the given database and bucket selections
303
- */
304
-
305
- /**
306
- * Checks if the migration from collections to tables should be allowed
307
- * Returns an object with:
308
- * - allowed: boolean indicating if migration should proceed
309
- * - reason: string explaining why migration was blocked (if not allowed)
310
- */
311
- function checkMigrationConditions(configPath: string): {
312
- allowed: boolean;
313
- reason?: string;
314
- } {
315
- const collectionsPath = path.join(configPath, "collections");
316
- const tablesPath = path.join(configPath, "tables");
317
-
318
- // Check if collections/ folder exists
319
- if (!fs.existsSync(collectionsPath)) {
320
- return {
321
- allowed: false,
322
- reason:
323
- "No collections/ folder found. Migration requires existing collections to migrate.",
324
- };
325
- }
326
-
327
- // Check if collections/ folder has YAML files
328
- const collectionFiles = fs
329
- .readdirSync(collectionsPath)
330
- .filter((file) => file.endsWith(".yaml") || file.endsWith(".yml"));
331
-
332
- if (collectionFiles.length === 0) {
333
- return {
334
- allowed: false,
335
- reason:
336
- "No YAML files found in collections/ folder. Migration requires existing collection YAML files.",
337
- };
338
- }
339
-
340
- // Check if tables/ folder exists and has YAML files
341
- if (fs.existsSync(tablesPath)) {
342
- const tableFiles = fs
343
- .readdirSync(tablesPath)
344
- .filter((file) => file.endsWith(".yaml") || file.endsWith(".yml"));
345
-
346
- if (tableFiles.length > 0) {
347
- return {
348
- allowed: false,
349
- reason: `Tables folder already exists with ${tableFiles.length} YAML file(s). Migration appears to have already been completed.`,
350
- };
351
- }
352
- }
353
-
354
- // All conditions met
355
- return { allowed: true };
356
- }
357
-
358
- const argv = yargs(hideBin(process.argv))
359
- .option("config", {
360
- type: "string",
361
- description: "Path to Appwrite configuration file (appwriteConfig.ts)",
362
- })
363
- .option("appwriteConfig", {
364
- alias: ["appwrite-config", "use-appwrite-config"],
365
- type: "boolean",
366
- description: "Prefer loading from appwrite.config.json instead of config.yaml",
367
- })
368
- .option("it", {
369
- alias: ["interactive", "i"],
370
- type: "boolean",
371
- description: "Launch interactive CLI mode with guided prompts",
372
- })
373
- .option("dbIds", {
374
- type: "string",
375
- description:
376
- "Comma-separated list of database IDs to target (e.g., 'db1,db2,db3')",
377
- })
378
- .option("collectionIds", {
379
- alias: ["collIds", "tableIds", "tables"],
380
- type: "string",
381
- description:
382
- "Comma-separated list of collection/table IDs to target (e.g., 'users,posts')",
383
- })
384
- .option("bucketIds", {
385
- type: "string",
386
- description: "Comma-separated list of bucket IDs to operate on",
387
- })
388
- .option("wipe", {
389
- choices: ["all", "docs", "users"] as const,
390
- description:
391
- "⚠️ DESTRUCTIVE: Wipe data (all: databases+storage+users, docs: documents only, users: user accounts only)",
392
- })
393
- .option("wipeCollections", {
394
- type: "boolean",
395
- description:
396
- "⚠️ DESTRUCTIVE: Wipe specific collections/tables (requires --collectionIds or --tableIds)",
397
- })
398
- .option("transferUsers", {
399
- type: "boolean",
400
- description: "Transfer users between projects",
401
- })
402
- .option("generate", {
403
- type: "boolean",
404
- description:
405
- "Generate TypeScript schemas and types from your Appwrite database schemas",
406
- })
407
- .option("import", {
408
- type: "boolean",
409
- description:
410
- "Import data from importData/ directory into your Appwrite databases",
411
- })
412
- .option("backup", {
413
- type: "boolean",
414
- description: "Create a complete backup of your databases and collections",
415
- })
416
- .option("backupFormat", {
417
- type: "string",
418
- choices: ["json", "zip"] as const,
419
- default: "json",
420
- description: "Backup file format (json or zip)",
421
- })
422
- .option("listBackups", {
423
- type: "boolean",
424
- description: "List all backups for databases",
425
- })
426
- .option("comprehensiveBackup", {
427
- alias: ["comprehensive", "backup-all"],
428
- type: "boolean",
429
- description:
430
- "🚀 Create comprehensive backup of ALL databases and ALL storage buckets",
431
- })
432
- .option("trackingDatabaseId", {
433
- alias: ["tracking-db"],
434
- type: "string",
435
- description:
436
- "Database ID to use for centralized backup tracking (interactive prompt if not specified)",
437
- })
438
- .option("parallelDownloads", {
439
- type: "number",
440
- default: 10,
441
- description:
442
- "Number of parallel file downloads for bucket backups (default: 10)",
443
- })
444
- .option("writeData", {
445
- type: "boolean",
446
- description:
447
- "Output converted import data to files for validation before importing",
448
- })
449
- .option("push", {
450
- type: "boolean",
451
- description:
452
- "Deploy your local configuration (collections, attributes, indexes) to Appwrite",
453
- })
454
- .option("sync", {
455
- type: "boolean",
456
- description:
457
- "Pull and synchronize your local config with the remote Appwrite project schema",
458
- })
459
- .option("autoSync", {
460
- alias: ["auto"],
461
- type: "boolean",
462
- description: "Skip prompts and sync all databases, tables, and buckets (current behavior)"
463
- })
464
- .option("selectBuckets", {
465
- type: "boolean",
466
- description: "Force bucket selection dialog even if buckets are already configured"
467
- })
468
- .option("endpoint", {
469
- type: "string",
470
- description: "Set the Appwrite endpoint",
471
- })
472
- .option("projectId", {
473
- type: "string",
474
- description: "Set the Appwrite project ID",
475
- })
476
- .option("apiKey", {
477
- type: "string",
478
- description: "Set the Appwrite API key",
479
- })
480
- .option("transfer", {
481
- type: "boolean",
482
- description:
483
- "Transfer documents and files between databases, collections, or projects",
484
- })
485
- .option("fromDbId", {
486
- alias: ["fromDb", "sourceDbId", "sourceDb"],
487
- type: "string",
488
- description: "Source database ID for transfer operations",
489
- })
490
- .option("toDbId", {
491
- alias: ["toDb", "targetDbId", "targetDb"],
492
- type: "string",
493
- description: "Target database ID for transfer operations",
494
- })
495
- .option("fromCollectionId", {
496
- alias: ["fromCollId", "fromColl"],
497
- type: "string",
498
- description: "Set the source collection ID for transfer",
499
- })
500
- .option("toCollectionId", {
501
- alias: ["toCollId", "toColl"],
502
- type: "string",
503
- description: "Set the destination collection ID for transfer",
504
- })
505
- .option("fromBucketId", {
506
- type: "string",
507
- description: "Set the source bucket ID for transfer",
508
- })
509
- .option("toBucketId", {
510
- type: "string",
511
- description: "Set the destination bucket ID for transfer",
512
- })
513
- .option("remoteEndpoint", {
514
- type: "string",
515
- description: "Set the remote Appwrite endpoint for transfer",
516
- })
517
- .option("remoteProjectId", {
518
- type: "string",
519
- description: "Set the remote Appwrite project ID for transfer",
520
- })
521
- .option("remoteApiKey", {
522
- type: "string",
523
- description: "Set the remote Appwrite API key for transfer",
524
- })
525
- .option("setup", {
526
- type: "boolean",
527
- description:
528
- "Initialize project with configuration files and directory structure",
529
- })
530
- .option("updateFunctionSpec", {
531
- type: "boolean",
532
- description: "Update function specifications",
533
- })
534
- .option("functionId", {
535
- type: "string",
536
- description: "Function ID to update",
537
- })
538
- .option("specification", {
539
- type: "string",
540
- description: "New function specification (e.g., 's-1vcpu-1gb')",
541
- choices: [
542
- "s-0.5vcpu-512mb",
543
- "s-1vcpu-1gb",
544
- "s-2vcpu-2gb",
545
- "s-2vcpu-4gb",
546
- "s-4vcpu-4gb",
547
- "s-4vcpu-8gb",
548
- "s-8vcpu-4gb",
549
- "s-8vcpu-8gb",
550
- ],
551
- })
552
- .option("migrateConfig", {
553
- alias: ["migrate"],
554
- type: "boolean",
555
- description:
556
- "Migrate appwriteConfig.ts to .appwrite structure with YAML configuration",
557
- })
558
- .option("generateConstants", {
559
- alias: ["constants"],
560
- type: "boolean",
561
- description:
562
- "Generate cross-language constants file with database, collection, bucket, and function IDs",
563
- })
564
- .option("constantsLanguages", {
565
- type: "string",
566
- description:
567
- "Comma-separated list of languages for constants (typescript,javascript,python,php,dart,json,env)",
568
- default: "typescript",
569
- })
570
- .option("constantsOutput", {
571
- type: "string",
572
- description:
573
- "Output directory for generated constants files (default: config-folder/constants)",
574
- default: "auto",
575
- })
576
- .option("constantsInclude", {
577
- type: "string",
578
- description:
579
- "Comma-separated categories to include: databases,collections,buckets,functions",
580
- })
581
- .option("generateSchemas", {
582
- type: "boolean",
583
- description: "Generate schemas/models without interactive prompts",
584
- })
585
- .option("schemaFormat", {
586
- type: "string",
587
- choices: ["zod", "json", "pydantic", "both", "all"],
588
- description: "Schema format: zod, json, pydantic, both (zod+json), or all",
589
- })
590
- .option("schemaOutDir", {
591
- type: "string",
592
- description: "Output directory for generated schemas (absolute path respected)",
593
- })
594
- .option("migrateCollectionsToTables", {
595
- alias: ["migrate-collections"],
596
- type: "boolean",
597
- description:
598
- "Migrate collections to tables format for TablesDB API compatibility",
599
- })
600
- .option("useSession", {
601
- alias: ["session"],
602
- type: "boolean",
603
- description: "Use Appwrite CLI session authentication instead of API key",
604
- })
605
- .option("sessionCookie", {
606
- type: "string",
607
- description: "Explicit session cookie to use for authentication",
608
- })
609
- .option("importFile", {
610
- alias: ["import-file"],
611
- type: "string",
612
- description: "Import a CSV or JSON file directly into a table (no config needed)",
613
- })
614
- .option("targetDb", {
615
- alias: ["target-db"],
616
- type: "string",
617
- description: "Target database ID for --importFile (prompted if omitted)",
618
- })
619
- .option("targetTable", {
620
- alias: ["target-table"],
621
- type: "string",
622
- description: "Target table ID for --importFile (prompted if omitted)",
623
- })
624
- .parse() as ParsedArgv;
625
-
626
- async function main() {
627
- const startTime = Date.now();
628
- const operationStats: Record<string, number> = {};
629
-
630
- if (argv.it) {
631
- const cli = new InteractiveCLI(process.cwd(), {
632
- useSession: argv.useSession,
633
- sessionCookie: argv.sessionCookie
634
- });
635
- await cli.run();
636
- } else {
637
- // Non-interactive mode - pass auth flags through to controller
638
- // ConfigManager will handle config discovery and auth decisions
639
- // Users can provide credentials via CLI flags even without a config file
640
-
641
- const controller = UtilsController.getInstance(process.cwd());
642
-
643
- // Build init options from CLI flags
644
- const initOptions: any = {
645
- useSession: argv.useSession,
646
- sessionCookie: argv.sessionCookie,
647
- preferJson: argv.appwriteConfig,
648
- };
649
-
650
- // Add CLI overrides if provided - these can work even without a config file
651
- if (argv.endpoint || argv.projectId || argv.apiKey) {
652
- initOptions.overrides = {
653
- appwriteEndpoint: argv.endpoint,
654
- appwriteProject: argv.projectId,
655
- appwriteKey: argv.apiKey,
656
- };
657
- }
658
-
659
- try {
660
- await controller.init(initOptions);
661
- } catch (error) {
662
- if (error instanceof AuthenticationError) {
663
- MessageFormatter.error(error.getFormattedMessage(), undefined, { prefix: "Auth" });
664
- process.exit(1);
665
- }
666
- // Re-throw other errors
667
- throw error;
668
- }
669
-
670
- // After init, check if we have a valid config (from file OR CLI overrides)
671
- if (!controller.config) {
672
- MessageFormatter.error("No Appwrite configuration available", undefined, { prefix: "CLI" });
673
- MessageFormatter.info("Provide credentials via CLI flags (--endpoint, --projectId, --apiKey or --session)", { prefix: "CLI" });
674
- MessageFormatter.info("Or create a config file using --setup", { prefix: "CLI" });
675
- return;
676
- }
677
-
678
- const parsedArgv = argv;
679
-
680
- if (argv.importFile) {
681
- const { importFileFromPath, importFilePromptMissing } = await import("./cli/commands/importFileCommands.js");
682
- if (!controller.adapter) {
683
- MessageFormatter.error("No adapter available — check your credentials", undefined, { prefix: "Import" });
684
- return;
685
- }
686
- if (parsedArgv.targetDb && parsedArgv.targetTable) {
687
- await importFileFromPath(controller.adapter, argv.importFile, parsedArgv.targetDb, parsedArgv.targetTable);
688
- } else {
689
- await importFilePromptMissing(controller.adapter, controller.database, argv.importFile, parsedArgv.targetDb, parsedArgv.targetTable);
690
- }
691
- return;
692
- }
693
-
694
- if (argv.setup) {
695
- await setupDirsFiles(false, process.cwd());
696
- return;
697
- }
698
-
699
- if (argv.migrateConfig) {
700
- const { migrateConfig } = await import("./utils/configMigration.js");
701
- await migrateConfig(process.cwd());
702
- return;
703
- }
704
-
705
- if (argv.generateConstants) {
706
- const { ConstantsGenerator } = await import(
707
- "appwrite-utils-helpers"
708
- );
709
- type SupportedLanguage =
710
- import("appwrite-utils-helpers").SupportedLanguage;
711
-
712
- if (!controller.config) {
713
- MessageFormatter.error("No Appwrite configuration found", undefined, {
714
- prefix: "Constants",
715
- });
716
- return;
717
- }
718
-
719
- const languages = argv
720
- .constantsLanguages!.split(",")
721
- .map((l) => l.trim()) as SupportedLanguage[];
722
-
723
- // Determine output directory - use config folder/constants by default, or custom path if specified
724
- let outputDir: string;
725
- if (argv.constantsOutput === "auto") {
726
- // Default case: use config directory + constants, fallback to current directory
727
- const configPath = controller.getAppwriteFolderPath();
728
- outputDir = configPath
729
- ? path.join(configPath, "constants")
730
- : path.join(process.cwd(), "constants");
731
- } else {
732
- // Custom output directory specified
733
- outputDir = argv.constantsOutput!;
734
- }
735
-
736
- MessageFormatter.info(
737
- `Generating constants for languages: ${languages.join(", ")}`,
738
- { prefix: "Constants" }
739
- );
740
-
741
- const generator = new ConstantsGenerator(controller.config);
742
- await generator.generateFiles(languages, outputDir);
743
-
744
- operationStats.generatedConstants = languages.length;
745
- MessageFormatter.success(`Constants generated in ${outputDir}`, {
746
- prefix: "Constants",
747
- });
748
- return;
749
- }
750
-
751
- if (argv.migrateCollectionsToTables) {
752
- try {
753
- if (!controller.config) {
754
- MessageFormatter.error("No Appwrite configuration found", undefined, {
755
- prefix: "Migration",
756
- });
757
- return;
758
- }
759
-
760
- // Get the config path from the controller or use .appwrite in current directory
761
- let configPath = controller.getAppwriteFolderPath();
762
- if (!configPath) {
763
- // Try .appwrite in current directory
764
- const defaultPath = path.join(process.cwd(), ".appwrite");
765
- if (fs.existsSync(defaultPath)) {
766
- configPath = defaultPath;
767
- } else {
768
- MessageFormatter.error(
769
- "Could not determine configuration folder path",
770
- undefined,
771
- { prefix: "Migration" }
772
- );
773
- MessageFormatter.info(
774
- "Make sure you have a .appwrite/ folder in your current directory",
775
- { prefix: "Migration" }
776
- );
777
- return;
778
- }
779
- }
780
-
781
- // Check if migration conditions are met
782
- const migrationCheck = checkMigrationConditions(configPath);
783
- if (!migrationCheck.allowed) {
784
- MessageFormatter.error(
785
- `Migration not allowed: ${migrationCheck.reason}`,
786
- undefined,
787
- { prefix: "Migration" }
788
- );
789
- MessageFormatter.info("Migration requirements:", {
790
- prefix: "Migration",
791
- });
792
- MessageFormatter.info(
793
- " • Configuration must be loaded (use --config or have .appwrite/ folder)",
794
- { prefix: "Migration" }
795
- );
796
- MessageFormatter.info(
797
- " • collections/ folder must exist with YAML files",
798
- { prefix: "Migration" }
799
- );
800
- MessageFormatter.info(
801
- " • tables/ folder must not exist or be empty",
802
- { prefix: "Migration" }
803
- );
804
- return;
805
- }
806
-
807
- const { migrateCollectionsToTables } = await import(
808
- "appwrite-utils-helpers"
809
- );
810
-
811
- MessageFormatter.info("Starting collections to tables migration...", {
812
- prefix: "Migration",
813
- });
814
- const result = migrateCollectionsToTables(controller.config, {
815
- strategy: "full_migration",
816
- validateResult: true,
817
- dryRun: false,
818
- });
819
-
820
- if (result.success) {
821
- operationStats.migratedCollections = result.changes.length;
822
- MessageFormatter.success(
823
- "Collections migration completed successfully",
824
- { prefix: "Migration" }
825
- );
826
- } else {
827
- MessageFormatter.error(
828
- `Migration failed: ${result.errors.join(", ")}`,
829
- undefined,
830
- { prefix: "Migration" }
831
- );
832
- process.exit(1);
833
- }
834
- } catch (error) {
835
- MessageFormatter.error(
836
- "Migration failed",
837
- error instanceof Error ? error : new Error(String(error)),
838
- { prefix: "Migration" }
839
- );
840
- process.exit(1);
841
- }
842
- return;
843
- }
844
-
845
- // List backups if requested
846
- if (parsedArgv.listBackups) {
847
- const { AdapterFactory } = await import("appwrite-utils-helpers");
848
- const { listBackups } = await import("./shared/backupTracking.js");
849
-
850
- if (!controller.config) {
851
- MessageFormatter.error("No Appwrite configuration found", undefined, {
852
- prefix: "Backups",
853
- });
854
- return;
855
- }
856
-
857
- const { adapter } = await AdapterFactory.create({
858
- appwriteEndpoint: controller.config.appwriteEndpoint,
859
- appwriteProject: controller.config.appwriteProject,
860
- appwriteKey: controller.config.appwriteKey,
861
- });
862
-
863
- const databases = parsedArgv.dbIds
864
- ? await controller.getDatabasesByIds(parsedArgv.dbIds.split(","))
865
- : await fetchAllDatabases(controller.database!);
866
-
867
- if (!databases || databases.length === 0) {
868
- MessageFormatter.info("No databases found", { prefix: "Backups" });
869
- return;
870
- }
871
-
872
- for (const db of databases!) {
873
- const backups = await listBackups(adapter, db.$id);
874
-
875
- MessageFormatter.info(
876
- `\nBackups for database: ${db.name} (${db.$id})`,
877
- { prefix: "Backups" }
878
- );
879
-
880
- if (backups.length === 0) {
881
- MessageFormatter.info(" No backups found", { prefix: "Backups" });
882
- } else {
883
- backups.forEach((backup, index) => {
884
- const date = new Date(backup.$createdAt).toLocaleString();
885
- const size = MessageFormatter.formatBytes(backup.sizeBytes);
886
- MessageFormatter.info(
887
- ` ${
888
- index + 1
889
- }. ${date} - ${backup.format.toUpperCase()} - ${size} - ${
890
- backup.collections
891
- } collections, ${backup.documents} documents`,
892
- { prefix: "Backups" }
893
- );
894
- });
895
- }
896
- }
897
-
898
- return;
899
- }
900
-
901
- const options: SetupOptions = {
902
- databases: parsedArgv.dbIds
903
- ? await controller.getDatabasesByIds(parsedArgv.dbIds.split(","))
904
- : undefined,
905
- collections: parsedArgv.collectionIds?.split(","),
906
- doBackup: parsedArgv.backup,
907
- wipeDatabase: parsedArgv.wipe === "all" || parsedArgv.wipe === "docs",
908
- wipeDocumentStorage:
909
- parsedArgv.wipe === "all" || parsedArgv.wipe === "storage",
910
- wipeUsers: parsedArgv.wipe === "all" || parsedArgv.wipe === "users",
911
- generateSchemas: parsedArgv.generate,
912
- importData: parsedArgv.import,
913
- shouldWriteFile: parsedArgv.writeData,
914
- wipeCollections: parsedArgv.wipeCollections,
915
- transferUsers: parsedArgv.transferUsers,
916
- };
917
-
918
- if (parsedArgv.updateFunctionSpec) {
919
- if (!parsedArgv.functionId || !parsedArgv.specification) {
920
- throw new Error(
921
- "Function ID and specification are required for updating function specs"
922
- );
923
- }
924
- MessageFormatter.info(
925
- `Updating function specification for ${parsedArgv.functionId} to ${parsedArgv.specification}`,
926
- { prefix: "Functions" }
927
- );
928
- const specifications = await listSpecifications(
929
- controller.appwriteServer!
930
- );
931
- if (
932
- !specifications.specifications.some(
933
- (s: { slug: string }) => s.slug === parsedArgv.specification
934
- )
935
- ) {
936
- MessageFormatter.error(
937
- `Specification ${parsedArgv.specification} not found`,
938
- undefined,
939
- { prefix: "Functions" }
940
- );
941
- return;
942
- }
943
- await controller.updateFunctionSpecifications(
944
- parsedArgv.functionId,
945
- parsedArgv.specification as Specification
946
- );
947
- }
948
-
949
- // Add default databases if not specified (only if we need them for operations)
950
- const needsDatabases =
951
- options.doBackup ||
952
- options.wipeDatabase ||
953
- options.wipeDocumentStorage ||
954
- options.wipeUsers ||
955
- options.wipeCollections ||
956
- options.importData ||
957
- parsedArgv.sync ||
958
- parsedArgv.transfer;
959
-
960
- if (
961
- needsDatabases &&
962
- (!options.databases || options.databases.length === 0)
963
- ) {
964
- const allDatabases = await fetchAllDatabases(controller.database!);
965
- options.databases = allDatabases;
966
- }
967
-
968
- // Add default collections if not specified
969
- if (!options.collections || options.collections.length === 0) {
970
- if (controller.config && controller.config.collections) {
971
- options.collections = controller.config.collections.map(
972
- (c: any) => c.name
973
- );
974
- } else {
975
- options.collections = [];
976
- }
977
- }
978
-
979
- // Comprehensive backup (all databases + all buckets)
980
- if (parsedArgv.comprehensiveBackup) {
981
- const { comprehensiveBackup } = await import(
982
- "./backups/operations/comprehensiveBackup.js"
983
- );
984
- const { AdapterFactory } = await import("appwrite-utils-helpers");
985
-
986
- // Get tracking database ID (interactive prompt if not specified)
987
- let trackingDatabaseId = parsedArgv.trackingDatabaseId;
988
-
989
- if (!trackingDatabaseId) {
990
- // Fetch all databases for selection
991
- const allDatabases = await fetchAllDatabases(controller.database!);
992
-
993
- if (allDatabases.length === 0) {
994
- MessageFormatter.error(
995
- "No databases found. Cannot create comprehensive backup without a tracking database.",
996
- undefined,
997
- { prefix: "Backup" }
998
- );
999
- return;
1000
- }
1001
-
1002
- if (allDatabases.length === 1) {
1003
- trackingDatabaseId = allDatabases[0].$id;
1004
- MessageFormatter.info(
1005
- `Using only available database for tracking: ${allDatabases[0].name} (${trackingDatabaseId})`,
1006
- { prefix: "Backup" }
1007
- );
1008
- } else {
1009
- // Interactive selection
1010
- const inquirer = (await import("inquirer")).default;
1011
- const answer = await inquirer.prompt([
1012
- {
1013
- type: "list",
1014
- name: "trackingDb",
1015
- message: "Select database to store backup tracking metadata:",
1016
- choices: allDatabases.map((db) => ({
1017
- name: `${db.name} (${db.$id})`,
1018
- value: db.$id,
1019
- })),
1020
- },
1021
- ]);
1022
- trackingDatabaseId = answer.trackingDb;
1023
- }
1024
- }
1025
-
1026
- // Ensure trackingDatabaseId is defined before proceeding
1027
- if (!trackingDatabaseId) {
1028
- throw new Error(
1029
- "Tracking database ID is required for comprehensive backup"
1030
- );
1031
- }
1032
-
1033
- MessageFormatter.info(`Using tracking database: ${trackingDatabaseId}`, {
1034
- prefix: "Backup",
1035
- });
1036
-
1037
- // Create adapter for backup tracking
1038
- const { adapter } = await AdapterFactory.create({
1039
- appwriteEndpoint: controller.config!.appwriteEndpoint,
1040
- appwriteProject: controller.config!.appwriteProject,
1041
- appwriteKey: controller.config!.appwriteKey,
1042
- sessionCookie: controller.config!.sessionCookie,
1043
- });
1044
-
1045
- const result = await comprehensiveBackup(
1046
- controller.config!,
1047
- controller.database!,
1048
- controller.storage!,
1049
- adapter,
1050
- {
1051
- trackingDatabaseId,
1052
- backupFormat: parsedArgv.backupFormat || "zip",
1053
- parallelDownloads: parsedArgv.parallelDownloads || 10,
1054
- onProgress: (message) => {
1055
- MessageFormatter.info(message, { prefix: "Backup" });
1056
- },
1057
- }
1058
- );
1059
-
1060
- operationStats.comprehensiveBackup = 1;
1061
- operationStats.databasesBackedUp = result.databaseBackups.length;
1062
- operationStats.bucketsBackedUp = result.bucketBackups.length;
1063
- operationStats.totalBackupSize = result.totalSizeBytes;
1064
-
1065
- if (result.status === "completed") {
1066
- MessageFormatter.success(
1067
- `Comprehensive backup completed successfully (ID: ${result.backupId})`,
1068
- { prefix: "Backup" }
1069
- );
1070
- } else if (result.status === "partial") {
1071
- MessageFormatter.warning(
1072
- `Comprehensive backup completed with errors (ID: ${result.backupId})`,
1073
- { prefix: "Backup" }
1074
- );
1075
- result.errors.forEach((err) =>
1076
- MessageFormatter.warning(err, { prefix: "Backup" })
1077
- );
1078
- } else {
1079
- MessageFormatter.error(
1080
- `Comprehensive backup failed (ID: ${result.backupId})`,
1081
- undefined,
1082
- { prefix: "Backup" }
1083
- );
1084
- result.errors.forEach((err) =>
1085
- MessageFormatter.error(err, undefined, { prefix: "Backup" })
1086
- );
1087
- }
1088
- }
1089
-
1090
- if (options.doBackup && options.databases) {
1091
- MessageFormatter.info(
1092
- `Creating backups for ${options.databases.length} database(s) in ${parsedArgv.backupFormat} format`,
1093
- { prefix: "Backup" }
1094
- );
1095
- for (const db of options.databases) {
1096
- await controller.backupDatabase(db, parsedArgv.backupFormat || "json");
1097
- }
1098
- operationStats.backups = options.databases.length;
1099
- MessageFormatter.success(
1100
- `Backup completed for ${options.databases.length} database(s)`,
1101
- { prefix: "Backup" }
1102
- );
1103
- }
1104
-
1105
- if (
1106
- options.wipeDatabase ||
1107
- options.wipeDocumentStorage ||
1108
- options.wipeUsers ||
1109
- options.wipeCollections
1110
- ) {
1111
- // Confirm destructive operations
1112
- const databaseNames = options.databases?.map((db) => db.name) || [];
1113
- const confirmed = await ConfirmationDialogs.confirmDatabaseWipe(
1114
- databaseNames,
1115
- {
1116
- includeStorage: options.wipeDocumentStorage,
1117
- includeUsers: options.wipeUsers,
1118
- }
1119
- );
1120
-
1121
- if (!confirmed) {
1122
- MessageFormatter.info("Operation cancelled by user", { prefix: "CLI" });
1123
- return;
1124
- }
1125
-
1126
- let wipeStats = { databases: 0, collections: 0, users: 0, buckets: 0 };
1127
-
1128
- if (parsedArgv.wipe === "all") {
1129
- if (options.databases) {
1130
- for (const db of options.databases) {
1131
- await controller.wipeDatabase(db, true); // true to wipe associated buckets
1132
- }
1133
- wipeStats.databases = options.databases.length;
1134
- }
1135
- await controller.wipeUsers();
1136
- wipeStats.users = 1;
1137
- } else if (parsedArgv.wipe === "docs") {
1138
- if (options.databases) {
1139
- for (const db of options.databases) {
1140
- await controller.wipeBucketFromDatabase(db);
1141
- }
1142
- wipeStats.databases = options.databases.length;
1143
- }
1144
- if (parsedArgv.bucketIds) {
1145
- const bucketIds = parsedArgv.bucketIds.split(",");
1146
- for (const bucketId of bucketIds) {
1147
- await controller.wipeDocumentStorage(bucketId);
1148
- }
1149
- wipeStats.buckets = bucketIds.length;
1150
- }
1151
- } else if (parsedArgv.wipe === "users") {
1152
- await controller.wipeUsers();
1153
- wipeStats.users = 1;
1154
- }
1155
-
1156
- // Handle specific collection wipes
1157
- if (options.wipeCollections && options.databases) {
1158
- for (const db of options.databases) {
1159
- const dbCollections = await fetchAllCollections(
1160
- db.$id,
1161
- controller.database!
1162
- );
1163
- const collectionsToWipe = dbCollections.filter((c) =>
1164
- options.collections!.includes(c.$id)
1165
- );
1166
-
1167
- // Confirm collection wipe
1168
- const collectionNames = collectionsToWipe.map((c) => c.name);
1169
- const collectionConfirmed =
1170
- await ConfirmationDialogs.confirmCollectionWipe(
1171
- db.name,
1172
- collectionNames
1173
- );
1174
-
1175
- if (collectionConfirmed) {
1176
- for (const collection of collectionsToWipe) {
1177
- await controller.wipeCollection(db, collection);
1178
- }
1179
- wipeStats.collections += collectionsToWipe.length;
1180
- }
1181
- }
1182
- }
1183
-
1184
- // Show wipe operation summary
1185
- if (
1186
- wipeStats.databases > 0 ||
1187
- wipeStats.collections > 0 ||
1188
- wipeStats.users > 0 ||
1189
- wipeStats.buckets > 0
1190
- ) {
1191
- operationStats.wipedDatabases = wipeStats.databases;
1192
- operationStats.wipedCollections = wipeStats.collections;
1193
- operationStats.wipedUsers = wipeStats.users;
1194
- operationStats.wipedBuckets = wipeStats.buckets;
1195
- }
1196
- }
1197
-
1198
- if (parsedArgv.push) {
1199
- await controller.init();
1200
- if (!controller.database || !controller.config) {
1201
- MessageFormatter.error("Database or config not initialized", undefined, { prefix: "Push" });
1202
- return;
1203
- }
1204
-
1205
- // Fetch available DBs
1206
- const availableDatabases = await fetchAllDatabases(controller.database);
1207
- if (availableDatabases.length === 0) {
1208
- MessageFormatter.warning("No databases found in remote project", { prefix: "Push" });
1209
- return;
1210
- }
1211
-
1212
- // Determine selected DBs
1213
- let selectedDbIds: string[] = [];
1214
- if (parsedArgv.dbIds) {
1215
- selectedDbIds = parsedArgv.dbIds.split(/[,\s]+/).filter(Boolean);
1216
- } else {
1217
- selectedDbIds = await SelectionDialogs.selectDatabases(
1218
- availableDatabases,
1219
- controller.config.databases || [],
1220
- { showSelectAll: false, allowNewOnly: false, defaultSelected: [] }
1221
- );
1222
- }
1223
-
1224
- if (selectedDbIds.length === 0) {
1225
- MessageFormatter.warning("No databases selected for push", { prefix: "Push" });
1226
- return;
1227
- }
1228
-
1229
- // Build DatabaseSelection[] with tableIds per DB
1230
- const databaseSelections: DatabaseSelection[] = [];
1231
- const allConfigItems = [
1232
- ...(controller.config.collections || []),
1233
- ...(controller.config.tables || [])
1234
- ];
1235
- let lastSelectedTableIds: string[] | null = null;
1236
-
1237
- for (const dbId of selectedDbIds) {
1238
- const db = availableDatabases.find(d => d.$id === dbId);
1239
- if (!db) continue;
1240
-
1241
- // Filter config items eligible for this DB according to databaseId/databaseIds rule
1242
- const eligibleConfigItems = (allConfigItems as any[]).filter(item => {
1243
- const one = item.databaseId as string | undefined;
1244
- const many = item.databaseIds as string[] | undefined;
1245
- if (Array.isArray(many) && many.length > 0) return many.includes(dbId);
1246
- if (one) return one === dbId;
1247
- return true; // eligible everywhere if unspecified
1248
- });
1249
-
1250
- // Fetch available tables from remote for status/context
1251
- const availableTables = await fetchAllCollections(dbId, controller.database);
1252
- const remoteTableIds = new Set(availableTables.map(table => table.$id));
1253
- const localItems = eligibleConfigItems;
1254
- const localItemIds = localItems.map(item => item.$id || (item as any).id || (item as any).tableId || item.name);
1255
- const localNewItems = localItems.filter(item => {
1256
- const itemId = item.$id || (item as any).id || (item as any).tableId || item.name;
1257
- return !remoteTableIds.has(itemId);
1258
- });
1259
- const localNewIds = localNewItems.map(item => item.$id || (item as any).id || (item as any).tableId || item.name);
1260
-
1261
- // Determine selected table IDs
1262
- let selectedTableIds: string[] = [];
1263
- if (parsedArgv.collectionIds) {
1264
- // Non-interactive: respect provided table IDs as-is (apply to each selected DB)
1265
- selectedTableIds = parsedArgv.collectionIds.split(/[\,\s]+/).filter(Boolean);
1266
- } else {
1267
- const inquirer = (await import("inquirer")).default;
1268
- const choices: Array<{ name: string; value: string }> = [];
1269
-
1270
- if (lastSelectedTableIds && lastSelectedTableIds.length > 0) {
1271
- choices.push({
1272
- name: `Use same selection as previous (${lastSelectedTableIds.length} items)`,
1273
- value: "same"
1274
- });
1275
- }
1276
-
1277
- if (localItemIds.length > 0) {
1278
- choices.push({
1279
- name: `Select all local items for ${db.name} (${localItemIds.length} items)`,
1280
- value: "all_local"
1281
- });
1282
- }
1283
-
1284
- if (localNewIds.length > 0) {
1285
- choices.push({
1286
- name: `Select only new local items (not on remote) (${localNewIds.length} items)`,
1287
- value: "new_only"
1288
- });
1289
- }
1290
-
1291
- choices.push({
1292
- name: "Manual selection",
1293
- value: "manual"
1294
- });
1295
-
1296
- const { selectionMode } = await inquirer.prompt([
1297
- {
1298
- type: "list",
1299
- name: "selectionMode",
1300
- message: `How do you want to select tables for ${db.name}?`,
1301
- choices,
1302
- default: choices[0]?.value || "manual"
1303
- }
1304
- ]);
1305
-
1306
- if (selectionMode === "same") {
1307
- selectedTableIds = [...(lastSelectedTableIds || [])];
1308
- } else if (selectionMode === "all_local") {
1309
- selectedTableIds = [...localItemIds];
1310
- } else if (selectionMode === "new_only") {
1311
- selectedTableIds = [...localNewIds];
1312
- } else {
1313
- if (localItems.length === 0) {
1314
- MessageFormatter.warning(`No local tables/collections available for ${db.name}`, { prefix: "Push" });
1315
- selectedTableIds = [];
1316
- } else {
1317
- selectedTableIds = await SelectionDialogs.selectTablesForDatabase(
1318
- dbId,
1319
- db.name,
1320
- localItems as any[],
1321
- availableTables as any[],
1322
- { showSelectAll: localItems.length > 1, allowNewOnly: false, defaultSelected: lastSelectedTableIds || [] }
1323
- );
1324
- }
1325
- }
1326
- }
1327
-
1328
- databaseSelections.push({
1329
- databaseId: db.$id,
1330
- databaseName: db.name,
1331
- tableIds: selectedTableIds,
1332
- tableNames: [],
1333
- isNew: false,
1334
- });
1335
- if (!parsedArgv.collectionIds) {
1336
- lastSelectedTableIds = selectedTableIds;
1337
- }
1338
- }
1339
-
1340
- if (databaseSelections.every(sel => sel.tableIds.length === 0)) {
1341
- MessageFormatter.warning("No tables/collections selected for push", { prefix: "Push" });
1342
- return;
1343
- }
1344
-
1345
- const pushSummary: Record<string, string | number | string[]> = {
1346
- databases: databaseSelections.length,
1347
- collections: databaseSelections.reduce((sum, s) => sum + s.tableIds.length, 0),
1348
- details: databaseSelections.map(s => `${s.databaseId}: ${s.tableIds.length} items`),
1349
- };
1350
- // Skip confirmation if both dbIds and collectionIds are provided (non-interactive)
1351
- if (!(parsedArgv.dbIds && parsedArgv.collectionIds)) {
1352
- const confirmed = await ConfirmationDialogs.showOperationSummary('Push', pushSummary, { confirmationRequired: true });
1353
- if (!confirmed) {
1354
- MessageFormatter.info("Push operation cancelled", { prefix: "Push" });
1355
- return;
1356
- }
1357
- }
1358
-
1359
- await controller.selectivePush(databaseSelections, []);
1360
- operationStats.pushedDatabases = databaseSelections.length;
1361
- operationStats.pushedCollections = databaseSelections.reduce((sum, s) => sum + s.tableIds.length, 0);
1362
- } else if (parsedArgv.sync) {
1363
- // Enhanced SYNC: Pull from remote with intelligent configuration detection
1364
- if (parsedArgv.autoSync) {
1365
- // Legacy behavior: sync everything without prompts
1366
- MessageFormatter.info("Using auto-sync mode (legacy behavior)", { prefix: "Sync" });
1367
- const databases =
1368
- options.databases || (await fetchAllDatabases(controller.database!));
1369
- await controller.synchronizeConfigurations(databases);
1370
- operationStats.syncedDatabases = databases.length;
1371
- } else {
1372
- // Enhanced sync flow with selection dialogs
1373
- const syncResult = await performEnhancedSync(controller, parsedArgv);
1374
- if (syncResult) {
1375
- operationStats.syncedDatabases = syncResult.databases.length;
1376
- operationStats.syncedCollections = syncResult.totalTables;
1377
- operationStats.syncedBuckets = syncResult.buckets.length;
1378
- }
1379
- }
1380
- }
1381
-
1382
- if (options.generateSchemas) {
1383
- await controller.generateSchemas();
1384
- operationStats.generatedSchemas = 1;
1385
- }
1386
-
1387
- if (options.importData) {
1388
- await controller.importData(options);
1389
- operationStats.importCompleted = 1;
1390
- }
1391
-
1392
- if (parsedArgv.transfer) {
1393
- const isRemote = !!parsedArgv.remoteEndpoint;
1394
- let fromDb, toDb: Models.Database | undefined;
1395
- let targetDatabases: Databases | undefined;
1396
- let targetStorage: Storage | undefined;
1397
-
1398
- // Only fetch databases if database IDs are provided
1399
- if (parsedArgv.fromDbId && parsedArgv.toDbId) {
1400
- MessageFormatter.info(
1401
- `Starting database transfer from ${parsedArgv.fromDbId} to ${parsedArgv.toDbId}`,
1402
- { prefix: "Transfer" }
1403
- );
1404
- fromDb = (
1405
- await controller.getDatabasesByIds([parsedArgv.fromDbId])
1406
- )?.[0];
1407
- if (!fromDb) {
1408
- MessageFormatter.error("Source database not found", undefined, {
1409
- prefix: "Transfer",
1410
- });
1411
- return;
1412
- }
1413
- if (isRemote) {
1414
- if (
1415
- !parsedArgv.remoteEndpoint ||
1416
- !parsedArgv.remoteProjectId ||
1417
- !parsedArgv.remoteApiKey
1418
- ) {
1419
- throw new Error("Remote transfer details are missing");
1420
- }
1421
- const remoteClient = getClient(
1422
- parsedArgv.remoteEndpoint,
1423
- parsedArgv.remoteProjectId,
1424
- parsedArgv.remoteApiKey
1425
- );
1426
- targetDatabases = new Databases(remoteClient);
1427
- targetStorage = new Storage(remoteClient);
1428
- const remoteDbs = await fetchAllDatabases(targetDatabases);
1429
- toDb = remoteDbs.find((db) => db.$id === parsedArgv.toDbId);
1430
- if (!toDb) {
1431
- MessageFormatter.error("Target database not found", undefined, {
1432
- prefix: "Transfer",
1433
- });
1434
- return;
1435
- }
1436
- } else {
1437
- toDb = (await controller.getDatabasesByIds([parsedArgv.toDbId]))?.[0];
1438
- if (!toDb) {
1439
- MessageFormatter.error("Target database not found", undefined, {
1440
- prefix: "Transfer",
1441
- });
1442
- return;
1443
- }
1444
- }
1445
-
1446
- if (!fromDb || !toDb) {
1447
- MessageFormatter.error(
1448
- "Source or target database not found",
1449
- undefined,
1450
- { prefix: "Transfer" }
1451
- );
1452
- return;
1453
- }
1454
- }
1455
-
1456
- // Handle storage setup
1457
- let sourceBucket, targetBucket;
1458
- if (parsedArgv.fromBucketId) {
1459
- sourceBucket = await controller.storage?.getBucket(
1460
- parsedArgv.fromBucketId
1461
- );
1462
- }
1463
- if (parsedArgv.toBucketId) {
1464
- if (isRemote) {
1465
- if (!targetStorage) {
1466
- const remoteClient = getClient(
1467
- parsedArgv.remoteEndpoint!,
1468
- parsedArgv.remoteProjectId!,
1469
- parsedArgv.remoteApiKey!
1470
- );
1471
- targetStorage = new Storage(remoteClient);
1472
- }
1473
- targetBucket = await targetStorage?.getBucket(parsedArgv.toBucketId);
1474
- } else {
1475
- targetBucket = await controller.storage?.getBucket(
1476
- parsedArgv.toBucketId
1477
- );
1478
- }
1479
- }
1480
-
1481
- // Validate that at least one transfer type is specified
1482
- if (!fromDb && !sourceBucket && !options.transferUsers) {
1483
- throw new Error("No source database or bucket specified for transfer");
1484
- }
1485
-
1486
- const transferOptions: TransferOptions = {
1487
- isRemote,
1488
- fromDb,
1489
- targetDb: toDb,
1490
- transferEndpoint: parsedArgv.remoteEndpoint,
1491
- transferProject: parsedArgv.remoteProjectId,
1492
- transferKey: parsedArgv.remoteApiKey,
1493
- sourceBucket: sourceBucket,
1494
- targetBucket: targetBucket,
1495
- transferUsers: options.transferUsers,
1496
- };
1497
-
1498
- await controller.transferData(transferOptions);
1499
- operationStats.transfers = 1;
1500
- }
1501
-
1502
- // Show final operation summary if any operations were performed
1503
- if (Object.keys(operationStats).length > 0) {
1504
- const duration = Date.now() - startTime;
1505
- MessageFormatter.operationSummary(
1506
- "CLI Operations",
1507
- operationStats,
1508
- duration
1509
- );
1510
- }
1511
- }
1512
- }
1513
-
1514
- main().catch((error) => {
1515
- MessageFormatter.error("CLI execution failed", error, { prefix: "CLI" });
1516
- process.exit(1);
1517
- });