1.2.14 • Published 8 years ago

hapi-shelf v1.2.14

Weekly downloads
2
License
MIT
Repository
github
Last release
8 years ago

hapi-shelf

Build Status Dependency Status devDependency Status npm version

hapi-shelf is a Hapi plugin for Bookshelf.js, an ORM for RDBMS.

Usage

Install hapi-shelf into your hapi project folder:

npm install hapi-shelf --save

Register the plugin with the server:

'use strict';

const Hapi = require('hapi');
const HapiShelf = require('hapi-shelf');

const server = new Hapi.Server();

server.register(
    {
        register: HapiShelf,
        options: {
            // Knex connection, refer to http://knexjs.org
            knex: {
                client: 'mysql'
                connection: {
                    host: '127.0.0.1',
                    user: 'db_user',
                    password: 'db_secret',
                    database: 'db_name'
                }
            },
            pool: {
                min: 2,
                max: 10
            },
            acquireConnectionTimeout: 10000,
            // Bookshelf Plugins
            plugins: ['registry'],
            // Register models w/ Bookshelf
            // If models are in the root of your app, if they are inside a directory
            // deeper, make sure to include the link to there. `./src/models/user`, for example
            models: ['./models/user'],
        }
    },
    (err) => {

        if (err) {
            // Cannot proceed from here.
            throw err;
        }
        // Bookshelf instance is now available at server.plugins['hapi-shelf']
    }
);

Options

  • knex - (required) the knex configuration object.
  • plugins - an optional array of strings. Defaults to ['registry'].
  • models - an optional array of model strings.

Models

Models are registered automatically upon plugin registration if defined in options.models, using relative paths. Models are defined as follows.

'use strict';

module.exports = (bookshelf) => {

    const MyModel = bookshelf.Model.extend({
        tableName: 'my_model'
    });

    return bookshelf.model('MyModel', MyModel);
};

Access this model in your route

'use strict';

const MyModel = server.plugins['hapi-shelf'].model('MyModel');

server.route([
    {
        method: 'GET',
        path: '/projects',
        config: {
            handler: (request, reply) => {

                MyModel.fetchAll().then((models) => {

                    reply(models);
                });
            }
        }
    }
]);

Attributes are exposed as camelCase, and saved to the DB as under_score.

For details refer to Bookshelf Models.

1.2.14

8 years ago

1.2.13

8 years ago

1.2.12

8 years ago

1.2.11

8 years ago

1.2.10

9 years ago

1.2.9

9 years ago

1.2.8

9 years ago

1.2.7

9 years ago

1.2.6

9 years ago

1.2.5

9 years ago

1.2.4

9 years ago

1.2.2

9 years ago

1.2.1

10 years ago

1.2.0

10 years ago

1.1.11

10 years ago

1.1.10

10 years ago

1.1.9

10 years ago

1.1.8

10 years ago

1.1.7

10 years ago

1.1.6

10 years ago

1.1.5

10 years ago

1.1.4

10 years ago

1.1.3

10 years ago

1.1.2

10 years ago

1.1.1

10 years ago

1.1.0

10 years ago

1.0.1

10 years ago

1.0.0

10 years ago