[go: up one dir, main page]

Skip to content

An opinionated Bookshelf model loader with bookshelf-modelbase

Notifications You must be signed in to change notification settings

funsocietyirc/bookshelf-model-loader

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Bookshelf Model Loader

An opinionated model loader for Bookshelf. Inspired by Ghost.

Also includes bookshelf-modelbase which the Base model.

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
    modelOptions: {}, // Optional - options to pass to the base model
});

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();

Soft Deletes

All models provide soft deletes by default. As a result, all tables are required to have a deleted_at column. However, you can disable soft deletes on a per model or global basis.

Per Model

To disable soft deletes for a particular model simply add soft: false to the model declaration.

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

Global

To disable soft deletes for all models by default add the soft: false flag to the modelOptions object when calling the init() method.

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
    modelOptions: {
      soft: false
    }
});

About

An opinionated Bookshelf model loader with bookshelf-modelbase

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 100.0%