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

Commit 6489e7e

Browse files
committed
Merge branch 'v2'
2 parents 330b853 + 95e672c commit 6489e7e

File tree

7 files changed

+111
-75
lines changed

7 files changed

+111
-75
lines changed

.eslintrc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"rules": {
3+
"quotes": [2, "single"]
4+
},
5+
"env": {
6+
"node": true
7+
}
8+
}

.gitignore

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,8 @@ node_modules
3030
.lock-wscript
3131

3232
# Webstorm
33-
.idea
33+
.idea
34+
35+
npm-debug.log
36+
37+
.DS_Store

README.md

Lines changed: 63 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
## hapi-sequelized - a plugin for using sequelize with hapi
1+
## hapi-sequelized - a hapi plugin for the sequelize orm
22

33
[![Build Status](https://travis-ci.org/danecando/hapi-sequelized.svg)](https://travis-ci.org/danecando/hapi-sequelized)
44
[![npm](https://img.shields.io/npm/dm/localeval.svg)](https://www.npmjs.com/package/hapi-sequelized)
@@ -9,56 +9,98 @@
99
### Installation
1010
npm install --save hapi-sequelized
1111

12-
### Setup
13-
See http://hapijs.com/tutorials/plugins if you're not sure how hapi plugins work but here is an example:
12+
### Loading the plugin
13+
See http://hapijs.com/tutorials/plugins
1414

1515
```javascript
1616
server.register(
1717
[
1818
{
1919
register: require('hapi-sequelized'),
2020
options: {
21-
models: 'models',
22-
database: 'dbname',
21+
database: 'dbName',
2322
user: 'root',
2423
pass: 'root',
25-
port: 8889
24+
dialect: 'mysql',
25+
port: 8889,
26+
models: 'models/**/*.js',
27+
sequelize: {
28+
define: {
29+
underscoredAll: true
30+
}
31+
}
2632
}
27-
}
33+
},
2834
], function(err) {
2935
if (err) {
30-
console.log('failed to load plugin');
36+
console.error('failed to load plugin');
3137
}
3238
}
3339
);
3440
```
3541

3642
### Available Options
43+
3744
```javascript
3845
options: {
39-
host: 'localhost', // db host
40-
database: 'dbName', // name of your db
41-
user: 'dbUser', // db username
42-
pass: 'dbPass', // db password
46+
database: 'dbName', // database name
47+
user: 'root', // db username
48+
pass: 'root', // db password
4349
dialect: 'mysql', // database type
50+
host: 'localhost', // db host
4451
port: 8889, // database port #
45-
models: 'models', // path to models directory from project root
46-
// or optionally use an array of multiple model folders,
47-
//e.g. ['features/products', 'features/customers']
48-
defaults: { } // see: http://sequelize.readthedocs.org/en/latest/docs/getting-started/#application-wide-model-options
52+
models: ['models/**/*.js', 'other/models/*.js'], // glob or an array of globs to directories containing your sequelize models
53+
logging: false // sql query logging
54+
sequelize: {} // Options object passed to the Sequelize constructor http://docs.sequelizejs.com/en/latest/api/sequelize/#new-sequelizedatabase-usernamenull-passwordnull-options
4955
}
5056
```
5157

5258
### Usage
53-
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.
54-
55-
Your models will be availble throughout your application via server.plugins (which is also available through the request object ie: request.server.plugins)
59+
Your models will be available throughout your application via server.plugins (which is also available through the request object ie: request.server.plugins)
5660

5761
```javascript
5862
var db = request.server.plugins['hapi-sequelized'].db;
5963

60-
db.Test.create({
64+
db.User.create({
6165
62-
password: 'alskfjdfoa'
66+
password: 'password123'
6367
});
6468
```
69+
70+
### Model Definitions
71+
Exports a function that returns a model definition
72+
http://docs.sequelizejs.com/en/latest/docs/models-definition/
73+
74+
```javascript
75+
module.exports = function(sequelize, DataTypes) {
76+
var StoreOptions = sequelize.define(
77+
'StoreOptions',
78+
{
79+
optionName: {
80+
type: DataTypes.STRING,
81+
unique: true,
82+
allowNull: false
83+
},
84+
optionValue: {
85+
type: DataTypes.TEXT
86+
}
87+
},
88+
{
89+
tableName: 'store_config',
90+
timestamps: false
91+
}
92+
);
93+
94+
return StoreOptions;
95+
};
96+
```
97+
98+
### Syncing Models
99+
Creates all your tables and sets up your database
100+
101+
```javascript
102+
var db = server.plugins['hapi-sequelized'].db;
103+
db.sequelize.sync().then(function() {
104+
console.log('models synced');
105+
});
106+
```

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
module.exports = require('./lib');
1+
module.exports = require('./lib');

lib/index.js

Lines changed: 26 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,51 @@
11
'use strict';
22

3-
var fs = require('fs');
4-
var path = require('path');
3+
var Hoek = require('hoek');
4+
var sqlizr = require('sqlizr');
55
var Sequelize = require('sequelize');
66

77
/**
8-
* Register sequelize as a Hapi plugin
8+
* Creates database connection, imports models, and exposes them via the db object
99
* @param plugin
1010
* @param options
1111
* @param next
1212
*/
1313
exports.register = function(plugin, options, next) {
1414

15-
var db = {};
16-
var dir = [];
17-
Array.isArray(options.models) ? dir = options.models : dir.push(options.models);
15+
// sequelize query output disabled in production
16+
var logging = process.env.NODE_ENV === 'production' ? false : console.log;
1817

19-
dir.forEach(function(folder, index, arr) {
20-
arr[index] = path.join(process.cwd(), folder);
21-
});
22-
23-
24-
var sequelize = new Sequelize(options.database, options.user, options.pass, {
18+
var defaultOptions = {
2519
host: options.host || 'localhost',
2620
dialect: options.dialect || 'mysql',
2721
port: options.port || 3306,
28-
storage: options.storage || '',
29-
define: options.defaults || {}
30-
});
31-
32-
33-
//recursively get all models in 'models' directory, and all nested sub-directories
34-
var getJSModels = function (folder) {
35-
var fileContents = fs.readdirSync(folder),
36-
stats;
37-
38-
fileContents.forEach(function (fileName) {
39-
stats = fs.lstatSync(folder + '/' + fileName);
40-
41-
if (stats.isDirectory()) {
42-
getJSModels(folder + '/' + fileName)
43-
} else {
44-
if(fileName.indexOf('.') !== 0) {
45-
var filePath = path.join(folder, fileName);
46-
var model = sequelize.import(filePath);
47-
db[model.name] = model;
48-
}
49-
}
50-
});
51-
22+
logging: logging
5223
};
5324

54-
dir.forEach(function(folder) {
55-
getJSModels(folder);
56-
});
25+
// apply top level plugin options to a full sequelize options object
26+
var sequelizeOptions = Hoek.applyToDefaults(defaultOptions, options.sequelize || {});
27+
28+
var sequelize = new Sequelize(options.database, options.user, options.pass, sequelizeOptions);
5729

58-
Object.keys(db).forEach(function(modelName) {
59-
if ("associate" in db[modelName]) {
60-
db[modelName].associate(db);
61-
}
62-
});
30+
// test the database connection
31+
sequelize.authenticate()
32+
.then(function() {
6333

64-
db.sequelize = sequelize;
65-
db.Sequelize = Sequelize;
34+
// import models
35+
var db = sqlizr(sequelize, options.models);
36+
db.sequelize = sequelize;
37+
db.Sequelize = Sequelize;
6638

67-
plugin.expose('db', db);
39+
// make available in hapi application
40+
plugin.expose('db', db);
6841

69-
next();
42+
return next();
43+
})
44+
.catch(function(err) {
45+
return next(err);
46+
});
7047
};
7148

7249
exports.register.attributes = {
7350
pkg: require('../package.json')
74-
};
51+
};

package.json

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
{
22
"name": "hapi-sequelized",
3-
"version": "1.3.1",
4-
"description": "A plugin for using sequlize with the hapi.js web framework",
3+
"version": "2.0.0",
4+
"description": "A hapi plugin for the sequelize ORM",
55
"main": "index.js",
66
"scripts": {
7-
"test": "lab"
7+
"test": "lab",
8+
"lint": "eslint lib/*.js"
89
},
910
"repository": {
1011
"type": "git",
@@ -33,5 +34,9 @@
3334
},
3435
"peerDependencies": {
3536
"sequelize": "2.*"
37+
},
38+
"dependencies": {
39+
"hoek": "^2.12.0",
40+
"sqlizr": "^1.0.0"
3641
}
3742
}

test/other-models/.DS_Store

-6 KB
Binary file not shown.

0 commit comments

Comments
 (0)