Skip to content

Commit dd5895e

Browse files
authored
remove method
fix for failed test that uses id + query to remove record(s)
1 parent 7d9edeb commit dd5895e

File tree

1 file changed

+23
-24
lines changed

1 file changed

+23
-24
lines changed

lib/service.js

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -224,14 +224,14 @@ class Service extends AdapterService {
224224

225225
_patch (id, data, params = {}) {
226226
const { query } = this.filterQuery(params);
227-
const mapIds = data => data.map(current => current[this.id]);
227+
const mapIds = data => Array.isArray(data) ? data.map(current => current[this.id]) : [data[this.id]];
228228

229229
// By default we will just query for the one id. For multi patch
230230
// we create a list of the ids of all items that will be changed
231231
// to re-query them after the update
232-
const ids = id === null ? this._find(Object.assign({}, params, {
232+
const ids = this._getOrFind(id, Object.assign({}, params, {
233233
paginate: false
234-
})).then(mapIds) : Promise.resolve([id]);
234+
})).then(mapIds);
235235

236236
// Handle case where data might be a mongoose model
237237
if (typeof data.toObject === 'function') {
@@ -268,7 +268,7 @@ class Service extends AdapterService {
268268
const { query: { $populate } = {} } = params;
269269
// Create a new query that re-queries all ids that
270270
// were originally changed
271-
const updatedQuery = (idList.length && id === null) ? { [this.id]: { $in: idList } } : params.query;
271+
const updatedQuery = { [this.id]: { $in: idList } };
272272
const findParams = Object.assign({}, params, {
273273
paginate: false,
274274
query: $populate ? Object.assign(updatedQuery, { $populate }) : updatedQuery
@@ -286,6 +286,9 @@ class Service extends AdapterService {
286286
if (options.writeResult) {
287287
return writeResult;
288288
}
289+
if ('upserted' in writeResult) {
290+
return this._getOrFind(id, Object.assign({}, params, { query: { [this.id]: { $in: writeResult.upserted.map(doc => doc._id) } } }, { paginate: false }));
291+
}
289292
return this._getOrFind(id, findParams);
290293
});
291294
}).then(select(params, this.id)).catch(errorHandler);
@@ -301,36 +304,32 @@ class Service extends AdapterService {
301304
query.collation = params.collation;
302305
}
303306

304-
if (id !== null) {
305-
query.$and = (query.$and || []).concat({ [this.id]: id });
306-
307-
return this.Model.findById(id)
308-
.then((result) => {
309-
if (result === null) {
310-
throw new errors.NotFound(`No record found for id '${id}'`, query);
311-
}
312-
return this.Model.deleteOne(query, params.mongoose).lean(this.lean)
313-
.exec()
314-
.then(() => result)
315-
.then(select(params, this.id));
316-
})
317-
.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)