From 5afa535c0ee37aceb087b7b3514955ff262f14ca Mon Sep 17 00:00:00 2001 From: Louis Chatriot Date: Sat, 7 Nov 2015 09:44:59 +0100 Subject: [PATCH] Add timestamp if required during document insertion --- lib/datastore.js | 6 +++-- test/db.test.js | 69 +++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 69 insertions(+), 6 deletions(-) diff --git a/lib/datastore.js b/lib/datastore.js index 1ad0863..2bcddba 100644 --- a/lib/datastore.js +++ b/lib/datastore.js @@ -314,7 +314,7 @@ Datastore.prototype.createNewId = function () { /** * Prepare a document (or array of documents) to be inserted in a database - * Meaning adds _id and createdAt if necessary on a copy of newDoc to avoid any side effect on user input + * Meaning adds _id and timestamps if necessary on a copy of newDoc to avoid any side effect on user input * @api private */ Datastore.prototype.prepareDocumentForInsertion = function (newDoc) { @@ -326,7 +326,9 @@ Datastore.prototype.prepareDocumentForInsertion = function (newDoc) { } else { preparedDoc = model.deepCopy(newDoc); if (preparedDoc._id === undefined) { preparedDoc._id = this.createNewId(); } - if (this.timestampData && preparedDoc.createdAt === undefined) { preparedDoc.createdAt = new Date(); } + var now = new Date(); + if (this.timestampData && preparedDoc.createdAt === undefined) { preparedDoc.createdAt = now; } + if (this.timestampData && preparedDoc.updatedAt === undefined) { preparedDoc.updatedAt = now; } model.checkObject(preparedDoc); } diff --git a/test/db.test.js b/test/db.test.js index 76567c9..3cf5b7b 100644 --- a/test/db.test.js +++ b/test/db.test.js @@ -281,25 +281,27 @@ describe('Database', function () { // Insert doc has two new fields, _id and createdAt insertedDoc.hello.should.equal('world'); assert.isDefined(insertedDoc.createdAt); + assert.isDefined(insertedDoc.updatedAt); + insertedDoc.createdAt.should.equal(insertedDoc.updatedAt); assert.isDefined(insertedDoc._id); - Object.keys(insertedDoc).length.should.equal(3); + Object.keys(insertedDoc).length.should.equal(4); assert.isBelow(Math.abs(insertedDoc.createdAt.getTime() - beginning), 15); // No more than 15ms should have elapsed // Modifying results of insert doesn't change the cache insertedDoc.bloup = "another"; - Object.keys(insertedDoc).length.should.equal(4); + Object.keys(insertedDoc).length.should.equal(5); d.find({}, function (err, docs) { docs.length.should.equal(1); assert.deepEqual(newDoc, { hello: 'world' }); - assert.deepEqual({ hello: 'world', _id: insertedDoc._id, createdAt: insertedDoc.createdAt }, docs[0]); + assert.deepEqual({ hello: 'world', _id: insertedDoc._id, createdAt: insertedDoc.createdAt, updatedAt: insertedDoc.updatedAt }, docs[0]); // All data correctly persisted on disk d.loadDatabase(function () { d.find({}, function (err, docs) { docs.length.should.equal(1); assert.deepEqual(newDoc, { hello: 'world' }); - assert.deepEqual({ hello: 'world', _id: insertedDoc._id, createdAt: insertedDoc.createdAt }, docs[0]); + assert.deepEqual({ hello: 'world', _id: insertedDoc._id, createdAt: insertedDoc.createdAt, updatedAt: insertedDoc.updatedAt }, docs[0]); done(); }); @@ -309,6 +311,65 @@ describe('Database', function () { }); }); + it("If timestampData option not set, don't create a createdAt and a updatedAt field", function (done) { + d.insert({ hello: 'world' }, function (err, insertedDoc) { + Object.keys(insertedDoc).length.should.equal(2); + assert.isUndefined(insertedDoc.createdAt); + assert.isUndefined(insertedDoc.updatedAt); + + d.find({}, function (err, docs) { + docs.length.should.equal(1); + assert.deepEqual(docs[0], insertedDoc); + + done(); + }); + }); + }); + + it("If timestampData is set but createdAt is specified by user, don't change it", function (done) { + var newDoc = { hello: 'world', createdAt: new Date(234) }, beginning = Date.now(); + d = new Datastore({ filename: testDb, timestampData: true, autoload: true }); + d.insert(newDoc, function (err, insertedDoc) { + Object.keys(insertedDoc).length.should.equal(4); + insertedDoc.createdAt.getTime().should.equal(234); // Not modified + assert.isBelow(insertedDoc.updatedAt.getTime() - beginning, 15); // Created + + d.find({}, function (err, docs) { + assert.deepEqual(insertedDoc, docs[0]); + + d.loadDatabase(function () { + d.find({}, function (err, docs) { + assert.deepEqual(insertedDoc, docs[0]); + + done(); + }); + }); + }); + }); + }); + + it("If timestampData is set but updatedAt is specified by user, don't change it", function (done) { + var newDoc = { hello: 'world', updatedAt: new Date(234) }, beginning = Date.now(); + d = new Datastore({ filename: testDb, timestampData: true, autoload: true }); + d.insert(newDoc, function (err, insertedDoc) { + Object.keys(insertedDoc).length.should.equal(4); + insertedDoc.updatedAt.getTime().should.equal(234); // Not modified + assert.isBelow(insertedDoc.createdAt.getTime() - beginning, 15); // Created + + d.find({}, function (err, docs) { + assert.deepEqual(insertedDoc, docs[0]); + + d.loadDatabase(function () { + d.find({}, function (err, docs) { + assert.deepEqual(insertedDoc, docs[0]); + + done(); + }); + }); + }); + }); + }); + it('Can insert a doc with id 0', function (done) { d.insert({ _id: 0, hello: 'world' }, function (err, doc) { doc._id.should.equal(0);