Skip to content

Commit

Permalink
Fixed issue where updated nested data wasn't returned when using find…
Browse files Browse the repository at this point in the history
…OneAndUpdate() with NeDB. Fixes #55
  • Loading branch information
scottwrobinson committed Mar 2, 2016
1 parent 5a2cd85 commit 6d28abb
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/clients/nedbclient.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,12 @@ class NeDbClient extends DatabaseClient {
} else {
return db.update(query, { $set: values }, function(error, result) {
if (error) return reject(error);
result = _.assign(data, values);
return resolve(result);

// Fixes issue #55. Remove when NeDB is updated to v1.8+
db.findOne({_id: data._id}, function(error, doc) {
if (error) return reject(error);
resolve(doc);
});
});
}
});
Expand Down
50 changes: 50 additions & 0 deletions test/issues.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
var expect = require('chai').expect;
var connect = require('../index').connect;
var Document = require('../index').Document;
var EmbeddedDocument = require('../index').EmbeddedDocument;
var ValidationError = require('../lib/errors').ValidationError;
var validateId = require('./util').validateId;

Expand Down Expand Up @@ -291,6 +292,55 @@ describe('Issues', function() {
});
});

describe('#55', function() {
it('should return updated data on findOneAndUpdate when updating nested data', function(done) {
/*
* When updating nested data with findOneAndUpdate,
* the document returned to you should contain
* all of the updated data. But due to lack of
* support in NeDB versions < 1.8, I had to use
* a hack (_.assign) to update the document. This
* doesn't properly update nested data.
*
* Temporary fix is to just reload the document
* with findOne.
*/

class Contact extends EmbeddedDocument {
constructor() {
super();

this.email = String;
this.phone = String;
}
}

class Person extends Document {
constructor() {
super();
this.name = String;
this.contact = Contact;
}
}

var person = Person.create({
name: 'John Doe',
contact: {
email: 'john@doe.info',
phone: 'NA'
}
});

person.save().then(function(person) {
return Person.findOneAndUpdate({_id: person._id}, {name: 'John Derp', 'contact.phone': '0123456789'});
}).then(function(person) {
expect(person.name).to.be.equal('John Derp');
expect(person.contact.email).to.be.equal('john@doe.info');
expect(person.contact.phone).to.be.equal('0123456789');
}).then(done, done);
});
});

describe('#57', function() {
it('should not save due to Promise.reject in hook', function(done) {
/*
Expand Down

0 comments on commit 6d28abb

Please sign in to comment.