npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

hapi-sequelize-orm

v1.0.3

Published

Sequelize plugin for HapiJS (v17+)

Downloads

7

Readme

Build Status Coverage Status

Hapi Sequelize ORM

A Hapijs (v17+) plugin for relational databases using Sequelize

Installation

# npm
npm install hapi-sequelize-orm

# yarn
yarn add hapi-sequelize-orm

Depending on which database(s) you will work with, you will need one or more of these

# Postgres
npm install pg pg-hstore
yarn add pg pg-hstore

# MySQL
npm install mysql2
yarn add mysql2

# SQLite
npm install sqlite3
yarn add sqlite3

# MS SQL
npm install tedious
yarn add tedious

Options

Database options are passed as a javascript object with the following structure:

{
    models: {},
    ...sequelizeOpts
}

Model Definition

Models are javascript objects with the following properties:

  • required name: A string that will be used to register the model
  • required definition: an object following the Sequelize models definition
  • optional options: an object following the Sequelize model configuration. This controls the table behavior, like fields with urderscore, timestamps, etc.
  • optional associations: an array of objects with the following properties:
    • required type: The association type. The ASSOCIATION_TYPES variable that comes with this library is a direct export of sequelize.DataTypes.
    • required target: the name of the target model.
    • optional | required options: an object that defines the specific association. See Sequelize Associations for more details.

Example

// user.js | User Model
import { ASSOCIATION_TYPES, DATA_TYPES } from 'hapi-sequelize-orm';

export default {
    name: 'User',
    definition: {
        id: {
            type: DATA_TYPES.STRING,
            primaryKey: true
        },
        name: DATA_TYPES.STRING,
        lastname: DATA_TYPES.STRING,
        email: {
            type: DATA_TYPES.STRING,
            unique: true
        }
    },
    associations: [
        {
            type: ASSOCIATION_TYPES.belongsToMany,
            target: 'Job',
            options: {
                through: 'UserJob'
            }
        }
    ]
};

Usage

You can either implement this as a HapiJS plugin (intended) or as a standalone using the Orm class directly.

HapiJS Plugin

The plugin approach allows you to connect to multiple databases and access them from a single point, which is the plugin itself.

// File where you register your plugins
import { plugin as ormPlugin } from 'hapi-sequelize-orm';

// other plugins
await server.register({
    plugin: ormPlugin,
    options: {
        databases: {
            bookstore: {
                models: {/* model definitions */},
                database: 'bookstore',
                username: 'dev',
                password: '123456',
                dialect: 'mysql'
            },

            statistics: {
                models: {/* model definitions */},
                database: 'stats',
                username: 'dev',
                password: '123456',
                dialect: 'postgres'
            },
        }
    }
});
// Handler
import { QUERY_TYPES } from 'hapi-sequelize-orm';

server.route({
    method:'GET',
    path:'/books',
    handler: function (request) {
        const database = request.server['hapi-sequelize-orm'].getDb('bookstore');
        const books = await database.query('SELECT * FROM books', { raw: true, type: QUERY_TYPES.SELECT });
        // QUERY_TYPES is a direct export of sequelize.QueryTypes

        return books;
    }
});

Standalone

The standalone approach is almost the same as the plugin one. You would create a new instance of the Orm class with a single database configuration object (see Options) and then use the database. You could also test the connection and/or invoke the init() method just to be sure that you can connect to the database. You are in control.

The difference here is that you would need to expose the implementation through your own plugin (you can see the plugin implementation for reference) or instantiate a new database connection across your API everytime you need to perform a query.

// Handler
import { Orm, QUERY_TYPES } from 'hapi-sequelize-orm';
import dbOpts from 'path/to/config/dbOpts';

server.route({
    method:'GET',
    path:'/books',
    handler: function (request) {
        const database = new Orm(dbOpts);
        const books = await database.query('SELECT * FROM books', { raw: true, type: QUERY_TYPES.SELECT });
        // QUERY_TYPES is a direct export of sequelize.QueryTypes

        return books;
    }
});

Disclaimer

I created this library to avoid the copy/paste across personal projects where I interact with relational databases.

If you find it useful and have any feedback / issue / improvement in mind, create an issue describing it. I'm open to discussion.