-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
42 lines (39 loc) · 1.14 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
const helper = require('think-helper');
const path = require('path');
const Model = require('./lib/model.js');
module.exports = app => {
function model(name, config, m = 'common') {
const models = app.modules.length ? (app.models[m] || {}) : app.models;
const Cls = models[name] || Model;
const modelName = path.basename(name);
const instance = new Cls(modelName, config);
// add models in config, it's need in model when get relation model instance
instance.models = models;
return instance;
};
function injectModel(name, config, m) {
const modelConfig = app.think.config('model', undefined, m);
config = helper.parseAdapterConfig(modelConfig, config);
return model(name, config, m);
}
return {
think: {
Mongo: Model,
mongo: injectModel
},
service: {
mongo: injectModel
},
controller: {
mongo(name, config, m) {
return this.ctx.mongo(name, config, m);
}
},
context: {
mongo(name, config, m = this.module) {
config = helper.parseAdapterConfig(this.config('model'), config);
return model(name, config, m);
}
}
};
};