Update function updates updatedAt field if necessary

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

@ -546,6 +546,7 @@ Datastore.prototype._update = function (query, updateQuery, options, cb) {
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 });
} }
} }

@ -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