Skip to content
This repository has been archived by the owner on Mar 15, 2023. It is now read-only.

Properly patch multiple #63

Merged
merged 2 commits into from
Sep 21, 2016
Merged
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 @@ -69,7 +69,7 @@
"feathers": "^2.0.0-pre.4",
"feathers-hooks": "^1.1.0",
"feathers-rest": "^1.2.2",
"feathers-service-tests": "^0.7.0",
"feathers-service-tests": "^0.8.0",
"feathers-socketio": "^1.3.3",
"jshint": "^2.8.0",
"mocha": "^3.0.0",
Expand Down
30 changes: 21 additions & 9 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class Service {

_multiOptions(id, params) {
let query = Object.assign({}, params.query);
let options = Object.assign({ multi: true }, params.options);
let options = Object.assign({ multi: true }, params.mongodb || params.options);

if (id !== null) {
options.multi = false;
Expand Down Expand Up @@ -167,12 +167,25 @@ class Service {
}

patch(id, data, params) {
let { query, options } = this._multiOptions(id, params);
const { query, options } = this._multiOptions(id, params);
const patchParams = Object.assign({}, params, {
query: Object.assign({}, query)
});

// Account for potentially modified data
Object.keys(query).forEach(key => {
if(query[key] !== undefined && data[key] !== undefined &&
typeof data[key] !== 'object') {
patchParams.query[key] = data[key];
} else {
patchParams.query[key] = query[key];
}
});

// Run the query
return this.Model
.update(query, { $set: this._normalizeId(id, data) }, options)
.then(() => this._findOrGet(id, params));
.then(() => this._findOrGet(id, patchParams));
}

update(id, data, params) {
Expand All @@ -191,12 +204,11 @@ class Service {
remove(id, params) {
let { query, options } = this._multiOptions(id, params);

return this._findOrGet(id, params).then(items => {
return this.Model
.remove(query, options)
.then(() => items)
.catch(errorHandler);
});
return this._findOrGet(id, params)
.then(items => this.Model
.remove(query, options)
.then(() => items)
).catch(errorHandler);
}
}

Expand Down