appwrite-utils-cli 1.5.2 → 1.6.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 (233) hide show
  1. package/CHANGELOG.md +199 -0
  2. package/README.md +251 -29
  3. package/dist/adapters/AdapterFactory.d.ts +10 -3
  4. package/dist/adapters/AdapterFactory.js +213 -17
  5. package/dist/adapters/TablesDBAdapter.js +60 -17
  6. package/dist/backups/operations/bucketBackup.d.ts +19 -0
  7. package/dist/backups/operations/bucketBackup.js +197 -0
  8. package/dist/backups/operations/collectionBackup.d.ts +30 -0
  9. package/dist/backups/operations/collectionBackup.js +201 -0
  10. package/dist/backups/operations/comprehensiveBackup.d.ts +25 -0
  11. package/dist/backups/operations/comprehensiveBackup.js +238 -0
  12. package/dist/backups/schemas/bucketManifest.d.ts +93 -0
  13. package/dist/backups/schemas/bucketManifest.js +33 -0
  14. package/dist/backups/schemas/comprehensiveManifest.d.ts +108 -0
  15. package/dist/backups/schemas/comprehensiveManifest.js +32 -0
  16. package/dist/backups/tracking/centralizedTracking.d.ts +34 -0
  17. package/dist/backups/tracking/centralizedTracking.js +274 -0
  18. package/dist/cli/commands/configCommands.d.ts +8 -0
  19. package/dist/cli/commands/configCommands.js +160 -0
  20. package/dist/cli/commands/databaseCommands.d.ts +13 -0
  21. package/dist/cli/commands/databaseCommands.js +478 -0
  22. package/dist/cli/commands/functionCommands.d.ts +7 -0
  23. package/dist/cli/commands/functionCommands.js +289 -0
  24. package/dist/cli/commands/schemaCommands.d.ts +7 -0
  25. package/dist/cli/commands/schemaCommands.js +134 -0
  26. package/dist/cli/commands/transferCommands.d.ts +5 -0
  27. package/dist/cli/commands/transferCommands.js +384 -0
  28. package/dist/collections/attributes.d.ts +5 -4
  29. package/dist/collections/attributes.js +539 -246
  30. package/dist/collections/indexes.js +39 -37
  31. package/dist/collections/methods.d.ts +2 -16
  32. package/dist/collections/methods.js +90 -538
  33. package/dist/collections/transferOperations.d.ts +7 -0
  34. package/dist/collections/transferOperations.js +331 -0
  35. package/dist/collections/wipeOperations.d.ts +16 -0
  36. package/dist/collections/wipeOperations.js +328 -0
  37. package/dist/config/configMigration.d.ts +87 -0
  38. package/dist/config/configMigration.js +390 -0
  39. package/dist/config/configValidation.d.ts +66 -0
  40. package/dist/config/configValidation.js +358 -0
  41. package/dist/config/yamlConfig.d.ts +455 -1
  42. package/dist/config/yamlConfig.js +145 -52
  43. package/dist/databases/methods.js +3 -2
  44. package/dist/databases/setup.d.ts +1 -2
  45. package/dist/databases/setup.js +9 -87
  46. package/dist/examples/yamlTerminologyExample.d.ts +42 -0
  47. package/dist/examples/yamlTerminologyExample.js +269 -0
  48. package/dist/functions/deployments.js +11 -10
  49. package/dist/functions/methods.d.ts +1 -1
  50. package/dist/functions/methods.js +5 -4
  51. package/dist/init.js +9 -9
  52. package/dist/interactiveCLI.d.ts +8 -17
  53. package/dist/interactiveCLI.js +181 -1172
  54. package/dist/main.js +364 -21
  55. package/dist/migrations/afterImportActions.js +22 -30
  56. package/dist/migrations/appwriteToX.js +71 -25
  57. package/dist/migrations/dataLoader.js +35 -26
  58. package/dist/migrations/importController.js +29 -30
  59. package/dist/migrations/relationships.js +13 -12
  60. package/dist/migrations/services/ImportOrchestrator.js +16 -19
  61. package/dist/migrations/transfer.js +46 -46
  62. package/dist/migrations/yaml/YamlImportConfigLoader.d.ts +3 -1
  63. package/dist/migrations/yaml/YamlImportConfigLoader.js +6 -3
  64. package/dist/migrations/yaml/YamlImportIntegration.d.ts +9 -3
  65. package/dist/migrations/yaml/YamlImportIntegration.js +22 -11
  66. package/dist/migrations/yaml/generateImportSchemas.d.ts +14 -1
  67. package/dist/migrations/yaml/generateImportSchemas.js +736 -7
  68. package/dist/schemas/authUser.d.ts +1 -1
  69. package/dist/setupController.js +3 -2
  70. package/dist/shared/backupMetadataSchema.d.ts +94 -0
  71. package/dist/shared/backupMetadataSchema.js +38 -0
  72. package/dist/shared/backupTracking.d.ts +18 -0
  73. package/dist/shared/backupTracking.js +176 -0
  74. package/dist/shared/confirmationDialogs.js +15 -15
  75. package/dist/shared/errorUtils.d.ts +54 -0
  76. package/dist/shared/errorUtils.js +95 -0
  77. package/dist/shared/functionManager.js +20 -19
  78. package/dist/shared/indexManager.js +12 -11
  79. package/dist/shared/jsonSchemaGenerator.js +10 -26
  80. package/dist/shared/logging.d.ts +51 -0
  81. package/dist/shared/logging.js +70 -0
  82. package/dist/shared/messageFormatter.d.ts +2 -0
  83. package/dist/shared/messageFormatter.js +10 -0
  84. package/dist/shared/migrationHelpers.d.ts +6 -16
  85. package/dist/shared/migrationHelpers.js +24 -21
  86. package/dist/shared/operationLogger.d.ts +8 -1
  87. package/dist/shared/operationLogger.js +11 -24
  88. package/dist/shared/operationQueue.d.ts +28 -1
  89. package/dist/shared/operationQueue.js +268 -66
  90. package/dist/shared/operationsTable.d.ts +26 -0
  91. package/dist/shared/operationsTable.js +286 -0
  92. package/dist/shared/operationsTableSchema.d.ts +48 -0
  93. package/dist/shared/operationsTableSchema.js +35 -0
  94. package/dist/shared/relationshipExtractor.d.ts +56 -0
  95. package/dist/shared/relationshipExtractor.js +138 -0
  96. package/dist/shared/schemaGenerator.d.ts +19 -1
  97. package/dist/shared/schemaGenerator.js +56 -75
  98. package/dist/storage/backupCompression.d.ts +20 -0
  99. package/dist/storage/backupCompression.js +67 -0
  100. package/dist/storage/methods.d.ts +16 -2
  101. package/dist/storage/methods.js +98 -14
  102. package/dist/users/methods.js +9 -8
  103. package/dist/utils/configDiscovery.d.ts +78 -0
  104. package/dist/utils/configDiscovery.js +430 -0
  105. package/dist/utils/directoryUtils.d.ts +22 -0
  106. package/dist/utils/directoryUtils.js +59 -0
  107. package/dist/utils/getClientFromConfig.d.ts +17 -8
  108. package/dist/utils/getClientFromConfig.js +162 -17
  109. package/dist/utils/helperFunctions.d.ts +16 -2
  110. package/dist/utils/helperFunctions.js +19 -5
  111. package/dist/utils/loadConfigs.d.ts +34 -9
  112. package/dist/utils/loadConfigs.js +236 -316
  113. package/dist/utils/pathResolvers.d.ts +53 -0
  114. package/dist/utils/pathResolvers.js +72 -0
  115. package/dist/utils/projectConfig.d.ts +119 -0
  116. package/dist/utils/projectConfig.js +171 -0
  117. package/dist/utils/retryFailedPromises.js +4 -2
  118. package/dist/utils/sessionAuth.d.ts +48 -0
  119. package/dist/utils/sessionAuth.js +164 -0
  120. package/dist/utils/sessionPreservationExample.d.ts +1666 -0
  121. package/dist/utils/sessionPreservationExample.js +101 -0
  122. package/dist/utils/setupFiles.js +301 -41
  123. package/dist/utils/typeGuards.d.ts +35 -0
  124. package/dist/utils/typeGuards.js +57 -0
  125. package/dist/utils/versionDetection.js +145 -9
  126. package/dist/utils/yamlConverter.d.ts +53 -3
  127. package/dist/utils/yamlConverter.js +232 -13
  128. package/dist/utils/yamlLoader.d.ts +70 -0
  129. package/dist/utils/yamlLoader.js +263 -0
  130. package/dist/utilsController.d.ts +36 -3
  131. package/dist/utilsController.js +186 -56
  132. package/package.json +12 -2
  133. package/src/adapters/AdapterFactory.ts +263 -35
  134. package/src/adapters/TablesDBAdapter.ts +225 -36
  135. package/src/backups/operations/bucketBackup.ts +277 -0
  136. package/src/backups/operations/collectionBackup.ts +310 -0
  137. package/src/backups/operations/comprehensiveBackup.ts +342 -0
  138. package/src/backups/schemas/bucketManifest.ts +78 -0
  139. package/src/backups/schemas/comprehensiveManifest.ts +76 -0
  140. package/src/backups/tracking/centralizedTracking.ts +352 -0
  141. package/src/cli/commands/configCommands.ts +194 -0
  142. package/src/cli/commands/databaseCommands.ts +635 -0
  143. package/src/cli/commands/functionCommands.ts +379 -0
  144. package/src/cli/commands/schemaCommands.ts +163 -0
  145. package/src/cli/commands/transferCommands.ts +457 -0
  146. package/src/collections/attributes.ts +900 -621
  147. package/src/collections/attributes.ts.backup +1555 -0
  148. package/src/collections/indexes.ts +116 -114
  149. package/src/collections/methods.ts +295 -968
  150. package/src/collections/transferOperations.ts +516 -0
  151. package/src/collections/wipeOperations.ts +501 -0
  152. package/src/config/README.md +274 -0
  153. package/src/config/configMigration.ts +575 -0
  154. package/src/config/configValidation.ts +445 -0
  155. package/src/config/yamlConfig.ts +168 -55
  156. package/src/databases/methods.ts +3 -2
  157. package/src/databases/setup.ts +11 -138
  158. package/src/examples/yamlTerminologyExample.ts +341 -0
  159. package/src/functions/deployments.ts +14 -12
  160. package/src/functions/methods.ts +11 -11
  161. package/src/functions/templates/hono-typescript/README.md +286 -0
  162. package/src/functions/templates/hono-typescript/package.json +26 -0
  163. package/src/functions/templates/hono-typescript/src/adapters/request.ts +74 -0
  164. package/src/functions/templates/hono-typescript/src/adapters/response.ts +106 -0
  165. package/src/functions/templates/hono-typescript/src/app.ts +180 -0
  166. package/src/functions/templates/hono-typescript/src/context.ts +103 -0
  167. package/src/functions/templates/hono-typescript/src/index.ts +54 -0
  168. package/src/functions/templates/hono-typescript/src/middleware/appwrite.ts +119 -0
  169. package/src/functions/templates/hono-typescript/tsconfig.json +20 -0
  170. package/src/functions/templates/typescript-node/package.json +2 -1
  171. package/src/functions/templates/typescript-node/src/context.ts +103 -0
  172. package/src/functions/templates/typescript-node/src/index.ts +18 -12
  173. package/src/functions/templates/uv/pyproject.toml +1 -0
  174. package/src/functions/templates/uv/src/context.py +125 -0
  175. package/src/functions/templates/uv/src/index.py +35 -5
  176. package/src/init.ts +9 -11
  177. package/src/interactiveCLI.ts +278 -1596
  178. package/src/main.ts +418 -24
  179. package/src/migrations/afterImportActions.ts +71 -44
  180. package/src/migrations/appwriteToX.ts +100 -34
  181. package/src/migrations/dataLoader.ts +48 -34
  182. package/src/migrations/importController.ts +44 -39
  183. package/src/migrations/relationships.ts +28 -18
  184. package/src/migrations/services/ImportOrchestrator.ts +24 -27
  185. package/src/migrations/transfer.ts +159 -121
  186. package/src/migrations/yaml/YamlImportConfigLoader.ts +11 -4
  187. package/src/migrations/yaml/YamlImportIntegration.ts +47 -20
  188. package/src/migrations/yaml/generateImportSchemas.ts +751 -12
  189. package/src/setupController.ts +3 -2
  190. package/src/shared/backupMetadataSchema.ts +93 -0
  191. package/src/shared/backupTracking.ts +211 -0
  192. package/src/shared/confirmationDialogs.ts +19 -19
  193. package/src/shared/errorUtils.ts +110 -0
  194. package/src/shared/functionManager.ts +21 -20
  195. package/src/shared/indexManager.ts +12 -11
  196. package/src/shared/jsonSchemaGenerator.ts +38 -52
  197. package/src/shared/logging.ts +75 -0
  198. package/src/shared/messageFormatter.ts +14 -1
  199. package/src/shared/migrationHelpers.ts +45 -38
  200. package/src/shared/operationLogger.ts +11 -36
  201. package/src/shared/operationQueue.ts +322 -93
  202. package/src/shared/operationsTable.ts +338 -0
  203. package/src/shared/operationsTableSchema.ts +60 -0
  204. package/src/shared/relationshipExtractor.ts +214 -0
  205. package/src/shared/schemaGenerator.ts +179 -219
  206. package/src/storage/backupCompression.ts +88 -0
  207. package/src/storage/methods.ts +131 -34
  208. package/src/users/methods.ts +11 -9
  209. package/src/utils/configDiscovery.ts +502 -0
  210. package/src/utils/directoryUtils.ts +61 -0
  211. package/src/utils/getClientFromConfig.ts +205 -22
  212. package/src/utils/helperFunctions.ts +23 -5
  213. package/src/utils/loadConfigs.ts +313 -345
  214. package/src/utils/pathResolvers.ts +81 -0
  215. package/src/utils/projectConfig.ts +299 -0
  216. package/src/utils/retryFailedPromises.ts +4 -2
  217. package/src/utils/sessionAuth.ts +230 -0
  218. package/src/utils/setupFiles.ts +322 -54
  219. package/src/utils/typeGuards.ts +65 -0
  220. package/src/utils/versionDetection.ts +218 -64
  221. package/src/utils/yamlConverter.ts +296 -13
  222. package/src/utils/yamlLoader.ts +364 -0
  223. package/src/utilsController.ts +314 -110
  224. package/tests/README.md +497 -0
  225. package/tests/adapters/AdapterFactory.test.ts +277 -0
  226. package/tests/integration/syncOperations.test.ts +463 -0
  227. package/tests/jest.config.js +25 -0
  228. package/tests/migration/configMigration.test.ts +546 -0
  229. package/tests/setup.ts +62 -0
  230. package/tests/testUtils.ts +340 -0
  231. package/tests/utils/loadConfigs.test.ts +350 -0
  232. package/tests/validation/configValidation.test.ts +412 -0
  233. package/src/utils/schemaStrings.ts +0 -517
@@ -0,0 +1,328 @@
1
+ import { Databases, Query, } from "node-appwrite";
2
+ import { tryAwaitWithRetry } from "../utils/helperFunctions.js";
3
+ import { MessageFormatter } from "../shared/messageFormatter.js";
4
+ import { ProgressManager } from "../shared/progressManager.js";
5
+ import { isRetryableError, isBulkNotSupportedError, isCriticalError } from "../shared/errorUtils.js";
6
+ import { delay } from "../utils/helperFunctions.js";
7
+ import { chunk } from "es-toolkit";
8
+ import pLimit from "p-limit";
9
+ import { fetchAllCollections } from "./methods.js";
10
+ /**
11
+ * Optimized streaming deletion of all documents from a collection
12
+ * Uses memory-efficient pagination instead of loading all documents into memory
13
+ */
14
+ async function wipeDocumentsFromCollection(database, databaseId, collectionId) {
15
+ try {
16
+ // Use streaming deletion pattern - fetch and delete in batches without accumulating
17
+ const FETCH_BATCH_SIZE = 1000; // How many to fetch per query
18
+ const DELETE_BATCH_SIZE = 200; // How many to delete concurrently
19
+ const MAX_CONCURRENT_DELETIONS = 10; // Concurrent deletion operations
20
+ let totalDeleted = 0;
21
+ let cursor;
22
+ let hasMoreDocuments = true;
23
+ MessageFormatter.info("Starting optimized document deletion...", { prefix: "Wipe" });
24
+ // Create progress tracker (we'll update the total as we discover more documents)
25
+ const progress = ProgressManager.create(`delete-${collectionId}`, 1, // Start with 1, will update as we go
26
+ { title: "Deleting documents" });
27
+ while (hasMoreDocuments) {
28
+ // Fetch next batch of documents
29
+ const queries = [Query.limit(FETCH_BATCH_SIZE)];
30
+ if (cursor) {
31
+ queries.push(Query.cursorAfter(cursor));
32
+ }
33
+ const response = await database.listDocuments(databaseId, collectionId, queries);
34
+ const documents = response.documents;
35
+ if (documents.length === 0) {
36
+ hasMoreDocuments = false;
37
+ break;
38
+ }
39
+ // Update progress total as we discover more documents
40
+ if (documents.length === FETCH_BATCH_SIZE) {
41
+ // There might be more documents, update progress total
42
+ progress.setTotal(totalDeleted + documents.length + 1000); // Estimate more
43
+ }
44
+ MessageFormatter.progress(`Processing batch: ${documents.length} documents (${totalDeleted + documents.length} total so far)`, { prefix: "Wipe" });
45
+ // Delete this batch using optimized concurrent deletion
46
+ const documentBatches = chunk(documents, DELETE_BATCH_SIZE);
47
+ const limit = pLimit(MAX_CONCURRENT_DELETIONS);
48
+ const deletePromises = documentBatches.map((batch) => limit(async () => {
49
+ const batchDeletePromises = batch.map(async (doc) => {
50
+ try {
51
+ await tryAwaitWithRetry(async () => database.deleteDocument(databaseId, collectionId, doc.$id));
52
+ totalDeleted++;
53
+ progress.update(totalDeleted);
54
+ }
55
+ catch (error) {
56
+ const errorMessage = error.message || String(error);
57
+ // Enhanced error handling for document deletion
58
+ if (errorMessage.includes("Document with the requested ID could not be found")) {
59
+ // Document already deleted, skip silently
60
+ totalDeleted++;
61
+ progress.update(totalDeleted);
62
+ }
63
+ else if (isCriticalError(errorMessage)) {
64
+ // Critical error, log and rethrow to stop operation
65
+ MessageFormatter.error(`Critical error deleting document ${doc.$id}: ${errorMessage}`, error, { prefix: "Wipe" });
66
+ throw error;
67
+ }
68
+ else if (isRetryableError(errorMessage)) {
69
+ // Retryable error, will be handled by tryAwaitWithRetry
70
+ MessageFormatter.progress(`Retryable error for document ${doc.$id}, will retry`, { prefix: "Wipe" });
71
+ totalDeleted++;
72
+ progress.update(totalDeleted);
73
+ }
74
+ else {
75
+ // Other non-critical errors, log but continue
76
+ MessageFormatter.error(`Failed to delete document ${doc.$id}: ${errorMessage}`, error, { prefix: "Wipe" });
77
+ totalDeleted++;
78
+ progress.update(totalDeleted);
79
+ }
80
+ }
81
+ });
82
+ await Promise.all(batchDeletePromises);
83
+ }));
84
+ await Promise.all(deletePromises);
85
+ // Set up cursor for next iteration
86
+ if (documents.length < FETCH_BATCH_SIZE) {
87
+ hasMoreDocuments = false;
88
+ }
89
+ else {
90
+ cursor = documents[documents.length - 1].$id;
91
+ }
92
+ // Small delay between fetch cycles to be respectful to the API
93
+ await delay(10);
94
+ }
95
+ // Update final progress total
96
+ progress.setTotal(totalDeleted);
97
+ progress.stop();
98
+ if (totalDeleted === 0) {
99
+ MessageFormatter.info("No documents found to delete", { prefix: "Wipe" });
100
+ }
101
+ else {
102
+ MessageFormatter.success(`Successfully deleted ${totalDeleted} documents from collection ${collectionId}`, { prefix: "Wipe" });
103
+ }
104
+ }
105
+ catch (error) {
106
+ MessageFormatter.error(`Error wiping documents from collection ${collectionId}`, error instanceof Error ? error : new Error(String(error)), { prefix: "Wipe" });
107
+ throw error;
108
+ }
109
+ }
110
+ export const wipeDatabase = async (database, databaseId) => {
111
+ MessageFormatter.info(`Wiping database: ${databaseId}`, { prefix: "Wipe" });
112
+ const existingCollections = await fetchAllCollections(databaseId, database);
113
+ let collectionsDeleted = [];
114
+ if (existingCollections.length === 0) {
115
+ MessageFormatter.info("No collections to delete", { prefix: "Wipe" });
116
+ return collectionsDeleted;
117
+ }
118
+ const progress = ProgressManager.create(`wipe-db-${databaseId}`, existingCollections.length, { title: "Deleting collections" });
119
+ let processed = 0;
120
+ for (const { $id: collectionId, name: name } of existingCollections) {
121
+ MessageFormatter.progress(`Deleting collection: ${collectionId}`, { prefix: "Wipe" });
122
+ collectionsDeleted.push({
123
+ collectionId: collectionId,
124
+ collectionName: name,
125
+ });
126
+ tryAwaitWithRetry(async () => await database.deleteCollection(databaseId, collectionId)); // Try to delete the collection and ignore errors if it doesn't exist or if it's already being deleted
127
+ processed++;
128
+ progress.update(processed);
129
+ await delay(100);
130
+ }
131
+ progress.stop();
132
+ MessageFormatter.success(`Deleted ${collectionsDeleted.length} collections from database`, { prefix: "Wipe" });
133
+ return collectionsDeleted;
134
+ };
135
+ export const wipeCollection = async (database, databaseId, collectionId) => {
136
+ const collections = await database.listCollections(databaseId, [
137
+ Query.equal("$id", collectionId),
138
+ ]);
139
+ if (collections.total === 0) {
140
+ MessageFormatter.warning(`Collection ${collectionId} not found`, { prefix: "Wipe" });
141
+ return;
142
+ }
143
+ const collection = collections.collections[0];
144
+ await wipeDocumentsFromCollection(database, databaseId, collection.$id);
145
+ };
146
+ // TablesDB helpers for wiping
147
+ export const wipeAllTables = async (adapter, databaseId) => {
148
+ MessageFormatter.info(`Wiping tables in database: ${databaseId}`, { prefix: 'Wipe' });
149
+ const res = await adapter.listTables({ databaseId, queries: [Query.limit(500)] });
150
+ const tables = res.tables || [];
151
+ const deleted = [];
152
+ const progress = ProgressManager.create(`wipe-db-${databaseId}`, tables.length, { title: 'Deleting tables' });
153
+ let processed = 0;
154
+ for (const t of tables) {
155
+ try {
156
+ await adapter.deleteTable({ databaseId, tableId: t.$id });
157
+ deleted.push({ tableId: t.$id, tableName: t.name });
158
+ }
159
+ catch (e) {
160
+ MessageFormatter.error(`Failed deleting table ${t.$id}`, e instanceof Error ? e : new Error(String(e)), { prefix: 'Wipe' });
161
+ }
162
+ processed++;
163
+ progress.update(processed);
164
+ await delay(100);
165
+ }
166
+ progress.stop();
167
+ return deleted;
168
+ };
169
+ /**
170
+ * Optimized streaming deletion of all rows from a table
171
+ * Uses bulk deletion when available, falls back to optimized individual deletion
172
+ */
173
+ export const wipeTableRows = async (adapter, databaseId, tableId) => {
174
+ try {
175
+ // Configuration for optimized deletion
176
+ const FETCH_BATCH_SIZE = 1000; // How many to fetch per query
177
+ const BULK_DELETE_BATCH_SIZE = 500; // How many to bulk delete at once
178
+ const INDIVIDUAL_DELETE_BATCH_SIZE = 200; // For fallback individual deletion
179
+ const MAX_CONCURRENT_OPERATIONS = 10; // Concurrent bulk/individual operations
180
+ let totalDeleted = 0;
181
+ let cursor;
182
+ let hasMoreRows = true;
183
+ MessageFormatter.info("Starting optimized table row deletion...", { prefix: "Wipe" });
184
+ // Create progress tracker (we'll update the total as we discover more rows)
185
+ const progress = ProgressManager.create(`delete-${tableId}`, 1, // Start with 1, will update as we go
186
+ { title: "Deleting table rows" });
187
+ while (hasMoreRows) {
188
+ // Fetch next batch of rows
189
+ const queries = [Query.limit(FETCH_BATCH_SIZE)];
190
+ if (cursor) {
191
+ queries.push(Query.cursorAfter(cursor));
192
+ }
193
+ const response = await adapter.listRows({ databaseId, tableId, queries });
194
+ const rows = response.rows || [];
195
+ if (rows.length === 0) {
196
+ hasMoreRows = false;
197
+ break;
198
+ }
199
+ // Update progress total as we discover more rows
200
+ if (rows.length === FETCH_BATCH_SIZE) {
201
+ // There might be more rows, update progress total
202
+ progress.setTotal(totalDeleted + rows.length + 1000); // Estimate more
203
+ }
204
+ MessageFormatter.progress(`Processing batch: ${rows.length} rows (${totalDeleted + rows.length} total so far)`, { prefix: "Wipe" });
205
+ // Try to use bulk deletion first, fall back to individual deletion
206
+ const rowIds = rows.map((row) => row.$id);
207
+ // Check if bulk deletion is available and try it first
208
+ if (adapter.bulkDeleteRows) {
209
+ try {
210
+ // Attempt bulk deletion (available in TablesDB)
211
+ await tryBulkDeletion(adapter, databaseId, tableId, rowIds, BULK_DELETE_BATCH_SIZE, MAX_CONCURRENT_OPERATIONS);
212
+ totalDeleted += rows.length;
213
+ progress.update(totalDeleted);
214
+ }
215
+ catch (bulkError) {
216
+ // Enhanced error handling: categorize the error and decide on fallback strategy
217
+ const errorMessage = bulkError instanceof Error ? bulkError.message : String(bulkError);
218
+ if (isRetryableError(errorMessage)) {
219
+ MessageFormatter.progress(`Bulk deletion encountered retryable error, retrying with individual deletion for ${rows.length} rows`, { prefix: "Wipe" });
220
+ }
221
+ else if (isBulkNotSupportedError(errorMessage)) {
222
+ MessageFormatter.progress(`Bulk deletion not supported by server, switching to individual deletion for ${rows.length} rows`, { prefix: "Wipe" });
223
+ }
224
+ else {
225
+ MessageFormatter.progress(`Bulk deletion failed (${errorMessage}), falling back to individual deletion for ${rows.length} rows`, { prefix: "Wipe" });
226
+ }
227
+ await tryIndividualDeletion(adapter, databaseId, tableId, rows, INDIVIDUAL_DELETE_BATCH_SIZE, MAX_CONCURRENT_OPERATIONS, progress, totalDeleted);
228
+ totalDeleted += rows.length;
229
+ }
230
+ }
231
+ else {
232
+ // Bulk deletion not available, use optimized individual deletion
233
+ MessageFormatter.progress(`Using individual deletion for ${rows.length} rows (bulk deletion not available)`, { prefix: "Wipe" });
234
+ await tryIndividualDeletion(adapter, databaseId, tableId, rows, INDIVIDUAL_DELETE_BATCH_SIZE, MAX_CONCURRENT_OPERATIONS, progress, totalDeleted);
235
+ totalDeleted += rows.length;
236
+ }
237
+ // Set up cursor for next iteration
238
+ if (rows.length < FETCH_BATCH_SIZE) {
239
+ hasMoreRows = false;
240
+ }
241
+ else {
242
+ cursor = rows[rows.length - 1].$id;
243
+ }
244
+ // Small delay between fetch cycles to be respectful to the API
245
+ await delay(10);
246
+ }
247
+ // Update final progress total
248
+ progress.setTotal(totalDeleted);
249
+ progress.stop();
250
+ if (totalDeleted === 0) {
251
+ MessageFormatter.info("No rows found to delete", { prefix: "Wipe" });
252
+ }
253
+ else {
254
+ MessageFormatter.success(`Successfully deleted ${totalDeleted} rows from table ${tableId}`, { prefix: "Wipe" });
255
+ }
256
+ }
257
+ catch (error) {
258
+ MessageFormatter.error(`Error wiping rows from table ${tableId}`, error instanceof Error ? error : new Error(String(error)), { prefix: "Wipe" });
259
+ throw error;
260
+ }
261
+ };
262
+ /**
263
+ * Helper function to attempt bulk deletion of row IDs
264
+ */
265
+ async function tryBulkDeletion(adapter, databaseId, tableId, rowIds, batchSize, maxConcurrent) {
266
+ if (!adapter.bulkDeleteRows) {
267
+ throw new Error("Bulk deletion not available on this adapter");
268
+ }
269
+ const limit = pLimit(maxConcurrent);
270
+ const batches = chunk(rowIds, batchSize);
271
+ const deletePromises = batches.map((batch) => limit(async () => {
272
+ try {
273
+ await tryAwaitWithRetry(async () => adapter.bulkDeleteRows({ databaseId, tableId, rowIds: batch }));
274
+ }
275
+ catch (error) {
276
+ const errorMessage = error.message || String(error);
277
+ // Enhanced error handling for bulk deletion
278
+ if (isCriticalError(errorMessage)) {
279
+ MessageFormatter.error(`Critical error in bulk deletion batch: ${errorMessage}`, error, { prefix: "Wipe" });
280
+ throw error;
281
+ }
282
+ else {
283
+ // For non-critical errors in bulk deletion, re-throw to trigger fallback
284
+ throw new Error(`Bulk deletion batch failed: ${errorMessage}`);
285
+ }
286
+ }
287
+ }));
288
+ await Promise.all(deletePromises);
289
+ }
290
+ /**
291
+ * Helper function for fallback individual deletion
292
+ */
293
+ async function tryIndividualDeletion(adapter, databaseId, tableId, rows, batchSize, maxConcurrent, progress, baseDeleted) {
294
+ const limit = pLimit(maxConcurrent);
295
+ const batches = chunk(rows, batchSize);
296
+ let processedInBatch = 0;
297
+ const deletePromises = batches.map((batch) => limit(async () => {
298
+ const batchDeletePromises = batch.map(async (row) => {
299
+ try {
300
+ await tryAwaitWithRetry(async () => adapter.deleteRow({ databaseId, tableId, id: row.$id }));
301
+ }
302
+ catch (error) {
303
+ const errorMessage = error.message || String(error);
304
+ // Enhanced error handling for row deletion
305
+ if (errorMessage.includes("Row with the requested ID could not be found")) {
306
+ // Row already deleted, skip silently
307
+ }
308
+ else if (isCriticalError(errorMessage)) {
309
+ // Critical error, log and rethrow to stop operation
310
+ MessageFormatter.error(`Critical error deleting row ${row.$id}: ${errorMessage}`, error, { prefix: "Wipe" });
311
+ throw error;
312
+ }
313
+ else if (isRetryableError(errorMessage)) {
314
+ // Retryable error, will be handled by tryAwaitWithRetry
315
+ MessageFormatter.progress(`Retryable error for row ${row.$id}, will retry`, { prefix: "Wipe" });
316
+ }
317
+ else {
318
+ // Other non-critical errors, log but continue
319
+ MessageFormatter.error(`Failed to delete row ${row.$id}: ${errorMessage}`, error, { prefix: "Wipe" });
320
+ }
321
+ }
322
+ processedInBatch++;
323
+ progress.update(baseDeleted + processedInBatch);
324
+ });
325
+ await Promise.all(batchDeletePromises);
326
+ }));
327
+ await Promise.all(deletePromises);
328
+ }
@@ -0,0 +1,87 @@
1
+ import { type AppwriteConfig, type CollectionCreate, type TableCreate, type TableCreateInput } from "appwrite-utils";
2
+ import { type ValidationResult } from "./configValidation.js";
3
+ /**
4
+ * Migration strategy types
5
+ */
6
+ export type MigrationStrategy = "full_migration" | "dual_format" | "incremental" | "tables_only";
7
+ /**
8
+ * Migration plan detailing what will be changed
9
+ */
10
+ export interface MigrationPlan {
11
+ strategy: MigrationStrategy;
12
+ collectionsToMigrate: CollectionMigrationItem[];
13
+ collectionsToKeep: CollectionCreate[];
14
+ tablesToCreate: TableCreate[];
15
+ expectedChanges: MigrationChange[];
16
+ estimatedComplexity: "low" | "medium" | "high";
17
+ warnings: string[];
18
+ recommendations: string[];
19
+ }
20
+ /**
21
+ * Individual collection migration details
22
+ */
23
+ export interface CollectionMigrationItem {
24
+ collection: CollectionCreate;
25
+ index: number;
26
+ newTable: TableCreate;
27
+ changes: string[];
28
+ warnings: string[];
29
+ }
30
+ /**
31
+ * Migration change description
32
+ */
33
+ export interface MigrationChange {
34
+ type: "add" | "remove" | "modify" | "rename";
35
+ description: string;
36
+ impact: "low" | "medium" | "high";
37
+ location: string;
38
+ }
39
+ /**
40
+ * Migration execution result
41
+ */
42
+ export interface MigrationResult {
43
+ success: boolean;
44
+ newConfig: AppwriteConfig;
45
+ changes: MigrationChange[];
46
+ validation: ValidationResult;
47
+ warnings: string[];
48
+ errors: string[];
49
+ }
50
+ /**
51
+ * Migration options
52
+ */
53
+ export interface MigrationOptions {
54
+ preserveOriginal?: boolean;
55
+ validateResult?: boolean;
56
+ dryRun?: boolean;
57
+ backupConfig?: boolean;
58
+ targetDirectory?: string;
59
+ }
60
+ /**
61
+ * Creates a migration plan for converting collections to tables
62
+ */
63
+ export declare function createMigrationPlan(config: AppwriteConfig, strategy?: MigrationStrategy, specificCollections?: string[]): MigrationPlan;
64
+ /**
65
+ * Executes a migration plan
66
+ */
67
+ export declare function executeMigrationPlan(config: AppwriteConfig, plan: MigrationPlan, options?: MigrationOptions): MigrationResult;
68
+ /**
69
+ * Converts a single collection to table format
70
+ */
71
+ export declare function convertCollectionToTable(collection: CollectionCreate): TableCreateInput;
72
+ /**
73
+ * Utility function to migrate collections to tables with simple interface
74
+ */
75
+ export declare function migrateCollectionsToTables(config: AppwriteConfig, options?: {
76
+ strategy?: MigrationStrategy;
77
+ specificCollections?: string[];
78
+ validateResult?: boolean;
79
+ dryRun?: boolean;
80
+ }): MigrationResult;
81
+ /**
82
+ * Saves migration results to files
83
+ */
84
+ export declare function saveMigrationResult(result: MigrationResult, outputPath: string, options?: {
85
+ createBackup?: boolean;
86
+ originalConfigPath?: string;
87
+ }): Promise<void>;