|
3 | 3 | http://hapijs.com/ |
4 | 4 | http://sequelizejs.com/ |
5 | 5 |
|
6 | | -This plugin is a port of the example of using sequelize with express from: http://sequelizejs.com/articles/express |
7 | | - |
8 | 6 | ## Installation |
9 | | -npm install for the hoek dependency |
| 7 | +npm install hapi-sequelized --save |
10 | 8 |
|
11 | 9 | ## Setup |
12 | 10 | See http://hapijs.com/tutorials/plugins if you're not sure how hapi plugins work but here is an example: |
13 | 11 |
|
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 | + }); |
21 | 34 | } |
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 | | - }); |
37 | 35 | }); |
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 | +``` |
49 | 49 |
|
50 | 50 | ## 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. |
52 | 52 |
|
53 | 53 | Your models will be availble throughout your application via server.plugins or plugin.plugins |
54 | 54 | See: http://hapijs.com/api#pluginplugins |
55 | 55 |
|
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 | +``` |
0 commit comments