ModelFactory: refactor make into create
This commit is contained in:
parent
b179afdaf5
commit
be6e659ca2
@ -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);
|
||||
|
@ -32,7 +32,7 @@ export default abstract class AuthGuard<P extends AuthProof<User>> {
|
||||
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));
|
||||
}
|
||||
|
@ -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,
|
||||
});
|
||||
|
@ -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,
|
||||
|
@ -9,6 +9,10 @@ import ModelQuery, {ModelQueryResult} from "./ModelQuery";
|
||||
import {Request} from "express";
|
||||
|
||||
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> {
|
||||
return ModelFactory.get(this).select(...fields);
|
||||
}
|
||||
|
@ -29,7 +29,7 @@ export default class ModelFactory<T extends Model> {
|
||||
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));
|
||||
|
@ -170,7 +170,7 @@ export default class ModelQuery<M extends Model> {
|
||||
}
|
||||
|
||||
for (const result of queryResult.results) {
|
||||
const model = this.factory.make(result);
|
||||
const model = this.factory.create(result);
|
||||
models.push(model);
|
||||
|
||||
if (this._pivot) {
|
||||
|
@ -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>;
|
||||
}
|
||||
|
||||
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> {
|
||||
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[]> {
|
||||
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 = {
|
||||
localKey: string;
|
||||
foreignKey: string;
|
||||
|
@ -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,
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user