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
@@ -1,449 +0,0 @@
1
- import {
2
- Databases,
3
- Query,
4
- type Models,
5
- } from "node-appwrite";
6
- import type { DatabaseAdapter } from "appwrite-utils-helpers";
7
- import { tryAwaitWithRetry } from "appwrite-utils-helpers";
8
- import { MessageFormatter, isRetryableError, isCriticalError } from "appwrite-utils-helpers";
9
- import { ProgressManager } from "../shared/progressManager.js";
10
- import { delay } from "appwrite-utils-helpers";
11
- import { chunk } from "es-toolkit";
12
- import pLimit from "p-limit";
13
- import { fetchAllCollections } from "./methods.js";
14
-
15
- /**
16
- * Optimized streaming deletion of all documents from a collection
17
- * Uses memory-efficient pagination instead of loading all documents into memory
18
- */
19
- async function wipeDocumentsFromCollection(
20
- database: Databases,
21
- databaseId: string,
22
- collectionId: string
23
- ) {
24
- try {
25
- // Use streaming deletion pattern - fetch and delete in batches without accumulating
26
- const FETCH_BATCH_SIZE = 1000; // How many to fetch per query
27
- const DELETE_BATCH_SIZE = 200; // How many to delete concurrently
28
- const MAX_CONCURRENT_DELETIONS = 10; // Concurrent deletion operations
29
-
30
- let totalDeleted = 0;
31
- let cursor: string | undefined;
32
- let hasMoreDocuments = true;
33
-
34
- MessageFormatter.info("Starting optimized document deletion...", { prefix: "Wipe" });
35
-
36
- // Create progress tracker (we'll update the total as we discover more documents)
37
- const progress = ProgressManager.create(
38
- `delete-${collectionId}`,
39
- 1, // Start with 1, will update as we go
40
- { title: "Deleting documents" }
41
- );
42
-
43
- while (hasMoreDocuments) {
44
- // Fetch next batch of documents
45
- const queries = [Query.limit(FETCH_BATCH_SIZE)];
46
- if (cursor) {
47
- queries.push(Query.cursorAfter(cursor));
48
- }
49
-
50
- const response = await database.listDocuments(databaseId, collectionId, queries);
51
- const documents = response.documents;
52
-
53
- if (documents.length === 0) {
54
- hasMoreDocuments = false;
55
- break;
56
- }
57
-
58
- // Update progress total as we discover more documents
59
- if (documents.length === FETCH_BATCH_SIZE) {
60
- // There might be more documents, update progress total
61
- progress.setTotal(totalDeleted + documents.length + 1000); // Estimate more
62
- }
63
-
64
- MessageFormatter.progress(
65
- `Processing batch: ${documents.length} documents (${totalDeleted + documents.length} total so far)`,
66
- { prefix: "Wipe" }
67
- );
68
-
69
- // Delete this batch using optimized concurrent deletion
70
- const documentBatches = chunk(documents, DELETE_BATCH_SIZE);
71
- const limit = pLimit(MAX_CONCURRENT_DELETIONS);
72
-
73
- const deletePromises = documentBatches.map((batch) =>
74
- limit(async () => {
75
- const batchDeletePromises = batch.map(async (doc) => {
76
- try {
77
- await tryAwaitWithRetry(async () =>
78
- database.deleteDocument(databaseId, collectionId, doc.$id)
79
- );
80
- totalDeleted++;
81
- progress.update(totalDeleted);
82
- } catch (error: any) {
83
- const errorMessage = error.message || String(error);
84
-
85
- // Enhanced error handling for document deletion
86
- if (errorMessage.includes("Document with the requested ID could not be found")) {
87
- // Document already deleted, skip silently
88
- totalDeleted++;
89
- progress.update(totalDeleted);
90
- } else if (isCriticalError(errorMessage)) {
91
- // Critical error, log and rethrow to stop operation
92
- MessageFormatter.error(
93
- `Critical error deleting document ${doc.$id}: ${errorMessage}`,
94
- error,
95
- { prefix: "Wipe" }
96
- );
97
- throw error;
98
- } else if (isRetryableError(errorMessage)) {
99
- // Retryable error, will be handled by tryAwaitWithRetry
100
- MessageFormatter.progress(
101
- `Retryable error for document ${doc.$id}, will retry`,
102
- { prefix: "Wipe" }
103
- );
104
- totalDeleted++;
105
- progress.update(totalDeleted);
106
- } else {
107
- // Other non-critical errors, log but continue
108
- MessageFormatter.error(
109
- `Failed to delete document ${doc.$id}: ${errorMessage}`,
110
- error,
111
- { prefix: "Wipe" }
112
- );
113
- totalDeleted++;
114
- progress.update(totalDeleted);
115
- }
116
- }
117
- });
118
-
119
- await Promise.all(batchDeletePromises);
120
- })
121
- );
122
-
123
- await Promise.all(deletePromises);
124
-
125
- // Set up cursor for next iteration
126
- if (documents.length < FETCH_BATCH_SIZE) {
127
- hasMoreDocuments = false;
128
- } else {
129
- cursor = documents[documents.length - 1].$id;
130
- }
131
-
132
- // Small delay between fetch cycles to be respectful to the API
133
- await delay(10);
134
- }
135
-
136
- // Update final progress total
137
- progress.setTotal(totalDeleted);
138
- progress.stop();
139
-
140
- if (totalDeleted === 0) {
141
- MessageFormatter.info("No documents found to delete", { prefix: "Wipe" });
142
- } else {
143
- MessageFormatter.success(
144
- `Successfully deleted ${totalDeleted} documents from collection ${collectionId}`,
145
- { prefix: "Wipe" }
146
- );
147
- }
148
-
149
- } catch (error) {
150
- MessageFormatter.error(
151
- `Error wiping documents from collection ${collectionId}`,
152
- error instanceof Error ? error : new Error(String(error)),
153
- { prefix: "Wipe" }
154
- );
155
- throw error;
156
- }
157
- }
158
-
159
- export const wipeDatabase = async (
160
- database: Databases,
161
- databaseId: string
162
- ): Promise<{ collectionId: string; collectionName: string }[]> => {
163
- MessageFormatter.info(`Wiping database: ${databaseId}`, { prefix: "Wipe" });
164
- const existingCollections = await fetchAllCollections(databaseId, database);
165
- let collectionsDeleted: { collectionId: string; collectionName: string }[] =
166
- [];
167
-
168
- if (existingCollections.length === 0) {
169
- MessageFormatter.info("No collections to delete", { prefix: "Wipe" });
170
- return collectionsDeleted;
171
- }
172
-
173
- const progress = ProgressManager.create(
174
- `wipe-db-${databaseId}`,
175
- existingCollections.length,
176
- { title: "Deleting collections" }
177
- );
178
-
179
- let processed = 0;
180
- for (const { $id: collectionId, name: name } of existingCollections) {
181
- MessageFormatter.progress(`Deleting collection: ${collectionId}`, { prefix: "Wipe" });
182
- collectionsDeleted.push({
183
- collectionId: collectionId,
184
- collectionName: name,
185
- });
186
- tryAwaitWithRetry(
187
- async () => await database.deleteCollection(databaseId, collectionId)
188
- ); // Try to delete the collection and ignore errors if it doesn't exist or if it's already being deleted
189
- processed++;
190
- progress.update(processed);
191
- await delay(100);
192
- }
193
-
194
- progress.stop();
195
- MessageFormatter.success(`Deleted ${collectionsDeleted.length} collections from database`, { prefix: "Wipe" });
196
- return collectionsDeleted;
197
- };
198
-
199
- export const wipeCollection = async (
200
- database: Databases,
201
- databaseId: string,
202
- collectionId: string
203
- ): Promise<void> => {
204
- const collections = await database.listCollections(databaseId, [
205
- Query.equal("$id", collectionId),
206
- ]);
207
- if (collections.total === 0) {
208
- MessageFormatter.warning(`Collection ${collectionId} not found`, { prefix: "Wipe" });
209
- return;
210
- }
211
- const collection = collections.collections[0];
212
- await wipeDocumentsFromCollection(database, databaseId, collection.$id);
213
- };
214
-
215
- // TablesDB helpers for wiping
216
- export const wipeAllTables = async (
217
- adapter: DatabaseAdapter,
218
- databaseId: string
219
- ): Promise<{ tableId: string; tableName: string }[]> => {
220
- MessageFormatter.info(`Wiping tables in database: ${databaseId}`, { prefix: 'Wipe' });
221
- const res = await adapter.listTables({ databaseId, queries: [Query.limit(500)] });
222
- const tables: any[] = (res as any).tables || [];
223
- const deleted: { tableId: string; tableName: string }[] = [];
224
- const progress = ProgressManager.create(`wipe-db-${databaseId}`, tables.length, { title: 'Deleting tables' });
225
- let processed = 0;
226
- for (const t of tables) {
227
- try {
228
- await adapter.deleteTable({ databaseId, tableId: t.$id });
229
- deleted.push({ tableId: t.$id, tableName: t.name });
230
- } catch (e) {
231
- MessageFormatter.error(`Failed deleting table ${t.$id}`, e instanceof Error ? e : new Error(String(e)), { prefix: 'Wipe' });
232
- }
233
- processed++; progress.update(processed);
234
- await delay(100);
235
- }
236
- progress.stop();
237
- return deleted;
238
- };
239
-
240
- /**
241
- * Optimized deletion of all rows from a table.
242
- * Uses bulk deletion when possible, but falls back to individual row deletion
243
- * for tables with relationship columns (bulk delete not supported for those).
244
- */
245
- export const wipeTableRows = async (
246
- adapter: DatabaseAdapter,
247
- databaseId: string,
248
- tableId: string
249
- ): Promise<void> => {
250
- try {
251
- // Check if the table has relationship columns — bulk delete is not supported for those
252
- const tableInfo = await adapter.getTable({ databaseId, tableId });
253
- const columns: any[] = (tableInfo.data as any)?.columns || [];
254
- const hasRelationships = columns.some((col: any) => col.type === "relationship");
255
-
256
- const DELETE_BATCH_SIZE = 250;
257
- let totalDeleted = 0;
258
- let hasMoreRows = true;
259
-
260
- const progress = ProgressManager.create(
261
- `delete-${tableId}`,
262
- 1,
263
- { title: "Deleting table rows" }
264
- );
265
-
266
- if (hasRelationships) {
267
- // ── Relationship table: fetch rows then delete individually ──
268
- MessageFormatter.info(
269
- "Table has relationship columns — using individual row deletion (bulk delete not supported)",
270
- { prefix: "Wipe" }
271
- );
272
-
273
- const FETCH_BATCH_SIZE = 1000;
274
- const MAX_CONCURRENT_DELETES = 25;
275
- const limit = pLimit(MAX_CONCURRENT_DELETES);
276
-
277
- // Pipeline: prefetch the first batch, then overlap fetch+delete
278
- let pendingRows: any[] = [];
279
- let totalDiscovered = 0;
280
-
281
- // Fetch helper — always fetches from the top since we're deleting everything
282
- const fetchBatch = async (): Promise<any[]> => {
283
- const queries: any[] = [Query.limit(FETCH_BATCH_SIZE)];
284
- const response = await tryAwaitWithRetry(async () =>
285
- adapter.listRows({ databaseId, tableId, queries })
286
- );
287
- return (response as any).rows || (response as any).data || [];
288
- };
289
-
290
- // Kick off the first fetch
291
- let nextFetchPromise: Promise<any[]> | null = fetchBatch();
292
-
293
- while (hasMoreRows) {
294
- // Await the prefetched batch
295
- const rows = nextFetchPromise ? await nextFetchPromise : [];
296
- nextFetchPromise = null;
297
-
298
- if (rows.length === 0) {
299
- hasMoreRows = false;
300
- break;
301
- }
302
-
303
- totalDiscovered += rows.length;
304
- const isLastBatch = rows.length < FETCH_BATCH_SIZE;
305
-
306
- if (!isLastBatch) {
307
- progress.setTotal(totalDiscovered + 1000);
308
- } else {
309
- progress.setTotal(totalDiscovered);
310
- }
311
-
312
- MessageFormatter.progress(
313
- `Fetched ${rows.length} rows (${totalDiscovered} discovered, ${totalDeleted} deleted so far)`,
314
- { prefix: "Wipe" }
315
- );
316
-
317
- // Start deleting this batch — and prefetch the next one concurrently
318
- // (only prefetch if we expect more rows)
319
- if (!isLastBatch) {
320
- // Wait a moment before prefetching so the first few deletes free up API capacity
321
- nextFetchPromise = delay(200).then(() => fetchBatch());
322
- }
323
-
324
- // Delete each row with concurrency limit
325
- const deletePromises = rows.map((row: any) =>
326
- limit(async () => {
327
- try {
328
- await tryAwaitWithRetry(async () =>
329
- adapter.deleteRow({ databaseId, tableId, id: row.$id })
330
- );
331
- totalDeleted++;
332
- progress.update(totalDeleted);
333
- } catch (error: any) {
334
- const errorMessage = error.message || String(error);
335
- if (errorMessage.includes("could not be found")) {
336
- totalDeleted++;
337
- progress.update(totalDeleted);
338
- } else if (isCriticalError(errorMessage)) {
339
- MessageFormatter.error(
340
- `Critical error deleting row ${row.$id}: ${errorMessage}`,
341
- error,
342
- { prefix: "Wipe" }
343
- );
344
- throw error;
345
- } else {
346
- MessageFormatter.error(
347
- `Failed to delete row ${row.$id}: ${errorMessage}`,
348
- error,
349
- { prefix: "Wipe" }
350
- );
351
- totalDeleted++;
352
- progress.update(totalDeleted);
353
- }
354
- }
355
- })
356
- );
357
-
358
- await Promise.all(deletePromises);
359
-
360
- if (isLastBatch) {
361
- hasMoreRows = false;
362
- }
363
-
364
- await delay(10);
365
- }
366
- } else {
367
- // ── No relationships: use fast bulk deletion ──
368
- if (!adapter.bulkDeleteRows) {
369
- MessageFormatter.error(
370
- "Bulk deletion not available for this adapter - wipe operation not supported",
371
- new Error("bulkDeleteRows not available"),
372
- { prefix: "Wipe" }
373
- );
374
- throw new Error("Bulk deletion required for wipe operations");
375
- }
376
-
377
- MessageFormatter.info("Starting optimized table row deletion...", { prefix: "Wipe" });
378
-
379
- while (hasMoreRows) {
380
- try {
381
- const result = await tryAwaitWithRetry(async () =>
382
- adapter.bulkDeleteRows!({
383
- databaseId,
384
- tableId,
385
- rowIds: [],
386
- batchSize: DELETE_BATCH_SIZE
387
- })
388
- );
389
-
390
- const deletedCount = (result as any).total || 0;
391
-
392
- if (deletedCount === 0) {
393
- hasMoreRows = false;
394
- break;
395
- }
396
-
397
- totalDeleted += deletedCount;
398
- progress.setTotal(totalDeleted + 100);
399
- progress.update(totalDeleted);
400
-
401
- MessageFormatter.progress(
402
- `Deleted ${deletedCount} rows (${totalDeleted} total so far)`,
403
- { prefix: "Wipe" }
404
- );
405
-
406
- await delay(10);
407
- } catch (error: any) {
408
- const errorMessage = error.message || String(error);
409
-
410
- if (isCriticalError(errorMessage)) {
411
- MessageFormatter.error(
412
- `Critical error during bulk deletion: ${errorMessage}`,
413
- error,
414
- { prefix: "Wipe" }
415
- );
416
- throw error;
417
- } else {
418
- MessageFormatter.error(
419
- `Error during deletion batch: ${errorMessage}`,
420
- error,
421
- { prefix: "Wipe" }
422
- );
423
- }
424
- }
425
- }
426
- }
427
-
428
- // Update final progress total
429
- progress.setTotal(totalDeleted);
430
- progress.stop();
431
-
432
- if (totalDeleted === 0) {
433
- MessageFormatter.info("No rows found to delete", { prefix: "Wipe" });
434
- } else {
435
- MessageFormatter.success(
436
- `Successfully deleted ${totalDeleted} rows from table ${tableId}`,
437
- { prefix: "Wipe" }
438
- );
439
- }
440
-
441
- } catch (error) {
442
- MessageFormatter.error(
443
- `Error wiping rows from table ${tableId}`,
444
- error instanceof Error ? error : new Error(String(error)),
445
- { prefix: "Wipe" }
446
- );
447
- throw error;
448
- }
449
- };
@@ -1,49 +0,0 @@
1
- import { Databases, Query, type Models } from "node-appwrite";
2
- import { delay, tryAwaitWithRetry } from "appwrite-utils-helpers";
3
- import { fetchAllCollections } from "../collections/methods.js";
4
- import { MessageFormatter } from "appwrite-utils-helpers";
5
-
6
- export const fetchAllDatabases = async (
7
- database: Databases
8
- ): Promise<Models.Database[]> => {
9
- const databases = await tryAwaitWithRetry(
10
- async () => await database.list([Query.limit(25)])
11
- );
12
- const allDatabases = databases.databases;
13
- if (allDatabases.length === 0) return [];
14
- let lastDatabaseId = allDatabases[allDatabases.length - 1].$id;
15
-
16
- while (databases.databases.length === 25) {
17
- const moreDatabases = await database.list([
18
- Query.limit(25),
19
- Query.cursorAfter(lastDatabaseId),
20
- ]);
21
- allDatabases.push(...moreDatabases.databases);
22
- if (moreDatabases.databases.length < 25) break;
23
- lastDatabaseId =
24
- moreDatabases.databases[moreDatabases.databases.length - 1].$id;
25
- }
26
-
27
- return allDatabases;
28
- };
29
-
30
- export const wipeDatabase = async (
31
- database: Databases,
32
- databaseId: string
33
- ): Promise<{ collectionId: string; collectionName: string }[]> => {
34
- MessageFormatter.info(`Wiping database: ${databaseId}`, { prefix: "Database" });
35
- const existingCollections = await fetchAllCollections(databaseId, database);
36
- let collectionsDeleted: { collectionId: string; collectionName: string }[] =
37
- [];
38
-
39
- for (const { $id: collectionId, name } of existingCollections) {
40
- MessageFormatter.info(`Deleting collection: ${collectionId}`, { prefix: "Database" });
41
- collectionsDeleted.push({ collectionId, collectionName: name });
42
- await tryAwaitWithRetry(
43
- async () => await database.deleteCollection(databaseId, collectionId)
44
- );
45
- await delay(100);
46
- }
47
-
48
- return collectionsDeleted;
49
- };
@@ -1,77 +0,0 @@
1
- import { Databases, Query, type Models } from "node-appwrite";
2
- import { tryAwaitWithRetry } from "appwrite-utils-helpers";
3
- import { type AppwriteConfig } from "appwrite-utils";
4
- import { ulid } from "ulidx";
5
- import { MessageFormatter } from "appwrite-utils-helpers";
6
-
7
- export const ensureDatabasesExist = async (config: AppwriteConfig, databasesToEnsure?: Models.Database[]) => {
8
- if (!config.appwriteClient) {
9
- throw new Error("Appwrite client is not initialized in the config");
10
- }
11
- const database = new Databases(config.appwriteClient);
12
- const databasesToCreate = databasesToEnsure || config.databases || [];
13
-
14
- if (!databasesToCreate.length) {
15
- MessageFormatter.info("No databases to create");
16
- return;
17
- }
18
-
19
- const existingDatabases = await tryAwaitWithRetry(
20
- async () => await database.list([Query.limit(500)])
21
- );
22
-
23
- for (const db of databasesToCreate) {
24
- if (!existingDatabases.databases.some((d) => d.name === db.name)) {
25
- await tryAwaitWithRetry(
26
- async () => await database.create(db.$id || ulid(), db.name, true)
27
- );
28
- MessageFormatter.success(`${db.name} database created`);
29
- }
30
- }
31
- };
32
-
33
- export const wipeOtherDatabases = async (
34
- database: Databases,
35
- databasesToKeep: Models.Database[]
36
- ) => {
37
- MessageFormatter.info(`Databases to keep: ${databasesToKeep.map(db => db.name).join(", ")}`);
38
- const allDatabases = await tryAwaitWithRetry(
39
- async () => await database.list([Query.limit(500)])
40
- );
41
- for (const db of allDatabases.databases) {
42
- if (!databasesToKeep.some((d) => d.name === db.name)) {
43
- await tryAwaitWithRetry(async () => await database.delete(db.$id));
44
- MessageFormatter.success(`Deleted database: ${db.name}`);
45
- }
46
- }
47
- };
48
-
49
- export const ensureCollectionsExist = async (
50
- config: AppwriteConfig,
51
- database: Models.Database,
52
- collectionsToEnsure?: Models.Collection[]
53
- ) => {
54
- const databaseClient = new Databases(config.appwriteClient!);
55
- const collectionsToCreate = collectionsToEnsure ||
56
- (config.collections ? config.collections : []);
57
-
58
- const existingCollections = await tryAwaitWithRetry(
59
- async () => await databaseClient.listCollections(database.$id, [Query.limit(500)])
60
- );
61
-
62
- for (const collection of collectionsToCreate) {
63
- if (!existingCollections.collections.some((c) => c.name === collection.name)) {
64
- await tryAwaitWithRetry(
65
- async () => await databaseClient.createCollection(
66
- database.$id,
67
- ulid(),
68
- collection.name,
69
- undefined,
70
- true,
71
- true
72
- )
73
- );
74
- MessageFormatter.success(`${collection.name} collection created in ${database.name}`);
75
- }
76
- }
77
- };