diff --git a/lib/datastore.js b/lib/datastore.js index 9560f9d..003e1f7 100644 --- a/lib/datastore.js +++ b/lib/datastore.js @@ -13,8 +13,9 @@ var customUtils = require('./customUtils') /** * Create a new collection * @param {String} options.filename Optional, datastore will be in-memory only if not provided - * @param {Boolean} options.inMemoryOnly Optional, default to false - * @param {Boolean} options.nodeWebkitAppName Optional, specify the name of your NW app if you want options.filename to be relative to the directory where + * @param {Boolean} options.timestampData Optional, defaults to false. If set to true, createdAt and updatedAt will be created and populated automatically (if not specified by user) + * @param {Boolean} options.inMemoryOnly Optional, defaults to false + * @param {String} options.nodeWebkitAppName Optional, specify the name of your NW app if you want options.filename to be relative to the directory where * Node Webkit stores application data such as cookies and local storage (the best place to store data in my opinion) * @param {Boolean} options.autoload Optional, defaults to false * @param {Function} options.onload Optional, if autoload is used this will be called after the load database with the error object as parameter. If you don't pass it the error will be thrown @@ -134,17 +135,17 @@ Datastore.prototype.ensureIndex = function (options, cb) { /** * Remove an index * @param {String} fieldName - * @param {Function} cb Optional callback, signature: err + * @param {Function} cb Optional callback, signature: err */ Datastore.prototype.removeIndex = function (fieldName, cb) { var callback = cb || function () {}; - + delete this.indexes[fieldName]; - + this.persistence.persistNewState([{ $$indexRemoved: fieldName }], function (err) { if (err) { return callback(err); } return callback(null); - }); + }); }; @@ -319,13 +320,13 @@ Datastore.prototype.prepareDocumentForInsertion = function (newDoc) { preparedDoc = []; newDoc.forEach(function (doc) { preparedDoc.push(self.prepareDocumentForInsertion(doc)); }); } else { - if (newDoc._id === undefined) { - newDoc._id = this.createNewId(); - } + if (newDoc._id === undefined) { newDoc._id = this.createNewId(); } preparedDoc = model.deepCopy(newDoc); + //if (preparedDoc._id === undefined) { preparedDoc._id = this.createNewId(); } + if (this.timestampData && preparedDoc.createdAt === undefined) { preparedDoc.createdAt = new Date(); } model.checkObject(preparedDoc); } - + return preparedDoc; }; @@ -337,7 +338,7 @@ Datastore.prototype._insertInCache = function (newDoc) { if (util.isArray(newDoc)) { this._insertMultipleDocsInCache(newDoc); } else { - this.addToIndexes(this.prepareDocumentForInsertion(newDoc)); + this.addToIndexes(this.prepareDocumentForInsertion(newDoc)); } }; @@ -360,12 +361,12 @@ Datastore.prototype._insertMultipleDocsInCache = function (newDocs) { break; } } - + if (error) { for (i = 0; i < failingI; i += 1) { this.removeFromIndexes(preparedDocs[i]); } - + throw error; } }; diff --git a/test/db.test.js b/test/db.test.js index 8a28e9f..9eba510 100644 --- a/test/db.test.js +++ b/test/db.test.js @@ -278,7 +278,7 @@ describe('Database', function () { done(); }); }); - + /** * Complicated behavior here. Basically we need to test that when a user function throws an exception, it is not caught * in NeDB and the callback called again, transforming a user error into a NeDB error. @@ -297,18 +297,18 @@ describe('Database', function () { ; process.removeAllListeners('uncaughtException'); - + process.on('uncaughtException', function MINE (ex) { for (i = 0; i < currentUncaughtExceptionHandlers.length; i += 1) { process.on('uncaughtException', currentUncaughtExceptionHandlers[i]); } - + ex.should.equal('SOME EXCEPTION'); done(); }); d.insert({ a: 5 }, function () { - d.findOne({ a : 5}, function (err, doc) { + d.findOne({ a : 5}, function (err, doc) { if (tryCount === 0) { tryCount += 1; throw 'SOME EXCEPTION'; @@ -316,7 +316,7 @@ describe('Database', function () { done('Callback was called twice'); } }); - }); + }); }); }); // ==== End of 'Insert' ==== //