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
@@ -0,0 +1,306 @@
1
+ import { Databases, Query, } from "node-appwrite";
2
+ import { tryAwaitWithRetry } from "appwrite-utils-helpers";
3
+ import { MessageFormatter, isRetryableError, isCriticalError } from "appwrite-utils-helpers";
4
+ import { ProgressManager } from "../shared/progressManager.js";
5
+ import { delay } from "appwrite-utils-helpers";
6
+ import { chunk } from "es-toolkit";
7
+ import pLimit from "p-limit";
8
+ import { fetchAllCollections } from "./methods.js";
9
+ /**
10
+ * Optimized streaming deletion of all documents from a collection
11
+ * Uses memory-efficient pagination instead of loading all documents into memory
12
+ */
13
+ async function wipeDocumentsFromCollection(database, databaseId, collectionId) {
14
+ try {
15
+ // Use streaming deletion pattern - fetch and delete in batches without accumulating
16
+ const FETCH_BATCH_SIZE = 1000; // How many to fetch per query
17
+ const DELETE_BATCH_SIZE = 200; // How many to delete concurrently
18
+ const MAX_CONCURRENT_DELETIONS = 10; // Concurrent deletion operations
19
+ let totalDeleted = 0;
20
+ let cursor;
21
+ let hasMoreDocuments = true;
22
+ MessageFormatter.info("Starting optimized document deletion...", { prefix: "Wipe" });
23
+ // Create progress tracker (we'll update the total as we discover more documents)
24
+ const progress = ProgressManager.create(`delete-${collectionId}`, 1, // Start with 1, will update as we go
25
+ { title: "Deleting documents" });
26
+ while (hasMoreDocuments) {
27
+ // Fetch next batch of documents
28
+ const queries = [Query.limit(FETCH_BATCH_SIZE)];
29
+ if (cursor) {
30
+ queries.push(Query.cursorAfter(cursor));
31
+ }
32
+ const response = await database.listDocuments(databaseId, collectionId, queries);
33
+ const documents = response.documents;
34
+ if (documents.length === 0) {
35
+ hasMoreDocuments = false;
36
+ break;
37
+ }
38
+ // Update progress total as we discover more documents
39
+ if (documents.length === FETCH_BATCH_SIZE) {
40
+ // There might be more documents, update progress total
41
+ progress.setTotal(totalDeleted + documents.length + 1000); // Estimate more
42
+ }
43
+ MessageFormatter.progress(`Processing batch: ${documents.length} documents (${totalDeleted + documents.length} total so far)`, { prefix: "Wipe" });
44
+ // Delete this batch using optimized concurrent deletion
45
+ const documentBatches = chunk(documents, DELETE_BATCH_SIZE);
46
+ const limit = pLimit(MAX_CONCURRENT_DELETIONS);
47
+ const deletePromises = documentBatches.map((batch) => limit(async () => {
48
+ const batchDeletePromises = batch.map(async (doc) => {
49
+ try {
50
+ await tryAwaitWithRetry(async () => database.deleteDocument(databaseId, collectionId, doc.$id));
51
+ totalDeleted++;
52
+ progress.update(totalDeleted);
53
+ }
54
+ catch (error) {
55
+ const errorMessage = error.message || String(error);
56
+ // Enhanced error handling for document deletion
57
+ if (errorMessage.includes("Document with the requested ID could not be found")) {
58
+ // Document already deleted, skip silently
59
+ totalDeleted++;
60
+ progress.update(totalDeleted);
61
+ }
62
+ else if (isCriticalError(errorMessage)) {
63
+ // Critical error, log and rethrow to stop operation
64
+ MessageFormatter.error(`Critical error deleting document ${doc.$id}: ${errorMessage}`, error, { prefix: "Wipe" });
65
+ throw error;
66
+ }
67
+ else if (isRetryableError(errorMessage)) {
68
+ // Retryable error, will be handled by tryAwaitWithRetry
69
+ MessageFormatter.progress(`Retryable error for document ${doc.$id}, will retry`, { prefix: "Wipe" });
70
+ totalDeleted++;
71
+ progress.update(totalDeleted);
72
+ }
73
+ else {
74
+ // Other non-critical errors, log but continue
75
+ MessageFormatter.error(`Failed to delete document ${doc.$id}: ${errorMessage}`, error, { prefix: "Wipe" });
76
+ totalDeleted++;
77
+ progress.update(totalDeleted);
78
+ }
79
+ }
80
+ });
81
+ await Promise.all(batchDeletePromises);
82
+ }));
83
+ await Promise.all(deletePromises);
84
+ // Set up cursor for next iteration
85
+ if (documents.length < FETCH_BATCH_SIZE) {
86
+ hasMoreDocuments = false;
87
+ }
88
+ else {
89
+ cursor = documents[documents.length - 1].$id;
90
+ }
91
+ // Small delay between fetch cycles to be respectful to the API
92
+ await delay(10);
93
+ }
94
+ // Update final progress total
95
+ progress.setTotal(totalDeleted);
96
+ progress.stop();
97
+ if (totalDeleted === 0) {
98
+ MessageFormatter.info("No documents found to delete", { prefix: "Wipe" });
99
+ }
100
+ else {
101
+ MessageFormatter.success(`Successfully deleted ${totalDeleted} documents from collection ${collectionId}`, { prefix: "Wipe" });
102
+ }
103
+ }
104
+ catch (error) {
105
+ MessageFormatter.error(`Error wiping documents from collection ${collectionId}`, error instanceof Error ? error : new Error(String(error)), { prefix: "Wipe" });
106
+ throw error;
107
+ }
108
+ }
109
+ export const wipeDatabase = async (database, databaseId) => {
110
+ MessageFormatter.info(`Wiping database: ${databaseId}`, { prefix: "Wipe" });
111
+ const existingCollections = await fetchAllCollections(databaseId, database);
112
+ let collectionsDeleted = [];
113
+ if (existingCollections.length === 0) {
114
+ MessageFormatter.info("No collections to delete", { prefix: "Wipe" });
115
+ return collectionsDeleted;
116
+ }
117
+ const progress = ProgressManager.create(`wipe-db-${databaseId}`, existingCollections.length, { title: "Deleting collections" });
118
+ let processed = 0;
119
+ for (const { $id: collectionId, name: name } of existingCollections) {
120
+ MessageFormatter.progress(`Deleting collection: ${collectionId}`, { prefix: "Wipe" });
121
+ collectionsDeleted.push({
122
+ collectionId: collectionId,
123
+ collectionName: name,
124
+ });
125
+ 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
126
+ processed++;
127
+ progress.update(processed);
128
+ await delay(100);
129
+ }
130
+ progress.stop();
131
+ MessageFormatter.success(`Deleted ${collectionsDeleted.length} collections from database`, { prefix: "Wipe" });
132
+ return collectionsDeleted;
133
+ };
134
+ export const wipeCollection = async (database, databaseId, collectionId) => {
135
+ const collections = await database.listCollections(databaseId, [
136
+ Query.equal("$id", collectionId),
137
+ ]);
138
+ if (collections.total === 0) {
139
+ MessageFormatter.warning(`Collection ${collectionId} not found`, { prefix: "Wipe" });
140
+ return;
141
+ }
142
+ const collection = collections.collections[0];
143
+ await wipeDocumentsFromCollection(database, databaseId, collection.$id);
144
+ };
145
+ // TablesDB helpers for wiping
146
+ export const wipeAllTables = async (adapter, databaseId) => {
147
+ MessageFormatter.info(`Wiping tables in database: ${databaseId}`, { prefix: 'Wipe' });
148
+ const res = await adapter.listTables({ databaseId, queries: [Query.limit(500)] });
149
+ const tables = res.tables || [];
150
+ const deleted = [];
151
+ const progress = ProgressManager.create(`wipe-db-${databaseId}`, tables.length, { title: 'Deleting tables' });
152
+ let processed = 0;
153
+ for (const t of tables) {
154
+ try {
155
+ await adapter.deleteTable({ databaseId, tableId: t.$id });
156
+ deleted.push({ tableId: t.$id, tableName: t.name });
157
+ }
158
+ catch (e) {
159
+ MessageFormatter.error(`Failed deleting table ${t.$id}`, e instanceof Error ? e : new Error(String(e)), { prefix: 'Wipe' });
160
+ }
161
+ processed++;
162
+ progress.update(processed);
163
+ await delay(100);
164
+ }
165
+ progress.stop();
166
+ return deleted;
167
+ };
168
+ /**
169
+ * Optimized deletion of all rows from a table.
170
+ * Uses bulk deletion when possible, but falls back to individual row deletion
171
+ * for tables with relationship columns (bulk delete not supported for those).
172
+ */
173
+ export const wipeTableRows = async (adapter, databaseId, tableId) => {
174
+ try {
175
+ // Check if the table has relationship columns — bulk delete is not supported for those
176
+ const tableInfo = await adapter.getTable({ databaseId, tableId });
177
+ const columns = tableInfo.data?.columns || [];
178
+ const hasRelationships = columns.some((col) => col.type === "relationship");
179
+ const DELETE_BATCH_SIZE = 250;
180
+ let totalDeleted = 0;
181
+ let hasMoreRows = true;
182
+ const progress = ProgressManager.create(`delete-${tableId}`, 1, { title: "Deleting table rows" });
183
+ if (hasRelationships) {
184
+ // ── Relationship table: fetch rows then delete individually ──
185
+ MessageFormatter.info("Table has relationship columns — using individual row deletion (bulk delete not supported)", { prefix: "Wipe" });
186
+ const FETCH_BATCH_SIZE = 1000;
187
+ const MAX_CONCURRENT_DELETES = 25;
188
+ const limit = pLimit(MAX_CONCURRENT_DELETES);
189
+ // Pipeline: prefetch the first batch, then overlap fetch+delete
190
+ let pendingRows = [];
191
+ let totalDiscovered = 0;
192
+ // Fetch helper — always fetches from the top since we're deleting everything
193
+ const fetchBatch = async () => {
194
+ const queries = [Query.limit(FETCH_BATCH_SIZE)];
195
+ const response = await tryAwaitWithRetry(async () => adapter.listRows({ databaseId, tableId, queries }));
196
+ return response.rows || response.data || [];
197
+ };
198
+ // Kick off the first fetch
199
+ let nextFetchPromise = fetchBatch();
200
+ while (hasMoreRows) {
201
+ // Await the prefetched batch
202
+ const rows = nextFetchPromise ? await nextFetchPromise : [];
203
+ nextFetchPromise = null;
204
+ if (rows.length === 0) {
205
+ hasMoreRows = false;
206
+ break;
207
+ }
208
+ totalDiscovered += rows.length;
209
+ const isLastBatch = rows.length < FETCH_BATCH_SIZE;
210
+ if (!isLastBatch) {
211
+ progress.setTotal(totalDiscovered + 1000);
212
+ }
213
+ else {
214
+ progress.setTotal(totalDiscovered);
215
+ }
216
+ MessageFormatter.progress(`Fetched ${rows.length} rows (${totalDiscovered} discovered, ${totalDeleted} deleted so far)`, { prefix: "Wipe" });
217
+ // Start deleting this batch — and prefetch the next one concurrently
218
+ // (only prefetch if we expect more rows)
219
+ if (!isLastBatch) {
220
+ // Wait a moment before prefetching so the first few deletes free up API capacity
221
+ nextFetchPromise = delay(200).then(() => fetchBatch());
222
+ }
223
+ // Delete each row with concurrency limit
224
+ const deletePromises = rows.map((row) => limit(async () => {
225
+ try {
226
+ await tryAwaitWithRetry(async () => adapter.deleteRow({ databaseId, tableId, id: row.$id }));
227
+ totalDeleted++;
228
+ progress.update(totalDeleted);
229
+ }
230
+ catch (error) {
231
+ const errorMessage = error.message || String(error);
232
+ if (errorMessage.includes("could not be found")) {
233
+ totalDeleted++;
234
+ progress.update(totalDeleted);
235
+ }
236
+ else if (isCriticalError(errorMessage)) {
237
+ MessageFormatter.error(`Critical error deleting row ${row.$id}: ${errorMessage}`, error, { prefix: "Wipe" });
238
+ throw error;
239
+ }
240
+ else {
241
+ MessageFormatter.error(`Failed to delete row ${row.$id}: ${errorMessage}`, error, { prefix: "Wipe" });
242
+ totalDeleted++;
243
+ progress.update(totalDeleted);
244
+ }
245
+ }
246
+ }));
247
+ await Promise.all(deletePromises);
248
+ if (isLastBatch) {
249
+ hasMoreRows = false;
250
+ }
251
+ await delay(10);
252
+ }
253
+ }
254
+ else {
255
+ // ── No relationships: use fast bulk deletion ──
256
+ if (!adapter.bulkDeleteRows) {
257
+ MessageFormatter.error("Bulk deletion not available for this adapter - wipe operation not supported", new Error("bulkDeleteRows not available"), { prefix: "Wipe" });
258
+ throw new Error("Bulk deletion required for wipe operations");
259
+ }
260
+ MessageFormatter.info("Starting optimized table row deletion...", { prefix: "Wipe" });
261
+ while (hasMoreRows) {
262
+ try {
263
+ const result = await tryAwaitWithRetry(async () => adapter.bulkDeleteRows({
264
+ databaseId,
265
+ tableId,
266
+ rowIds: [],
267
+ batchSize: DELETE_BATCH_SIZE
268
+ }));
269
+ const deletedCount = result.total || 0;
270
+ if (deletedCount === 0) {
271
+ hasMoreRows = false;
272
+ break;
273
+ }
274
+ totalDeleted += deletedCount;
275
+ progress.setTotal(totalDeleted + 100);
276
+ progress.update(totalDeleted);
277
+ MessageFormatter.progress(`Deleted ${deletedCount} rows (${totalDeleted} total so far)`, { prefix: "Wipe" });
278
+ await delay(10);
279
+ }
280
+ catch (error) {
281
+ const errorMessage = error.message || String(error);
282
+ if (isCriticalError(errorMessage)) {
283
+ MessageFormatter.error(`Critical error during bulk deletion: ${errorMessage}`, error, { prefix: "Wipe" });
284
+ throw error;
285
+ }
286
+ else {
287
+ MessageFormatter.error(`Error during deletion batch: ${errorMessage}`, error, { prefix: "Wipe" });
288
+ }
289
+ }
290
+ }
291
+ }
292
+ // Update final progress total
293
+ progress.setTotal(totalDeleted);
294
+ progress.stop();
295
+ if (totalDeleted === 0) {
296
+ MessageFormatter.info("No rows found to delete", { prefix: "Wipe" });
297
+ }
298
+ else {
299
+ MessageFormatter.success(`Successfully deleted ${totalDeleted} rows from table ${tableId}`, { prefix: "Wipe" });
300
+ }
301
+ }
302
+ catch (error) {
303
+ MessageFormatter.error(`Error wiping rows from table ${tableId}`, error instanceof Error ? error : new Error(String(error)), { prefix: "Wipe" });
304
+ throw error;
305
+ }
306
+ };
@@ -0,0 +1,6 @@
1
+ import { Databases, type Models } from "node-appwrite";
2
+ export declare const fetchAllDatabases: (database: Databases) => Promise<Models.Database[]>;
3
+ export declare const wipeDatabase: (database: Databases, databaseId: string) => Promise<{
4
+ collectionId: string;
5
+ collectionName: string;
6
+ }[]>;
@@ -0,0 +1,35 @@
1
+ import { Databases, Query } 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
+ export const fetchAllDatabases = async (database) => {
6
+ const databases = await tryAwaitWithRetry(async () => await database.list([Query.limit(25)]));
7
+ const allDatabases = databases.databases;
8
+ if (allDatabases.length === 0)
9
+ return [];
10
+ let lastDatabaseId = allDatabases[allDatabases.length - 1].$id;
11
+ while (databases.databases.length === 25) {
12
+ const moreDatabases = await database.list([
13
+ Query.limit(25),
14
+ Query.cursorAfter(lastDatabaseId),
15
+ ]);
16
+ allDatabases.push(...moreDatabases.databases);
17
+ if (moreDatabases.databases.length < 25)
18
+ break;
19
+ lastDatabaseId =
20
+ moreDatabases.databases[moreDatabases.databases.length - 1].$id;
21
+ }
22
+ return allDatabases;
23
+ };
24
+ export const wipeDatabase = async (database, databaseId) => {
25
+ MessageFormatter.info(`Wiping database: ${databaseId}`, { prefix: "Database" });
26
+ const existingCollections = await fetchAllCollections(databaseId, database);
27
+ let collectionsDeleted = [];
28
+ for (const { $id: collectionId, name } of existingCollections) {
29
+ MessageFormatter.info(`Deleting collection: ${collectionId}`, { prefix: "Database" });
30
+ collectionsDeleted.push({ collectionId, collectionName: name });
31
+ await tryAwaitWithRetry(async () => await database.deleteCollection(databaseId, collectionId));
32
+ await delay(100);
33
+ }
34
+ return collectionsDeleted;
35
+ };
@@ -0,0 +1,5 @@
1
+ import { Databases, type Models } from "node-appwrite";
2
+ import { type AppwriteConfig } from "appwrite-utils";
3
+ export declare const ensureDatabasesExist: (config: AppwriteConfig, databasesToEnsure?: Models.Database[]) => Promise<void>;
4
+ export declare const wipeOtherDatabases: (database: Databases, databasesToKeep: Models.Database[]) => Promise<void>;
5
+ export declare const ensureCollectionsExist: (config: AppwriteConfig, database: Models.Database, collectionsToEnsure?: Models.Collection[]) => Promise<void>;
@@ -0,0 +1,45 @@
1
+ import { Databases, Query } from "node-appwrite";
2
+ import { tryAwaitWithRetry } from "appwrite-utils-helpers";
3
+ import {} from "appwrite-utils";
4
+ import { ulid } from "ulidx";
5
+ import { MessageFormatter } from "appwrite-utils-helpers";
6
+ export const ensureDatabasesExist = async (config, databasesToEnsure) => {
7
+ if (!config.appwriteClient) {
8
+ throw new Error("Appwrite client is not initialized in the config");
9
+ }
10
+ const database = new Databases(config.appwriteClient);
11
+ const databasesToCreate = databasesToEnsure || config.databases || [];
12
+ if (!databasesToCreate.length) {
13
+ MessageFormatter.info("No databases to create");
14
+ return;
15
+ }
16
+ const existingDatabases = await tryAwaitWithRetry(async () => await database.list([Query.limit(500)]));
17
+ for (const db of databasesToCreate) {
18
+ if (!existingDatabases.databases.some((d) => d.name === db.name)) {
19
+ await tryAwaitWithRetry(async () => await database.create(db.$id || ulid(), db.name, true));
20
+ MessageFormatter.success(`${db.name} database created`);
21
+ }
22
+ }
23
+ };
24
+ export const wipeOtherDatabases = async (database, databasesToKeep) => {
25
+ MessageFormatter.info(`Databases to keep: ${databasesToKeep.map(db => db.name).join(", ")}`);
26
+ const allDatabases = await tryAwaitWithRetry(async () => await database.list([Query.limit(500)]));
27
+ for (const db of allDatabases.databases) {
28
+ if (!databasesToKeep.some((d) => d.name === db.name)) {
29
+ await tryAwaitWithRetry(async () => await database.delete(db.$id));
30
+ MessageFormatter.success(`Deleted database: ${db.name}`);
31
+ }
32
+ }
33
+ };
34
+ export const ensureCollectionsExist = async (config, database, collectionsToEnsure) => {
35
+ const databaseClient = new Databases(config.appwriteClient);
36
+ const collectionsToCreate = collectionsToEnsure ||
37
+ (config.collections ? config.collections : []);
38
+ const existingCollections = await tryAwaitWithRetry(async () => await databaseClient.listCollections(database.$id, [Query.limit(500)]));
39
+ for (const collection of collectionsToCreate) {
40
+ if (!existingCollections.collections.some((c) => c.name === collection.name)) {
41
+ await tryAwaitWithRetry(async () => await databaseClient.createCollection(database.$id, ulid(), collection.name, undefined, true, true));
42
+ MessageFormatter.success(`${collection.name} collection created in ${database.name}`);
43
+ }
44
+ }
45
+ };
@@ -0,0 +1,42 @@
1
+ /**
2
+ * Example demonstrating YAML import/export with dual terminology support
3
+ *
4
+ * This example shows how to:
5
+ * 1. Generate YAML files for both collections and tables
6
+ * 2. Load and convert between terminologies
7
+ * 3. Validate terminology consistency
8
+ * 4. Migrate between formats
9
+ */
10
+ import { type CollectionCreate } from "appwrite-utils";
11
+ /**
12
+ * Example 1: Generate YAML templates for both collection and table formats
13
+ */
14
+ export declare function generateTemplateExamples(outputDir: string): Promise<void>;
15
+ /**
16
+ * Example 2: Convert existing collection definition to different formats
17
+ */
18
+ export declare function convertCollectionFormats(collection: CollectionCreate): void;
19
+ /**
20
+ * Example 3: Load and process YAML files with mixed terminologies
21
+ */
22
+ export declare function processYamlFiles(baseDir: string): Promise<void>;
23
+ /**
24
+ * Example 4: Migrate between terminologies
25
+ */
26
+ export declare function migrateTerminology(baseDir: string): Promise<void>;
27
+ /**
28
+ * Example 5: Validate terminology consistency
29
+ */
30
+ export declare function validateTerminology(baseDir: string): Promise<void>;
31
+ /**
32
+ * Example 6: Import configuration with terminology support
33
+ */
34
+ export declare function importConfigurationExample(appwriteFolderPath: string): Promise<void>;
35
+ /**
36
+ * Example 7: Manual terminology conversion
37
+ */
38
+ export declare function manualTerminologyConversion(): void;
39
+ /**
40
+ * Main example runner
41
+ */
42
+ export declare function runYamlTerminologyExamples(outputDir: string): Promise<void>;