import { IKeyGenParams } from '@antjs/ant-js/src/model/IKeyGenParams'; import { Model } from '@antjs/ant-js/src/model/Model'; import { IAntSQLColumn } from './IAntSQLColumn'; import { IAntSqlModel } from './IAntSqlModel'; export class AntSqlModel extends Model implements IAntSqlModel { /** * Auto generated column. */ protected _autoGeneratedColumn: IAntSQLColumn; /** * Map of table columns, including the id; * The key of the map is the alias of the column in the entities managed. * The value of the map is the column info. */ protected _columns: Map; /** * SQL table name. */ protected _tableName: string; /** * Constructor. * @param id Model's id. * @param keyGen Key generation config. * @param tableName SQL table name. */ public constructor( id: string, keyGen: IKeyGenParams, columns: Iterable, tableName: string, ) { super(id, keyGen); this._initializeColumns(columns); this._tableName = tableName; } /** * Map of table columns, including the id; * The key of the map is the alias of the column in the entities managed. * The value of the map is the column info. */ public get columns(): Map { return this._columns; } /** * SQL table name. */ public get tableName(): string { return this._tableName; } /** * Gets the auto generated column of the model. * @returns Auto generated column of the model or null if no column is auto generated. */ public getAutoGeneratedColumn(): IAntSQLColumn { return this._autoGeneratedColumn; } /** * Initializes the columns map. * @param columns Columns to set. */ private _initializeColumns(columns: Iterable): void { this._autoGeneratedColumn = null; this._columns = new Map(); for (const column of columns) { if (null != column.autoGenerationStrategy) { if (null === this._autoGeneratedColumn) { this._autoGeneratedColumn = column; } else { throw new Error( 'Unexpected auto generated column. There is already an auto generated column in this model', ); } } this._columns.set(column.entityAlias, column); } } }