import ModelComponent from "./ModelComponent"; import Model, {ModelType} from "./Model"; import ModelQuery, {ModelQueryResult, SelectFields} from "./ModelQuery"; import {Request} from "express"; export default class ModelFactory { private static readonly factories: { [modelType: string]: ModelFactory | undefined } = {}; public static register(modelType: ModelType): void { if (this.factories[modelType.name]) throw new Error(`Factory for type ${modelType.name} already defined.`); this.factories[modelType.name] = new ModelFactory(modelType) as unknown as ModelFactory; } public static get(modelType: ModelType): ModelFactory { const factory = this.factories[modelType.name]; if (!factory) throw new Error(`No factory registered for ${modelType.name}.`); return factory as unknown as ModelFactory; } public static has(modelType: ModelType): boolean { return !!this.factories[modelType.name]; } private readonly modelType: ModelType; private readonly components: ModelComponentFactory[] = []; protected constructor(modelType: ModelType) { this.modelType = modelType; } public addComponent(modelComponentFactory: ModelComponentFactory): void { this.components.push(modelComponentFactory); } public create(data: Pick, isNewModel: boolean): M { const model = new this.modelType(this as unknown as ModelFactory, isNewModel); for (const component of this.components) { model.addComponent(new component(model)); } model.updateWithData(data); return model; } public get table(): string { return this.modelType.table; } public select(...fields: SelectFields): ModelQuery { return ModelQuery.select(this, ...fields); } public update(data: Pick): ModelQuery { return ModelQuery.update(this, data); } public delete(): ModelQuery { return ModelQuery.delete(this); } public getPrimaryKeyFields(): (keyof M & string)[] { return this.modelType.getPrimaryKeyFields(); } public getPrimaryKey(modelData: Pick): Pick[keyof M & string][] { return this.getPrimaryKeyFields().map(f => modelData[f]); } public getPrimaryKeyString(modelData: Pick): string { return this.getPrimaryKey(modelData).join(','); } public async getById(...id: PrimaryKeyValue[]): Promise { let query = this.select(); const primaryKeyFields = this.getPrimaryKeyFields(); for (let i = 0; i < primaryKeyFields.length; i++) { query = query.where(primaryKeyFields[i], id[i]); } return await query.first(); } public async paginate(request: Request, perPage: number = 20, query?: ModelQuery): Promise> { const page = request.params.page ? parseInt(request.params.page) : 1; if (!query) query = this.select(); if (request.params.sortBy) { const dir = request.params.sortDirection; query = query.sortBy(request.params.sortBy, dir === 'ASC' || dir === 'DESC' ? dir : undefined); } else { query = query.sortBy('id'); } return await query.paginate(page, perPage); } } export type ModelComponentFactory = new (model: M) => ModelComponent; export type PrimaryKeyValue = string | number | boolean | null | undefined;