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

Commit

Permalink
Properly patch multiple (#63)
Browse files Browse the repository at this point in the history
* chore(package): update feathers-service-tests to version 0.8.0

* Fix for patching multiple and returning the correct results
  • Loading branch information
greenkeeperio-bot authored and daffl committed Sep 21, 2016
1 parent 1ac37cb commit 4d78671
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
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

0 comments on commit 4d78671

Please sign in to comment.