|
1 | | -## hapi-sequelized - a plugin for using sequelize with hapi |
| 1 | +## hapi-sequelized - a hapi plugin for the sequelize orm |
2 | 2 |
|
3 | 3 | [](https://travis-ci.org/danecando/hapi-sequelized) |
4 | 4 | [](https://www.npmjs.com/package/hapi-sequelized) |
|
9 | 9 | ### Installation |
10 | 10 | npm install --save hapi-sequelized |
11 | 11 |
|
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 |
14 | 14 |
|
15 | 15 | ```javascript |
16 | 16 | server.register( |
17 | 17 | [ |
18 | 18 | { |
19 | 19 | register: require('hapi-sequelized'), |
20 | 20 | options: { |
21 | | - models: 'models', |
22 | | - database: 'dbname', |
| 21 | + database: 'dbName', |
23 | 22 | user: 'root', |
24 | 23 | pass: 'root', |
25 | | - port: 8889 |
| 24 | + dialect: 'mysql', |
| 25 | + port: 8889, |
| 26 | + models: 'models/**/*.js', |
| 27 | + sequelize: { |
| 28 | + define: { |
| 29 | + underscoredAll: true |
| 30 | + } |
| 31 | + } |
26 | 32 | } |
27 | | - } |
| 33 | + }, |
28 | 34 | ], function(err) { |
29 | 35 | if (err) { |
30 | | - console.log('failed to load plugin'); |
| 36 | + console.error('failed to load plugin'); |
31 | 37 | } |
32 | 38 | } |
33 | 39 | ); |
34 | 40 | ``` |
35 | 41 |
|
36 | 42 | ### Available Options |
| 43 | + |
37 | 44 | ```javascript |
38 | 45 | 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 |
43 | 49 | dialect: 'mysql', // database type |
| 50 | + host: 'localhost', // db host |
44 | 51 | 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: {} // Application wide model options object passed to the Sequelize constructor http://docs.sequelizejs.com/en/latest/api/sequelize/#new-sequelizedatabase-usernamenull-passwordnull-options |
49 | 55 | } |
50 | 56 | ``` |
51 | 57 |
|
52 | 58 | ### 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) |
56 | 60 |
|
57 | 61 | ```javascript |
58 | 62 | var db = request.server.plugins['hapi-sequelized'].db; |
59 | 63 |
|
60 | | -db.Test.create({ |
| 64 | +db.User.create({ |
61 | 65 | |
62 | | - password: 'alskfjdfoa' |
| 66 | + password: 'password123' |
63 | 67 | }); |
64 | 68 | ``` |
| 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 | +``` |
0 commit comments