diff --git a/package.json b/package.json index 2fc455c..b2b0288 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "loopback-softdelete-mixin", "version": "0.0.6", "description": "A mixin to automatically generate created and updated Date attributes for loopback Models", - "main": "dist/index.js", + "main": "index.js", "scripts": { "preversion": "npm test", "pretest": "eslint ./src/*.js && gulp babel", diff --git a/src/soft-delete.js b/src/soft-delete.js index f950457..b091dff 100644 --- a/src/soft-delete.js +++ b/src/soft-delete.js @@ -51,12 +51,12 @@ export default (Model, { deletedAt = 'deletedAt', _isDeleted = '_isDeleted', scr Model.prototype.delete = Model.prototype.destroy; // Emulate default scope but with more flexibility. - const queryNonDeleted = {_isDeleted: false}; + const queryNonDeleted = {[_isDeleted]: false}; const _findOrCreate = Model.findOrCreate; Model.findOrCreate = function findOrCreateDeleted(query = {}, ...rest) { if (!query.deleted) { - if (!query.where) { + if (!query.where || Object.keys(query.where).length === 0) { query.where = queryNonDeleted; } else { query.where = { and: [ query.where, queryNonDeleted ] }; @@ -69,7 +69,7 @@ export default (Model, { deletedAt = 'deletedAt', _isDeleted = '_isDeleted', scr const _find = Model.find; Model.find = function findDeleted(query = {}, ...rest) { if (!query.deleted) { - if (!query.where) { + if (!query.where || Object.keys(query.where).length === 0) { query.where = queryNonDeleted; } else { query.where = { and: [ query.where, queryNonDeleted ] }; @@ -82,14 +82,15 @@ export default (Model, { deletedAt = 'deletedAt', _isDeleted = '_isDeleted', scr const _count = Model.count; Model.count = function countDeleted(where = {}, ...rest) { // Because count only receives a 'where', there's nowhere to ask for the deleted entities. - const whereNotDeleted = { and: [ where, queryNonDeleted ] }; + const whereNotDeleted = (!where || Object.keys(where).length === 0) ? queryNonDeleted : { and: [ where, queryNonDeleted ] }; + return _count.call(Model, whereNotDeleted, ...rest); }; const _update = Model.update; Model.update = Model.updateAll = function updateDeleted(where = {}, ...rest) { // Because update/updateAll only receives a 'where', there's nowhere to ask for the deleted entities. - const whereNotDeleted = { and: [ where, queryNonDeleted ] }; + const whereNotDeleted = (!where || Object.keys(where).length === 0) ? queryNonDeleted : { and: [ where, queryNonDeleted ] }; return _update.call(Model, whereNotDeleted, ...rest); }; };