From 5ca1ddef436e039010db76191441a399db7fb674 Mon Sep 17 00:00:00 2001 From: Alice Gaudon Date: Mon, 27 Jul 2020 10:53:46 +0200 Subject: [PATCH] Move responsability of table field from ModelFactory to Model (static) --- src/db/Model.ts | 7 +++++++ src/db/ModelFactory.ts | 6 ++---- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/db/Model.ts b/src/db/Model.ts index 7843ab2..25d3993 100644 --- a/src/db/Model.ts +++ b/src/db/Model.ts @@ -9,6 +9,13 @@ import ModelQuery, {ModelQueryResult} from "./ModelQuery"; import {Request} from "express"; 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(this: Type, data: any): T { return ModelFactory.get(this).create(data); } diff --git a/src/db/ModelFactory.ts b/src/db/ModelFactory.ts index 39b1216..1bfcd94 100644 --- a/src/db/ModelFactory.ts +++ b/src/db/ModelFactory.ts @@ -39,10 +39,8 @@ export default class ModelFactory { } public get table(): string { - return this.modelType.name - .replace(/(?:^|\.?)([A-Z])/g, (x, y) => '_' + y.toLowerCase()) - .replace(/^_/, '') - + 's'; + // @ts-ignore + return this.modelType.table; } public select(...fields: string[]): ModelQuery {