From 8e45ff434210675390a72de75193690548ac05e5 Mon Sep 17 00:00:00 2001 From: Devin Ivy Date: Mon, 4 Apr 2016 17:58:19 -0400 Subject: [PATCH] Waterline and hapi as peer deps, update style, remove fixtures. Closes #38, closes #39, closes #40 --- .travis.yml | 8 +- LICENSE | 2 +- lib/index.js | 101 +++++------ package.json | 37 ++-- test/index.js | 301 ++++++++++++-------------------- test/models.definition.funcs.js | 2 + test/models.definition.raw.js | 2 + test/models.fixtures.json | 24 --- 8 files changed, 180 insertions(+), 297 deletions(-) delete mode 100644 test/models.fixtures.json diff --git a/.travis.yml b/.travis.yml index 1ac8314..a657965 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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" diff --git a/LICENSE b/LICENSE index 06dd434..23d6307 100644 --- a/LICENSE +++ b/LICENSE @@ -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 diff --git a/lib/index.js b/lib/index.js index bae6b52..ef3e904 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,8 +1,11 @@ -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) { @@ -10,7 +13,7 @@ exports.register = function (server, options, next) { 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; @@ -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. @@ -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]]); } } @@ -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); } @@ -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 = { diff --git a/package.json b/package.json index d5f9e4a..1736067 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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 ", "license": "MIT" } diff --git a/test/index.js b/test/index.js index afdfddb..8c07758 100644 --- a/test/index.js +++ b/test/index.js @@ -1,46 +1,45 @@ +'use strict'; + // Load modules -var Lab = require('lab'); -var Code = require('code'); -var Hapi = require('hapi'); -var Path = require('path'); -var Waterline = require('waterline'); -var Memory = require('sails-memory'); + +const Lab = require('lab'); +const Code = require('code'); +const Hapi = require('hapi'); +const Path = require('path'); +const Waterline = require('waterline'); // Test shortcuts -var lab = exports.lab = Lab.script(); -var expect = Code.expect; -var beforeEach = lab.beforeEach; -var afterEach = lab.afterEach; -var experiment = lab.experiment; -var test = lab.test; +const lab = exports.lab = Lab.script(); +const expect = Code.expect; +const beforeEach = lab.beforeEach; +const afterEach = lab.afterEach; +const experiment = lab.experiment; +const test = lab.test; -experiment('Dogwater', function () { +experiment('Dogwater', () => { // This will be a hapi server for each test. - var server; + let server; // Setup dummy connections/adapters. - var connections = { + const connections = { 'myFoo': { adapter: 'foo' } }; - var dummyAdapters = { foo: {} }; + const dummyAdapters = { foo: {} }; - var defaults = { migrate: 'safe' }; + const defaults = { migrate: 'safe' }; // Pass adapters as string - var stringAdapters = { foo: 'sails-memory' }; - - // Setup adapters for testing fixtures - var fixtureAdapters = { foo: Memory }; + const stringAdapters = { foo: 'sails-memory' }; // Fail upon registering a connection - var failureAdapters = { + const failureAdapters = { foo: { - registerConnection: function (one, two, cb) { + registerConnection: (one, two, cb) => { cb(new Error('Adapter test error!')); } @@ -48,10 +47,10 @@ experiment('Dogwater', function () { }; // Teardown tests - var teardownSuccessAdapters = { + const teardownSuccessAdapters = { foo: { identity: 'foo', - teardown: function (cb) { + teardown: (cb) => { toreDown = true; cb(); @@ -59,9 +58,9 @@ experiment('Dogwater', function () { } }; - var teardownNoIdAdapters = { + const teardownNoIdAdapters = { foo: { - teardown: function (cb) { + teardown: (cb) => { toreDown = true; cb(); @@ -69,21 +68,20 @@ experiment('Dogwater', function () { } }; - var teardownNoMethodAdapters = { + const teardownNoMethodAdapters = { foo: { identity: 'foo' } }; - var modelsRawFile = './models.definition.raw.js'; - var modelsFuncFile = './models.definition.funcs.js'; - var fixturesFile = './models.fixtures.json'; + const modelsRawFile = './models.definition.raw.js'; + const modelsFuncFile = './models.definition.funcs.js'; - var performTeardown; - var toreDown; + let performTeardown; + let toreDown; // Setup hapi server to register the plugin - beforeEach(function (done) { + beforeEach((done) => { server = new Hapi.Server(); server.connection(); @@ -95,29 +93,30 @@ experiment('Dogwater', function () { }); // Teardown unless the test dictates otherwise - afterEach(function (done) { + afterEach((done) => { if (performTeardown) { server.plugins.dogwater.teardown(done); - } else { + } + else { done(); } }); - test('takes its `models` option as a relative path.', function (done) { + test('takes its `models` option as a relative path.', (done) => { - var options = { + const options = { connections: connections, adapters: dummyAdapters, models: Path.normalize('./test/' + modelsRawFile) }; - var plugin = { + const plugin = { register: require('..'), options: options }; - server.register(plugin, function (err) { + server.register(plugin, (err) => { expect(err).to.not.exist(); done(); @@ -125,20 +124,20 @@ experiment('Dogwater', function () { }); - test('takes its `models` option as an absolute path.', function (done) { + test('takes its `models` option as an absolute path.', (done) => { - var options = { + const options = { connections: connections, adapters: dummyAdapters, models: Path.normalize(__dirname + '/' + modelsRawFile) }; - var plugin = { + const plugin = { register: require('..'), options: options }; - server.register(plugin, function (err) { + server.register(plugin, (err) => { expect(err).to.not.exist(); done(); @@ -146,20 +145,20 @@ experiment('Dogwater', function () { }); - test('takes its `models` option as an array of objects.', function (done) { + test('takes its `models` option as an array of objects.', (done) => { - var options = { + const options = { connections: connections, adapters: dummyAdapters, models: require(modelsRawFile) }; - var plugin = { + const plugin = { register: require('..'), options: options }; - server.register(plugin, function (err) { + server.register(plugin, (err) => { expect(err).to.not.exist(); done(); @@ -167,20 +166,20 @@ experiment('Dogwater', function () { }); - test('takes its `models` option as an array of functions returning objects.', function (done) { + test('takes its `models` option as an array of functions returning objects.', (done) => { - var options = { + const options = { connections: connections, adapters: dummyAdapters, models: require(modelsFuncFile) }; - var plugin = { + const plugin = { register: require('..'), options: options }; - server.register(plugin, function (err) { + server.register(plugin, (err) => { expect(err).to.not.exist(); done(); @@ -188,22 +187,24 @@ experiment('Dogwater', function () { }); - test('throws if the `models` option is not an array or string.', function (done) { + test('throws if the `models` option is not an array or string.', (done) => { - var options = { + const options = { connections: connections, adapters: dummyAdapters, models: { some: 'object' } }; - var plugin = { + const plugin = { register: require('..'), options: options }; - expect(function () { + expect(() => { - server.register(plugin, function (err) { + server.register(plugin, (err) => { + + expect(err).to.not.exist(); // We should never get here expect(true).to.not.equal(true); @@ -215,20 +216,20 @@ experiment('Dogwater', function () { done(); }); - test('takes its `adapters` specified as a string.', function (done) { + test('takes its `adapters` specified as a string.', (done) => { - var options = { + const options = { connections: connections, adapters: stringAdapters, models: Path.normalize(__dirname + '/' + modelsRawFile) }; - var plugin = { + const plugin = { register: require('..'), options: options }; - server.register(plugin, function (err) { + server.register(plugin, (err) => { expect(err).to.not.exist(); done(); @@ -236,26 +237,26 @@ experiment('Dogwater', function () { }); - test('exposes Waterline collections, connections, and schema.', function (done) { + test('exposes Waterline collections, connections, and schema.', (done) => { - var options = { + const options = { connections: connections, adapters: dummyAdapters, models: require(modelsRawFile) }; - var plugin = { + const plugin = { register: require('..'), options: options }; - server.register(plugin, function (err) { + server.register(plugin, (err) => { expect(err).not.to.exist(); - var collections = server.plugins.dogwater.collections; - var conns = server.plugins.dogwater.connections; - var schema = server.plugins.dogwater.schema; + const collections = server.plugins.dogwater.collections; + const conns = server.plugins.dogwater.connections; + const schema = server.plugins.dogwater.schema; expect(collections.bar).to.be.an.object(); expect(collections.zoo).to.be.an.object(); @@ -275,25 +276,25 @@ experiment('Dogwater', function () { }); - test('passes its `defaults` option to waterline.', function (done) { + test('passes its `defaults` option to waterline.', (done) => { - var options = { + const options = { connections: connections, adapters: dummyAdapters, models: require(modelsRawFile), defaults: defaults }; - var plugin = { + const plugin = { register: require('..'), options: options }; - server.register(plugin, function (err) { + server.register(plugin, (err) => { expect(err).to.not.exist(); - var collections = server.plugins.dogwater.collections; + const collections = server.plugins.dogwater.collections; expect(collections.bar.migrate).to.equal('create'); expect(collections.zoo.migrate).to.equal('safe'); @@ -302,27 +303,27 @@ experiment('Dogwater', function () { }); - test('exposes connection teardown method, skips when method missing.', function (done) { + test('exposes connection teardown method, skips when method missing.', (done) => { - var options = { + const options = { connections: connections, adapters: teardownNoMethodAdapters, models: require(modelsRawFile) }; - var plugin = { + const plugin = { register: require('..'), options: options }; - server.register(plugin, function (err) { + server.register(plugin, (err) => { expect(err).not.to.exist(); - var teardown = server.plugins.dogwater.teardown; + const teardown = server.plugins.dogwater.teardown; expect(toreDown).to.equal(false); - teardown(function (err) { + teardown((err) => { expect(err).to.not.exist(); expect(toreDown).to.equal(false); @@ -332,27 +333,27 @@ experiment('Dogwater', function () { }); - test('exposes connection teardown method, succeeds when adapter identity missing.', function (done) { + test('exposes connection teardown method, succeeds when adapter identity missing.', (done) => { - var options = { + const options = { connections: connections, adapters: teardownNoIdAdapters, models: require(modelsRawFile) }; - var plugin = { + const plugin = { register: require('..'), options: options }; - server.register(plugin, function (err) { + server.register(plugin, (err) => { expect(err).not.to.exist(); - var teardown = server.plugins.dogwater.teardown; + const teardown = server.plugins.dogwater.teardown; expect(toreDown).to.equal(false); - teardown(function (err) { + teardown((err) => { expect(err).to.not.exist(); expect(toreDown).to.equal(true); @@ -362,27 +363,27 @@ experiment('Dogwater', function () { }); - test('exposes connection teardown method, succeeds with identity and method.', function (done) { + test('exposes connection teardown method, succeeds with identity and method.', (done) => { - var options = { + const options = { connections: connections, adapters: teardownSuccessAdapters, models: require(modelsRawFile) }; - var plugin = { + const plugin = { register: require('..'), options: options }; - server.register(plugin, function (err) { + server.register(plugin, (err) => { expect(err).not.to.exist(); - var teardown = server.plugins.dogwater.teardown; + const teardown = server.plugins.dogwater.teardown; expect(toreDown).to.equal(false); - teardown(function (err) { + teardown((err) => { expect(err).to.not.exist(); expect(toreDown).to.equal(true); @@ -392,29 +393,29 @@ experiment('Dogwater', function () { }); - test('decorates Waterline collections onto request.', function (done) { + test('decorates Waterline collections onto request.', (done) => { - var options = { + const options = { connections: connections, adapters: dummyAdapters, models: require(modelsRawFile) }; - var plugin = { + const plugin = { register: require('..'), options: options }; - server.register(plugin, function (err) { + server.register(plugin, (err) => { expect(err).not.to.exist(); server.route({ path: '/', method: 'GET', - handler: function (request, reply) { + handler: (request, reply) => { - var collections = request.collections; + const collections = request.collections; expect(collections.bar).to.be.an.object(); expect(collections.zoo).to.be.an.object(); @@ -423,7 +424,7 @@ experiment('Dogwater', function () { } }); - server.inject({ url: '/', method: 'GET' }, function (response) { + server.inject({ url: '/', method: 'GET' }, (response) => { done(); }); @@ -431,20 +432,20 @@ experiment('Dogwater', function () { }); - test('decorates Waterline onto the server.', function (done) { + test('decorates Waterline onto the server.', (done) => { - var options = { + const options = { connections: connections, adapters: dummyAdapters, models: require(modelsRawFile) }; - var plugin = { + const plugin = { register: require('..'), options: options }; - server.register(plugin, function (err) { + server.register(plugin, (err) => { expect(err).to.not.exist(); expect(server.waterline).to.be.instanceof(Waterline); @@ -453,25 +454,25 @@ experiment('Dogwater', function () { }); - test('exposes the raw ORM to the model definition when the definition is specified as a function.', function (done) { + test('exposes the raw ORM to the model definition when the definition is specified as a function.', (done) => { - var options = { + const options = { connections: connections, adapters: dummyAdapters, models: require(modelsFuncFile) }; - var plugin = { + const plugin = { register: require('..'), options: options }; - server.register(plugin, function (err) { + server.register(plugin, (err) => { expect(err).to.not.exist(); - var Bar = server.waterline.collections.bar; - var Zoo = server.waterline.collections.zoo; + const Bar = server.waterline.collections.bar; + const Zoo = server.waterline.collections.zoo; expect(Bar).to.be.an.object(); expect(Zoo).to.be.an.object(); @@ -482,96 +483,20 @@ experiment('Dogwater', function () { }); - test('loads fixtures using waterline-fixtures with a standard object configuration.', function (done) { - - var options = { - connections: connections, - adapters: fixtureAdapters, - models: require(modelsRawFile), - fixtures: { - fixtures: require(fixturesFile) - } - }; - - var plugin = { - register: require('..'), - options: options - }; - - server.register(plugin, function (err) { - - expect(err).to.not.exist(); - - var collections = server.plugins.dogwater.collections; - - collections.bar.find() - .then(function (bars) { - - collections.zoo.find() - .then(function (zoos) { - - expect(bars).to.have.length(2); - expect(zoos).to.have.length(1); - - done(); - }); - - }); - }); - - }); - - test('loads fixtures using waterline-fixtures with fixture data directly specified.', function (done) { - - var options = { - connections: connections, - adapters: fixtureAdapters, - models: require(modelsRawFile), - fixtures: require(fixturesFile) - }; - - var plugin = { - register: require('..'), - options: options - }; - - server.register(plugin, function (err) { - - expect(err).to.not.exist(); - - var collections = server.plugins.dogwater.collections; - - collections.bar.find() - .then(function (bars) { - - collections.zoo.find() - .then(function (zoos) { - - expect(bars).to.have.length(2); - expect(zoos).to.have.length(1); - - done(); - }); - - }); - }); - - }); - - test('errors on Waterline failure.', function (done) { + test('errors on Waterline failure.', (done) => { - var options = { + const options = { connections: connections, adapters: failureAdapters, models: require(modelsRawFile) }; - var plugin = { + const plugin = { register: require('..'), options: options }; - server.register(plugin, function (err) { + server.register(plugin, (err) => { expect(err).to.exist(); expect(err.message).to.equal('Adapter test error!'); diff --git a/test/models.definition.funcs.js b/test/models.definition.funcs.js index 84285c0..73ec6ba 100644 --- a/test/models.definition.funcs.js +++ b/test/models.definition.funcs.js @@ -1,3 +1,5 @@ +'use strict'; + // Can pass waterline to model definition module.exports = [ function (waterline) { diff --git a/test/models.definition.raw.js b/test/models.definition.raw.js index 021271d..5fbb9d8 100644 --- a/test/models.definition.raw.js +++ b/test/models.definition.raw.js @@ -1,3 +1,5 @@ +'use strict'; + module.exports = [ { identity: 'bar', diff --git a/test/models.fixtures.json b/test/models.fixtures.json deleted file mode 100644 index 9e1a636..0000000 --- a/test/models.fixtures.json +++ /dev/null @@ -1,24 +0,0 @@ -[ - { - "model": "bar", - "items":[ - { - "id": 1, - "foo": "Stimpy" - }, - { - "id":2, - "foo": "Alonzo" - } - ] - }, - { - "model": "zoo", - "items":[ - { - "id": 1, - "ding": 666 - } - ] - } -] \ No newline at end of file