UNPKG

lambdaorm-base

Version:
297 lines 17 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.MatchSchema = void 0; const domain_1 = require("../../domain"); class MatchSchema { // eslint-disable-next-line no-useless-constructor constructor(schemaService, helper) { this.schemaService = schemaService; this.helper = helper; } match(schema, mappings, options) { if (!schema.infrastructure) { schema.infrastructure = this.schemaService.newInfrastructure(); } if (!schema.infrastructure.mappings) { schema.infrastructure.mappings = []; } const entitiesToRemove = []; for (const mapping of mappings) { let currentMapping = schema.infrastructure.mappings.find(p => this.helper.equalName(p.name, mapping.name)); if (currentMapping === undefined) { currentMapping = { name: mapping.name, entities: [] }; schema.infrastructure.mappings.push(currentMapping); } else if (!currentMapping.entities) { currentMapping.entities = []; } const toRemove = this.matchMapping(schema.domain.entities, currentMapping, mapping, options); entitiesToRemove.push(...toRemove.filter(p => !entitiesToRemove.includes(p))); } // remove entities and properties this.removeEntitiesAndProperties(schema, mappings, entitiesToRemove, options); } matchMapping(entities, currentMapping, mapping, options = {}) { var _a; if (mapping.entities === undefined) { return []; } // create and update entities and properties for (const entityMapping of mapping.entities) { const currentEntityMapping = (_a = currentMapping.entities) === null || _a === void 0 ? void 0 : _a.find(p => this.helper.equalName(p.mapping, entityMapping.mapping) || this.helper.equalName(p.name, entityMapping.name)); // Si existe busca por el mapeo actual , sino busca por el nombre del nuevo mapeo const currentEntity = currentEntityMapping ? entities.find(p => p.name === currentEntityMapping.name) : entities.find(p => this.helper.equalName(p.name, entityMapping.name)); // if not exists create entity if (!currentEntity) { this.createEntity(entities, currentMapping, entityMapping); } else { this.mergeValues(currentEntity.primaryKey || [], entityMapping.primaryKey || []); this.mergeValues(currentEntity.uniqueKey || [], entityMapping.uniqueKey || []); currentEntity.view = entityMapping.view; this.upsertProperties(entityMapping, currentEntity, currentEntityMapping); this.mergeIndexes(entityMapping, currentEntity); this.mergeRelations(entityMapping, currentEntity, options); if (currentEntityMapping) { this.updateEntityMapping(currentEntityMapping, entityMapping); } else { this.createEntityMapping(currentMapping, currentEntity, entityMapping); } } } // remove entities mapping and properties const entitiesToRemove = this.removeEntityMappingsAndProperties(entities, currentMapping, mapping.entities, options); return entitiesToRemove; } createEntity(entities, currentMapping, entityMapping) { var _a, _b, _c, _d, _e; const newEntity = { name: entityMapping.name, properties: [], relations: [], primaryKey: entityMapping.primaryKey, uniqueKey: entityMapping.uniqueKey }; const newEntityMapping = { name: entityMapping.name, mapping: entityMapping.mapping, properties: [] }; for (const propertyMapping of entityMapping.properties || []) { if (propertyMapping.name !== propertyMapping.mapping) { (_a = newEntityMapping.properties) === null || _a === void 0 ? void 0 : _a.push({ name: propertyMapping.name, mapping: propertyMapping.mapping }); } (_b = newEntity.properties) === null || _b === void 0 ? void 0 : _b.push(this.propertyFromPropertyMapping(propertyMapping)); } if (entityMapping.relations) { for (const relation of entityMapping.relations) { const newRelation = { name: relation.name, type: relation.type, from: relation.from, entity: this.helper.entityName(relation.entity), to: relation.to }; (_c = newEntity.relations) === null || _c === void 0 ? void 0 : _c.push(newRelation); } } entities.push(newEntity); if (newEntityMapping.mapping || (((_d = newEntityMapping.properties) === null || _d === void 0 ? void 0 : _d.length) || 0) > 0) { (_e = currentMapping.entities) === null || _e === void 0 ? void 0 : _e.push(newEntityMapping); } } createEntityMapping(currentMapping, currentEntity, entityMapping) { var _a, _b, _c, _d, _e; const newEntityMapping = { name: currentEntity.name, mapping: entityMapping.mapping, sequence: entityMapping.sequence, properties: [] }; for (const propertyMapping of entityMapping.properties || []) { const currentProperty = (_a = currentEntity.properties) === null || _a === void 0 ? void 0 : _a.find(p => this.helper.equalName(p.name, propertyMapping.name)); if (!currentProperty) { throw new domain_1.SchemaError(`Property ${propertyMapping.name} not found in entity ${currentEntity.name}`); } if (currentProperty.name !== propertyMapping.mapping) { (_b = newEntityMapping.properties) === null || _b === void 0 ? void 0 : _b.push({ name: currentProperty.name, mapping: propertyMapping.mapping }); } } if ((newEntityMapping.name !== newEntityMapping.mapping) || newEntityMapping.sequence || (((_c = newEntityMapping.properties) === null || _c === void 0 ? void 0 : _c.length) || 0) > 0) { if (((_d = newEntityMapping.properties) === null || _d === void 0 ? void 0 : _d.length) === 0) { newEntityMapping.properties = undefined; } (_e = currentMapping.entities) === null || _e === void 0 ? void 0 : _e.push(newEntityMapping); } } upsertProperties(entityMapping, currentEntity, currentEntityMapping) { var _a, _b, _c; for (const propertyMapping of entityMapping.properties || []) { // en el caso que exista el mapeo actual, busca la propiedad en el mapeo actual por el mapping, // sino busca la propiedad en la entidad por el nombre const currentProperty = currentEntityMapping ? (_a = currentEntityMapping.properties) === null || _a === void 0 ? void 0 : _a.find(p => this.helper.equalName(p.mapping, propertyMapping.mapping)) : (_b = currentEntity.properties) === null || _b === void 0 ? void 0 : _b.find(p => this.helper.equalName(p.name, propertyMapping.name)); // si no existe la propiedad la agrega , sino actualiza los valores if (!currentProperty) { (_c = currentEntity.properties) === null || _c === void 0 ? void 0 : _c.push(this.propertyFromPropertyMapping(propertyMapping)); } else { currentProperty.type = this.helper.type(propertyMapping.type); currentProperty.length = this.helper.length(propertyMapping.length); currentProperty.required = propertyMapping.required ? true : undefined; currentProperty.autoIncrement = propertyMapping.autoIncrement ? true : undefined; currentProperty.view = propertyMapping.view; } } } propertyFromPropertyMapping(propertyMapping) { return { name: this.helper.propertyName(propertyMapping.name), type: this.helper.type(propertyMapping.type), length: this.helper.length(propertyMapping.length), required: propertyMapping.required ? true : undefined, autoIncrement: propertyMapping.autoIncrement ? true : undefined, view: propertyMapping.view }; } mergeIndexes(entityMapping, currentEntity) { var _a; // Upsert indexes if (entityMapping.indexes) { for (const index of entityMapping.indexes) { if (!currentEntity.indexes) currentEntity.indexes = []; const currentIndex = currentEntity.indexes.find(p => this.helper.equalName(p.name, index.name)); if (!currentIndex) { currentEntity.indexes.push(index); } else { this.mergeValues(currentIndex.fields, index.fields); } } } // Remove indexes if (currentEntity.indexes) { const indexesToRemove = []; for (const index of currentEntity.indexes) { const currentIndex = (_a = entityMapping.indexes) === null || _a === void 0 ? void 0 : _a.find(p => this.helper.equalName(p.name, index.name)); if (!currentIndex) { indexesToRemove.push(index.name); } } currentEntity.indexes = currentEntity.indexes.filter(p => !indexesToRemove.includes(p.name)); } } mergeRelations(entityMapping, currentEntity, options) { var _a; // Upsert relations if (entityMapping.relations) { for (const relation of entityMapping.relations) { if (!currentEntity.relations) currentEntity.relations = []; const currentRelation = currentEntity.relations.find(p => this.helper.equalName(p.name, relation.name)); if (!currentRelation) { relation.entity = this.helper.entityName(relation.entity); currentEntity.relations.push(relation); } else { if (!this.helper.equalName(currentRelation.type, relation.type)) { currentRelation.type = relation.type; } if (!this.helper.equalName(currentRelation.from, relation.from)) { currentRelation.from = relation.from; } if (!this.helper.equalName(currentRelation.entity, relation.entity)) { currentRelation.entity = this.helper.entityName(relation.entity); } if (!this.helper.equalName(currentRelation.to, relation.to)) { currentRelation.to = relation.to; } } } } // Remove relations if (options.removeRelations && currentEntity.relations) { const relationsToRemove = []; for (const relation of currentEntity.relations.filter(p => p.type === domain_1.RelationType.oneToMany)) { const currentRelation = (_a = entityMapping.relations) === null || _a === void 0 ? void 0 : _a.find(p => this.helper.equalName(p.name, relation.name)); if (!currentRelation) { relationsToRemove.push(relation.name); } } currentEntity.relations = currentEntity.relations.filter(p => !relationsToRemove.includes(p.name)); } } updateEntityMapping(currentEntityMapping, entityMapping) { var _a, _b; currentEntityMapping.mapping = entityMapping.mapping; currentEntityMapping.sequence = entityMapping.sequence; for (const property of entityMapping.properties || []) { const currentProperty = (_a = currentEntityMapping.properties) === null || _a === void 0 ? void 0 : _a.find(p => this.helper.equalName(p.mapping, property.mapping)); if (currentProperty) { currentProperty.mapping = property.mapping; } else if (property.name !== property.mapping) { (_b = currentEntityMapping.properties) === null || _b === void 0 ? void 0 : _b.push({ name: property.name, mapping: property.mapping }); } } } removeEntitiesAndProperties(schema, mappings, entitiesToRemove, options) { var _a, _b; if (options.removeEntities) { // remove entities from domain for (const entityToRemove of entitiesToRemove) { const entity = schema.domain.entities.find(p => this.helper.equalName(p.name, entityToRemove)); if (entity) { // remove entity if not used in other mappings const existsInOtherMappings = (_b = (_a = schema.infrastructure) === null || _a === void 0 ? void 0 : _a.mappings) === null || _b === void 0 ? void 0 : _b.some(p => { var _a; return (_a = p.entities) === null || _a === void 0 ? void 0 : _a.some(q => this.helper.equalName(q.name, entityToRemove)); }); const existsInMappings = mappings.some(p => { var _a; return (_a = p.entities) === null || _a === void 0 ? void 0 : _a.some(q => this.helper.equalName(q.name, entityToRemove)); }); if (!existsInOtherMappings && existsInMappings) { schema.domain.entities = schema.domain.entities.filter(p => !this.helper.equalName(p.name, entityToRemove)); } } } } } removeEntityMappingsAndProperties(entities, currentMapping, entitiesMapping, options) { const entitiesToRemove = []; if (currentMapping.entities && (options.removeEntities || options.removeProperties)) { for (const currentEntityMapping of currentMapping.entities) { const entityMapping = entitiesMapping.find(p => this.helper.equalName(p.mapping, currentEntityMapping.mapping)); if (!entityMapping) { if (options.removeEntities) { entitiesToRemove.push(currentEntityMapping.name); } } else if (options.removeProperties) { const currentEntity = entities.find(p => p.name === currentEntityMapping.name); if (currentEntity) { this.removeProperties(currentEntity, currentEntityMapping, entityMapping); } } } // remove entities mappings currentMapping.entities = currentMapping.entities.filter(p => !entitiesToRemove.includes(p.name)); } return entitiesToRemove; } removeProperties(currentEntity, currentEntityMapping, entityMapping) { var _a, _b, _c, _d, _e; const propertiesToRemove = []; for (const currentProperty of currentEntity.properties || []) { const currentPropertyMapping = (_a = currentEntityMapping.properties) === null || _a === void 0 ? void 0 : _a.find(p => p.name === currentProperty.name); if (currentPropertyMapping) { // si existe la propiedad en el mapeo actual y no existe en el nuevo mapeo lo setea para eliminar if (!((_b = entityMapping.properties) === null || _b === void 0 ? void 0 : _b.some(p => this.helper.equalName(p.mapping, currentPropertyMapping.mapping)))) { propertiesToRemove.push(currentProperty.name); } // si no existe la propiedad en el mapeo actual , busca la propiedad en la entidad de dominio, si no existe la setea para eliminar } else if (!((_c = entityMapping.properties) === null || _c === void 0 ? void 0 : _c.some(p => this.helper.equalName(p.name, currentProperty.name)))) { propertiesToRemove.push(currentProperty.name); } } currentEntityMapping.properties = (_d = currentEntityMapping.properties) === null || _d === void 0 ? void 0 : _d.filter(p => !propertiesToRemove.includes(p.name)); currentEntity.properties = (_e = currentEntity.properties) === null || _e === void 0 ? void 0 : _e.filter(p => !propertiesToRemove.includes(p.name)); } mergeValues(current, change) { const currentLowerCase = current.map(p => p.toLowerCase()); const changeLowerCase = change.map(p => p.toLowerCase()); const newValues = change.filter(p => !currentLowerCase.includes(p.toLowerCase())); const removeValues = current.filter(p => !changeLowerCase.includes(p.toLowerCase())); current.push(...newValues); current = current.filter(p => !removeValues.includes(p)); } } exports.MatchSchema = MatchSchema; //# sourceMappingURL=match.js.map