forked from bixlabs/bookshelf-model-loader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
52 lines (40 loc) · 1.17 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
'use strict';
var _ = require('lodash');
var RequireTree = require('require-tree');
var Bookshelf = require('bookshelf');
module.exports = {
init: function initialize(bookshelf, options) {
var self = this;
options = _.defaults(options || {}, {
excludes: [],
plugins: ['virtuals', 'visibility', 'registry'],
includeBase: true,
modelOptions: {},
});
if (!options.path) {
throw new Error('You must specify the `path` option');
}
// Register all plugins
options.plugins.map(function (plugin) {
return bookshelf.plugin(plugin);
});
self.Bookshelf = self.Bookshelf || bookshelf;
// Load the base Model Instance
if (options.includeBase) {
self.Base = self.Base || require('./lib/base')(bookshelf, options.modelOptions);
}
// Require all files in this directory
RequireTree(options.path, {
filter: filterModels,
each: loadModels
});
function filterModels(filename) {
return options.excludes.indexOf(filename) === -1;
}
function loadModels(model) {
// Cache its `export` object onto this object.
_.extend(self, model);
}
return self;
}
};