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-bookshelf-models2

v3.1.0

Published

Hapi Plugin to Register Bookshelf Models

Downloads

4

Readme

Hapi Bookshelf Models

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')