# hapi-sequelize-load

> load the sequelize models/associates for hapi

Latest version **1.2.3** (published 2017-10-29) · MIT license · 0 weekly downloads

## Install

```sh
npm install hapi-sequelize-load
pnpm add hapi-sequelize-load
yarn add hapi-sequelize-load
bun add hapi-sequelize-load
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.2.3 |
| Published | 2017-10-29 |
| First published | 2017-10-10 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >= 4.8.0 |
| Dependencies | 2 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | moremay |
| Maintainers | moremay |
| Keywords | sequelize, models, autoload, hapi |

## Links

- npm: https://www.npmjs.com/package/hapi-sequelize-load
- Repository: https://github.com/moremay/hapi-sequelize-load
- Homepage: https://github.com/moremay/hapi-sequelize-load#readme
- Issues: https://github.com/moremay/hapi-sequelize-load/issues
- npm.io page: https://npm.io/package/hapi-sequelize-load

## Dependencies (2)

- [glob](https://npm.io/package/glob.md) 7.x.x
- [sequelize-load](https://npm.io/package/sequelize-load.md) ^0.4.0

## Recent versions

- 1.2.3 (latest) — 2017-10-29
- 1.2.2 — 2017-10-28
- 1.2.1 — 2017-10-28
- 1.2.0 — 2017-10-28
- 1.1.0 — 2017-10-26
- 1.0.4 — 2017-10-22
- 1.0.3 — 2017-10-19
- 1.0.2 — 2017-10-11
- 1.0.1 — 2017-10-10
- 1.0.0 — 2017-10-10

## README

# hapi-sequelize-load
load the sequelize models/associates for [hapi](https://hapijs.com)

When registering this module, it loads model files, handles model associations, and sync to the DB.  
The [sequelize-load](https://github.com/moremay/sequelize-load) object is attached to the server/request object.

## Installation
```
npm install --save hapi-sequelize-load

npm install --save sequelize

# And one of the following:
npm install --save pg pg-hstore
npm install --save mysql2
npm install --save sqlite3
npm install --save tedious // MSSQL
```

## HISTORY
_1.2.3_ only ever create one instance per database
_1.2.2_ db.getModels is attached to the server.app object.   
_1.1.0_ uses sequelize-load@^0.4.0: associates is no longer an object, but an array.   
option logging changed.

## Register Plugin

```js
server.register(
  {
    register: require( "hapi-sequlize-load" ),
    options: {
      dialect: 'postgres',
      host: 'localhost',
      port: 5432,
      database: 'mydb',
      username: 'xxxx',
      password: 'xxxx',
      define: {
        freezeTableName: true,
      },
      operatorsAliases: false,
      // ... other sequelize options
      syncData: __dirname + "/data/sync1.json",
      pattern: __dirname + "/**/*.model.js"
    }
  },
  function ( err ){
    if ( err ){
      console.log( err );
    }
  }
);
```

This plugin can connect to different databases.

```js
server.register(
  {
    register: require( "hapi-sequlize-load" ),
    options: [
      {
        dialect: 'postgres',
        // ... sequelize options
        syncData: __dirname + "/data/pgsync.json",
        pattern: __dirname + "/**/*.pg.js",
        loaderName: "dbA"
      },
      {
        dialect: 'mysql',
        // ... sequelize options
        syncData: __dirname + "/data/mysync.json",
        pattern: __dirname + "/**/*.my.js",
        loaderName: "dbB"
      },
      {
        dialect: 'mysql',
        // ... sequelize options
        syncData: __dirname + "/data/mysync2.json",
        pattern: __dirname + "/**/*.my2.js"
        // loaderName == "db2"
      },
    ]
  },
  function ( err ){
    if ( err ){
      console.log( err );
    }
  }
);
```

## Options

The multiple option defaults to false, which means only ever create one instance per database.

The syncData option is a file path, which is used to save the sync information and must be writable. Each connection must specify a different file.

The pattern option is a string or array that specifies the model file path. Using the [glob](https://github.com/isaacs/node-glob) format.

The loaderName option indicates the request property name.  
If not specified, the single database defaults to "db", and multiple databases default to the "db" + options array index.

The sequelize option logging values are extended (insensitive): "SERVER",  "CON",  "BOTH" , or a function.  
"SERVER"|true : uses server.log.  
"CON"         : uses console.log.  
"BOTH"        : using both server.log and console.log output.  
"benchmark"   : uses console.log, and set options.benchmark = true

## Usage

An instance of sequelize-load is not created until getModels runs.

```js
function ( request, reply ){
  return reply({
    // request.dbA.sequelize.foo(); ERROR
    var Users = request.dbA.getModels( "users" );
    // request.dbA.sequelize.foo(); OK
    var [Foo1, Foo2] = request.dbB.getModels( "foo1", "foo2" );
  });
});
```

---
_Source: https://npm.io/package/hapi-sequelize-load · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
