Skip to content

Commit

Permalink
Waterline and hapi as peer deps, update style, remove fixtures. Closes
Browse files Browse the repository at this point in the history
…#38, closes #39, closes #40
  • Loading branch information
devinivy committed Apr 4, 2016
1 parent 6b3e367 commit 8e45ff4
Show file tree
Hide file tree
Showing 8 changed files with 180 additions and 297 deletions.
8 changes: 3 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
language: node_js

node_js:
- 0.10
- 0.12
- iojs
- 4
- 5
- "4.0"
- "4"
- "5"

after_script: "npm run coveralls"
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2014 Devin Ivy
Copyright (c) 2014-2016 Devin Ivy and other contributors.

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
Expand Down
101 changes: 41 additions & 60 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
var Path = require('path');
var Hoek = require('hoek');
var Items = require('items');
var Waterline = require('waterline');
var WaterlineFixtures = require('waterline-fixtures');
'use strict';

const Path = require('path');
const Hoek = require('hoek');
const Items = require('items');
const Waterline = require('waterline');

const internals = {};

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;
let models;
if (Array.isArray(options.models)) {

models = options.models;
Expand All @@ -19,23 +22,20 @@ exports.register = function (server, options, next) {
if (typeof options.models === 'string') {

if (Hoek.isAbsolutePath(options.models)) {

models = require(options.models);
} else {

}
else {
models = require(Path.resolve(process.cwd(), options.models));
}
}

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

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

modelDef = models[i];
let 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.
Expand All @@ -44,18 +44,18 @@ exports.register = function (server, options, next) {
modelDef = modelDef(waterline);
}

modelExtended = Waterline.Collection.extend(modelDef);
const 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++) {
const keys = Object.keys(options.adapters);
for (let i = 0; i < keys.length; ++i) {

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

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

}
Expand All @@ -67,10 +67,9 @@ exports.register = function (server, options, next) {
connections: options.connections,
adapters: options.adapters,
defaults: options.defaults
}, function (err, ontology) {
}, (err, ontology) => {

if (err) {

return next(err);
}

Expand All @@ -86,59 +85,41 @@ exports.register = function (server, options, next) {
server.decorate('request', 'collections', ontology.collections);

// Expose an all-connections teardown method
server.expose('teardown', function (cb) {

var teardowns = [];

var connection;
var teardown;
var connectionNames = Object.keys(ontology.connections);
for (var k = 0; k < connectionNames.length; ++k) {
server.expose('teardown', internals.teardown(ontology.connections));

connection = ontology.connections[connectionNames[k]];
teardown = connection._adapter.teardown;

if (typeof teardown === 'function' &&
teardowns.indexOf(teardown) === -1) {

teardowns.push(teardown);
}
// Have dogwater
next();
});

}
};

var run = function (item, done) {
internals.teardown = function (connections) {

return item(done);
};
return function (cb) {

Items.parallel(teardowns, run, cb);
const teardowns = [];

});
const connectionNames = Object.keys(connections);
for (let i = 0; i < connectionNames.length; ++i) {

// Are there fixtures?
if (options.fixtures) {
const connection = connections[connectionNames[i]];
const teardown = connection._adapter.teardown;

// Load fixtures then have dogwater
var fixturesOptions = {
collections: ontology.collections
};
if (typeof teardown === 'function' &&
teardowns.indexOf(teardown) === -1) {

// If the option is an array, assume those are the fixtures themselves
if (Array.isArray(options.fixtures)) {
fixturesOptions.fixtures = options.fixtures;
} else {
Hoek.merge(fixturesOptions, options.fixtures);
teardowns.push(teardown);
}

WaterlineFixtures.init(fixturesOptions, next);
} else {

// Have dogwater
next();
}

});
const run = function (item, done) {

return item(done);
};

Items.parallel(teardowns, run, cb);
};
};

exports.register.attributes = {
Expand Down
37 changes: 18 additions & 19 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@
"version": "1.1.0",
"description": "A hapi plugin integrating Waterline ORM",
"main": "lib/index.js",
"scripts": {
"test": "lab -a code -t 100 -L",
"coveralls": "lab -r lcov | coveralls"
},
"repository": {
"type": "git",
"url": "https://github.com/devinivy/dogwater"
},
"contributors": [
{
"name": "devinivy",
"github": "https://github.com/devinivy"
}
],
"engines": {
"node": ">=4.0.0"
},
"keywords": [
"hapi",
"plugin",
Expand All @@ -21,22 +22,20 @@
"orm"
],
"dependencies": {
"hoek": "~2.14.0",
"items": "~1.1.0",
"waterline": "^0.10.x",
"waterline-fixtures": "^0.2.x"
"hoek": "3.x.x",
"items": "2.x.x"
},
"peerDependencies": {
"hapi": ">=10.0.0 <14.0.0",
"waterline": ">=0.10.0 <0.13.0"
},
"devDependencies": {
"lab": "6.x.x",
"code": "1.x.x",
"code": "2.x.x",
"coveralls": "2.x.x",
"hapi": "8.x.x",
"sails-memory": "0.10.x"
},
"scripts": {
"test": "node_modules/.bin/lab -I \"inspect,URI\" -a code -t 100 -L",
"coveralls": "node_modules/.bin/lab -r lcov | node_modules/.bin/coveralls"
"hapi": "13.x.x",
"lab": "10.x.x",
"waterline": "0.10.x"
},
"author": "Devin Ivy",
"author": "Devin Ivy <devin@bigroomstudios.com>",
"license": "MIT"
}

0 comments on commit 8e45ff4

Please sign in to comment.