UNPKG

ts-db-helper

Version:
162 lines 6.16 kB
import { ModelManager } from '../../managers/model-manager'; import { BadColumnDeclarationError } from '../../errors/bad-column-declaration.error'; import { RelationType } from '../constants/relation-type.constant'; /** * @public * @class DbRelationModel * * @description * This class add relation mapping to the table * * @author Olivier Margarit * @since 0.2 */ var DbRelationModel = /** @class */ (function () { /** * @public * @constructor create db relation model * * @param {{new(): DbHelperModel}} srcModel the source model of the relation from the column owner * @param {{new(): DbHelperModel}} foreignModel the target model of the relation from the column owner * @param {boolean} reverse tag reverse to follow the link reverse from targetted model */ function DbRelationModel(srcModel, foreignModel, reverse) { if (reverse === void 0) { reverse = false; } this.srcModel = srcModel; this.foreignModel = foreignModel; this.reverse = reverse; /** * @private * @property {Array<DbColumn>} dbColumns list of columns related to other model */ this.dbColumns = []; /** * @public * @property {string} key the relation key, allow defining multiple relations between two * models and reaching only one of its */ this.key = null; /** * @private * @property {string} relationType the relation type @see RelationType */ this.relationType = null; } Object.defineProperty(DbRelationModel.prototype, "sourceModel", { /** * @public * @property {{new(): DbHelperModel}} sourceModel source of the relation */ get: function () { if (this.reverse) { return this.targetModel; } return this.srcModel; }, enumerable: true, configurable: true }); Object.defineProperty(DbRelationModel.prototype, "targetModel", { /** * @public * @property {{new(): DbHelperModel}} targetModel target of the relation */ get: function () { if (this.reverse) { return this.srcModel; } return this.foreignModel; }, enumerable: true, configurable: true }); Object.defineProperty(DbRelationModel.prototype, "columns", { /** * @public * @property {Array<DbColumn>} columns columns list of the relation, multiple columns * are needed for composite keys */ get: function () { return this.dbColumns; }, enumerable: true, configurable: true }); Object.defineProperty(DbRelationModel.prototype, "isReverse", { /** * @public * @property {boolean} isReverse check the relation direction */ get: function () { return this.reverse; }, enumerable: true, configurable: true }); Object.defineProperty(DbRelationModel.prototype, "type", { /** * @public * @property {string} type the computed relation type @see RelationType */ get: function () { if (this.reverse && this.relationType === RelationType.MANY_TO_ONE) { return RelationType.ONE_TO_MANY; } return this.relationType; }, enumerable: true, configurable: true }); /** * @public * @method add add column to the relation * * @param {DbColumn} column the column to add */ DbRelationModel.prototype.add = function (column) { var table = ModelManager.getInstance().getModel(this.foreignModel); var foreignColumn = null; if (column.foreignKey) { foreignColumn = table.columns[column.foreignKey]; if (!foreignColumn) { throw new BadColumnDeclarationError('Can\'t resolve foreignKey "' + this.srcModel.name + '.' + column.field + '", no primary found in target model "' + this.foreignModel.name + '" with column name "' + column.foreignKey + '".'); } } else { var primaryColumns = table.getPrimaryColumns(); if (primaryColumns.length === 1) { foreignColumn = primaryColumns[0]; } else if (primaryColumns.length > 1) { throw new BadColumnDeclarationError('Can\'t resolve foreignKey "' + this.srcModel.name + '.' + column.field + '", multiple primary found in target model "' + this.foreignModel.name + '", add foreign column name in "foreignKey" of the column configurator.'); } else { throw new BadColumnDeclarationError('Can\'t resolve foreignKey "' + this.srcModel.name + '.' + column.field + '", no primary found in target model "' + this.foreignModel.name + '".'); } column.foreignKey = foreignColumn.name; } if (!column.name) { column.name = column.field + foreignColumn.name.slice(0, 1).toLocaleUpperCase() + foreignColumn.name.slice(1, foreignColumn.name.length); } column.foreignTable = table.name; column.foreignField = foreignColumn.field; column.type = foreignColumn.type; this.dbColumns.push(column); var relationType = column.unique ? RelationType.ONE_TO_ONE : RelationType.MANY_TO_ONE; if (relationType === RelationType.MANY_TO_MANY || this.relationType === null || relationType === RelationType.MANY_TO_ONE) { this.relationType = relationType; } }; return DbRelationModel; }()); export { DbRelationModel }; //# sourceMappingURL=db-relation.model.js.map