bookshelf-model-loader

A opinionated Bookshelf model loader with bookshelf-modelbase

Usage no npm install needed!

<script type="module">
  import bookshelfModelLoader from 'https://cdn.skypack.dev/bookshelf-model-loader';
</script>

README

Bookshelf Model Loader

An opinionated model loader for Bookshelf. Inspired by Ghost.

Also includes bookshelf-modelbase.

Installation

npm install bookshelf-model-loader --save

Usage

During your application's startup phase load the module calling the init method. Pass your bookshelf instance in along with the path to the directory you want to autoload your models from.

var bookshelf = require('bookshelf')(knex);

require('bookshelf-model-loader').init(bookshelf, {
    plugins: ['virtuals', 'visibility', 'registry'], // Optional - Bookshelf plugins to load. Defaults to loading the 'virtuals', 'visibility' & 'registry' plugins
    excludes: [], // Optional - files to ignore
    path: __dirname + '/models' // Required
});

Then, elsewhere in the application simply:

var Models = require('bookshelf-model-loader');

Creating Models

Models should export an object with a key of the name you'd like to reference the model as.

For example: ./models/user.js;


'use strict';
var Models = require('bookshelf-model-loader');

var User = Models.Base.extend({
  tableName: 'users'
});

module.exports = {
  User: Models.Bookshelf.model('User', User)
};

The model will then be available:

var Models = require('bookshelf-model-loader);

Models.User.findOne();