From a7343ba72cbdd7d4796baad1eab94964005ad64e Mon Sep 17 00:00:00 2001 From: DaddyWarbucks Date: Mon, 14 Nov 2022 13:53:33 -0600 Subject: [PATCH] Update remove method --- lib/index.js | 51 ++++++++++++++++++++++++++++++++++----------------- 1 file changed, 34 insertions(+), 17 deletions(-) diff --git a/lib/index.js b/lib/index.js index a034d5e..0274b4b 100644 --- a/lib/index.js +++ b/lib/index.js @@ -279,28 +279,45 @@ class Service extends AdapterService { } _remove (id, params = {}) { - const opts = Object.assign({ raw: this.raw }, params); - const where = Object.assign({}, this.filterQuery(params).query); + const Model = this.applyScope(params); - if (id !== null) { - where[this.Op.and] = { [this.id]: id }; + if (params.$returning === false) { + params = { + ...params, + query: { + ...params.query, + $select: [this.id] + } + }; + } else if (params.query && params.query.$select) { + params = { + ...params, + query: { + ...params.query, + $select: [...params.query.$select, this.id] + } + }; } - const options = Object.assign({}, { where }, params.sequelize); + return this._getOrFind(id, params).then(results => { + const items = Array.isArray(results) ? results : [results]; + const idList = items.map(item => item[this.id]); - const Model = this.applyScope(params); + const seqOptions = Object.assign( + { raw: this.raw }, + params.sequelize, + { where: { [this.id]: { [this.Op.in]: idList } } } + ); - if (params.$returning !== false) { - return this._getOrFind(id, opts).then(data => { - return Model.destroy(options).then(() => data); - }) - .then(select(params, this.id)) - .catch(utils.errorHandler); - } else { - return Model.destroy(options).then(() => Promise.resolve([])) - .then(select(params, this.id)) - .catch(utils.errorHandler); - } + return Model.destroy(seqOptions) + .then(() => { + if (params.$returning === false) { + return Promise.resolve([]); + } else { + return results; + } + }); + }).then(select(params, this.id)).catch(utils.errorHandler); } }