@fredguile/hapi-sequelize
Advanced tools
Comparing version 5.0.0 to 6.0.0
@@ -28,34 +28,26 @@ /** | ||
// Module globals | ||
const internals = {}; | ||
async function configure(opts = {}) { | ||
const { forceSync, models, onConnect, sequelize, sync } = opts; | ||
if (!sequelize) throw new Error("Missing sequelize in options"); | ||
internals.configure = function(opts) { | ||
return opts.sequelize | ||
.authenticate() | ||
.then(() => { | ||
const files = Models.getFiles(opts.models); | ||
const models = Models.applyRelations( | ||
Models.load(files, opts.sequelize.import.bind(opts.sequelize)) | ||
); | ||
return models; | ||
}) | ||
.then(models => { | ||
if (opts.sync) { | ||
return opts.sequelize | ||
.sync({ force: opts.forceSync }) | ||
.then(() => new DB(opts.sequelize, models)); | ||
} | ||
return new DB(opts.sequelize, models); | ||
}) | ||
.then(database => { | ||
if (opts.onConnect) { | ||
let maybePromise = opts.onConnect(opts.sequelize); | ||
if (maybePromise && typeof maybePromise.then === "function") | ||
return maybePromise.then(() => database); | ||
} | ||
return database; | ||
}); | ||
}; | ||
await sequelize.authenticate(); | ||
exports.plugin = { | ||
const sequelizeModels = Models.applyRelations( | ||
Models.load(Models.getFiles(models), sequelize.import.bind(sequelize)) | ||
); | ||
if (sync) { | ||
await sequelize.sync({ force: forceSync }); | ||
} | ||
const database = new DB(sequelize, sequelizeModels); | ||
if (onConnect) { | ||
await onConnect(sequelize); | ||
} | ||
return database; | ||
} | ||
const HapiSequelize = { | ||
pkg: Pkg, | ||
@@ -69,10 +61,8 @@ register: async function register(server, options) { | ||
const getDb = request => { | ||
return function getDb(name) { | ||
if (!name || !request.server.plugins[Pkg.name].hasOwnProperty(name)) { | ||
const key = Object.keys(request.server.plugins[Pkg.name]).shift(); | ||
return request.server.plugins[Pkg.name][key]; | ||
} | ||
return request.server.plugins[Pkg.name][name]; | ||
}; | ||
const getDb = request => name => { | ||
if (!name || !request.server.plugins[Pkg.name].hasOwnProperty(name)) { | ||
const key = Object.keys(request.server.plugins[Pkg.name]).shift(); | ||
return request.server.plugins[Pkg.name][key]; | ||
} | ||
return request.server.plugins[Pkg.name][name]; | ||
}; | ||
@@ -82,13 +72,17 @@ | ||
const configured = options.reduce((acc, opts) => { | ||
return [].concat(acc, [ | ||
internals.configure(opts).then(db => { | ||
server.expose(opts.name, db); | ||
return Promise.resolve(db); | ||
}) | ||
]); | ||
}, []); | ||
await Promise.all(configured); | ||
await Promise.all( | ||
options.reduce( | ||
(acc, opts) => [ | ||
...acc, | ||
configure(opts).then(db => { | ||
server.expose(opts.name, db); | ||
return db; | ||
}) | ||
], | ||
[] | ||
) | ||
); | ||
} | ||
}; | ||
module.exports = HapiSequelize; |
@@ -18,2 +18,2 @@ 'use strict'; | ||
exports.options = Joi.alternatives().try(Joi.array().items(internals.option), internals.option); | ||
module.exports.options = Joi.alternatives().try(Joi.array().items(internals.option), internals.option); |
{ | ||
"name": "@fredguile/hapi-sequelize", | ||
"version": "5.0.0", | ||
"version": "6.0.0", | ||
"description": "A Hapi plugin for the fabulous Sequelize ORM", | ||
@@ -8,3 +8,4 @@ "main": "lib/index.js", | ||
"test": "lab --reporter junit --output report.xml", | ||
"test-cov-html": "lab -a code -r html -o coverage.html -r lcov -o lcov.info", | ||
"test-cov-html": | ||
"lab -a code -r html -o coverage.html -r lcov -o lcov.info", | ||
"coverage-report": "cat lcov.info | coveralls", | ||
@@ -11,0 +12,0 @@ "lint": "eslint lib/*.js" |
@@ -1,2 +0,2 @@ | ||
## hapi-sequelize - a hapi plugin for the sequelize orm | ||
## hapi-sequelize - a hapi plugin for the sequelize ORM | ||
@@ -7,8 +7,7 @@ [![Build Status](https://travis-ci.org/fredguile/hapi-sequelize.svg?branch=master)](https://travis-ci.org/fredguile/hapi-sequelize) | ||
### Important infos | ||
This version of `hapi-sequelize` should be compatible with Hapi 17.x & Sequelize 4.x. If you're | ||
This version of `hapi-sequelize` is compatible with Hapi 17.x & Sequelize 4.x. If you're | ||
encountering an issue related to any specific version please open an issue. | ||
### Installation | ||
@@ -21,22 +20,21 @@ | ||
Simply pass in your sequelize instance and a few basic options and voila. Options accepts a single object | ||
or an array for multiple dbs. | ||
or an array for multiple dbs. | ||
```javascript | ||
server.register([ | ||
{ | ||
plugin: require('hapi-sequelize').plugin, | ||
options: [ | ||
{ | ||
name: 'dbname', // identifier | ||
models: ['./server/models/**/*.js'], // paths/globs to model files | ||
sequelize: new Sequelize(config, opts), // sequelize instance | ||
sync: true, // sync models - default false | ||
forceSync: false, // force sync (drops tables) - default false | ||
onConnect: function (database) { // Optional | ||
// migrations, seeders, etc. | ||
} | ||
} | ||
] | ||
} | ||
]); | ||
await server.register({ | ||
plugin: require("hapi-sequelize"), | ||
options: [ | ||
{ | ||
name: "dbname", // identifier | ||
models: ["./server/models/**/*.js"], // paths/globs to model files | ||
sequelize: new Sequelize(config, opts), // sequelize instance | ||
sync: true, // sync models - default false | ||
forceSync: false, // force sync (drops tables) - default false | ||
onConnect: function(sequelize) { | ||
// Optional | ||
// migrations, seeders, etc. | ||
} | ||
} | ||
] | ||
}); | ||
``` | ||
@@ -46,3 +44,3 @@ | ||
Each registration adds a DB instance to the `server.plugins['hapi-sequelize']` object with the | ||
Each registration adds a DB instance to the `server.plugins['@fredguile/hapi-sequelize']` object with the | ||
name option as the key. | ||
@@ -54,6 +52,9 @@ | ||
this.models = models; | ||
} | ||
} | ||
// smth like this | ||
server.plugins['hapi-sequelize'][opts.name] = new DB(opts.sequelize, models); | ||
server.plugins["@fredguile/hapi-sequelize"][opts.name] = new DB( | ||
opts.sequelize, | ||
models | ||
); | ||
``` | ||
@@ -85,7 +86,8 @@ | ||
### Contributing | ||
If you have any ideas for useful additions to the API or any other improvements to the plugin | ||
please open an issue or a PR. | ||
Also feel free to tackle any of the outstanding todo's in the issues. These are mostly currently | ||
for testing, documentation. I hope to at least provide a reliable, developer friendly plugin. | ||
### Contributing | ||
If you have any ideas for useful additions to the API or any other improvements to the plugin | ||
please open an issue or a PR. | ||
Also feel free to tackle any of the outstanding todo's in the issues. These are mostly currently | ||
for testing, documentation. I hope to at least provide a reliable, developer friendly plugin. |
@@ -24,3 +24,3 @@ /* eslint-disable no-unused-vars */ | ||
lab.suite("@fredguile/hapi-sequelize", () => { | ||
test("plugin works", { parallel: true }, done => { | ||
test("plugin works", { parallel: true }, async () => { | ||
const server = new Hapi.Server(); | ||
@@ -35,36 +35,31 @@ | ||
const spy = Sinon.spy((sequelize) => server.log("onConnect called")); | ||
const spy = Sinon.spy(sequelize => server.log("onConnect called")); | ||
server | ||
.register([ | ||
await server.register({ | ||
plugin: require("../lib"), | ||
options: [ | ||
{ | ||
plugin: require("../lib").plugin, | ||
options: [ | ||
{ | ||
name: "shop", | ||
models: ["./test/models/**/*.js"], | ||
sequelize: sequelize, | ||
sync: true, | ||
forceSync: true, | ||
onConnect: spy | ||
} | ||
] | ||
name: "shop", | ||
models: ["./test/models/**/*.js"], | ||
sequelize, | ||
sync: true, | ||
forceSync: true, | ||
onConnect: spy | ||
} | ||
]) | ||
.then(() => { | ||
expect(server.plugins["@fredguile/hapi-sequelize"]).to.exist(); | ||
] | ||
}); | ||
expect( | ||
server.plugins["@fredguile/hapi-sequelize"]["shop"].sequelize | ||
).to.be.an.instanceOf(Sequelize); | ||
expect(server.plugins["@fredguile/hapi-sequelize"]).to.exist(); | ||
expect(spy.getCall(0).args[0]).to.be.an.instanceOf(Sequelize); | ||
expect( | ||
server.plugins["@fredguile/hapi-sequelize"]["shop"].sequelize | ||
).to.be.an.instanceOf(Sequelize); | ||
server.plugins["@fredguile/hapi-sequelize"]["shop"].sequelize | ||
.query("show tables", { type: Sequelize.QueryTypes.SELECT }) | ||
.then(tables => { | ||
expect(tables.length).to.equal(6); | ||
done(); | ||
}); | ||
}); | ||
expect(spy.getCall(0).args[0]).to.be.an.instanceOf(Sequelize); | ||
const tables = await server.plugins["@fredguile/hapi-sequelize"][ | ||
"shop" | ||
].sequelize.query("show tables", { type: Sequelize.QueryTypes.SELECT }); | ||
expect(tables.length).to.equal(6); | ||
}); | ||
@@ -75,3 +70,3 @@ | ||
{ parallel: true }, | ||
done => { | ||
async () => { | ||
const server = new Hapi.Server(); | ||
@@ -86,23 +81,20 @@ | ||
server | ||
.register([ | ||
{ | ||
plugin: require("../lib").plugin, | ||
options: [ | ||
{ | ||
name: "foo", | ||
models: ["./foo/**/*.js"], | ||
sequelize: sequelize, | ||
sync: true, | ||
forceSync: true | ||
} | ||
] | ||
} | ||
]) | ||
.catch(err => { | ||
expect(err).to.exist(); | ||
done(); | ||
try { | ||
await server.register({ | ||
plugin: require("../lib"), | ||
options: [ | ||
{ | ||
name: "foo", | ||
models: ["./foo/**/*.js"], | ||
sequelize, | ||
sync: true, | ||
forceSync: true | ||
} | ||
] | ||
}); | ||
} catch (err) { | ||
expect(err).to.exist(); | ||
} | ||
} | ||
); | ||
}); |
Sorry, the diff of this file is not supported yet
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 4 instances in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
89
4
14612
271