@fibery/ai-utils
Version:
Utilities for Fibery AI
47 lines (42 loc) • 1.41 kB
JavaScript
const ejs = require('ejs');
const _ = require('lodash');
const getFieldsRelationsForStructure = (values, getField) => {
const fields = [];
const field = getField
? getField
: (name, type) => {
if (_.isObject(type)) {
const isManyToMany = type.relation === `many-to-many`;
const refType = isManyToMany ? `(many-to-many to ${type.type})` : `(many-to-one to ${type.type})`;
return `${name}${refType}`;
}
return name;
};
_.keys(values).forEach((name) => {
const type = values[name];
fields.push(field(name, type));
});
return {fields};
};
module.exports.getDataToRenderTemplate = ({app, getField}) => {
const data = _.cloneDeep(app);
data.types = _.keys(data.schema).map((name) => {
const {fields} = getFieldsRelationsForStructure(data.schema[name].fields, getField);
return {
name,
description: data.schema[name].description,
fields,
};
});
return data;
};
module.exports.renderToString = async ({template, data}) => {
return new Promise((resolve, reject) => {
ejs.renderFile(`${__dirname}/${template}.ejs`, data, {}, (err, value) => {
if (err) {
return reject(err);
}
resolve(value.trim());
});
});
};