UNPKG

ts-db-helper

Version:
154 lines 5.29 kB
import { BadColumnDeclarationError } from '../../errors/bad-column-declaration.error'; /** * @public * @class DbTable * * @description * This class is a table model to help model migration to do his soup * * @author Olivier Margarit * @since 0.1 */ var DbTable = /** @class */ (function () { function DbTable() { /** * @public * @property {number} version, the table model version, information to help migration */ this.version = 0; /** * @public * @property {Array<DbColumn>} columnList is an array of DbColumn listing each column * sql properties */ this.columnList = []; /** * @public * @property {{[index: string]: DbColumn}} column key/value column list with column name as key */ this.columns = {}; /** * @public * @property {{[index: string]: DbColumn}} fields key/value column list with field name as key */ this.fields = {}; /** * @public * @property {{[index: string]: {[index: string]: DbRelationModel}}} relations two dimensional relation access by * model name and relation key */ this.relations = {}; } /** * @public * @method configure methode to configure table from configurator object * * @param {TableConfig} config the configurator * * @since 0.2 */ DbTable.prototype.configure = function (config) { if (config.version) { this.version = config.version; } }; /** * @public * @method hasAutoIncrementedPrimaryKey check if table has auto increment column * * @return {boolean} true if table has auto increment column * * @since 0.2 */ DbTable.prototype.hasAutoIncrementedPrimaryKey = function () { for (var _i = 0, _a = this.columnList; _i < _a.length; _i++) { var column = _a[_i]; if (column.primaryKey && column.autoIncrement) { return true; } } return false; }; /** * @public * @method hasNoPrimaryKey check if table has no primary key * * @return {boolean} return true if table has no primary key * * @since 0.2 */ DbTable.prototype.hasNoPrimaryKey = function () { for (var _i = 0, _a = this.columnList; _i < _a.length; _i++) { var column = _a[_i]; if (column.primaryKey) { return false; } } return true; }; /** * @public * @method getPrimaryColumns get column playing primary role among table's columns * * @return {Array<DbColumn>} the structured primary column list * * @since 0.2 */ DbTable.prototype.getPrimaryColumns = function () { var columns = []; for (var _i = 0, _a = this.columnList; _i < _a.length; _i++) { var column = _a[_i]; if (column.primaryKey) { columns.push(column); } } return columns; }; /** * @public * @method addRelation add relation to table model * * @param {{new(): DbHelperModel}} model the target model of the relation * @param {DbRalationModel} relation the relation * @param {string} key optional key to manage multiple relations to the same model * * @since 0.2 */ DbTable.prototype.addRelation = function (model, relation, key) { if (this.relations.hasOwnProperty(model.name) && this.relations[model.name].hasOwnProperty(key || DbTable.RELATIONS_DEFAULT_KEY)) { throw new BadColumnDeclarationError('relation with key "' + (key || DbTable.RELATIONS_DEFAULT_KEY) + '" is inserted twice'); } else { if (!this.relations[model.name]) { this.relations[model.name] = {}; } this.relations[model.name][key || DbTable.RELATIONS_DEFAULT_KEY] = relation; } }; /** * @public * @method getRelation get relation from the targetted model and the optionale relation key * * @param {{new(): DbHelperModel}} model the target model of the relation * @param {string} key optional key to manage multiple relations to the same model * * @return {DbRelationModel} the relation matching to the properties, return is null if no relation matches * * @since 0.2 */ DbTable.prototype.getRelation = function (model, key) { if (this.relations.hasOwnProperty(model.name) && this.relations[model.name].hasOwnProperty(key || DbTable.RELATIONS_DEFAULT_KEY)) { return this.relations[model.name][key || DbTable.RELATIONS_DEFAULT_KEY]; } return null; }; /** * @private * @static * @constant {string} RELATIONS_DEFAULT_KEY the default relation key */ DbTable.RELATIONS_DEFAULT_KEY = 'default'; return DbTable; }()); export { DbTable }; //# sourceMappingURL=db-table.model.js.map