DB state shouldn't be modified when the returned objects are modified

pull/2/head
Louis Chatriot 12 years ago
parent 7096cdb144
commit f91a41b534
  1. 4
      lib/datastore.js
  2. 24
      test/db.test.js

@ -129,7 +129,7 @@ Datastore.prototype.find = function (query, callback) {
for (i = 0; i < self.data.length; i += 1) { for (i = 0; i < self.data.length; i += 1) {
if (Datastore.match(self.data[i], query)) { if (Datastore.match(self.data[i], query)) {
res.push(self.data[i]); res.push(model.deepCopy(self.data[i]));
} }
} }
@ -148,7 +148,7 @@ Datastore.prototype.findOne = function (query, callback) {
for (i = 0; i < self.data.length; i += 1) { for (i = 0; i < self.data.length; i += 1) {
if (Datastore.match(self.data[i], query)) { if (Datastore.match(self.data[i], query)) {
return callback(null, self.data[i]); return callback(null, model.deepCopy(self.data[i]));
} }
} }

@ -108,6 +108,30 @@ describe('Database', function () {
}); });
}); });
it('If an object returned from the DB is modified and refetched, the original value should be found', function (done) {
d.insert({ a: 'something' }, function () {
d.findOne({}, function (err, doc) {
doc.a.should.equal('something');
doc.a = 'another thing';
doc.a.should.equal('another thing');
// Re-fetching with findOne should yield the persisted value
d.findOne({}, function (err, doc) {
doc.a.should.equal('something');
doc.a = 'another thing';
doc.a.should.equal('another thing');
// Re-fetching with find should yield the persisted value
d.find({}, function (err, docs) {
docs[0].a.should.equal('something');
done();
});
});
});
});
});
}); // ==== End of 'Insert' ==== // }); // ==== End of 'Insert' ==== //

Loading…
Cancel
Save