-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
77 lines (69 loc) · 2.08 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
/*
* @Author: lushijie
* @Date: 2017-08-23 09:44:20
* @Last Modified by: lushijie
* @Last Modified time: 2017-11-03 11:36:23
*/
const helper = require('think-helper');
const path = require('path');
const Model = require('./lib/model.js');
const Relations = {};
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, name);
instance.models = models;
//set relation
let relations = instance.schemaOptions.relations;
if(relations) {
if(helper.isObject(relations)) {
relations = [relations];
}
relations.forEach((ele,index) => {
let relationModelName = Object.keys(ele).filter(e => {
return e !== 'options';
})[0];
let relationName = ele[relationModelName];
let relationInstance = model(relationModelName, config, m);
let relationOptions = Object.assign({}, ele.options);
let relationUnique = `${name}${relationName}${relationModelName}${relationOptions.as}`;
if (relationOptions.as) {
if(Relations[relationUnique]) {
return;
} else {
Relations[relationUnique] = true;
}
}
instance[relationName](relationInstance, relationOptions);
});
}
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: {
Sequel: Model,
sequel: injectModel
},
service: {
sequel: injectModel
},
controller: {
sequel(name, config, m) {
return this.ctx.sequel(name, config, m);
}
},
context: {
sequel(name, config, m = this.module) {
config = helper.parseAdapterConfig(this.config('model'), config);
return model(name, config, m);
}
}
};
};