Skip to content

Commit

Permalink
Ensure we convert mongoose models to regular objects. Closes #110. (#112
Browse files Browse the repository at this point in the history
)
  • Loading branch information
ekryski authored Aug 23, 2016
1 parent 51c2547 commit dbc3bae
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,15 @@ class Service {
}

update(id, data, params) {
if(id === null) {
if (id === null) {
return Promise.reject('Not replacing multiple records. Did you mean `patch`?');
}

// Handle case where data might be a mongoose model
if (typeof data.toObject === 'function') {
data = data.toObject();
}

const options = Object.assign({
new: true,
overwrite: this.overwrite,
Expand All @@ -163,6 +168,13 @@ class Service {

patch(id, data, params) {
params.query = params.query || {};

// Handle case where data might be a mongoose model
if (typeof data.toObject === 'function') {
data = data.toObject();
}

// ensure we are working on a copy
data = Object.assign({}, data);

// If we are updating multiple records
Expand Down
18 changes: 18 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,24 @@ describe('Feathers Mongoose Service', () => {
}).catch(done);
});

it('can patch a mongoose model', function (done) {
people.get(_ids.Doug).then(dougModel => {
people.patch(_ids.Doug, dougModel).then(data => {
expect(data.name).to.equal('Doug');
done();
}).catch(done);
}).catch(done);
});

it('can patch a mongoose model', function (done) {
people.get(_ids.Doug).then(dougModel => {
people.update(_ids.Doug, dougModel).then(data => {
expect(data.name).to.equal('Doug');
done();
}).catch(done);
}).catch(done);
});

it('can $push an item onto an array with update', function(done) {
pets.create({ type: 'cat', name: 'Margeaux' }).then(margeaux => {
people.update(_ids.Doug, { $push: { pets: margeaux } })
Expand Down

0 comments on commit dbc3bae

Please sign in to comment.