2020-07-24 12:13:28 +02:00
|
|
|
import ModelComponent from "./ModelComponent";
|
2020-09-06 10:23:32 +02:00
|
|
|
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> } = {};
|
|
|
|
|
2020-09-06 10:23:32 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2020-09-06 10:23:32 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-09-06 10:23:32 +02:00
|
|
|
private readonly modelType: ModelType<T>;
|
2020-07-24 12:13:28 +02:00
|
|
|
private readonly components: ModelComponentFactory<T>[] = [];
|
|
|
|
|
2020-09-06 10:23:32 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2020-07-27 10:52:39 +02:00
|
|
|
public create(data: any): T {
|
2020-07-24 15:40:10 +02:00
|
|
|
const model = new this.modelType(this, data);
|
2020-07-24 12:13:28 +02:00
|
|
|
for (const component of this.components) {
|
|
|
|
model.addComponent(new component(model));
|
|
|
|
}
|
2020-07-24 15:40:10 +02:00
|
|
|
model.updateWithData(data);
|
2020-07-24 12:13:28 +02:00
|
|
|
return model;
|
|
|
|
}
|
|
|
|
|
|
|
|
public get table(): string {
|
2020-07-27 10:53:46 +02:00
|
|
|
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[] {
|
2020-09-06 10:23:32 +02:00
|
|
|
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>;
|