From b6ef5544cce4d48bc49df668648e6a670abda1ee Mon Sep 17 00:00:00 2001 From: xivSolutions Date: Thu, 2 Apr 2015 19:00:59 -0500 Subject: [PATCH] add synchronous load method closes #44 --- index.js | 14 ++++++++++++++ package.json | 1 + test/loader_spec.js | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+) diff --git a/index.js b/index.js index 3e0670e..441f115 100644 --- a/index.js +++ b/index.js @@ -8,6 +8,8 @@ var Document = require("./lib/document"); var ArgTypes = require("./lib/arg_types"); var Args = require("args-js"); var path = require("path"); +var deasync = require('deasync'); + var self; var Massive = function(args){ @@ -260,3 +262,15 @@ exports.connect = function(args, next){ }); }); }; + +exports.loadSync = function(args) { + var done = false; + this.connect(args, function (err, res) { + result = res; + done = true; + }); + while(!done) { + deasync.runLoopOnce(); + } + return result; +} diff --git a/package.json b/package.json index 187cce9..aea55db 100644 --- a/package.json +++ b/package.json @@ -24,6 +24,7 @@ "dependencies": { "args-js": "^0.10.6", "async": "^0.9.0", + "deasync": "^0.0.10", "commander": "^2.6.0", "glob": "^4.4.1", "pg": "^4.3.0", diff --git a/test/loader_spec.js b/test/loader_spec.js index 72e8f92..9299430 100644 --- a/test/loader_spec.js +++ b/test/loader_spec.js @@ -31,3 +31,37 @@ describe('On spin up', function () { assert.equal(db.functions.length,3) }); }); + +var syncLoaded; +var constr = "postgres://rob:password@localhost/massive"; +var path = require("path"); +var scriptsDir = path.join(__dirname, ".", "db"); + +describe('Synchronous Load', function () { + + it('loads the db synchronously and blocks execution until complete', function() { + // no need to re-set the back-end; it wasn't changed by the previous 'suite' ... + syncLoaded = require("../index").loadSync({connectionString: constr, scripts: scriptsDir}); + assert(syncLoaded && syncLoaded.tables && syncLoaded.queryFiles && syncLoaded.connectionString); + }); + it('returns a valid db instance from sync load function', function () { + assert(syncLoaded && syncLoaded.tables && syncLoaded.queryFiles && syncLoaded.connectionString); + }); + it('loads non-public schema as namespace property', function () { + assert(syncLoaded.myschema, "No Schema loaded"); + }); + it('loads tables in syncLoaded schema as properties of namespace', function() { + assert(syncLoaded.myschema.artists && syncLoaded.myschema.albums, 'No tables loaded on schema') + }); + it('loads up 5 tables with 2 in schema object in array property', function () { + assert.equal(syncLoaded.tables.length, 5); + }); + + // including one nested in a deeper folder... total of 4 now. + it('loads up 7 queries', function () { + assert.equal(syncLoaded.queryFiles.length, 7); + }); + it('loads up functions', function () { + assert.equal(syncLoaded.functions.length,3) + }); +});