UNPKG

lambdaorm-base

Version:
115 lines 5.59 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.UpdateSchema = void 0; const typ3s_1 = require("typ3s"); class UpdateSchema { // eslint-disable-next-line no-useless-constructor constructor(createEntitiesService) { this.createEntitiesService = createEntitiesService; } update(schema, data, name) { if (!data || typeof data !== 'object') { throw new Error('Invalid argument data'); } if (typeof name !== 'string') { throw new Error('Argument name undefined'); } const type = typ3s_1.Type.type(data, { info: true, describe: true }); const entities = this.createEntitiesService.getEntities(name, type); this.updateEntities(schema, entities); return type; } updateEntities(schema, entities) { var _a, _b, _c; for (const entity of entities) { const currentEntity = schema.domain.entities.find(p => p.name === entity.name); if (currentEntity) { currentEntity.abstract = entity.abstract; currentEntity.singular = entity.singular; currentEntity.view = entity.view; currentEntity.composite = entity.composite; currentEntity.intermediate = entity.intermediate; for (const property of entity.properties || []) { const currentProperty = (_a = currentEntity.properties) === null || _a === void 0 ? void 0 : _a.find(p => p.name === property.name); if (!currentProperty) { (_b = currentEntity.properties) === null || _b === void 0 ? void 0 : _b.push(property); } else { currentProperty.type = property.type; currentProperty.length = property.length; currentProperty.required = property.required; currentProperty.primaryKey = property.primaryKey; currentProperty.autoIncrement = property.autoIncrement; currentProperty.view = property.view; currentProperty.readExp = property.readExp; currentProperty.writeValue = property.writeValue; currentProperty.readValue = property.readValue; currentProperty.writeExp = property.writeExp; currentProperty.default = property.default; currentProperty.enum = property.enum; currentProperty.key = property.key; } } if (entity.required) { currentEntity.required = entity.required; } if (entity.primaryKey) { currentEntity.primaryKey = entity.primaryKey; } if (entity.uniqueKey) { currentEntity.uniqueKey = entity.uniqueKey; } if (entity.indexes) { for (const index of entity.indexes) { if (!currentEntity.indexes) currentEntity.indexes = []; const currentIndex = currentEntity.indexes.find(p => p.name === index.name); if (!currentIndex) { currentEntity.indexes.push(index); } else { currentIndex.fields = index.fields; } } } if (entity.relations) { for (const relation of entity.relations) { if (!currentEntity.relations) currentEntity.relations = []; const currentRelation = currentEntity.relations.find(p => p.name === relation.name); if (!currentRelation) { currentEntity.relations.push(relation); } else { currentRelation.type = relation.type; currentRelation.entity = relation.entity; currentRelation.from = relation.from; currentRelation.to = relation.to; currentRelation.target = relation.target; currentRelation.weak = relation.weak; currentRelation.composite = relation.composite; } } } if (entity.constraints) { for (const constraint of entity.constraints) { if (!currentEntity.constraints) currentEntity.constraints = []; const currentConstraint = (_c = currentEntity.constraints) === null || _c === void 0 ? void 0 : _c.find(p => p.message === constraint.message); if (!currentConstraint) { currentEntity.constraints.push(constraint); } else { currentConstraint.condition = constraint.condition; } } } } else { schema.domain.entities.push(entity); } } } } exports.UpdateSchema = UpdateSchema; //# sourceMappingURL=update.js.map