appwrite-utils-cli 0.10.84 → 0.10.86

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -150,6 +150,8 @@ This updated CLI ensures that developers have robust tools at their fingertips t
150
150
 
151
151
  ## Changelog
152
152
 
153
+ - 0.10.86: Fixed `selectCollections` not always filtering by `databaseId`
154
+ - 0.10.85: Added logging to `wipeCollection`
153
155
  - 0.10.83: Actually fixed the import oops
154
156
  - 0.10.82: Fixed the `lodash` import, replaced with `es-toolkit`
155
157
  - 0.10.81: Fixed `wipeCollection` -- it wasn't properly deleting all files in a loop
@@ -3,7 +3,7 @@ import { nameToIdMapping, processQueue } from "../migrations/queue.js";
3
3
  import { createUpdateCollectionAttributes } from "./attributes.js";
4
4
  import { createOrUpdateIndexes } from "./indexes.js";
5
5
  import { SchemaGenerator } from "../migrations/schemaStrings.js";
6
- import { isNull, isUndefined, isNil, isPlainObject, isString, isJSONValue, } from "es-toolkit";
6
+ import { isNull, isUndefined, isNil, isPlainObject, isString, isJSONValue, chunk, } from "es-toolkit";
7
7
  import { delay, tryAwaitWithRetry } from "../utils/helperFunctions.js";
8
8
  export const documentExists = async (db, dbId, targetCollectionId, toCreateObject) => {
9
9
  const collection = await db.getCollection(dbId, targetCollectionId);
@@ -102,11 +102,16 @@ async function wipeDocumentsFromCollection(database, databaseId, collectionId) {
102
102
  docsResponse.documents.length >= 1000
103
103
  ? docsResponse.documents[docsResponse.documents.length - 1].$id
104
104
  : undefined;
105
+ if (totalDocuments % 10000 === 0) {
106
+ console.log(`Found ${totalDocuments} documents...`);
107
+ }
105
108
  }
106
109
  console.log(`Found ${totalDocuments} documents to delete`);
107
110
  const maxStackSize = 50; // Reduced batch size
108
- for (let i = 0; i < documents.length; i += maxStackSize) {
109
- const batch = documents.slice(i, i + maxStackSize);
111
+ const docBatches = chunk(documents, maxStackSize);
112
+ const quarterBatchSize = Math.ceil(docBatches.length / 4);
113
+ for (let i = 0; i < docBatches.length; i++) {
114
+ const batch = docBatches[i];
110
115
  const deletePromises = batch.map(async (doc) => {
111
116
  try {
112
117
  await tryAwaitWithRetry(async () => database.deleteDocument(databaseId, collectionId, doc.$id));
@@ -119,8 +124,13 @@ async function wipeDocumentsFromCollection(database, databaseId, collectionId) {
119
124
  }
120
125
  });
121
126
  await Promise.all(deletePromises);
122
- await delay(100); // Increased delay between batches
123
- console.log(`Deleted batch of ${batch.length} documents (${i + batch.length}/${totalDocuments})`);
127
+ await delay(50); // Increased delay between batches
128
+ // Log at 25%, 50%, 75% and 100% completion
129
+ if ((i + 1) % quarterBatchSize === 0 || i === docBatches.length - 1) {
130
+ const percentComplete = Math.round(((i + 1) / docBatches.length) * 100);
131
+ const documentsProcessed = Math.min((i + 1) * maxStackSize, totalDocuments);
132
+ console.log(`Deleted ${documentsProcessed} documents (${percentComplete}% complete)`);
133
+ }
124
134
  }
125
135
  console.log(`Completed deletion of ${totalDocuments} documents from collection ${collectionId}`);
126
136
  }
@@ -172,11 +172,10 @@ export class InteractiveCLI {
172
172
  ]);
173
173
  return selectedDatabases;
174
174
  }
175
- async selectCollections(database, databasesClient, message, multiSelect = true, preferLocal = false) {
175
+ async selectCollections(database, databasesClient, message, multiSelect = true, preferLocal = false, shouldFilterByDatabase = false) {
176
176
  await this.initControllerIfNeeded();
177
177
  const configCollections = this.getLocalCollections();
178
178
  let remoteCollections = [];
179
- let shouldFilterByDatabase = true;
180
179
  const dbExists = await databasesClient.list([
181
180
  Query.equal("name", database.name),
182
181
  ]);
@@ -1053,7 +1052,7 @@ export class InteractiveCLI {
1053
1052
  const databases = await fetchAllDatabases(this.controller.database);
1054
1053
  const selectedDatabases = await this.selectDatabases(databases, "Select the database(s) containing the collections to wipe:", true);
1055
1054
  for (const database of selectedDatabases) {
1056
- const collections = await this.selectCollections(database, this.controller.database, `Select collections to wipe from ${database.name}:`, true);
1055
+ const collections = await this.selectCollections(database, this.controller.database, `Select collections to wipe from ${database.name}:`, true, undefined, true);
1057
1056
  const { confirm } = await inquirer.prompt([
1058
1057
  {
1059
1058
  type: "confirm",
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "appwrite-utils-cli",
3
3
  "description": "Appwrite Utility Functions to help with database management, data conversion, data import, migrations, and much more. Meant to be used as a CLI tool, I do not recommend installing this in frontend environments.",
4
- "version": "0.10.84",
4
+ "version": "0.10.86",
5
5
  "main": "src/main.ts",
6
6
  "type": "module",
7
7
  "repository": {
@@ -18,6 +18,7 @@ import {
18
18
  isPlainObject,
19
19
  isString,
20
20
  isJSONValue,
21
+ chunk,
21
22
  } from "es-toolkit";
22
23
  import { delay, tryAwaitWithRetry } from "../utils/helperFunctions.js";
23
24
 
@@ -165,13 +166,19 @@ async function wipeDocumentsFromCollection(
165
166
  docsResponse.documents.length >= 1000
166
167
  ? docsResponse.documents[docsResponse.documents.length - 1].$id
167
168
  : undefined;
169
+ if (totalDocuments % 10000 === 0) {
170
+ console.log(`Found ${totalDocuments} documents...`);
171
+ }
168
172
  }
169
173
 
170
174
  console.log(`Found ${totalDocuments} documents to delete`);
171
175
 
172
176
  const maxStackSize = 50; // Reduced batch size
173
- for (let i = 0; i < documents.length; i += maxStackSize) {
174
- const batch = documents.slice(i, i + maxStackSize);
177
+ const docBatches = chunk(documents, maxStackSize);
178
+ const quarterBatchSize = Math.ceil(docBatches.length / 4);
179
+
180
+ for (let i = 0; i < docBatches.length; i++) {
181
+ const batch = docBatches[i];
175
182
  const deletePromises = batch.map(async (doc) => {
176
183
  try {
177
184
  await tryAwaitWithRetry(async () =>
@@ -193,13 +200,19 @@ async function wipeDocumentsFromCollection(
193
200
  });
194
201
 
195
202
  await Promise.all(deletePromises);
196
- await delay(100); // Increased delay between batches
197
-
198
- console.log(
199
- `Deleted batch of ${batch.length} documents (${
200
- i + batch.length
201
- }/${totalDocuments})`
202
- );
203
+ await delay(50); // Increased delay between batches
204
+
205
+ // Log at 25%, 50%, 75% and 100% completion
206
+ if ((i + 1) % quarterBatchSize === 0 || i === docBatches.length - 1) {
207
+ const percentComplete = Math.round(((i + 1) / docBatches.length) * 100);
208
+ const documentsProcessed = Math.min(
209
+ (i + 1) * maxStackSize,
210
+ totalDocuments
211
+ );
212
+ console.log(
213
+ `Deleted ${documentsProcessed} documents (${percentComplete}% complete)`
214
+ );
215
+ }
203
216
  }
204
217
 
205
218
  console.log(
@@ -233,13 +233,13 @@ export class InteractiveCLI {
233
233
  databasesClient: Databases,
234
234
  message: string,
235
235
  multiSelect = true,
236
- preferLocal = false
236
+ preferLocal = false,
237
+ shouldFilterByDatabase = false
237
238
  ): Promise<Models.Collection[]> {
238
239
  await this.initControllerIfNeeded();
239
240
 
240
241
  const configCollections = this.getLocalCollections();
241
242
  let remoteCollections: Models.Collection[] = [];
242
- let shouldFilterByDatabase = true;
243
243
 
244
244
  const dbExists = await databasesClient.list([
245
245
  Query.equal("name", database.name),
@@ -1454,6 +1454,8 @@ export class InteractiveCLI {
1454
1454
  database,
1455
1455
  this.controller!.database,
1456
1456
  `Select collections to wipe from ${database.name}:`,
1457
+ true,
1458
+ undefined,
1457
1459
  true
1458
1460
  );
1459
1461