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
69 lines (59 loc) · 1.82 KB
/
meta.js
File metadata and controls
69 lines (59 loc) · 1.82 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
65
66
67
68
69
var utils = require('loopback-datasource-juggler/lib/utils');
var _ = require('lodash');
module.exports = function (Meta) {
/**
* Helper method for format the type of the properties
*/
function formatProperties (properties) {
var result = {};
for (var 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
var model = Meta.app.models[modelName];
// Create the base return object
var 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
var keys = ['description', 'plural', 'base', 'idInjection', 'persistUndefinedAsNull', 'strict', 'hidden',
'validations', 'relations', 'acls', 'methods', 'mixins'
];
keys.forEach(function (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();
var modelNames = Object.keys(Meta.app.models);
process.nextTick(function () {
cb(null, modelNames.sort().map(function (modelName) {
return getModelInfo(modelName);
}));
});
return cb.promise;
};
/**
* Get one model with its information
*/
Meta.getModelById = function (modelName, cb) {
cb = cb || utils.createPromiseCallback();
process.nextTick(function () {
cb(null, getModelInfo(modelName));
});
return cb.promise;
};
};