salix/loopback/common/methods/schema/model-info.js

83 lines
2.1 KiB
JavaScript

module.exports = Self => {
Self.remoteMethod('modelInfo', {
description: 'Gets all models information',
accepts: [
{
arg: 'ctx',
type: 'Object',
http: {source: 'context'}
}
],
returns: {
type: 'Object',
root: true
},
http: {
path: `/modelInfo`,
verb: 'GET'
}
});
Self.modelInfo = async function(ctx) {
let json = {};
let models = Self.app.models;
for (let modelName in models) {
let model = models[modelName];
let validations = model.validations;
let jsonValidations = {};
for (let fieldName in validations) {
let jsonField = [];
for (let validation of validations[fieldName]) {
let options = validation.options;
if ((options && options.async) ||
(validation.validation == 'custom' && !validation.isExportable))
continue;
let validationCp = Object.assign({}, validation);
if (validationCp.message)
validationCp.message = ctx.req.__(validationCp.message);
jsonField.push(toJson(validationCp));
}
jsonValidations[fieldName] = jsonField;
}
json[modelName] = {
properties: model.definition.rawProperties,
validations: jsonValidations
};
}
return json;
};
function toJson(object) {
let json = {};
for (let prop in object) {
let value = object[prop];
switch (typeof value) {
case 'object':
if (value instanceof RegExp)
json[prop] = value.source;
break;
case 'function':
json[prop] = value.toString();
break;
default:
json[prop] = value;
}
}
return json;
}
};