swaf/src/db/ModelFactory.ts

91 lines
3.1 KiB
TypeScript
Raw Normal View History

2020-07-24 12:13:28 +02:00
import ModelComponent from "./ModelComponent";
import Model, {ModelType} from "./Model";
2020-09-05 15:33:51 +02:00
import ModelQuery, {ModelQueryResult, SelectFields} from "./ModelQuery";
2020-07-24 12:13:28 +02:00
import {Request} from "express";
export default class ModelFactory<T extends Model> {
private static readonly factories: { [modelType: string]: ModelFactory<any> } = {};
public static register<M extends Model>(modelType: ModelType<M>) {
2020-07-24 12:13:28 +02:00
if (this.factories[modelType.name]) throw new Error(`Factory for type ${modelType.name} already defined.`);
this.factories[modelType.name] = new ModelFactory<M>(modelType);
}
public static get<M extends Model>(modelType: ModelType<M>): ModelFactory<M> {
2020-07-24 12:13:28 +02:00
const factory = this.factories[modelType.name];
if (!factory) throw new Error(`No factory registered for ${modelType.name}.`);
return factory;
}
private readonly modelType: ModelType<T>;
2020-07-24 12:13:28 +02:00
private readonly components: ModelComponentFactory<T>[] = [];
protected constructor(modelType: ModelType<T>) {
2020-07-24 12:13:28 +02:00
this.modelType = modelType;
}
public addComponent(modelComponentFactory: ModelComponentFactory<T>) {
this.components.push(modelComponentFactory);
}
public create(data: any, isNewModel: boolean): T {
const model = new this.modelType(this, isNewModel);
2020-07-24 12:13:28 +02:00
for (const component of this.components) {
model.addComponent(new component(model));
}
model.updateWithData(data);
2020-07-24 12:13:28 +02:00
return model;
}
public get table(): string {
return this.modelType.table;
2020-07-24 12:13:28 +02:00
}
2020-09-05 15:33:51 +02:00
public select(...fields: SelectFields): ModelQuery<T> {
2020-07-24 12:13:28 +02:00
return ModelQuery.select(this, ...fields);
}
public update(data: { [key: string]: any }): ModelQuery<T> {
return ModelQuery.update(this, data);
}
public delete(): ModelQuery<T> {
return ModelQuery.delete(this);
}
public getPrimaryKeyFields(): string[] {
return this.modelType.getPrimaryKeyFields();
2020-07-24 12:13:28 +02:00
}
public getPrimaryKey(modelData: any): any[] {
return this.getPrimaryKeyFields().map(f => modelData[f]);
}
public getPrimaryKeyString(modelData: any): string {
return this.getPrimaryKey(modelData).join(',');
}
public async getById(...id: any): Promise<T | null> {
let query = this.select();
const primaryKeyFields = this.getPrimaryKeyFields();
for (let i = 0; i < primaryKeyFields.length; i++) {
query = query.where(primaryKeyFields[i], id[i]);
}
return query.first();
}
public async paginate(request: Request, perPage: number = 20, query?: ModelQuery<T>): Promise<ModelQueryResult<T>> {
let 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<T extends Model> = new (model: T) => ModelComponent<T>;