/
index.js
48 lines (47 loc) · 1.35 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
const Bookshelf = require('bookshelf')
const knex = require('knex')
const cascadeDelete = require('bookshelf-cascade-delete')
const softDelete = require('bookshelf-paranoia')
const mask = require('bookshelf-mask')
const modelBase = require('bookshelf-modelbase')
const uuid = require('bookshelf-uuid')
const validate = require('./validate')
const formatter = require('./formatter')
const patch = require('./patch')
const internal = {bookshelf: null, connection: null}
const getInternal = name => {
if (!internal.bookshelf) throw new Error('You should call initialize before')
return !name ? internal : internal[name]
}
module.exports = {
connect (knexConfig) {
if (!internal.connection) {
internal.connection = knexConfig instanceof knex.constructor
? knexConfig
: knex(knexConfig)
}
if (!internal.bookshelf) {
internal.bookshelf = Bookshelf(internal.connection)
.plugin('registry')
.plugin('virtuals')
.plugin('visibility')
.plugin('pagination')
.plugin(modelBase.pluggable)
.plugin(cascadeDelete)
.plugin(softDelete)
.plugin(mask)
.plugin(uuid)
.plugin(validate)
.plugin(formatter)
.plugin(patch)
}
return internal
},
base (knexConfig) {
return getInternal('bookshelf').Model
},
get Base () {
return module.exports.base()
},
getInternal
}