Skip to content

Commit 5fbe560

Browse files
Merge pull request #364 from sarkistlt/patch-1
update '_remove' method
2 parents 7aed505 + dd5895e commit 5fbe560

File tree

1 file changed

+16
-17
lines changed

1 file changed

+16
-17
lines changed

lib/service.js

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -304,33 +304,32 @@ class Service extends AdapterService {
304304
query.collation = params.collation;
305305
}
306306

307-
if (id !== null) {
308-
query.$and = (query.$and || []).concat({ [this.id]: id });
309-
310-
return this.Model.findOneAndDelete(query, params.mongoose).lean(this.lean)
311-
.exec().then(result => {
312-
if (result === null) {
313-
throw new errors.NotFound(`No record found for id '${id}'`, query);
314-
}
315-
316-
return result;
317-
}).then(select(params, this.id)).catch(errorHandler);
318-
}
319-
320307
const findParams = Object.assign({}, params, {
321308
paginate: false,
322309
query
323310
});
324311

312+
if (id !== null) {
313+
query.$and = (query.$and || []).concat({ [this.id]: id });
314+
}
315+
325316
// NOTE (EK): First fetch the record(s) so that we can return
326317
// it/them when we delete it/them.
327-
return this._getOrFind(id, findParams).then(data =>
328-
this.Model.deleteMany(query, params.mongoose)
318+
return this._getOrFind(id, findParams).then((data) => {
319+
if (id !== null) {
320+
return this.Model.deleteOne(query, params.mongoose)
321+
.lean(this.lean)
322+
.exec()
323+
.then(() => data)
324+
.then(select(params, this.id));
325+
}
326+
327+
return this.Model.deleteMany(query, params.mongoose)
329328
.lean(this.lean)
330329
.exec()
331330
.then(() => data)
332-
.then(select(params, this.id))
333-
).catch(errorHandler);
331+
.then(select(params, this.id));
332+
}).catch(errorHandler);
334333
}
335334
}
336335

0 commit comments

Comments
 (0)