From 3a2db5f56ebc697d8d23ae308136b8a70df4fe95 Mon Sep 17 00:00:00 2001 From: Louis Chatriot Date: Thu, 9 Jan 2014 09:28:22 +0100 Subject: [PATCH] Handle errors in autoload --- lib/datastore.js | 7 ++++++- test/db.test.js | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/lib/datastore.js b/lib/datastore.js index 886e7e5..bf5792f 100644 --- a/lib/datastore.js +++ b/lib/datastore.js @@ -16,6 +16,7 @@ var customUtils = require('./customUtils') * @param {Boolean} 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 */ function Datastore (options) { var filename; @@ -53,7 +54,11 @@ function Datastore (options) { this.indexes = {}; this.indexes._id = new Index({ fieldName: '_id', unique: true }); - if (this.autoload) { this.loadDatabase(); } + // Queue a load of the database right away and call the onload handler + // By default (no onload handler), if there is an error there, no operation will be possible so warn the user by throwing an exception + if (this.autoload) { this.loadDatabase(options.onload || function (err) { + if (err) { throw err; } + }); } } diff --git a/test/db.test.js b/test/db.test.js index e9f59f4..1dcf067 100644 --- a/test/db.test.js +++ b/test/db.test.js @@ -53,6 +53,46 @@ describe('Database', function () { dbef.inMemoryOnly.should.equal(true); }); + describe('Autoloading', function () { + + it('Can autoload a database and query it right away', function (done) { + var fileStr = model.serialize({ _id: '1', a: 5, planet: 'Earth' }) + '\n' + model.serialize({ _id: '2', a: 5, planet: 'Mars' }) + '\n' + , autoDb = 'workspace/auto.db' + , db + ; + + fs.writeFileSync(autoDb, fileStr, 'utf8'); + db = new Datastore({ filename: autoDb, autoload: true }) + + db.find({}, function (err, docs) { + assert.isNull(err); + docs.length.should.equal(2); + done(); + }); + }); + + it('Throws if autoload fails', function (done) { + var fileStr = model.serialize({ _id: '1', a: 5, planet: 'Earth' }) + '\n' + model.serialize({ _id: '2', a: 5, planet: 'Mars' }) + '\n' + '{"$$indexCreated":{"fieldName":"a","unique":true}}' + , autoDb = 'workspace/auto.db' + , db + ; + + fs.writeFileSync(autoDb, fileStr, 'utf8'); + + // Check the loadDatabase generated an error + function onload (err) { + err.errorType.should.equal('uniqueViolated'); + done(); + } + + db = new Datastore({ filename: autoDb, autoload: true, onload: onload }) + + db.find({}, function (err, docs) { + done("Find should not be executed since autoload failed"); + }); + }); + + }); describe('Insert', function () {