diff --git a/src/auth/models/User.ts b/src/auth/models/User.ts index d53485c..e54c26a 100644 --- a/src/auth/models/User.ts +++ b/src/auth/models/User.ts @@ -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' }); diff --git a/src/auth/models/UserEmail.ts b/src/auth/models/UserEmail.ts index 0833e52..5718dbc 100644 --- a/src/auth/models/UserEmail.ts +++ b/src/auth/models/UserEmail.ts @@ -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(this, ModelFactory.get(User), { + public readonly user = new OneModelRelation(this, User, { localKey: 'user_id', foreignKey: 'id' }); diff --git a/src/db/ModelRelation.ts b/src/db/ModelRelation.ts index 120ad05..388d1a7 100644 --- a/src/db/ModelRelation.ts +++ b/src/db/ModelRelation.ts @@ -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 { protected readonly model: S; - protected readonly foreignFactory: ModelFactory; + protected readonly foreignModelType: ModelType; protected readonly dbProperties: RelationDatabaseProperties; protected readonly queryModifiers: QueryModifier[] = []; protected readonly filters: ModelFilter[] = []; protected cachedModels?: O[]; - protected constructor(model: S, foreignFactory: ModelFactory, dbProperties: RelationDatabaseProperties) { + protected constructor(model: S, foreignModelType: ModelType, dbProperties: RelationDatabaseProperties) { this.model = model; - this.foreignFactory = foreignFactory; + this.foreignModelType = foreignModelType; this.dbProperties = dbProperties; } @@ -29,7 +29,7 @@ export default abstract class ModelRelation { - 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 extends ModelRelation { - public constructor(model: S, foreignFactory: ModelFactory, dbProperties: RelationDatabaseProperties) { - super(model, foreignFactory, dbProperties); + public constructor(model: S, foreignModelType: ModelType, dbProperties: RelationDatabaseProperties) { + super(model, foreignModelType, dbProperties); } public clone(): OneModelRelation { - 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 extends ModelRel export class ManyModelRelation extends ModelRelation { protected readonly paginatedCache: { [perPage: number]: { [pageNumber: number]: ModelQueryResult } } = {}; - constructor(model: S, foreignFactory: ModelFactory, dbProperties: RelationDatabaseProperties) { - super(model, foreignFactory, dbProperties); + constructor(model: S, foreignModelType: ModelType, dbProperties: RelationDatabaseProperties) { + super(model, foreignModelType, dbProperties); } public clone(): ManyModelRelation { - return new ManyModelRelation(this.model, this.foreignFactory, this.dbProperties); + return new ManyModelRelation(this.model, this.foreignModelType, this.dbProperties); } public cloneReduceToOne(): OneModelRelation { - return new OneModelRelation(this.model, this.foreignFactory, this.dbProperties); + return new OneModelRelation(this.model, this.foreignModelType, this.dbProperties); } protected collectionToOutput(models: O[]): O[] { @@ -158,17 +158,17 @@ export class ManyModelRelation extends ModelRe export class ManyThroughModelRelation extends ManyModelRelation { protected readonly dbProperties: PivotRelationDatabaseProperties; - constructor(model: S, foreignFactory: ModelFactory, dbProperties: PivotRelationDatabaseProperties) { - super(model, foreignFactory, dbProperties); + constructor(model: S, foreignModelType: ModelType, 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 { - return new ManyThroughModelRelation(this.model, this.foreignFactory, this.dbProperties); + return new ManyThroughModelRelation(this.model, this.foreignModelType, this.dbProperties); } public cloneReduceToOne(): OneModelRelation { @@ -201,13 +201,13 @@ export class ManyThroughModelRelation extends } export class RecursiveModelRelation extends ManyModelRelation { - public constructor(model: M, foreignFactory: ModelFactory, dbProperties: RelationDatabaseProperties) { - super(model, foreignFactory, dbProperties); + public constructor(model: M, foreignModelType: ModelType, dbProperties: RelationDatabaseProperties) { + super(model, foreignModelType, dbProperties); this.constraint(query => query.recursive(this.dbProperties)); } public clone(): RecursiveModelRelation { - return new RecursiveModelRelation(this.model, this.foreignFactory, this.dbProperties); + return new RecursiveModelRelation(this.model, this.foreignModelType, this.dbProperties); } public async populate(models: ModelQueryResult): Promise { diff --git a/test/Model.test.ts b/test/Model.test.ts index 7f8b486..9e8e6f0 100644 --- a/test/Model.test.ts +++ b/test/Model.test.ts @@ -22,7 +22,7 @@ class Post extends Model { public author_id?: number = undefined; public content?: string = undefined; - public readonly author: OneModelRelation = new OneModelRelation(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 = new ManyThroughModelRelation(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 = new ManyThroughModelRelation(this, ModelFactory.get(Permission), { + public readonly permissions = new ManyThroughModelRelation(this, Permission, { localKey: 'id', foreignKey: 'id', pivotTable: 'role_permission',