forked from adonisjs/lucid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
100 lines (83 loc) · 1.85 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
'use strict'
/*
* adonis-fold
*
* (c) Harminder Virk <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* This file is used when you are not using lucid
* with adonis framework, since framework will
* use the providers.
*/
const _ = require('lodash')
const iocResolver = require('./lib/iocResolver')
let config = null
/**
* The config class for using lucid standalone
*/
class Config {
constructor (map) {
this._map = map
}
get (connection) {
return _.get(this._map, connection)
}
}
class Ioc {
constructor () {
this._bindings = {}
}
bind (name, implementation) {
this._bindings[name] = implementation
}
use (name) {
return this._bindings[name]
}
make (name) {
return this._bindings[name]
}
}
class Resolver {
forDir () {
}
}
const ioc = new Ioc()
const resolver = new Resolver()
iocResolver.setFold({ ioc, resolver })
module.exports = function (configMap) {
config = new Config({ database: configMap })
const Database = require('./src/Database/Manager')
const Model = require('./src/Lucid/Model')
const Schema = require('./src/Schema')
const Migration = require('./src/Migration')
const Factory = require('./src/Factory')
const db = new Database(config)
ioc.bind('Adonis/Src/Database', db)
return {
db,
Model,
Schema,
Migration,
Factory,
Models: {
add (name, implementation) {
implementation._bootIfNotBooted()
ioc.bind(`model:${name}`, implementation)
return this
},
get (name) {
return ioc.use(`model:${name}`)
},
clear () {
_.each(ioc._bindings, (value, name) => {
if (name.startsWith('model:')) {
delete ioc._bindings[name]
}
})
}
}
}
}