1.2.3 • Published 7 years ago

hapi-sequelize-load v1.2.3

Weekly downloads
1
License
MIT
Repository
github
Last release
7 years ago

hapi-sequelize-load

load the sequelize models/associates for hapi

When registering this module, it loads model files, handles model associations, and sync to the DB.
The 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

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.

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 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.

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" );
  });
});
1.2.3

7 years ago

1.2.2

7 years ago

1.2.1

7 years ago

1.2.0

7 years ago

1.1.0

7 years ago

1.0.4

7 years ago

1.0.3

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago