Skip to content

Commit

Permalink
Fix and tests for patch to allow NeDB modifiers (#72)
Browse files Browse the repository at this point in the history
  • Loading branch information
daffl authored Aug 17, 2017
1 parent f461424 commit 081fb9a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,11 +154,18 @@ class Service {
[this.id]: { $in: idList }
}
});
const updateData = { $set: {} };

return nfcall(this.Model, 'update', query, {
$set: omit(data, this.id, '_id')
}, options)
.then(() => this._findOrGet(id, findParams));
Object.keys(data).forEach(key => {
if (key.indexOf('$') === 0) {
updateData[key] = data[key];
} else if (key !== '_id' && key !== this.id) {
updateData.$set[key] = data[key];
}
});

return nfcall(this.Model, 'update', query, updateData, options)
.then(() => this._findOrGet(id, findParams));
})
.then(select(params, this.id));
}
Expand Down
13 changes: 13 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,19 @@ describe('NeDB Service', function () {
assert.deepEqual(updated.data, ['first', 'second'])
);
});

it('allows NeDB modifiers in patch (#65)', () => {
const service = app.service('people');

return service.create({
name: 'Modifier',
data: [ 'first' ]
}).then(person =>
service.patch(person._id, { $push: { data: 'second' } })
).then(updated =>
assert.deepEqual(updated.data, ['first', 'second'])
);
});
});
});

Expand Down

0 comments on commit 081fb9a

Please sign in to comment.