Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update '_remove' method #364

Merged
merged 2 commits into from
Jan 8, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
44 changes: 23 additions & 21 deletions lib/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,14 +224,14 @@ class Service extends AdapterService {

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

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

// Handle case where data might be a mongoose model
if (typeof data.toObject === 'function') {
Expand Down Expand Up @@ -268,7 +268,7 @@ class Service extends AdapterService {
const { query: { $populate } = {} } = params;
// Create a new query that re-queries all ids that
// were originally changed
const updatedQuery = (idList.length && id === null) ? { [this.id]: { $in: idList } } : params.query;
const updatedQuery = { [this.id]: { $in: idList } };
const findParams = Object.assign({}, params, {
paginate: false,
query: $populate ? Object.assign(updatedQuery, { $populate }) : updatedQuery
Expand All @@ -286,6 +286,9 @@ class Service extends AdapterService {
if (options.writeResult) {
return writeResult;
}
if ('upserted' in writeResult) {
return this._getOrFind(id, Object.assign({}, params, { query: { [this.id]: { $in: writeResult.upserted.map(doc => doc._id) } } }, { paginate: false }));
}
return this._getOrFind(id, findParams);
});
}).then(select(params, this.id)).catch(errorHandler);
Expand All @@ -301,33 +304,32 @@ class Service extends AdapterService {
query.collation = params.collation;
}

if (id !== null) {
query.$and = (query.$and || []).concat({ [this.id]: id });

return this.Model.findOneAndDelete(query, params.mongoose).lean(this.lean)
.exec().then(result => {
if (result === null) {
throw new errors.NotFound(`No record found for id '${id}'`, query);
}

return result;
}).then(select(params, this.id)).catch(errorHandler);
}

const findParams = Object.assign({}, params, {
paginate: false,
query
});

if (id !== null) {
query.$and = (query.$and || []).concat({ [this.id]: id });
}

// NOTE (EK): First fetch the record(s) so that we can return
// it/them when we delete it/them.
return this._getOrFind(id, findParams).then(data =>
this.Model.deleteMany(query, params.mongoose)
return this._getOrFind(id, findParams).then((data) => {
if (id !== null) {
return this.Model.deleteOne(query, params.mongoose)
.lean(this.lean)
.exec()
.then(() => data)
.then(select(params, this.id));
}

return this.Model.deleteMany(query, params.mongoose)
.lean(this.lean)
.exec()
.then(() => data)
.then(select(params, this.id))
).catch(errorHandler);
.then(select(params, this.id));
}).catch(errorHandler);
}
}

Expand Down