Skip to content
This repository has been archived by the owner on Mar 4, 2019. It is now read-only.

Commit

Permalink
add synchronous load method closes #44
Browse files Browse the repository at this point in the history
  • Loading branch information
xivSolutions committed Apr 3, 2015
1 parent f00c793 commit b6ef554
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
14 changes: 14 additions & 0 deletions index.js
Expand Up @@ -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){
Expand Down Expand Up @@ -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;
}
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -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",
Expand Down
34 changes: 34 additions & 0 deletions test/loader_spec.js
Expand Up @@ -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)
});
});

0 comments on commit b6ef554

Please sign in to comment.