-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
66 lines (56 loc) · 1.57 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
const debug = require('debug')('loopback-component-meta')
const modelDefinition = require('./models/meta.json')
// Remove properties that will confuse LB
function getSettings (def) {
var settings = {}
for (var s in def) {
if (def.hasOwnProperty(s)) {
if (s !== 'name' || s !== 'properties') {
settings[ s ] = def[ s ]
}
}
}
return settings
}
module.exports = function (app, options) {
debug('Component configuration:')
debug(options)
options = options || {}
if (!options.filter) {
debug('No filter defined')
options.filter = []
}
var dataSource = options.dataSource || 'db'
if (typeof dataSource === 'string') {
dataSource = app.dataSources[ dataSource ]
}
if (typeof options.acls !== 'object') {
modelDefinition.acls = []
} else {
debug('Enable ACL')
debug(options.acls)
modelDefinition.acls = options.acls
}
// Support for loopback 2.x.
if (app.loopback.version.startsWith(2)) {
Object.keys(modelDefinition.methods).forEach(method => {
modelDefinition.methods[method].isStatic = true
})
}
const NewModel = dataSource.createModel(
modelDefinition.name,
modelDefinition.properties,
getSettings(modelDefinition)
)
const Model = require('./models/meta.js')(NewModel, options)
app.model(Model)
if (!options.enableRest) {
Object.keys(modelDefinition.methods).forEach(function (methodName) {
debug('REST disabled for method ' + methodName)
Model.disableRemoteMethod(methodName, true)
})
} else {
debug('REST enabled')
}
Model.createStructure()
}