ModelRelation: simplify foreign model constructor parameter
This commit is contained in:
parent
b88f4e1b64
commit
2f822aa61c
@ -18,7 +18,7 @@ export default class User extends Model {
|
|||||||
public created_at?: Date = undefined;
|
public created_at?: Date = undefined;
|
||||||
public updated_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',
|
localKey: 'id',
|
||||||
foreignKey: 'user_id'
|
foreignKey: 'user_id'
|
||||||
});
|
});
|
||||||
|
@ -10,7 +10,7 @@ export default class UserEmail extends Model {
|
|||||||
public readonly email?: string = undefined;
|
public readonly email?: string = undefined;
|
||||||
public created_at?: Date = 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',
|
localKey: 'user_id',
|
||||||
foreignKey: 'id'
|
foreignKey: 'id'
|
||||||
});
|
});
|
||||||
|
@ -1,18 +1,18 @@
|
|||||||
import ModelQuery, {ModelQueryResult, WhereTest} from "./ModelQuery";
|
import ModelQuery, {ModelQueryResult, WhereTest} from "./ModelQuery";
|
||||||
import Model from "./Model";
|
import Model, {ModelType} from "./Model";
|
||||||
import ModelFactory from "./ModelFactory";
|
import ModelFactory from "./ModelFactory";
|
||||||
|
|
||||||
export default abstract class ModelRelation<S extends Model, O extends Model, R extends O | O[] | null> {
|
export default abstract class ModelRelation<S extends Model, O extends Model, R extends O | O[] | null> {
|
||||||
protected readonly model: S;
|
protected readonly model: S;
|
||||||
protected readonly foreignFactory: ModelFactory<O>;
|
protected readonly foreignModelType: ModelType<O>;
|
||||||
protected readonly dbProperties: RelationDatabaseProperties;
|
protected readonly dbProperties: RelationDatabaseProperties;
|
||||||
protected readonly queryModifiers: QueryModifier<O>[] = [];
|
protected readonly queryModifiers: QueryModifier<O>[] = [];
|
||||||
protected readonly filters: ModelFilter<O>[] = [];
|
protected readonly filters: ModelFilter<O>[] = [];
|
||||||
protected cachedModels?: 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.model = model;
|
||||||
this.foreignFactory = foreignFactory;
|
this.foreignModelType = foreignModelType;
|
||||||
this.dbProperties = dbProperties;
|
this.dbProperties = dbProperties;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -29,7 +29,7 @@ export default abstract class ModelRelation<S extends Model, O extends Model, R
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected makeQuery(): ModelQuery<O> {
|
protected makeQuery(): ModelQuery<O> {
|
||||||
const query = this.foreignFactory.select();
|
const query = ModelFactory.get(this.foreignModelType).select();
|
||||||
for (const modifier of this.queryModifiers) modifier(query);
|
for (const modifier of this.queryModifiers) modifier(query);
|
||||||
return 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> {
|
export class OneModelRelation<S extends Model, O extends Model> extends ModelRelation<S, O, O | null> {
|
||||||
public constructor(model: S, foreignFactory: ModelFactory<O>, dbProperties: RelationDatabaseProperties) {
|
public constructor(model: S, foreignModelType: ModelType<O>, dbProperties: RelationDatabaseProperties) {
|
||||||
super(model, foreignFactory, dbProperties);
|
super(model, foreignModelType, dbProperties);
|
||||||
}
|
}
|
||||||
|
|
||||||
public clone(): OneModelRelation<S, O> {
|
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 {
|
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[]> {
|
export class ManyModelRelation<S extends Model, O extends Model> extends ModelRelation<S, O, O[]> {
|
||||||
protected readonly paginatedCache: { [perPage: number]: { [pageNumber: number]: ModelQueryResult<O> } } = {};
|
protected readonly paginatedCache: { [perPage: number]: { [pageNumber: number]: ModelQueryResult<O> } } = {};
|
||||||
|
|
||||||
constructor(model: S, foreignFactory: ModelFactory<O>, dbProperties: RelationDatabaseProperties) {
|
constructor(model: S, foreignModelType: ModelType<O>, dbProperties: RelationDatabaseProperties) {
|
||||||
super(model, foreignFactory, dbProperties);
|
super(model, foreignModelType, dbProperties);
|
||||||
}
|
}
|
||||||
|
|
||||||
public clone(): ManyModelRelation<S, O> {
|
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> {
|
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[] {
|
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> {
|
export class ManyThroughModelRelation<S extends Model, O extends Model> extends ManyModelRelation<S, O> {
|
||||||
protected readonly dbProperties: PivotRelationDatabaseProperties;
|
protected readonly dbProperties: PivotRelationDatabaseProperties;
|
||||||
|
|
||||||
constructor(model: S, foreignFactory: ModelFactory<O>, dbProperties: PivotRelationDatabaseProperties) {
|
constructor(model: S, foreignModelType: ModelType<O>, dbProperties: PivotRelationDatabaseProperties) {
|
||||||
super(model, foreignFactory, dbProperties);
|
super(model, foreignModelType, dbProperties);
|
||||||
this.dbProperties = dbProperties;
|
this.dbProperties = dbProperties;
|
||||||
this.constraint(query => query
|
this.constraint(query => query
|
||||||
.leftJoin(this.dbProperties.pivotTable, 'pivot')
|
.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> {
|
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> {
|
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> {
|
export class RecursiveModelRelation<M extends Model> extends ManyModelRelation<M, M> {
|
||||||
public constructor(model: M, foreignFactory: ModelFactory<M>, dbProperties: RelationDatabaseProperties) {
|
public constructor(model: M, foreignModelType: ModelType<M>, dbProperties: RelationDatabaseProperties) {
|
||||||
super(model, foreignFactory, dbProperties);
|
super(model, foreignModelType, dbProperties);
|
||||||
this.constraint(query => query.recursive(this.dbProperties));
|
this.constraint(query => query.recursive(this.dbProperties));
|
||||||
}
|
}
|
||||||
|
|
||||||
public clone(): RecursiveModelRelation<M> {
|
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> {
|
public async populate(models: ModelQueryResult<M>): Promise<void> {
|
||||||
|
@ -22,7 +22,7 @@ class Post extends Model {
|
|||||||
public author_id?: number = undefined;
|
public author_id?: number = undefined;
|
||||||
public content?: string = 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',
|
localKey: 'author_id',
|
||||||
foreignKey: 'id',
|
foreignKey: 'id',
|
||||||
});
|
});
|
||||||
@ -36,7 +36,7 @@ class Author extends Model {
|
|||||||
public id?: number = undefined;
|
public id?: number = undefined;
|
||||||
public name?: string = 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',
|
localKey: 'id',
|
||||||
foreignKey: 'id',
|
foreignKey: 'id',
|
||||||
pivotTable: 'author_role',
|
pivotTable: 'author_role',
|
||||||
@ -52,7 +52,7 @@ class Role extends Model {
|
|||||||
public id?: number = undefined;
|
public id?: number = undefined;
|
||||||
public name?: string = 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',
|
localKey: 'id',
|
||||||
foreignKey: 'id',
|
foreignKey: 'id',
|
||||||
pivotTable: 'role_permission',
|
pivotTable: 'role_permission',
|
||||||
|
Loading…
Reference in New Issue
Block a user