Skip to content

Commit

Permalink
early decorations, teardowns, start fixing-up tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
devinivy committed Jul 28, 2015
1 parent e2874c7 commit 5941010
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 302 deletions.
72 changes: 43 additions & 29 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,51 @@
var Hoek = require('hoek');
var Items = require('items');
var Waterline = require('waterline');
var WaterlineFixtures = require('waterline-fixtures');

exports.register = function (server, options, next) {

// Models come in as an array or a path to be required
Hoek.assert(Array.isArray(options.models) || typeof options.models === 'string',
'Model definitions need to be specified as an array or path to be required.');

var models;
if (options.models instanceof Array) {
if (Array.isArray(options.models)) {

models = options.models;
} else if (typeof options.models === 'string') {

models = require(options.models);
} else {
next(new Error('The models need to be specified as an array or path to be required.'));
return;
}

// Here's the ORM!
var waterline = new Waterline();

// Give the models to waterline
var modelDef;
var modelExtended;
for (var i = 0; i < models.length; i++) {

modelDef = models[i];

// If the provided model info is a function, it wants waterline passed to it.
// This is used for lifecycle callbacks to have access to collections, etc.
if (typeof modelDef === 'function') {

modelDef = modelDef(waterline);
}

waterline.loadCollection(
Waterline.Collection.extend(modelDef)
);
modelExtended = Waterline.Collection.extend(modelDef);

waterline.loadCollection(modelExtended);
}

// Require the adapters modules if strings are passed instead of objects
var keys = Object.keys(options.adapters);
for (var j = 0; j < keys.length; j++) {

if (typeof options.adapters[keys[j]] === 'string') {

options.adapters[keys[j]] = require(options.adapters[keys[j]]);
}

Expand All @@ -48,46 +55,53 @@ exports.register = function (server, options, next) {
waterline.initialize({
connections: options.connections,
adapters: options.adapters
}, function (err, model) {
}, function (err, ontology) {

if (err) {

return next(err);
}

// Expose collections to server
server.expose(model.collections);
// Expose public objects from the ORM
server.expose('collections', ontology.collections);
server.expose('connections', ontology.connections);
server.expose('schema', waterline.schema);

// Expose collections to requests so they can be used in handlers easily
server.ext('onPreHandler', function (modelCollections) {
// Decorate server with the raw ORM
server.decorate('server', 'waterline', waterline);

return function (request, reply) {
// Decorate request with collections so they can be used in extensions easily
server.decorate('request', 'collections', ontology.collections);

request.model = modelCollections;
if (parseInt(server.version) >= 8) {
reply.continue();
} else {
reply();
}
};
}(model.collections));
// Expose a connection teardown method
server.expose('teardown', function (cb) {

var teardowns = {};

// Allow servers to access the raw ORM!
server.method('getWaterline', function (orm) {
var connection;
var identity;
var connectionNames = Object.keys(ontology.connections);
for (var k = 0; k < connectionNames.length; ++k) {

return function (cb) {
connection = ontology.connections[connectionNames[i]];
identity = connection._adapter.identity;
if (identity) {

return cb(null, orm);
};
}(waterline));
teardowns[identity] = teardowns[identity] || connection._adapter.teardown;
}
}

Items.execute.parallel(teardowns, cb);

});

// Are there fixtures?
if (options.data) {

// Load fixtures then have dogwater
options.data.collections = model.collections;
options.data.collections = ontology.collections;

WaterlineFixtures.init(options.data, next);

} else {

// Have dogwater
Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "dogwater",
"version": "0.4.7",
"description": "A hapi plugin integrating Waterline ORM.",
"description": "A hapi plugin integrating Waterline ORM",
"main": "lib/index.js",
"repository": {
"type": "git",
Expand All @@ -21,16 +21,16 @@
"orm"
],
"dependencies": {
"waterline": "^0.10.8",
"hoek": "~2.14.0",
"items": "~1.1.0",
"waterline": "^0.10.x",
"waterline-fixtures": "^0.2.x"
},
"devDependencies": {
"lab": "5.x.x",
"code": "1.x.x",
"coveralls": "2.x.x",
"hapi": "~8.x.x",
"hapi-v7": "git://github.com/devinivy/hapi-v7.git",
"items": "1.x.x",
"hapi": "8.x.x",
"sails-memory": "0.10.x"
},
"scripts": {
Expand Down
Loading

0 comments on commit 5941010

Please sign in to comment.