ModelRelation: simplify foreign model constructor parameter

This commit is contained in:
Alice Gaudon 2020-09-07 14:28:18 +02:00
parent b88f4e1b64
commit 2f822aa61c
4 changed files with 24 additions and 24 deletions

View File

@ -18,7 +18,7 @@ export default class User extends Model {
public created_at?: Date = undefined;
public updated_at?: Date = undefined;
public readonly emails = new ManyModelRelation(this, ModelFactory.get(UserEmail), {
public readonly emails = new ManyModelRelation(this, UserEmail, {
localKey: 'id',
foreignKey: 'user_id'
});

View File

@ -10,7 +10,7 @@ export default class UserEmail extends Model {
public readonly email?: string = undefined;
public created_at?: Date = undefined;
public readonly user = new OneModelRelation<UserEmail, User>(this, ModelFactory.get(User), {
public readonly user = new OneModelRelation(this, User, {
localKey: 'user_id',
foreignKey: 'id'
});

View File

@ -1,18 +1,18 @@
import ModelQuery, {ModelQueryResult, WhereTest} from "./ModelQuery";
import Model from "./Model";
import Model, {ModelType} from "./Model";
import ModelFactory from "./ModelFactory";
export default abstract class ModelRelation<S extends Model, O extends Model, R extends O | O[] | null> {
protected readonly model: S;
protected readonly foreignFactory: ModelFactory<O>;
protected readonly foreignModelType: ModelType<O>;
protected readonly dbProperties: RelationDatabaseProperties;
protected readonly queryModifiers: QueryModifier<O>[] = [];
protected readonly filters: ModelFilter<O>[] = [];
protected cachedModels?: O[];
protected constructor(model: S, foreignFactory: ModelFactory<O>, dbProperties: RelationDatabaseProperties) {
protected constructor(model: S, foreignModelType: ModelType<O>, dbProperties: RelationDatabaseProperties) {
this.model = model;
this.foreignFactory = foreignFactory;
this.foreignModelType = foreignModelType;
this.dbProperties = dbProperties;
}
@ -29,7 +29,7 @@ export default abstract class ModelRelation<S extends Model, O extends Model, R
}
protected makeQuery(): ModelQuery<O> {
const query = this.foreignFactory.select();
const query = ModelFactory.get(this.foreignModelType).select();
for (const modifier of this.queryModifiers) modifier(query);
return query;
}
@ -100,12 +100,12 @@ export default abstract class ModelRelation<S extends Model, O extends Model, R
}
export class OneModelRelation<S extends Model, O extends Model> extends ModelRelation<S, O, O | null> {
public constructor(model: S, foreignFactory: ModelFactory<O>, dbProperties: RelationDatabaseProperties) {
super(model, foreignFactory, dbProperties);
public constructor(model: S, foreignModelType: ModelType<O>, dbProperties: RelationDatabaseProperties) {
super(model, foreignModelType, dbProperties);
}
public clone(): OneModelRelation<S, O> {
return new OneModelRelation(this.model, this.foreignFactory, this.dbProperties);
return new OneModelRelation(this.model, this.foreignModelType, this.dbProperties);
}
protected collectionToOutput(models: O[]): O | null {
@ -124,16 +124,16 @@ export class OneModelRelation<S extends Model, O extends Model> extends ModelRel
export class ManyModelRelation<S extends Model, O extends Model> extends ModelRelation<S, O, O[]> {
protected readonly paginatedCache: { [perPage: number]: { [pageNumber: number]: ModelQueryResult<O> } } = {};
constructor(model: S, foreignFactory: ModelFactory<O>, dbProperties: RelationDatabaseProperties) {
super(model, foreignFactory, dbProperties);
constructor(model: S, foreignModelType: ModelType<O>, dbProperties: RelationDatabaseProperties) {
super(model, foreignModelType, dbProperties);
}
public clone(): ManyModelRelation<S, O> {
return new ManyModelRelation<S, O>(this.model, this.foreignFactory, this.dbProperties);
return new ManyModelRelation<S, O>(this.model, this.foreignModelType, this.dbProperties);
}
public cloneReduceToOne(): OneModelRelation<S, O> {
return new OneModelRelation<S, O>(this.model, this.foreignFactory, this.dbProperties);
return new OneModelRelation<S, O>(this.model, this.foreignModelType, this.dbProperties);
}
protected collectionToOutput(models: O[]): O[] {
@ -158,17 +158,17 @@ export class ManyModelRelation<S extends Model, O extends Model> extends ModelRe
export class ManyThroughModelRelation<S extends Model, O extends Model> extends ManyModelRelation<S, O> {
protected readonly dbProperties: PivotRelationDatabaseProperties;
constructor(model: S, foreignFactory: ModelFactory<O>, dbProperties: PivotRelationDatabaseProperties) {
super(model, foreignFactory, dbProperties);
constructor(model: S, foreignModelType: ModelType<O>, dbProperties: PivotRelationDatabaseProperties) {
super(model, foreignModelType, dbProperties);
this.dbProperties = dbProperties;
this.constraint(query => query
.leftJoin(this.dbProperties.pivotTable, 'pivot')
.on(`pivot.${this.dbProperties.foreignPivotKey}`, `${this.foreignFactory.table}.${this.dbProperties.foreignKey}`)
.on(`pivot.${this.dbProperties.foreignPivotKey}`, `${this.foreignModelType.table}.${this.dbProperties.foreignKey}`)
);
}
public clone(): ManyThroughModelRelation<S, O> {
return new ManyThroughModelRelation<S, O>(this.model, this.foreignFactory, this.dbProperties);
return new ManyThroughModelRelation<S, O>(this.model, this.foreignModelType, this.dbProperties);
}
public cloneReduceToOne(): OneModelRelation<S, O> {
@ -201,13 +201,13 @@ export class ManyThroughModelRelation<S extends Model, O extends Model> extends
}
export class RecursiveModelRelation<M extends Model> extends ManyModelRelation<M, M> {
public constructor(model: M, foreignFactory: ModelFactory<M>, dbProperties: RelationDatabaseProperties) {
super(model, foreignFactory, dbProperties);
public constructor(model: M, foreignModelType: ModelType<M>, dbProperties: RelationDatabaseProperties) {
super(model, foreignModelType, dbProperties);
this.constraint(query => query.recursive(this.dbProperties));
}
public clone(): RecursiveModelRelation<M> {
return new RecursiveModelRelation(this.model, this.foreignFactory, this.dbProperties);
return new RecursiveModelRelation(this.model, this.foreignModelType, this.dbProperties);
}
public async populate(models: ModelQueryResult<M>): Promise<void> {

View File

@ -22,7 +22,7 @@ class Post extends Model {
public author_id?: number = undefined;
public content?: string = undefined;
public readonly author: OneModelRelation<Post, Author> = new OneModelRelation<Post, Author>(this, ModelFactory.get(Author), {
public readonly author = new OneModelRelation(this, Author, {
localKey: 'author_id',
foreignKey: 'id',
});
@ -36,7 +36,7 @@ class Author extends Model {
public id?: number = undefined;
public name?: string = undefined;
public readonly roles: ManyThroughModelRelation<Author, Role> = new ManyThroughModelRelation<Author, Role>(this, ModelFactory.get(Role), {
public readonly roles = new ManyThroughModelRelation(this, Role, {
localKey: 'id',
foreignKey: 'id',
pivotTable: 'author_role',
@ -52,7 +52,7 @@ class Role extends Model {
public id?: number = undefined;
public name?: string = undefined;
public readonly permissions: ManyThroughModelRelation<Role, Permission> = new ManyThroughModelRelation<Role, Permission>(this, ModelFactory.get(Permission), {
public readonly permissions = new ManyThroughModelRelation(this, Permission, {
localKey: 'id',
foreignKey: 'id',
pivotTable: 'role_permission',