├── .editorconfig ├── .eslintrc ├── .gitignore ├── .travis.yml ├── 127.0.0.1 ├── AUTHORS ├── CHANGELOG.md ├── LICENSE ├── README.md ├── lib ├── DB.js ├── index.js ├── models.js └── schema.js ├── npm-shrinkwrap.json ├── package.json └── test ├── index.js └── models ├── blog ├── blog.js ├── blogroll.js └── post.js └── shop ├── product ├── category.js └── product.js └── user.js /.editorconfig: -------------------------------------------------------------------------------- 1 | # http://editorconfig.org 2 | root = true 3 | 4 | [*] 5 | charset = utf-8 6 | end_of_line = lf 7 | indent_size = 2 8 | indent_style = space 9 | insert_final_newline = true 10 | max_line_length = 80 11 | trim_trailing_whitespace = true 12 | 13 | [*.md] 14 | max_line_length = 0 15 | trim_trailing_whitespace = false 16 | -------------------------------------------------------------------------------- /.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "extends": ["eslint:recommended"], 3 | "parserOptions": { 4 | "ecmaVersion": 6, 5 | "sourceType": "module" 6 | }, 7 | "rules": { 8 | "no-console": 0, 9 | "padded-blocks": 0 10 | }, 11 | "env": { 12 | "es6": true, 13 | "node": true 14 | }, 15 | "globals": { 16 | 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | .idea 3 | .vscode 4 | npm-debug.log 5 | .DS_Store 6 | myhostname 7 | test/db 8 | coverage.html 9 | lcov.info 10 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | env: 3 | - NODE_ENV=travis 4 | node_js: 5 | - "4.4" 6 | - "5.10" 7 | - "6.3" 8 | sudo: false 9 | cache: 10 | directories: 11 | - node_modules 12 | before_script: 13 | - mysql -e 'create database shop;' 14 | - mysql -e 'create database blog;' 15 | script: 16 | - npm run lint 17 | - npm test 18 | -------------------------------------------------------------------------------- /127.0.0.1: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/danecando/hapi-sequelize/c577ce8efc03109d83695aa49509f77471d0dd77/127.0.0.1 -------------------------------------------------------------------------------- /AUTHORS: -------------------------------------------------------------------------------- 1 | Dane Grant 2 | YossiH 3 | Jacky Alcine 4 | Aaron Tribou 5 | mikeburgh 6 | Sujeet Pillai 7 | Zach Guo 8 | ben_acker 9 | Richard C Isaacson 10 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | * resolved conflicts 2 | * resolved conflicts 3 | * cb bug 4 | * readme update 5 | * bug fixes 6 | * other changaroos 7 | * untested v3 codez 8 | * plugin db instance class 9 | * model utility fns 10 | * new options schema 11 | * spring cleaning, v3 rewrite 12 | * Merge pull request #41 from zzbo/master 13 | * Update server.js 14 | * fix module name 15 | * bump 16 | * starting rewrite 17 | * fix ci img again 18 | * bump version fixed ci img 19 | * update name in tests 20 | * dropped d from name 21 | * changelog, authors, and editorconfig 22 | * updated module path in tests 23 | * wrong major ver 24 | * add sequelize to peerDependencies + remove root index following hapi guidelines 25 | * Merge branch 'master' of https://github.com/danecando/hapi-seq 26 | * Merge branch 'develop' 27 | * updated sequelize module 28 | * Merge pull request #30 from nvcexploder/master 29 | * Spelling fix on README 30 | * Merge branch 'jalcine-prs' 31 | * version bump 32 | * Merge branch 'master' of https://github.com/danecando/hapi-seq into jalcine-prs 33 | * Merge pull request #28 from jalcine/allow-uri-for-config 34 | * removed iojs 35 | * doc: Update provided options in the README. 36 | * lib: Drop initialization of 'sequelize' to undefined. 37 | * lib: Allow for connection string to be used. 38 | * test: Add test for connection URI. 39 | * test: Move sample options setting to 'beforeEach'. 40 | * Merge pull request #25 from mikeburgh/master 41 | * Merge pull request #1 from mikeburgh/mikeburgh-patch-1 42 | * Update README.md 43 | * version bump 44 | * fix sentence 45 | * removed peer dependency, updated to latest version of sequelize, readme additions 46 | * update version 47 | * Merge pull request #23 from zachguo/patch-1 48 | * give user freedom to configure logging 49 | * updated sqlizr & sync example 50 | * updated sequelize and readme * updated to sequelize 3, removed redundant models on db object, updated readme and example 51 | * Merge branch 'tests' 52 | * some fixes 53 | * empty password is null 54 | * host is on options 55 | * more test fixes 56 | * updated tests to support new options 57 | * connect to mysql with travis * Merge branch 'v2' 58 | * bad info 59 | * updated readme 60 | * remove prefix option 61 | * added lint, using sqlizr for model import, pass all options to sequelize constructor 62 | * remove node 0.10 from travis 63 | * mucking up the works 64 | * temporarily remove connection test 65 | * disable logging in production, check for successful database connection 66 | * err 67 | * disable logging in production, check for successful database connection 68 | * Merge pull request #15 from sujeetpillai/patch-2 69 | * Added storage option in object passed to Sequelize 70 | * Updating test case for storage option 71 | * added downloads badge 72 | * #11 simple example 73 | * update version 74 | * Merge pull request #14 from YossiH/master 75 | * Delete .DS_Store 76 | * Revert c14a4d2..099370e 77 | * Revert 888cbb4..2c7f79c 78 | * Merge remote-tracking branch 'origin/master' 79 | * Revert "multiple model folder feature added, tests added" 80 | * multiple model folder feature added, tests added 81 | * Merge pull request #13 from YossiH/YossiH-enh-recursive-models 82 | * more keywords 83 | * Merge pull request #1 from YossiH/YossiH-enh-recursive-models-1 84 | * Update index.js to recursively fetch models in nested folders 85 | * Update index.js 86 | * moved to lib directory to comply with hapi style 87 | * Merge pull request #10 from tribou/master 88 | * Add Travis CI build status 89 | * Write and pass tests for v1.2.3 Get 94.87% code coverage 90 | * Write and pass all v1.2.1 tests Add required testing packages to package.json Add test model User 91 | * up version 92 | * added new options to readme 93 | * up version 94 | * host and model default options #9 95 | * close #7 & #8 * updated for sequelize v2 & hapi v8, added model dir option #6 96 | * Merge pull request #3 from risaacson/syntax_fix 97 | * Found a syntax error with an extra close parenthesis. * fixed package.json 98 | * hapi 8 compatible & update with fix for npm install closing issue #2 99 | * corrected default port 100 | * name changed 101 | * changed named from hapi-seq to hapi-sequelized 102 | * replaced lodash with hoek 103 | * added options to readme 104 | * initial commit 105 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Dane Grant 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## hapi-sequelize - a hapi plugin for the sequelize orm 2 | 3 | [![Build Status](https://travis-ci.org/danecando/hapi-sequelize.svg?branch=master)](https://travis-ci.org/danecando/hapi-sequelize) 4 | [![npm](https://img.shields.io/npm/dm/localeval.svg)](https://www.npmjs.com/package/hapi-sequelize) 5 | 6 | 7 | ### Warning :warning: 8 | 9 | This project is no longer actively maintained. The current version has been tested for versions of Hapi up to 13.x & 10 | Sequelize 3.x. It is known to be incompatible with versions of Hapi 17+ & Sequelize 4+. 11 | 12 | There is a great fork of this project that has been in active development located at: https://github.com/valtlfelipe/hapi-sequelizejs 13 | 14 | ### Installation 15 | 16 | `npm install --save hapi-sequelize` 17 | 18 | ### Configuration 19 | 20 | Simply pass in your sequelize instance and a few basic options and voila. Options accepts a single object 21 | or an array for multiple dbs. 22 | 23 | ```javascript 24 | server.register([ 25 | { 26 | register: require('hapi-sequelize'), 27 | options: [ 28 | { 29 | name: 'dbname', // identifier 30 | models: ['./server/models/**/*.js'], // paths/globs to model files 31 | sequelize: new Sequelize(config, opts), // sequelize instance 32 | sync: true, // sync models - default false 33 | forceSync: false, // force sync (drops tables) - default false 34 | onConnect: function (database) { // Optional 35 | // migrations, seeders, etc. 36 | } 37 | } 38 | ] 39 | } 40 | ]); 41 | ``` 42 | 43 | ### Database Instances 44 | 45 | Each registration adds a DB instance to the `server.plugins['hapi-sequelize']` object with the 46 | name option as the key. 47 | 48 | ```javascript 49 | function DB(sequelize, models) { 50 | this.sequelize = sequelize; 51 | this.models = models; 52 | } 53 | 54 | // smth like this 55 | server.plugins['hapi-sequelize'][opts.name] = new DB(opts.sequelize, models); 56 | ``` 57 | 58 | ### Usage with Glue 59 | 60 | If you use [Glue](https://github.com/hapijs/glue) to compose your server, you'll need to load ``hapi-sequelize`` like this; 61 | 62 | ```javascript 63 | var manifest = require('./config/manifest'); 64 | 65 | manifest.registrations.push({ 66 | "plugin": { 67 | "register": "hapi-sequelize", 68 | "options": { 69 | "name": "name", 70 | "models": 'models/models/*.js', 71 | "sequelize": new Sequelize(config, opts), 72 | "sync": true, 73 | "forceSync": false // force sync (drops tables) - default false 74 | } 75 | } 76 | }) 77 | 78 | // Load the manifest and start the server 79 | ``` 80 | 81 | ### API 82 | 83 | #### `getDb(name)` 84 | 85 | The request object gets decorated with the method `getDb`. This allows you to easily grab a 86 | DB instance in a route handler. If you have multiple registrations pass the name of the one 87 | you would like returned or else the single or first registration will be returned. 88 | 89 | ```javascript 90 | handler(request, reply) { 91 | const db1 = request.getDb('db1'); 92 | console.log(db1.sequelize); 93 | console.log(db1.models); 94 | } 95 | ``` 96 | 97 | #### `db.getModel('User')` 98 | 99 | Returns single model that matches the passed argument or null if the model doesn't exist. 100 | 101 | #### `db.getModels()` 102 | 103 | Returns all models on the db instance 104 | 105 | ### Contributing 106 | If you have any ideas for useful additions to the API or any other improvements to the plugin 107 | please open an issue or a PR. 108 | 109 | Also feel free to tackle any of the outstanding todo's in the issues. These are mostly currently 110 | for testing, documentation. I hope to at least provide a reliable, developer friendly plugin. 111 | -------------------------------------------------------------------------------- /lib/DB.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | function DB(sequelize, models) { 4 | this.sequelize = sequelize; 5 | this.models = models; 6 | } 7 | 8 | DB.prototype.getModel = function getModel(name) { 9 | return this.models.hasOwnProperty(name) ? this.models[name] : null; 10 | }; 11 | 12 | DB.prototype.getModels = function getModels() { 13 | return this.models; 14 | }; 15 | 16 | module.exports = DB; 17 | -------------------------------------------------------------------------------- /lib/index.js: -------------------------------------------------------------------------------- 1 | /** 2 | * hapi-sequelize 3 | * 4 | * A Hapi plugin for the Sequelize ORM 5 | * 6 | * ## config 7 | * [{ 8 | * name: 'dbname', 9 | * models: ['path/one/*.js', 'path/two/*.js'], 10 | * sequelize: new Sequelize(options), 11 | * sync: true, 12 | * forceSync: false, 13 | * onConnect: function (database) { ... }, 14 | * debug: true 15 | * }] 16 | * 17 | * @exports register 18 | */ 19 | 20 | 'use strict'; 21 | 22 | const Joi = require('joi'); 23 | const Schema = require('./schema'); 24 | const Models = require('./models'); 25 | const DB = require('./DB'); 26 | const Pkg = require('../package.json'); 27 | 28 | // Module globals 29 | const internals = {}; 30 | 31 | internals.configure = function (opts) { 32 | return opts.sequelize.authenticate() 33 | .then(() => { 34 | const files = Models.getFiles(opts.models, opts.ignoredModels); 35 | const models = Models.applyRelations(Models.load(files, opts.sequelize.import.bind(opts.sequelize))); 36 | return models; 37 | }) 38 | .then((models) => { 39 | if (opts.sync) { 40 | return opts.sequelize.sync({ force: opts.forceSync }) 41 | .then(() => new DB(opts.sequelize, models)); 42 | } 43 | return new DB(opts.sequelize, models); 44 | }) 45 | .then((database) => { 46 | if (opts.onConnect) { 47 | let maybePromise = opts.onConnect(opts.sequelize); 48 | if (maybePromise && typeof maybePromise.then === 'function') 49 | return maybePromise.then(() => database); 50 | } 51 | return database; 52 | }); 53 | }; 54 | 55 | exports.register = function(server, options, next) { 56 | if (!options) throw new Error('Missing hapi-sequelize plugin options'); 57 | if (!Array.isArray(options)) options = [options]; 58 | 59 | const validation = Joi.validate(options, Schema.options); 60 | if (!validation || validation.error) throw validation.error; 61 | 62 | const getDb = (request) => { 63 | return function getDb(name) { 64 | if (!name || !request.server.plugins[Pkg.name].hasOwnProperty(name)) { 65 | const key = Object.keys(request.server.plugins[Pkg.name]).shift(); 66 | return request.server.plugins[Pkg.name][key]; 67 | } 68 | return request.server.plugins[Pkg.name][name]; 69 | }; 70 | }; 71 | 72 | server.decorate('request', 'getDb', getDb, { apply: true }); 73 | 74 | const configured = options.reduce((acc, opts) => { 75 | return [].concat(acc, [ 76 | internals.configure(opts) 77 | .then((db) => { 78 | server.expose(opts.name, db); 79 | return Promise.resolve(db); 80 | }) 81 | ]); 82 | }, []); 83 | 84 | Promise.all(configured) 85 | .then(() => { 86 | return next() 87 | }) 88 | .catch((err) => { 89 | return next(err) 90 | }); 91 | }; 92 | 93 | 94 | exports.register.attributes = { 95 | pkg: Pkg 96 | }; 97 | -------------------------------------------------------------------------------- /lib/models.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const Path = require('path'); 4 | const Glob = require('glob'); 5 | 6 | function getFiles(paths, ignored) { 7 | const opts = { 8 | nodir: true, 9 | dot: false 10 | }; 11 | 12 | if (!Array.isArray(paths)) paths = [paths]; 13 | if (ignored) opts.ignore = ignored; 14 | 15 | return paths.reduce((acc, pattern) => { 16 | const joinPaths = Array.prototype.concat.bind([], acc); 17 | try { 18 | const paths = Glob.sync(pattern, opts); 19 | return joinPaths(paths); 20 | } catch (e) { 21 | console.error(e); 22 | return joinPaths([]); 23 | } 24 | }, []); 25 | } 26 | 27 | function load(files, fn) { 28 | if (!files || !files.length) throw new Error('No model files were found'); 29 | if (files && !Array.isArray(files)) files = [files]; 30 | return files.reduce((acc, file) => { 31 | const models = {}; 32 | const filepath = Path.isAbsolute(file) ? file : Path.join(process.cwd(), file); 33 | try { 34 | const Model = fn(filepath); 35 | models[Model.name] = Model; 36 | } catch (e) { 37 | console.error(e); 38 | } 39 | return Object.assign({}, acc, models); 40 | }, {}); 41 | } 42 | 43 | function applyRelations(models) { 44 | if (!models || typeof models !== 'object') 45 | throw new Error('Can\'t apply relationships on invalid models object'); 46 | 47 | Object.keys(models).forEach((name) => { 48 | if (models[name].hasOwnProperty('associate')) { 49 | models[name].associate(models); 50 | } 51 | }); 52 | 53 | return models; 54 | } 55 | 56 | module.exports = { 57 | getFiles: getFiles, 58 | load: load, 59 | applyRelations: applyRelations 60 | }; 61 | 62 | -------------------------------------------------------------------------------- /lib/schema.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const Joi = require('joi'); 4 | const Sequelize = require('sequelize'); 5 | 6 | const internals = {}; 7 | 8 | internals.option = exports.option = Joi.object().keys({ 9 | name: Joi.string().token().required(), 10 | models: Joi.alternatives().try(Joi.string(), Joi.array().items(Joi.string())), 11 | ignoredModels: Joi.alternatives().try(Joi.string(), Joi.array().items(Joi.string())), 12 | sequelize: Joi.object().type(Sequelize).required(), 13 | sync: Joi.boolean().default(false), 14 | forceSync: Joi.boolean().default(false), 15 | debug: Joi.boolean(), 16 | onConnect: Joi.func().arity(1) 17 | }); 18 | 19 | exports.options = Joi.alternatives().try(Joi.array().items(internals.option), internals.option); 20 | -------------------------------------------------------------------------------- /npm-shrinkwrap.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "hapi-sequelize", 3 | "version": "3.0.4", 4 | "dependencies": { 5 | "balanced-match": { 6 | "version": "0.4.2", 7 | "from": "balanced-match@>=0.4.1 <0.5.0", 8 | "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-0.4.2.tgz" 9 | }, 10 | "brace-expansion": { 11 | "version": "1.1.6", 12 | "from": "brace-expansion@>=1.0.0 <2.0.0", 13 | "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.6.tgz" 14 | }, 15 | "concat-map": { 16 | "version": "0.0.1", 17 | "from": "concat-map@0.0.1", 18 | "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz" 19 | }, 20 | "fs.realpath": { 21 | "version": "1.0.0", 22 | "from": "fs.realpath@>=1.0.0 <2.0.0", 23 | "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz" 24 | }, 25 | "glob": { 26 | "version": "7.1.1", 27 | "from": "glob@>=7.0.3 <8.0.0", 28 | "resolved": "https://registry.npmjs.org/glob/-/glob-7.1.1.tgz" 29 | }, 30 | "hoek": { 31 | "version": "4.1.0", 32 | "from": "hoek@>=4.0.0 <5.0.0", 33 | "resolved": "https://registry.npmjs.org/hoek/-/hoek-4.1.0.tgz" 34 | }, 35 | "inflight": { 36 | "version": "1.0.6", 37 | "from": "inflight@>=1.0.4 <2.0.0", 38 | "resolved": "https://registry.npmjs.org/inflight/-/inflight-1.0.6.tgz" 39 | }, 40 | "inherits": { 41 | "version": "2.0.3", 42 | "from": "inherits@>=2.0.0 <3.0.0", 43 | "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.3.tgz" 44 | }, 45 | "isemail": { 46 | "version": "2.2.1", 47 | "from": "isemail@>=2.0.0 <3.0.0", 48 | "resolved": "https://registry.npmjs.org/isemail/-/isemail-2.2.1.tgz" 49 | }, 50 | "joi": { 51 | "version": "8.4.2", 52 | "from": "joi@>=8.1.0 <9.0.0", 53 | "resolved": "https://registry.npmjs.org/joi/-/joi-8.4.2.tgz" 54 | }, 55 | "minimatch": { 56 | "version": "3.0.3", 57 | "from": "minimatch@>=3.0.2 <4.0.0", 58 | "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.0.3.tgz" 59 | }, 60 | "moment": { 61 | "version": "2.17.0", 62 | "from": "moment@>=2.0.0 <3.0.0", 63 | "resolved": "https://registry.npmjs.org/moment/-/moment-2.17.0.tgz" 64 | }, 65 | "once": { 66 | "version": "1.4.0", 67 | "from": "once@>=1.3.0 <2.0.0", 68 | "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz" 69 | }, 70 | "path-is-absolute": { 71 | "version": "1.0.1", 72 | "from": "path-is-absolute@>=1.0.0 <2.0.0", 73 | "resolved": "https://registry.npmjs.org/path-is-absolute/-/path-is-absolute-1.0.1.tgz" 74 | }, 75 | "topo": { 76 | "version": "2.0.2", 77 | "from": "topo@>=2.0.0 <3.0.0", 78 | "resolved": "https://registry.npmjs.org/topo/-/topo-2.0.2.tgz" 79 | }, 80 | "wrappy": { 81 | "version": "1.0.2", 82 | "from": "wrappy@>=1.0.0 <2.0.0", 83 | "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz" 84 | } 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "hapi-sequelize", 3 | "version": "3.0.6", 4 | "description": "A Hapi plugin for the fabulous Sequelize ORM", 5 | "main": "lib/index.js", 6 | "scripts": { 7 | "test": "lab", 8 | "test-cov-html": "lab -a code -r html -o coverage.html -r lcov -o lcov.info", 9 | "coverage-report": "cat lcov.info | coveralls", 10 | "lint": "eslint lib/*.js" 11 | }, 12 | "repository": { 13 | "type": "git", 14 | "url": "https://github.com/danecando/hapi-sequelize.git" 15 | }, 16 | "keywords": [ 17 | "sequelize", 18 | "hapi", 19 | "mysql", 20 | "sqlite", 21 | "postgresql", 22 | "postgres", 23 | "orm", 24 | "plugin", 25 | "database", 26 | "node", 27 | "javascript" 28 | ], 29 | "author": "Dane Grant", 30 | "license": "MIT", 31 | "dependencies": { 32 | "glob": "^7.0.3", 33 | "joi": "^8.1.0" 34 | }, 35 | "devDependencies": { 36 | "code": "^4.0.0", 37 | "coveralls": "^2.11.14", 38 | "eslint": "^3.7.1", 39 | "hapi": "^15.1.1", 40 | "lab": "^11.1.0", 41 | "mysql": "^2.10.2", 42 | "sequelize": "^3.24.3", 43 | "sinon": "^1.17.6" 44 | }, 45 | "peerDependencies": { 46 | "sequelize": "^3.x" 47 | }, 48 | "engines": { 49 | "node": ">=4.0.0" 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /test/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | // Load modules 4 | const Lab = require('lab'); 5 | const Code = require('code'); 6 | const Sinon = require('sinon'); 7 | const Hapi = require('hapi'); 8 | const Sequelize = require('sequelize'); 9 | 10 | // Module globals 11 | const internals = {}; 12 | 13 | // Test shortcuts 14 | const lab = exports.lab = Lab.script(); 15 | const test = lab.test; 16 | const expect = Code.expect; 17 | 18 | lab.suite('hapi-sequelize', () => { 19 | 20 | test('plugin works', { parallel: true }, (done) => { 21 | 22 | const server = new Hapi.Server(); 23 | server.connection(); 24 | 25 | const sequelize = new Sequelize('shop', 'root', '', { 26 | host: '127.0.0.1', 27 | port: 3306, 28 | dialect: 'mysql' 29 | }); 30 | 31 | const onConnect = function (database) { 32 | server.log('onConnect called'); 33 | } 34 | 35 | const spy = Sinon.spy(onConnect); 36 | 37 | server.register([ 38 | { 39 | register: require('../lib'), 40 | options: [ 41 | { 42 | name: 'shop', 43 | models: ['./test/models/**/*.js'], 44 | sequelize: sequelize, 45 | sync: true, 46 | forceSync: true, 47 | onConnect: spy 48 | } 49 | ] 50 | } 51 | ], (err) => { 52 | expect(err).to.not.exist(); 53 | expect(server.plugins['hapi-sequelize']['shop'].sequelize).to.be.an.instanceOf(Sequelize); 54 | expect(spy.getCall(0).args[0]).to.be.an.instanceOf(Sequelize); 55 | server.plugins['hapi-sequelize']['shop'].sequelize.query('show tables', { type: Sequelize.QueryTypes.SELECT }).then((tables) => { 56 | expect(tables.length).to.equal(6); 57 | done(); 58 | }); 59 | }) 60 | }); 61 | 62 | test('plugin throws error when no models are found', { parallel: true }, (done) => { 63 | 64 | const server = new Hapi.Server(); 65 | server.connection(); 66 | 67 | const sequelize = new Sequelize('shop', 'root', '', { 68 | host: '127.0.0.1', 69 | port: 3306, 70 | dialect: 'mysql' 71 | }); 72 | 73 | server.register([ 74 | { 75 | register: require('../lib'), 76 | options: [ 77 | { 78 | name: 'foo', 79 | models: ['./foo/**/*.js'], 80 | sequelize: sequelize, 81 | sync: true, 82 | forceSync: true 83 | } 84 | ] 85 | } 86 | ], (err) => { 87 | expect(err).to.exist(); 88 | done(); 89 | }) 90 | }); 91 | }); 92 | -------------------------------------------------------------------------------- /test/models/blog/blog.js: -------------------------------------------------------------------------------- 1 | // return User model as a function to sequelize.import() 2 | 3 | module.exports = function (sequelize, DataTypes) { 4 | return sequelize.define('Blog', { 5 | name: DataTypes.STRING, 6 | url: DataTypes.STRING, 7 | owner: DataTypes.STRING 8 | }); 9 | }; 10 | -------------------------------------------------------------------------------- /test/models/blog/blogroll.js: -------------------------------------------------------------------------------- 1 | // return User model as a function to sequelize.import() 2 | 3 | module.exports = function (sequelize, DataTypes) { 4 | return sequelize.define('BlogRoll', { 5 | blogUrl: DataTypes.STRING, 6 | previousBlogUrl: DataTypes.STRING, 7 | nextBlogUrl: DataTypes.STRING 8 | }); 9 | }; 10 | -------------------------------------------------------------------------------- /test/models/blog/post.js: -------------------------------------------------------------------------------- 1 | // return User model as a function to sequelize.import() 2 | 3 | module.exports = function (sequelize, DataTypes) { 4 | return sequelize.define('Post', { 5 | title: DataTypes.STRING, 6 | content: DataTypes.TEXT 7 | }); 8 | }; 9 | -------------------------------------------------------------------------------- /test/models/shop/product/category.js: -------------------------------------------------------------------------------- 1 | // return User model as a function to sequelize.import() 2 | 3 | module.exports = function (sequelize, DataTypes) { 4 | return sequelize.define('Category', { 5 | name: DataTypes.STRING, 6 | rootCategory: DataTypes.BOOLEAN 7 | }); 8 | }; 9 | -------------------------------------------------------------------------------- /test/models/shop/product/product.js: -------------------------------------------------------------------------------- 1 | // return User model as a function to sequelize.import() 2 | 3 | module.exports = function (sequelize, DataTypes) { 4 | return sequelize.define('Product', { 5 | name: DataTypes.STRING, 6 | inventory: DataTypes.INTEGER 7 | }); 8 | }; 9 | -------------------------------------------------------------------------------- /test/models/shop/user.js: -------------------------------------------------------------------------------- 1 | // return User model as a function to sequelize.import() 2 | 3 | module.exports = function (sequelize, DataTypes) { 4 | return sequelize.define('User', { 5 | email: DataTypes.STRING, 6 | password: DataTypes.STRING 7 | }); 8 | }; 9 | --------------------------------------------------------------------------------