diff --git a/src/Logger.ts b/src/Logger.ts index fae6c03..13f89f3 100644 --- a/src/Logger.ts +++ b/src/Logger.ts @@ -1,7 +1,6 @@ import config from "config"; import {v4 as uuid} from "uuid"; import Log from "./models/Log"; -import ModelFactory from "./db/ModelFactory"; import {bufferToUUID} from "./Utils"; export default class Logger { @@ -106,7 +105,7 @@ export default class Logger { } if (shouldSaveToDB) { - const log = ModelFactory.get(Log).make({}); + const log = Log.create({}); log.setLevel(level); log.message = computedMsg; log.setError(error); diff --git a/src/auth/AuthGuard.ts b/src/auth/AuthGuard.ts index d213089..46cf1e6 100644 --- a/src/auth/AuthGuard.ts +++ b/src/auth/AuthGuard.ts @@ -32,7 +32,7 @@ export default abstract class AuthGuard

> { const callbacks: RegisterCallback[] = []; await MysqlConnectionManager.wrapTransaction(async connection => { - user = ModelFactory.get(User).make({}); + user = User.create({}); if (onRegister) { (await onRegister(connection, user)).forEach(c => callbacks.push(c)); } diff --git a/src/auth/magic_link/MagicLinkAuthController.ts b/src/auth/magic_link/MagicLinkAuthController.ts index b749afd..9d01242 100644 --- a/src/auth/magic_link/MagicLinkAuthController.ts +++ b/src/auth/magic_link/MagicLinkAuthController.ts @@ -9,7 +9,6 @@ import {AuthError, PendingApprovalAuthError, RegisterCallback} from "../AuthGuar import geoip from "geoip-lite"; import AuthController from "../AuthController"; import NunjucksComponent from "../../components/NunjucksComponent"; -import ModelFactory from "../../db/ModelFactory"; export default abstract class MagicLinkAuthController extends AuthController { @@ -23,7 +22,7 @@ export default abstract class MagicLinkAuthController extends AuthController { await req.authGuard.authenticateOrRegister(req.session!, magicLink, undefined, async (connection, user) => { const callbacks: RegisterCallback[] = []; - const userEmail = ModelFactory.get(UserEmail).make({ + const userEmail = UserEmail.create({ user_id: user.id, email: magicLink.getEmail(), }); @@ -84,7 +83,7 @@ export default abstract class MagicLinkAuthController extends AuthController { if (!userEmail) { isRegistration = true; - userEmail = ModelFactory.get(UserEmail).make({ + userEmail = UserEmail.create({ email: email, main: true, }); diff --git a/src/auth/magic_link/MagicLinkController.ts b/src/auth/magic_link/MagicLinkController.ts index 879be42..0aa6643 100644 --- a/src/auth/magic_link/MagicLinkController.ts +++ b/src/auth/magic_link/MagicLinkController.ts @@ -14,7 +14,7 @@ export default abstract class MagicLinkController extends Controller { Throttler.throttle('magic_link', 1, MagicLink.validityPeriod(), email, 0, 0); const link = await MagicLink.bySessionID(sessionID, actionType) || - ModelFactory.get(MagicLink).make({ + MagicLink.create({ session_id: sessionID, action_type: actionType, original_url: original_url, diff --git a/src/db/Model.ts b/src/db/Model.ts index 578705e..7843ab2 100644 --- a/src/db/Model.ts +++ b/src/db/Model.ts @@ -9,6 +9,10 @@ import ModelQuery, {ModelQueryResult} from "./ModelQuery"; import {Request} from "express"; export default abstract class Model { + public static create(this: Type, data: any): T { + return ModelFactory.get(this).create(data); + } + public static select(this: Type, ...fields: string[]): ModelQuery { return ModelFactory.get(this).select(...fields); } diff --git a/src/db/ModelFactory.ts b/src/db/ModelFactory.ts index a47f9c9..39b1216 100644 --- a/src/db/ModelFactory.ts +++ b/src/db/ModelFactory.ts @@ -29,7 +29,7 @@ export default class ModelFactory { this.components.push(modelComponentFactory); } - public make(data: any): T { + public create(data: any): T { const model = new this.modelType(this, data); for (const component of this.components) { model.addComponent(new component(model)); diff --git a/src/db/ModelQuery.ts b/src/db/ModelQuery.ts index 57bdbe7..0708c02 100644 --- a/src/db/ModelQuery.ts +++ b/src/db/ModelQuery.ts @@ -170,7 +170,7 @@ export default class ModelQuery { } for (const result of queryResult.results) { - const model = this.factory.make(result); + const model = this.factory.create(result); models.push(model); if (this._pivot) { diff --git a/src/db/ModelRelation.ts b/src/db/ModelRelation.ts index 1bdfd07..994cd78 100644 --- a/src/db/ModelRelation.ts +++ b/src/db/ModelRelation.ts @@ -42,8 +42,6 @@ export default abstract class ModelRelation): Promise; } -export type QueryModifier = (query: ModelQuery) => ModelQuery; - export class OneModelRelation extends ModelRelation { protected readonly dbProperties: RelationDatabaseProperties; @@ -120,7 +118,6 @@ export class ManyModelRelation extends ModelRe } - export class ManyThroughModelRelation extends ModelRelation { protected readonly dbProperties: PivotRelationDatabaseProperties; @@ -163,6 +160,9 @@ export class ManyThroughModelRelation extends } + +export type QueryModifier = (query: ModelQuery) => ModelQuery; + export type RelationDatabaseProperties = { localKey: string; foreignKey: string; diff --git a/test/Model.test.ts b/test/Model.test.ts index e4896c3..53bcb60 100644 --- a/test/Model.test.ts +++ b/test/Model.test.ts @@ -30,7 +30,7 @@ describe('Model', () => { it('should have a proper table name', async () => { const factory = ModelFactory.get(FakeDummyModel); expect(factory.table).toBe('fake_dummy_models'); - expect(factory.make({}).table).toBe('fake_dummy_models'); + expect(FakeDummyModel.create({}).table).toBe('fake_dummy_models'); }); it('should insert and retrieve properly', async () => { @@ -45,7 +45,7 @@ describe('Model', () => { )`); const date = new Date(569985); - let instance: FakeDummyModel | null = factory.make({ + let instance: FakeDummyModel | null = FakeDummyModel.create({ name: 'name1', date: date, });