forked from colmena/colmena
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmeta.js
More file actions
64 lines (55 loc) · 1.76 KB
/
meta.js
File metadata and controls
64 lines (55 loc) · 1.76 KB
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
const utils = require('loopback-datasource-juggler/lib/utils')
const _ = require('lodash')
module.exports = function (Meta) {
/**
* Helper method for format the type of the properties
*/
function formatProperties (properties) {
const result = {}
for (let key in properties) {
result[ key ] = _.clone(properties[ key ])
result[ key ].type = properties[ key ].type.name
}
return result
}
/**
* Get the definition of a model and format the result in a way that's similar to a LoopBack model definition file
*/
function getModelInfo (modelName) {
// Get the model
const model = Meta.app.models[ modelName ]
// Create the base return object
const result = {
id: model.definition.name,
name: model.definition.name,
properties: formatProperties(model.definition.properties),
}
// Get the following keys from the settings object, if they are set
const keys = [ 'description', 'plural', 'base', 'idInjection', 'persistUndefinedAsNull', 'strict', 'hidden',
'validations', 'relations', 'acls', 'methods', 'mixins',
]
keys.forEach((key) => {
result[ key ] = _.get(model.definition.settings, key)
})
return result
}
/**
* Get all the models with its information
*/
Meta.getModels = function (cb) {
cb = cb || utils.createPromiseCallback()
const modelNames = Object.keys(Meta.app.models)
process.nextTick(() => {
cb(null, modelNames.sort().map((modelName) => getModelInfo(modelName)))
})
return cb.promise
}
/**
* Get one model with its information
*/
Meta.getModelById = function (modelName, cb) {
cb = cb || utils.createPromiseCallback()
process.nextTick(() => cb(null, getModelInfo(modelName)))
return cb.promise
}
}