From 90a4a3f776f0cc054da436f7245e6aaf499980a9 Mon Sep 17 00:00:00 2001 From: "dr.dimitru" Date: Tue, 4 Feb 2025 09:13:50 +0200 Subject: [PATCH] feat: removeAsync - `removeAsync` for Client and Server - `removeAsync` returns `Promise` - `remove` now returns `error, number` --- client.js | 24 ++++++++++++++++++++++-- server.js | 36 ++++++++++++++++++------------------ 2 files changed, 40 insertions(+), 20 deletions(-) diff --git a/client.js b/client.js index 9f17df4..0a15133 100644 --- a/client.js +++ b/client.js @@ -265,11 +265,11 @@ class FilesCollection extends FilesCollectionCore { } /** - * @locus Anywhere + * @locus Client * @memberOf FilesCollection * @name remove * @param {String|Object} selector - Mongo-Style selector (http://docs.meteor.com/api/collections.html#selectors) - * @param {Function} callback - Callback with one `error` argument + * @param {Function} callback - Callback with `error` and `number` arguments * @summary Remove documents from the collection * @returns {FilesCollection} Instance */ @@ -287,6 +287,26 @@ class FilesCollection extends FilesCollectionCore { return this; } + + /** + * @locus Anywhere + * @memberOf FilesCollection + * @name removeAsync + * @param {String|Object} selector - Mongo-Style selector (http://docs.meteor.com/api/collections.html#selectors) + * @summary Remove documents from the collection + * @returns {Promise} number of matched and removed files/records + */ + async removeAsync(selector = {}) { + this._debug(`[FilesCollection] [removeAsync(${JSON.stringify(selector)})]`); + check(selector, Match.OneOf(Object, String)); + + if (this.allowClientCode) { + return await this.ddp.callAsync(this._methodNames._Remove, selector); + } + + this._debug('[FilesCollection] [removeAsync] Run code from client is not allowed!'); + return 0; + } } /* diff --git a/server.js b/server.js index fc0e3a0..e90d57d 100644 --- a/server.js +++ b/server.js @@ -836,14 +836,14 @@ class FilesCollection extends FilesCollectionCore { } const cursor = self.find(selector); - if ((await cursor.countAsync()) > 0) { - self.removeAsync(selector); - return true; + const count = await cursor.countAsync(); + if (count > 0) { + await self.removeAsync(selector); } - throw new Meteor.Error(404, 'Cursor is empty, no files is removed'); - } else { - throw new Meteor.Error(405, '[FilesCollection] [remove] Running code on a client is not allowed!'); + return count; } + + throw new Meteor.Error(405, '[FilesCollection] [remove] Running code on a client is not allowed!'); }; @@ -1585,7 +1585,7 @@ class FilesCollection extends FilesCollectionCore { * @param {String|Object} selector - Mongo-Style selector (http://docs.meteor.com/api/collections.html#selectors) * @throws {Meteor.Error} If cursor is empty * @summary Remove documents from the collection - * @returns {Promise} Instance + * @returns {Promise} number of matched and removed files/records */ async removeAsync(selector) { this._debug(`[FilesCollection] [removeAsync(${JSON.stringify(selector)})]`); @@ -1594,22 +1594,22 @@ class FilesCollection extends FilesCollectionCore { } const files = this.collection.find(selector); - if (await files.countAsync() > 0) { + const count = await files.countAsync(); + if (count > 0) { await files.forEachAsync((file) => { this.unlink(file); }); - } else { - throw new Meteor.Error(404, 'Cursor is empty, no files is removed'); - } - if (this.onAfterRemove) { - const docs = await files.fetchAsync(); - await this.collection.removeAsync(selector); - await this.onAfterRemove(docs); - } else { - await this.collection.removeAsync(selector); + if (this.onAfterRemove) { + const docs = await files.fetchAsync(); + await this.collection.removeAsync(selector); + await this.onAfterRemove(docs); + } else { + await this.collection.removeAsync(selector); + } } - return this; + + return count; } /**