lambdaorm-base
Version:
142 lines • 7.5 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.InterpretSchemaDataService = void 0;
const typ3s_1 = require("typ3s");
class InterpretSchemaDataService {
// eslint-disable-next-line no-useless-constructor
constructor(helper) {
this.helper = helper;
}
completeSchemaData(source, name, type, schemaData) {
const entityName = this.helper.entityName(name);
if (typ3s_1.Type.isList(type) && type.list && typ3s_1.Type.isObj(type.list.items) && type.list.items.obj) {
const children = this.objectsToRows(schemaData, type.list.items.obj, source, entityName, this.helper.getPkName(type.list.items.obj));
this.addRows(schemaData, entityName, children);
}
else if (typ3s_1.Type.isObj(type) && type.obj) {
const pkName = this.helper.getPkName(type.obj);
if (pkName === '_id') {
source[pkName] = this.helper.newId();
}
this.objectToRow(schemaData, type.obj, source, entityName, pkName);
}
}
objectsToRows(schemaData, objType, source, entityName, pkName, excludeRepeated = true) {
const rows = [];
if (excludeRepeated) {
const schemaEntityData = this.getSchemaEntityData(schemaData, entityName);
for (const item of source) {
if (pkName === '_id') {
item[pkName] = this.helper.newId();
}
else if (schemaEntityData.rows.some(p => p[pkName] === item[pkName])) {
// En el caso de que el id ya exista, no se añade la fila
continue;
}
const row = this.objectToRow(schemaData, objType, item, entityName, pkName);
rows.push(row);
}
}
else {
for (const item of source) {
if (pkName === '_id') {
item[pkName] = this.helper.newId();
}
const row = this.objectToRow(schemaData, objType, item, entityName, pkName);
rows.push(row);
}
}
return rows;
}
objectToRow(schemaData, objType, source, entityName, pkName) {
var _a, _b;
const row = {};
for (const prop of objType.properties) {
const value = source[prop.name];
if (value === undefined) {
continue;
}
if (prop.type && typ3s_1.Type.isPrimitive(prop.type)) {
row[prop.name] = value;
}
else if (prop.type && typ3s_1.Type.isObj(prop.type) && prop.type.obj) {
const fk = this.helper.getFk(prop.type.obj);
if (fk && fk.type) {
const propertyName = prop.name + this.helper.capitalize(fk.name);
const relatedEntityName = this.helper.entityName(prop.name);
const relatedPkName = this.helper.getPkName(prop.type.obj);
if (prop.type && prop.type.repeated !== undefined && prop.type.repeated === 0) {
const childRow = this.objectToRow(schemaData, prop.type.obj, value, relatedEntityName, relatedPkName);
row[prop.name] = childRow;
}
else {
const fkValue = value[fk.name];
if (fkValue !== undefined) {
row[propertyName] = fkValue;
}
const schemaEntityData = this.getSchemaEntityData(schemaData, relatedEntityName);
if (relatedPkName === '_id') {
row[relatedPkName] = this.helper.newId();
}
else if (!schemaEntityData.rows.some(p => p[relatedPkName] === value[relatedPkName])) {
// En el caso que el registro no exista, se añade
const childRow = this.objectToRow(schemaData, prop.type.obj, value, relatedEntityName, relatedPkName);
schemaEntityData.rows.push(childRow);
}
}
}
}
else if (prop.type && typ3s_1.Type.isList(prop.type) && ((_a = prop.type.list) === null || _a === void 0 ? void 0 : _a.items.obj)) {
const relatedEntityName = this.helper.entityName(prop.name);
const relatedPkName = this.helper.getPkName(prop.type.list.items.obj);
if (prop.type && prop.type.repeatRate && prop.type.repeatRate > 0.02) {
const rpk = this.helper.getFk((_b = prop.type.list) === null || _b === void 0 ? void 0 : _b.items.obj);
if (rpk && rpk.type) {
// Crea una tabla intermediaria con el id de la entidad y el id de la entidad relacionada
const parentPropertyName = this.helper.refPropertyName(entityName, pkName);
const childPropertyName = this.helper.refPropertyName(relatedEntityName, rpk.name);
const intermediaEntityName = entityName + this.helper.capitalize(prop.name);
const parentValue = source[pkName];
const intermediateObjects = [];
for (const item of value) {
const obj = {};
obj[childPropertyName] = item[rpk.name];
obj[parentPropertyName] = parentValue;
intermediateObjects.push(obj);
}
this.addRows(schemaData, intermediaEntityName, intermediateObjects);
const children = this.objectsToRows(schemaData, prop.type.list.items.obj, value, relatedEntityName, relatedPkName);
this.addRows(schemaData, relatedEntityName, children);
}
}
else if (prop.type && prop.type.repeated !== undefined && prop.type.repeated === 0) {
const children = this.objectsToRows(schemaData, prop.type.list.items.obj, value, relatedEntityName, relatedPkName, false);
row[prop.name] = children;
}
else {
const children = this.objectsToRows(schemaData, prop.type.list.items.obj, value, relatedEntityName, relatedPkName);
const propertyName = this.helper.refPropertyName(entityName, pkName);
for (const child of children) {
child[propertyName] = source[pkName];
}
this.addRows(schemaData, relatedEntityName, children);
}
}
}
return row;
}
getSchemaEntityData(schemaData, entityName) {
let schemaEntityData = schemaData.entities.find(p => p.entity === entityName);
if (!schemaEntityData) {
schemaEntityData = { entity: entityName, rows: [] };
schemaData.entities.push(schemaEntityData);
}
return schemaEntityData;
}
addRows(schemaData, entityName, rows) {
const schemaEntityData = this.getSchemaEntityData(schemaData, entityName);
schemaEntityData.rows.push(...rows);
}
}
exports.InterpretSchemaDataService = InterpretSchemaDataService;
//# sourceMappingURL=interpretSchemaDataService.js.map