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

Commit 118e3c4

Browse files
committed
updated for sequelize v2 & hapi v8, added model dir option #6
1 parent bbabbaf commit 118e3c4

File tree

3 files changed

+49
-49
lines changed

3 files changed

+49
-49
lines changed

README.md

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# hapi-sequelized - a plugin for using sequelize with hapi
22

3-
http://hapijs.com/
4-
http://sequelizejs.com/
3+
* http://hapijs.com/
4+
* http://sequelizejs.com/
55

66
## Installation
77
npm install hapi-sequelized --save
@@ -10,51 +10,46 @@ npm install hapi-sequelized --save
1010
See http://hapijs.com/tutorials/plugins if you're not sure how hapi plugins work but here is an example:
1111

1212
```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-
});
13+
server.register(
14+
[
15+
{
16+
register: require('hapi-sequelized'),
17+
options: {
18+
models: 'models',
19+
database: 'dbname',
20+
user: 'root',
21+
pass: 'root',
22+
port: 8889
23+
}
3424
}
35-
});
36-
});
25+
], function(err) {
26+
if (err) {
27+
console.log('failed to load plugin');
28+
}
29+
}
30+
);
3731
```
3832

39-
## All Options
33+
## Available Options
4034
```javascript
4135
options: {
4236
database: 'dbName', // name of your db
4337
user: 'dbUser', // db username
4438
pass: 'dbPass', // db password
4539
dialect: 'mysql' // database type
4640
port: 8889 // database port #
41+
models: 'models' // path to models directory from project root
4742
}
4843
```
4944

5045
## Usage
5146
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.
5247

53-
Your models will be availble throughout your application via server.plugins or plugin.plugins
48+
Your models will be availble throughout your application via server.plugins (which is also available through the request object ie: request.server.plugins) or plugin.plugins
5449
See: http://hapijs.com/api#pluginplugins
5550

5651
```javascript
57-
var db = plugin.plugins['hapi-sequelized'].models;
52+
var db = request.server.plugins['hapi-sequelized'].db;
5853

5954
db.Test.create({
6055

index.js

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,43 @@
1-
var fs = require('fs'),
2-
path = require('path'),
3-
Sequelize = require('sequelize'),
4-
db = {};
5-
1+
"use strict";
2+
3+
var fs = require('fs');
4+
var path = require('path');
5+
var Sequelize = require('sequelize');
6+
7+
/**
8+
* Register sequelize as a Hapi plugin
9+
* @param plugin
10+
* @param options
11+
* @param next
12+
*/
613
exports.register = function(plugin, options, next) {
714

8-
// Create db connection
15+
var db = {};
16+
var dir = path.join(process.env.PWD, options.models);
17+
918
var sequelize = new Sequelize(options.database, options.user, options.pass, {
10-
dialect: options.dialect || 'mysql',
19+
dialect: options.dialect || 'mysql',
1120
port: options.port || 3306
1221
});
1322

14-
// Add all models in directory to db object
15-
fs
16-
.readdirSync(path.resolve(__dirname, '../../models'))
17-
.filter(function(file) {
18-
return (file.indexOf('.') !== 0) && (file !== 'index.js');
19-
})
20-
.forEach(function(file) {
21-
var model = sequelize.import(path.join(path.resolve(__dirname, '../../models'), file));
23+
fs.readdirSync(dir).filter(function(file) {
24+
return file.indexOf('.') !== 0;
25+
}).forEach(function(file) {
26+
var filePath = path.join(dir, file);
27+
var model = sequelize.import(filePath);
2228
db[model.name] = model;
2329
});
2430

25-
// Create associations
26-
Object.keys(db).forEach(function (modelName) {
27-
if ('associate' in db[modelName]) {
28-
db[modelName].associate(db)
31+
Object.keys(db).forEach(function(modelName) {
32+
if ("associate" in db[modelName]) {
33+
db[modelName].associate(db);
2934
}
3035
});
3136

3237
db.sequelize = sequelize;
3338
db.Sequelize = Sequelize;
3439

35-
plugin.expose('models', db);
40+
plugin.expose('db', db);
3641

3742
next();
3843
};

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "hapi-sequelized",
3-
"version": "1.1.2",
3+
"version": "1.2.0",
44
"description": "A plugin for using sequlize with the hapi.js web framework",
55
"main": "index.js",
66
"scripts": {

0 commit comments

Comments
 (0)