Giter Site home page Giter Site logo

hapi-sequelize's Introduction

hapi-sequelize

Hapi.js plugin for the Sequelize ORM.

Compatibility

Compatible with hapi.js version 17.x and sequelize 4.x.

Installation

npm install --save @bakjs/sequelize

Configuration

Simply pass in your sequelize instance and a few basic options and voila. Options accepts a single object or an array for multiple dbs.

server.register([
    {
        register: require('@bakjs/sequelize'),
        options: [
            {
                // identifier
                name: 'dbname',

                // paths/globs to model files
                models: ['./server/models/**/*.js'],

                // sequelize: new Sequelize(/* ... */), // sequelize instance

                // or connection string
                connection: 'sqlite:dbname.db',

                // or connection options
                // connection: {
                //     dialect: '',
                //     username: '',
                //     password: ''
                // },

                // sync models - default false
                sync: true,

                // force sync (drops tables) - default false
                forceSync: false,
            },
        ],
    },
]);

Model Definitions

A model should export a function that returns a Sequelize model definition (http://docs.sequelizejs.com/en/latest/docs/models-definition/).

module.exports = function(sequelize, DataTypes) {
    const Category = sequelize.define('Category', {
        name: DataTypes.STRING,
        rootCategory: DataTypes.BOOLEAN,
    });

    return Category;
};

Setting Model associations

Using the sequelize model instance, define a method called associate, that is a function, and receives as parameter all models defined.

module.exports = function(sequelize, DataTypes) {
    const Category = sequelize.define('Category', {
        name: DataTypes.STRING,
        rootCategory: DataTypes.BOOLEAN,
    });

    Category.associate = function(models) {
        models.Category.hasMany(models.Product);
    };

    return Category;
};

Database Instances

Each registration adds a DB instance to the server.plugins['sequelize'] object with the name option as the key.

function DB(sequelize, models) {
    this.sequelize = sequelize;
    this.models = models;
}

// smth like this
server.plugins['sequelize'][opts.name] = new DB(opts.sequelize, models);

API

getDb(name)

The request object gets decorated with the method getDb. This allows you to easily grab a DB instance in a route handler. If you have multiple registrations pass the name of the one you would like returned or else the single or first registration will be returned.

handler(request, reply) {
    const db1 = request.getDb('db1');
    console.log(db1.sequelize);
    console.log(db1.models);
}

db.getModel('User')

Returns single model that matches the passed argument or null if the model doesn't exist.

db.getModels()

Returns all models on the db instance

License

MIT

Based on:

hapi-sequelize's People

Contributors

valtlfelipe avatar nargonath avatar renovate-bot avatar farnabaz avatar sougiovn avatar

Stargazers

 avatar Burak Sormageç avatar EvanJin avatar Pooya Parsa avatar

Watchers

James Cloos avatar Pooya Parsa avatar  avatar

Forkers

farnabaz

hapi-sequelize's Issues

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Repository problems

These problems occurred while renovating this repository. View logs.

  • WARN: Using npm packages for Renovate presets is now deprecated. Please migrate to repository-based presets instead.

Rate-Limited

These updates are currently rate-limited. Click on a checkbox below to force their creation now.

  • chore(deps): update devdependency eslint to v9
  • chore(deps): update devdependency prettier to v3
  • chore(deps): update npm to v10
  • 🔐 Create all rate-limited PRs at once 🔐

Edited/Blocked

These updates have been manually edited so Renovate will no longer make changes. To discard all commits and start over, click on a checkbox.

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

npm
package.json
  • glob 7.1.3
  • hoek 5.0.4
  • joi 13.7.0
  • code 5.2.3
  • coveralls 3.0.2
  • eslint 4.19.1
  • hapi 17.6.4
  • husky 0.14.3
  • lab 15.5.0
  • lint-staged 7.3.0
  • mysql2 1.6.2
  • prettier 1.14.3
  • sequelize 4.41.0
  • sqlite3 3.1.13
  • standard-version ^4.4.0
  • hapi ^17.6.4
  • sequelize ^4.41
  • node >=8.0.0
  • npm >=5.6.0
nvm
.nvmrc
  • node 8.12.0
travis
.travis.yml
  • node 8
  • node 9

  • Check this box to trigger a request for Renovate to run again on this repository

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.