Skip to content
This repository was archived by the owner on May 2, 2021. It is now read-only.

Feature/handle sequelize connections errors #74

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,11 @@ 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;
return Models.applyRelations(Models.load(files, opts.sequelize.import.bind(opts.sequelize)));
}, (err) => {
return Promise.reject(
new Error("An error occurred while attempting to connect to DB [" + opts.name + "], please check the configuration. Details : " + err.message)
);
})
.then((models) => {
if (opts.sync) {
Expand All @@ -45,7 +48,7 @@ internals.configure = function (opts) {
.then((database) => {
if (opts.onConnect) {
let maybePromise = opts.onConnect(opts.sequelize);
if (maybePromise && typeof maybePromise.then === 'function')
if (maybePromise && typeof maybePromise.then === 'function')
return maybePromise.then(() => database);
}
return database;
Expand Down Expand Up @@ -77,13 +80,17 @@ exports.register = function(server, options, next) {
.then((db) => {
server.expose(opts.name, db);
return Promise.resolve(db);
}, (err) => {
return Promise.reject(err)
})
]);
}, []);

Promise.all(configured)
.then(() => {
return next()
},(err) => {
return next(err)
})
.catch((err) => {
return next(err)
Expand Down
43 changes: 39 additions & 4 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ const Sinon = require('sinon');
const Hapi = require('hapi');
const Sequelize = require('sequelize');

// Module globals
const internals = {};

// Test shortcuts
const lab = exports.lab = Lab.script();
const test = lab.test;
Expand All @@ -30,7 +27,7 @@ lab.suite('hapi-sequelize', () => {

const onConnect = function (database) {
server.log('onConnect called');
}
};

const spy = Sinon.spy(onConnect);

Expand Down Expand Up @@ -59,6 +56,44 @@ lab.suite('hapi-sequelize', () => {
})
});

test('plugin fails to register when Sequelize fails to connect', { parallel: true }, (done) => {

const server = new Hapi.Server();
server.connection();

const onConnect = function (database) {
server.log('onConnect called');
};

const spy = Sinon.spy(onConnect);

const sequelize = new Sequelize('shop', 'root', '', {
host: '127.0.0.1',
port: 3307,
dialect: 'mysql'
});

server.register([
{
register: require('../lib'),
options: [
{
name: 'shop',
models: ['./test/models/**/*.js'],
sequelize: sequelize,
sync: true,
forceSync: true,
onConnect: spy
}
]
}
], (err) => {
expect(err).to.exist();
expect(err.message).to.include('ECONNREFUSED');
done();
})
});

test('plugin throws error when no models are found', { parallel: true }, (done) => {

const server = new Hapi.Server();
Expand Down