createdAt kept during any kind of update

pull/2/head
Louis Chatriot 9 years ago
parent 81f2c641c8
commit 65cc00b6d4
  1. 8
      lib/datastore.js
  2. 28
      test/db.test.js

@ -590,7 +590,7 @@ Datastore.prototype._update = function (query, updateQuery, options, cb) {
}); });
} }
, function () { // Perform the update , function () { // Perform the update
var modifiedDoc , modifications = []; var modifiedDoc , modifications = [], createdAt;
self.getCandidates(query, function (err, candidates) { self.getCandidates(query, function (err, candidates) {
if (err) { return callback(err); } if (err) { return callback(err); }
@ -601,8 +601,12 @@ Datastore.prototype._update = function (query, updateQuery, options, cb) {
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;
if (self.timestampData) { createdAt = candidates[i].createdAt; }
modifiedDoc = model.modify(candidates[i], updateQuery); modifiedDoc = model.modify(candidates[i], updateQuery);
if (self.timestampData) { modifiedDoc.updatedAt = new Date(); } if (self.timestampData) {
modifiedDoc.createdAt = createdAt;
modifiedDoc.updatedAt = new Date();
}
modifications.push({ oldDoc: candidates[i], newDoc: modifiedDoc }); modifications.push({ oldDoc: candidates[i], newDoc: modifiedDoc });
} }
} }

@ -1619,6 +1619,34 @@ describe('Database', function () {
}); });
}); });
it("createdAt property is unchanged and updatedAt correct after an update, even a complete document replacement", function (done) {
var d2 = new Datastore({ inMemoryOnly: true, timestampData: true });
d2.insert({ a: 1 });
d2.findOne({ a: 1 }, function (err, doc) {
var createdAt = doc.createdAt.getTime();
// Modifying update
setTimeout(function () {
d2.update({ a: 1 }, { $set: { b: 2 } }, {});
d2.findOne({ a: 1 }, function (err, doc) {
doc.createdAt.getTime().should.equal(createdAt);
assert.isBelow(Date.now() - doc.updatedAt.getTime(), 5);
// Complete replacement
setTimeout(function () {
d2.update({ a: 1 }, { c: 3 }, {});
d2.findOne({ c: 3 }, function (err, doc) {
doc.createdAt.getTime().should.equal(createdAt);
assert.isBelow(Date.now() - doc.updatedAt.getTime(), 5);
done();
});
}, 20);
});
}, 20);
});
});
}); // ==== End of 'Update' ==== // }); // ==== End of 'Update' ==== //

Loading…
Cancel
Save