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 = knex; ;
Then, elsewhere in the application simply:
var Models = ;
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 = ; var User = ModelsBase; moduleexports = User: ModelsBookshelf;
The model will then be available:
var Models = ; ModelsUser;
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 = ModelsBase;
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 = knex; ;