Update function updates updatedAt field if necessary

pull/2/head
Louis Chatriot 9 years ago
parent 5afa535c0e
commit e77c4a84c6
  1. 19
      lib/datastore.js
  2. 28
      test/db.test.js

@ -539,13 +539,14 @@ Datastore.prototype._update = function (query, updateQuery, options, cb) {
, modifications = [] , modifications = []
; ;
// Preparing update (if an error is thrown here neither the datafile nor // Preparing update (if an error is thrown here neither the datafile nor
// the in-memory indexes are affected) // the in-memory indexes are affected)
try { try {
for (i = 0; i < candidates.length; i += 1) { for (i = 0; i < candidates.length; i += 1) {
if (model.match(candidates[i], query) && (multi || numReplaced === 0)) { if (model.match(candidates[i], query) && (multi || numReplaced === 0)) {
numReplaced += 1; numReplaced += 1;
modifiedDoc = model.modify(candidates[i], updateQuery); modifiedDoc = model.modify(candidates[i], updateQuery);
if (self.timestampData) { modifiedDoc.updatedAt = new Date(); }
modifications.push({ oldDoc: candidates[i], newDoc: modifiedDoc }); modifications.push({ oldDoc: candidates[i], newDoc: modifiedDoc });
} }
} }
@ -553,14 +554,14 @@ Datastore.prototype._update = function (query, updateQuery, options, cb) {
return callback(err); return callback(err);
} }
// Change the docs in memory // Change the docs in memory
try { try {
self.updateIndexes(modifications); self.updateIndexes(modifications);
} catch (err) { } catch (err) {
return callback(err); return callback(err);
} }
// Update the datafile // Update the datafile
self.persistence.persistNewState(_.pluck(modifications, 'newDoc'), function (err) { self.persistence.persistNewState(_.pluck(modifications, 'newDoc'), function (err) {
if (err) { return callback(err); } if (err) { return callback(err); }
return callback(null, numReplaced); return callback(null, numReplaced);

@ -957,6 +957,34 @@ describe('Database', function () {
], done); ], done);
}); });
it("If timestampData option is set, update the updatedAt field", function (done) {
var beginning = Date.now();
d = new Datastore({ filename: testDb, autoload: true, timestampData: true });
d.insert({ hello: 'world' }, function (err, insertedDoc) {
assert.isBelow(insertedDoc.updatedAt.getTime() - beginning, 15);
assert.isBelow(insertedDoc.createdAt.getTime() - beginning, 15);
Object.keys(insertedDoc).length.should.equal(4);
// Wait 100ms before performing the update
setTimeout(function () {
var step1 = Date.now();
d.update({ _id: insertedDoc._id }, { $set: { hello: 'mars' } }, {}, function () {
d.find({ _id: insertedDoc._id }, function (err, docs) {
docs.length.should.equal(1);
Object.keys(docs[0]).length.should.equal(4);
docs[0]._id.should.equal(insertedDoc._id);
docs[0].createdAt.should.equal(insertedDoc.createdAt);
docs[0].hello.should.equal('mars');
assert.isAbove(docs[0].updatedAt.getTime() - beginning, 99); // updatedAt modified
assert.isBelow(docs[0].updatedAt.getTime() - step1, 15); // updatedAt modified
done();
});
})
}, 100);
});
});
it("Can update multiple documents matching the query", function (done) { it("Can update multiple documents matching the query", function (done) {
var id1, id2, id3; var id1, id2, id3;

Loading…
Cancel
Save