hapi-bookshelf-models

2.0.0 • Public • Published

Hapi Bookshelf Models

Build Status Coverage Status NPM version Downloads

The purpose of this plugin is to provide a convenient way to register Bookshelf.js models and expose them via a Hapi plugin.

Registering the Plugin

var Hapi = require('hapi');
 
var server = new Hapi.Server();
 
server.register([
  {
    register: require('hapi-bookshelf-models'),
    options: {
      knex: {
        client: 'pg',
        connection: {
          host: 'localhost',
          user: 'username',
          password: 'password',
          database: 'db_name',
          port: 5432
        }
      },
      plugins: ['registry'], // Required
      models: '../path/to/models/directory',
      collections: '../path/to/collections/directory',
      base: {
        model: require('../path/to/model/base'), // optional
        collection: require('../path/to/collection/base') // optional
      }
    }
  }
], function (err) {
  // An error will be available here if anything goes wrong
});
 
// You can now access Bookshelf.js via server.plugins.bookshelf and
// models can be retrieved via server.plugins.bookshelf.model('ModelName')

Options

  • knex Knex Configuration Object
  • plugins Bookshelf.js Plugins the registry plugin is required
  • models directory where you Bookshelf.js models are defined
  • base (optional) function that applies the Bookshelf.js extend method and returns the extended model, example below.
  • namespace (optional) string that will control how models are exposed in the plugin. The intent is to support models coming from multiple knex connections or to logically segment models. Models can be accessed via server.plugins.bookshelf.{namespace}.model('ModelName')

Example base

// Add timestamps to all models
base: function (bookshelf) {
  return bookshelf.Model.extend({
    hasTimestamps: true
  });
}

Defining Models

There is more extensive documentation about defining models for the registry plugin on the Bookshelf.js Wiki. Below is an example of defining two related models that can be placed in the models directory referenced above. baseModel is the Bookshelf model created by the base option or just the basic Bookshelf model if you didn't define one. bookshelf is a Bookshelf instance with a connection to the database defined when the plugin was registered.

// user.js
module.exports = function (baseModel, bookshelf) {
  return baseModel.extend({
    tableName: 'users',
    roles: function () {
      return this.belongsToMany('Role');
    }
  });
};
 
// role.js
module.exports = function (baseModel, bookshelf) {
  return baseModel.extend({
    tableName: 'roles'
  });
};

After loading these models you can access them via server.plugins.bookshelf.model('User') and server.plugins.bookshelf.model('Role') respectively.

Notes:

  • Models will be registered and made available under the file name with the first character capitalized. For example user.js becomes User and blogPost.js becomes BlogPost

Registering Multiple Namespaces

Modeling namespaces is a great way to expose models from multiple databases using the same interface. Below is an example of how you can do this.

var Hapi = require('hapi');
 
var server = new Hapi.Server();
 
server.register([
  {
    register: require('hapi-bookshelf-models'),
    options: {
      knex: {
        // connection one details
      },
      plugins: ['registry'],
      models: '../path/to/namespaceone/models/directory',
      namespace: 'namespaceone'
    }
  },
  {
    register: require('hapi-bookshelf-models'),
    options: {
      knex: {
        //connection two details
      },
      plugins: ['registry'],
      models: '../path/to/namespacetwo/models/directory',
      namespace: 'namespacetwo'
    }
  }
], function (err) {
  // An error will be available here if anything goes wrong
});
 
// You can access the namespaceone models via server.plugins.bookshelf.namespaceone.model('ModelName')
// You can access the namespacetwo models via server.plugins.bookshelf.namespacetwo.model('ModelName')

Readme

Keywords

Package Sidebar

Install

npm i hapi-bookshelf-models

Weekly Downloads

10

Version

2.0.0

License

MIT

Last publish

Collaborators

  • jayteelob
  • erik.forsman-lob
  • jkleung11
  • tanya.sah
  • hunteryoakum
  • rdimouro7373
  • lobstertroy
  • joshnkoy
  • michel_lob
  • haroutrs
  • kjones_lob
  • eamon-barisone
  • nathanielwaldschmidtlob
  • zach.reed
  • richpodrazalob
  • kencrim
  • jorgelob
  • nick-place-lob
  • andrew.guterres
  • ajorczak
  • hanqingchen-lob
  • juan.frissdekereki
  • mmorgan-lob
  • karankwatra-lob
  • joey-bates-lob
  • luke.birdeau
  • mg-lob
  • vmangwani
  • sachinlob
  • nick.perri
  • siddharthpant92
  • bethqiang
  • kplob
  • samkitsheth95
  • erin-doyle
  • jfdavidson
  • meussdorffer
  • shannamurry
  • amaan_lob
  • team.platform.lob.com
  • elijah-lob
  • jlowsley-lob
  • barnabygo
  • james.cho
  • douglaje
  • lob-owner
  • graeme.lowe.lob