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

Commit e1f7ab1

Browse files
committed
hapi 8 compatible & update with fix for npm install closing issue #2
1 parent 618d300 commit e1f7ab1

File tree

3 files changed

+53
-59
lines changed

3 files changed

+53
-59
lines changed

README.md

Lines changed: 45 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -3,59 +3,61 @@
33
http://hapijs.com/
44
http://sequelizejs.com/
55

6-
This plugin is a port of the example of using sequelize with express from: http://sequelizejs.com/articles/express
7-
86
## Installation
9-
npm install for the hoek dependency
7+
npm install hapi-sequelized --save
108

119
## Setup
1210
See http://hapijs.com/tutorials/plugins if you're not sure how hapi plugins work but here is an example:
1311

14-
server.pack.register({
15-
plugin: require('./hapi-sequelized'),
16-
options: {
17-
database: 'dbName',
18-
user: 'dbUser',
19-
pass: 'dbPass',
20-
dialect: 'mysql'
12+
```javascript
13+
server.pack.register({
14+
plugin: require('hapi-sequelized'),
15+
options: {
16+
database: 'dbName',
17+
user: 'dbUser',
18+
pass: 'dbPass',
19+
dialect: 'mysql'
20+
}
21+
}, function(err) {
22+
if (err) throw err;
23+
24+
var models = server.plugins['hapi-sequelized'].models;
25+
models
26+
.sequelize
27+
.sync()
28+
.complete(function(err) {
29+
if (err) throw err[0];
30+
else {
31+
server.start(function() {
32+
console.log('Server running at: ', server.info.uri);
33+
});
2134
}
22-
}, function(err) {
23-
if (err) throw err;
24-
25-
var models = server.plugins['hapi-sequelized'].models;
26-
models
27-
.sequelize
28-
.sync({ force: true })
29-
.complete(function(err) {
30-
if (err) throw err[0];
31-
else {
32-
server.start(function() {
33-
console.log('Server running at: ', server.info.uri);
34-
});
35-
}
36-
});
3735
});
38-
39-
40-
## Options
41-
42-
options: {
43-
database: 'dbName', // name of your db
44-
user: 'dbUser', // db username
45-
pass: 'dbPass', // db password
46-
dialect: 'mysql' // database type
47-
port: 8889 // database port #
48-
}
36+
});
37+
```
38+
39+
## All Options
40+
```javascript
41+
options: {
42+
database: 'dbName', // name of your db
43+
user: 'dbUser', // db username
44+
pass: 'dbPass', // db password
45+
dialect: 'mysql' // database type
46+
port: 8889 // database port #
47+
}
48+
```
4949

5050
## Usage
51-
Create your sequelize models in the hapi-sequelized directory. The plugin will automatically import all of your models and make them available throughout your application.
51+
Create your sequelize models in the models directory in the root of your hapi project. The plugin will automatically import all of your models and make them available throughout your application.
5252

5353
Your models will be availble throughout your application via server.plugins or plugin.plugins
5454
See: http://hapijs.com/api#pluginplugins
5555

56-
var db = plugin.plugins['hapi-sequelized'].models;
57-
58-
db.Test.create({
59-
60-
password: 'alskfjdfoa'
61-
});
56+
```javascript
57+
var db = plugin.plugins['hapi-sequelized'].models;
58+
59+
db.Test.create({
60+
61+
password: 'alskfjdfoa'
62+
});
63+
```

index.js

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
var fs = require('fs'),
22
path = require('path'),
33
Sequelize = require('sequelize'),
4-
Hoek = require('hoek'),
54
db = {};
65

76
exports.register = function(plugin, options, next) {
@@ -14,30 +13,26 @@ exports.register = function(plugin, options, next) {
1413

1514
// Add all models in directory to db object
1615
fs
17-
.readdirSync(__dirname)
16+
.readdirSync(path.resolve(__dirname, '../../models'))
1817
.filter(function(file) {
19-
return (file.indexOf('.') !== 0) && (file !== 'index.js') && (file !== 'package.json');
18+
return (file.indexOf('.') !== 0) && (file !== 'index.js'));
2019
})
2120
.forEach(function(file) {
22-
var model = sequelize.import(path.join(__dirname, file));
21+
var model = sequelize.import(path.join(path.resolve(__dirname, '../../models'), file));
2322
db[model.name] = model;
2423
});
2524

26-
27-
// Add associations here
28-
29-
3025
// Create associations
3126
Object.keys(db).forEach(function (modelName) {
3227
if ('associate' in db[modelName]) {
3328
db[modelName].associate(db)
3429
}
3530
});
3631

32+
db.sequelize = sequelize;
33+
db.Sequelize = Sequelize;
3734

38-
// Expose sequelize objects to the application
39-
plugin.expose('models', Hoek.merge(db, { sequelize: sequelize, Sequelize: Sequelize }));
40-
35+
plugin.expose('models', db);
4136

4237
next();
4338
};

package.json

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "hapi-sequelized",
3-
"version": "1.1.0",
4-
"description": "Port of sequelize's express demo to a hapi plugin",
3+
"version": "1.1.1",
4+
"description": "A plugin for using sequlize with the hapi.js web framework",
55
"main": "index.js",
66
"scripts": {
77
"test": "echo \"Error: no test specified\" && exit 1"
@@ -12,7 +12,4 @@
1212
],
1313
"author": "Dane Grant",
1414
"license": "MIT",
15-
"dependencies": {
16-
"hoek": "^2.4.1"
17-
}
1815
}

0 commit comments

Comments
 (0)