Skip to content

Commit

Permalink
feat: removeAsync
Browse files Browse the repository at this point in the history
- `removeAsync` for Client and Server
- `removeAsync` returns `Promise<number>`
- `remove` now returns `error, number`
  • Loading branch information
dr-dimitru committed Feb 4, 2025
1 parent 61e8250 commit 90a4a3f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 20 deletions.
24 changes: 22 additions & 2 deletions client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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>} 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;
}
}

/*
Expand Down
36 changes: 18 additions & 18 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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!');
};


Expand Down Expand Up @@ -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<FilesCollection>} Instance
* @returns {Promise<Number>} number of matched and removed files/records
*/
async removeAsync(selector) {
this._debug(`[FilesCollection] [removeAsync(${JSON.stringify(selector)})]`);
Expand All @@ -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;
}

/**
Expand Down

0 comments on commit 90a4a3f

Please sign in to comment.