ModelFactory: refactor make into create

This commit is contained in:
Alice Gaudon 2020-07-27 10:52:39 +02:00
parent b179afdaf5
commit be6e659ca2
9 changed files with 16 additions and 14 deletions

View File

@ -1,7 +1,6 @@
import config from "config"; import config from "config";
import {v4 as uuid} from "uuid"; import {v4 as uuid} from "uuid";
import Log from "./models/Log"; import Log from "./models/Log";
import ModelFactory from "./db/ModelFactory";
import {bufferToUUID} from "./Utils"; import {bufferToUUID} from "./Utils";
export default class Logger { export default class Logger {
@ -106,7 +105,7 @@ export default class Logger {
} }
if (shouldSaveToDB) { if (shouldSaveToDB) {
const log = ModelFactory.get(Log).make({}); const log = Log.create({});
log.setLevel(level); log.setLevel(level);
log.message = computedMsg; log.message = computedMsg;
log.setError(error); log.setError(error);

View File

@ -32,7 +32,7 @@ export default abstract class AuthGuard<P extends AuthProof<User>> {
const callbacks: RegisterCallback[] = []; const callbacks: RegisterCallback[] = [];
await MysqlConnectionManager.wrapTransaction(async connection => { await MysqlConnectionManager.wrapTransaction(async connection => {
user = ModelFactory.get(User).make({}); user = User.create({});
if (onRegister) { if (onRegister) {
(await onRegister(connection, user)).forEach(c => callbacks.push(c)); (await onRegister(connection, user)).forEach(c => callbacks.push(c));
} }

View File

@ -9,7 +9,6 @@ import {AuthError, PendingApprovalAuthError, RegisterCallback} from "../AuthGuar
import geoip from "geoip-lite"; import geoip from "geoip-lite";
import AuthController from "../AuthController"; import AuthController from "../AuthController";
import NunjucksComponent from "../../components/NunjucksComponent"; import NunjucksComponent from "../../components/NunjucksComponent";
import ModelFactory from "../../db/ModelFactory";
export default abstract class MagicLinkAuthController extends AuthController { 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) => { await req.authGuard.authenticateOrRegister(req.session!, magicLink, undefined, async (connection, user) => {
const callbacks: RegisterCallback[] = []; const callbacks: RegisterCallback[] = [];
const userEmail = ModelFactory.get(UserEmail).make({ const userEmail = UserEmail.create({
user_id: user.id, user_id: user.id,
email: magicLink.getEmail(), email: magicLink.getEmail(),
}); });
@ -84,7 +83,7 @@ export default abstract class MagicLinkAuthController extends AuthController {
if (!userEmail) { if (!userEmail) {
isRegistration = true; isRegistration = true;
userEmail = ModelFactory.get(UserEmail).make({ userEmail = UserEmail.create({
email: email, email: email,
main: true, main: true,
}); });

View File

@ -14,7 +14,7 @@ export default abstract class MagicLinkController extends Controller {
Throttler.throttle('magic_link', 1, MagicLink.validityPeriod(), email, 0, 0); Throttler.throttle('magic_link', 1, MagicLink.validityPeriod(), email, 0, 0);
const link = await MagicLink.bySessionID(sessionID, actionType) || const link = await MagicLink.bySessionID(sessionID, actionType) ||
ModelFactory.get(MagicLink).make({ MagicLink.create({
session_id: sessionID, session_id: sessionID,
action_type: actionType, action_type: actionType,
original_url: original_url, original_url: original_url,

View File

@ -9,6 +9,10 @@ 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 create<T extends Model>(this: Type<T>, data: any): T {
return ModelFactory.get(this).create(data);
}
public static select<T extends Model>(this: Type<T>, ...fields: string[]): ModelQuery<T> { public static select<T extends Model>(this: Type<T>, ...fields: string[]): ModelQuery<T> {
return ModelFactory.get(this).select(...fields); return ModelFactory.get(this).select(...fields);
} }

View File

@ -29,7 +29,7 @@ export default class ModelFactory<T extends Model> {
this.components.push(modelComponentFactory); this.components.push(modelComponentFactory);
} }
public make(data: any): T { public create(data: any): T {
const model = new this.modelType(this, data); const model = new this.modelType(this, data);
for (const component of this.components) { for (const component of this.components) {
model.addComponent(new component(model)); model.addComponent(new component(model));

View File

@ -170,7 +170,7 @@ export default class ModelQuery<M extends Model> {
} }
for (const result of queryResult.results) { for (const result of queryResult.results) {
const model = this.factory.make(result); const model = this.factory.create(result);
models.push(model); models.push(model);
if (this._pivot) { if (this._pivot) {

View File

@ -42,8 +42,6 @@ export default abstract class ModelRelation<S extends Model, O extends Model, R
public abstract async populate(models: ModelQueryResult<O>): Promise<void>; public abstract async populate(models: ModelQueryResult<O>): Promise<void>;
} }
export type QueryModifier<M extends Model> = (query: ModelQuery<M>) => ModelQuery<M>;
export class OneModelRelation<S extends Model, O extends Model> extends ModelRelation<S, O, O | null> { export class OneModelRelation<S extends Model, O extends Model> extends ModelRelation<S, O, O | null> {
protected readonly dbProperties: RelationDatabaseProperties; protected readonly dbProperties: RelationDatabaseProperties;
@ -120,7 +118,6 @@ export class ManyModelRelation<S extends Model, O extends Model> extends ModelRe
} }
export class ManyThroughModelRelation<S extends Model, O extends Model> extends ModelRelation<S, O, O[]> { export class ManyThroughModelRelation<S extends Model, O extends Model> extends ModelRelation<S, O, O[]> {
protected readonly dbProperties: PivotRelationDatabaseProperties; protected readonly dbProperties: PivotRelationDatabaseProperties;
@ -163,6 +160,9 @@ export class ManyThroughModelRelation<S extends Model, O extends Model> extends
} }
export type QueryModifier<M extends Model> = (query: ModelQuery<M>) => ModelQuery<M>;
export type RelationDatabaseProperties = { export type RelationDatabaseProperties = {
localKey: string; localKey: string;
foreignKey: string; foreignKey: string;

View File

@ -30,7 +30,7 @@ describe('Model', () => {
it('should have a proper table name', async () => { it('should have a proper table name', async () => {
const factory = ModelFactory.get(FakeDummyModel); const factory = ModelFactory.get(FakeDummyModel);
expect(factory.table).toBe('fake_dummy_models'); 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 () => { it('should insert and retrieve properly', async () => {
@ -45,7 +45,7 @@ describe('Model', () => {
)`); )`);
const date = new Date(569985); const date = new Date(569985);
let instance: FakeDummyModel | null = factory.make({ let instance: FakeDummyModel | null = FakeDummyModel.create({
name: 'name1', name: 'name1',
date: date, date: date,
}); });