Move responsability of table field from ModelFactory to Model (static)

This commit is contained in:
Alice Gaudon 2020-07-27 10:53:46 +02:00
parent be6e659ca2
commit 5ca1ddef43
2 changed files with 9 additions and 4 deletions

View File

@ -9,6 +9,13 @@ import ModelQuery, {ModelQueryResult} from "./ModelQuery";
import {Request} from "express"; import {Request} from "express";
export default abstract class Model { export default abstract class Model {
public static get table(): string {
return this.name
.replace(/(?:^|\.?)([A-Z])/g, (x, y) => '_' + y.toLowerCase())
.replace(/^_/, '')
+ 's';
}
public static create<T extends Model>(this: Type<T>, data: any): T { public static create<T extends Model>(this: Type<T>, data: any): T {
return ModelFactory.get(this).create(data); return ModelFactory.get(this).create(data);
} }

View File

@ -39,10 +39,8 @@ export default class ModelFactory<T extends Model> {
} }
public get table(): string { public get table(): string {
return this.modelType.name // @ts-ignore
.replace(/(?:^|\.?)([A-Z])/g, (x, y) => '_' + y.toLowerCase()) return this.modelType.table;
.replace(/^_/, '')
+ 's';
} }
public select(...fields: string[]): ModelQuery<T> { public select(...fields: string[]): ModelQuery<T> {