Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
11 changes: 6 additions & 5 deletions src/soft-delete.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ] };
Expand All @@ -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 ] };
Expand All @@ -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);
};
};