2020-06-27 14:36:50 +02:00
|
|
|
import ModelQuery, {ModelQueryResult, WhereTest} from "./ModelQuery";
|
2020-07-24 12:13:28 +02:00
|
|
|
import Model from "./Model";
|
|
|
|
import ModelFactory from "./ModelFactory";
|
2020-06-27 14:36:50 +02:00
|
|
|
|
|
|
|
export default abstract class ModelRelation<S extends Model, O extends Model, R extends O | O[] | null> {
|
|
|
|
protected readonly model: S;
|
2020-07-24 12:13:28 +02:00
|
|
|
protected readonly foreignFactory: ModelFactory<O>;
|
2020-08-26 14:41:40 +02:00
|
|
|
private readonly query: ModelQuery<O>;
|
|
|
|
protected readonly queryModifiers: QueryModifier<O>[] = [];
|
2020-07-27 10:56:10 +02:00
|
|
|
protected readonly filters: ModelFilter<O>[] = [];
|
2020-06-27 14:36:50 +02:00
|
|
|
protected cachedModels?: R;
|
|
|
|
|
2020-07-24 12:13:28 +02:00
|
|
|
protected constructor(model: S, foreignFactory: ModelFactory<O>) {
|
2020-06-27 14:36:50 +02:00
|
|
|
this.model = model;
|
2020-07-24 12:13:28 +02:00
|
|
|
this.foreignFactory = foreignFactory;
|
|
|
|
this.query = this.foreignFactory.select();
|
2020-06-27 14:36:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public abstract clone(): ModelRelation<S, O, R>;
|
|
|
|
|
|
|
|
public constraint(queryModifier: QueryModifier<O>): this {
|
2020-08-26 14:41:40 +02:00
|
|
|
this.queryModifiers.push(queryModifier);
|
2020-06-27 14:36:50 +02:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2020-07-27 10:56:10 +02:00
|
|
|
public filter(modelFilter: ModelFilter<O>): this {
|
|
|
|
this.filters.push(modelFilter);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2020-08-26 14:41:40 +02:00
|
|
|
protected getFinalQuery(): ModelQuery<O> {
|
|
|
|
for (const modifier of this.queryModifiers) modifier(this.query);
|
|
|
|
this.queryModifiers.splice(0, this.queryModifiers.length); // Empty modifier now that they were applied
|
|
|
|
return this.query;
|
|
|
|
}
|
|
|
|
|
2020-06-27 14:36:50 +02:00
|
|
|
public abstract getModelID(): any;
|
|
|
|
|
|
|
|
protected abstract async compute(query: ModelQuery<O>): Promise<R>;
|
|
|
|
|
|
|
|
public async get(): Promise<R> {
|
|
|
|
if (this.cachedModels === undefined) {
|
2020-08-26 14:41:40 +02:00
|
|
|
this.cachedModels = await this.compute(this.getFinalQuery());
|
2020-06-27 14:36:50 +02:00
|
|
|
}
|
|
|
|
return this.cachedModels;
|
|
|
|
}
|
|
|
|
|
2020-06-27 17:11:31 +02:00
|
|
|
public getOrFail(): R {
|
2020-07-28 15:02:40 +02:00
|
|
|
if (this.cachedModels === undefined) throw new Error('Models were not fetched');
|
2020-06-27 17:11:31 +02:00
|
|
|
return this.cachedModels;
|
|
|
|
}
|
|
|
|
|
2020-06-27 14:36:50 +02:00
|
|
|
public abstract async eagerLoad(relations: ModelRelation<S, O, R>[]): Promise<ModelQueryResult<O>>;
|
|
|
|
|
|
|
|
public abstract async populate(models: ModelQueryResult<O>): Promise<void>;
|
|
|
|
}
|
|
|
|
|
|
|
|
export class OneModelRelation<S extends Model, O extends Model> extends ModelRelation<S, O, O | null> {
|
|
|
|
protected readonly dbProperties: RelationDatabaseProperties;
|
|
|
|
|
2020-07-24 12:13:28 +02:00
|
|
|
constructor(model: S, foreignFactory: ModelFactory<O>, dbProperties: RelationDatabaseProperties) {
|
|
|
|
super(model, foreignFactory);
|
2020-06-27 14:36:50 +02:00
|
|
|
this.dbProperties = dbProperties;
|
|
|
|
}
|
|
|
|
|
|
|
|
public clone(): OneModelRelation<S, O> {
|
2020-07-24 12:13:28 +02:00
|
|
|
return new OneModelRelation(this.model, this.foreignFactory, this.dbProperties);
|
2020-06-27 14:36:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public getModelID() {
|
|
|
|
return this.model[this.dbProperties.localKey];
|
|
|
|
}
|
|
|
|
|
|
|
|
protected async compute(query: ModelQuery<O>): Promise<O | null> {
|
2020-08-26 14:41:40 +02:00
|
|
|
this.getFinalQuery().where(this.dbProperties.foreignKey, this.getModelID());
|
2020-06-27 14:36:50 +02:00
|
|
|
return await query.first();
|
|
|
|
}
|
|
|
|
|
2020-07-27 10:56:10 +02:00
|
|
|
public async get(): Promise<O | null> {
|
|
|
|
const model = await super.get();
|
|
|
|
if (model) {
|
|
|
|
for (const filter of this.filters) {
|
|
|
|
if (!(await filter(model))) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return model;
|
|
|
|
}
|
|
|
|
|
2020-06-27 14:36:50 +02:00
|
|
|
public async eagerLoad(relations: ModelRelation<S, O, O | null>[]): Promise<ModelQueryResult<O>> {
|
2020-07-29 16:17:30 +02:00
|
|
|
const ids = relations.map(r => r.getModelID()).filter(id => id !== null && id !== undefined);
|
|
|
|
if (ids.length === 0) return [];
|
|
|
|
|
2020-08-26 14:41:40 +02:00
|
|
|
const query = this.getFinalQuery();
|
|
|
|
query.where(this.dbProperties.foreignKey, ids, WhereTest.IN);
|
|
|
|
return await query.get();
|
2020-06-27 14:36:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public async populate(models: ModelQueryResult<O>): Promise<void> {
|
|
|
|
this.cachedModels = models.filter(m => m[this.dbProperties.foreignKey] === this.getModelID())[0] || null;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
export class ManyModelRelation<S extends Model, O extends Model> extends ModelRelation<S, O, O[]> {
|
|
|
|
protected readonly dbProperties: RelationDatabaseProperties;
|
|
|
|
|
2020-07-24 12:13:28 +02:00
|
|
|
constructor(model: S, foreignFactory: ModelFactory<O>, dbProperties: RelationDatabaseProperties) {
|
|
|
|
super(model, foreignFactory);
|
2020-06-27 14:36:50 +02:00
|
|
|
this.dbProperties = dbProperties;
|
|
|
|
}
|
|
|
|
|
|
|
|
public clone(): ManyModelRelation<S, O> {
|
2020-07-24 12:13:28 +02:00
|
|
|
return new ManyModelRelation<S, O>(this.model, this.foreignFactory, this.dbProperties);
|
2020-06-27 14:36:50 +02:00
|
|
|
}
|
|
|
|
|
2020-06-27 14:58:39 +02:00
|
|
|
public cloneReduceToOne(): OneModelRelation<S, O> {
|
2020-07-24 12:13:28 +02:00
|
|
|
return new OneModelRelation<S, O>(this.model, this.foreignFactory, this.dbProperties);
|
2020-06-27 14:58:39 +02:00
|
|
|
}
|
|
|
|
|
2020-06-27 14:36:50 +02:00
|
|
|
public getModelID(): any {
|
|
|
|
return this.model[this.dbProperties.localKey];
|
|
|
|
}
|
|
|
|
|
|
|
|
protected async compute(query: ModelQuery<O>): Promise<O[]> {
|
2020-08-26 14:41:40 +02:00
|
|
|
this.getFinalQuery().where(this.dbProperties.foreignKey, this.getModelID());
|
2020-06-27 14:36:50 +02:00
|
|
|
return await query.get();
|
|
|
|
}
|
|
|
|
|
2020-07-27 10:56:10 +02:00
|
|
|
public async get(): Promise<O[]> {
|
|
|
|
let models = await super.get();
|
|
|
|
for (const filter of this.filters) {
|
|
|
|
const newModels = [];
|
|
|
|
for (const model of models) {
|
|
|
|
if (await filter(model)) {
|
|
|
|
newModels.push(model);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
models = newModels;
|
|
|
|
}
|
|
|
|
return models;
|
|
|
|
}
|
|
|
|
|
2020-06-27 14:36:50 +02:00
|
|
|
public async eagerLoad(relations: ModelRelation<S, O, O[]>[]): Promise<ModelQueryResult<O>> {
|
2020-07-29 16:17:30 +02:00
|
|
|
const ids = relations.map(r => r.getModelID()).filter(id => id !== null && id !== undefined);
|
|
|
|
if (ids.length === 0) return [];
|
|
|
|
|
2020-08-26 14:41:40 +02:00
|
|
|
const query = this.getFinalQuery();
|
|
|
|
query.where(this.dbProperties.foreignKey, ids, WhereTest.IN);
|
|
|
|
return await query.get();
|
2020-06-27 14:36:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public async populate(models: ModelQueryResult<O>): Promise<void> {
|
2020-07-27 10:57:19 +02:00
|
|
|
this.cachedModels = models.filter(m => m[this.dbProperties.foreignKey] === this.getModelID());
|
2020-06-27 14:36:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
export class ManyThroughModelRelation<S extends Model, O extends Model> extends ModelRelation<S, O, O[]> {
|
|
|
|
protected readonly dbProperties: PivotRelationDatabaseProperties;
|
|
|
|
|
2020-07-24 12:13:28 +02:00
|
|
|
constructor(model: S, foreignFactory: ModelFactory<O>, dbProperties: PivotRelationDatabaseProperties) {
|
|
|
|
super(model, foreignFactory);
|
2020-06-27 14:36:50 +02:00
|
|
|
this.dbProperties = dbProperties;
|
2020-08-26 14:41:40 +02:00
|
|
|
this.constraint(query => query
|
2020-06-27 14:36:50 +02:00
|
|
|
.leftJoin(`${this.dbProperties.pivotTable} as pivot`)
|
2020-08-26 14:41:40 +02:00
|
|
|
.on(`pivot.${this.dbProperties.foreignPivotKey}`, `${this.foreignFactory.table}.${this.dbProperties.foreignKey}`)
|
|
|
|
);
|
2020-06-27 14:36:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public clone(): ManyThroughModelRelation<S, O> {
|
2020-07-24 12:13:28 +02:00
|
|
|
return new ManyThroughModelRelation<S, O>(this.model, this.foreignFactory, this.dbProperties);
|
2020-06-27 14:36:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public getModelID(): any {
|
|
|
|
return this.model[this.dbProperties.localKey];
|
|
|
|
}
|
|
|
|
|
|
|
|
protected async compute(query: ModelQuery<O>): Promise<O[]> {
|
2020-08-26 14:41:40 +02:00
|
|
|
this.getFinalQuery().where(`pivot.${this.dbProperties.localPivotKey}`, this.getModelID());
|
2020-06-27 14:36:50 +02:00
|
|
|
return await query.get();
|
|
|
|
}
|
|
|
|
|
2020-07-27 10:56:10 +02:00
|
|
|
public async get(): Promise<O[]> {
|
|
|
|
let models = await super.get();
|
|
|
|
for (const filter of this.filters) {
|
|
|
|
const newModels = [];
|
|
|
|
for (const model of models) {
|
|
|
|
if (await filter(model)) {
|
|
|
|
newModels.push(model);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
models = newModels;
|
|
|
|
}
|
|
|
|
return models;
|
|
|
|
}
|
|
|
|
|
2020-06-27 14:36:50 +02:00
|
|
|
public async eagerLoad(relations: ModelRelation<S, O, O[]>[]): Promise<ModelQueryResult<O>> {
|
2020-07-29 16:17:30 +02:00
|
|
|
const ids = relations.map(r => r.getModelID());
|
|
|
|
if (ids.length === 0) return [];
|
|
|
|
|
2020-08-26 14:41:40 +02:00
|
|
|
const query = this.getFinalQuery();
|
|
|
|
query.where(`pivot.${this.dbProperties.localPivotKey}`, ids, WhereTest.IN);
|
|
|
|
query.pivot(`pivot.${this.dbProperties.localPivotKey}`, `pivot.${this.dbProperties.foreignPivotKey}`);
|
|
|
|
return await query.get();
|
2020-06-27 14:36:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public async populate(models: ModelQueryResult<O>): Promise<void> {
|
|
|
|
const ids = models.pivot!
|
2020-08-30 18:56:27 +02:00
|
|
|
.filter(p => p[`pivot.${this.dbProperties.localPivotKey}`] === this.getModelID())
|
|
|
|
.map(p => p[`pivot.${this.dbProperties.foreignPivotKey}`]);
|
2020-06-27 14:36:50 +02:00
|
|
|
this.cachedModels = models.filter(m => ids.indexOf(m[this.dbProperties.foreignKey]) >= 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-07-27 10:52:39 +02:00
|
|
|
|
|
|
|
export type QueryModifier<M extends Model> = (query: ModelQuery<M>) => ModelQuery<M>;
|
|
|
|
|
2020-07-27 10:56:10 +02:00
|
|
|
export type ModelFilter<O extends Model> = (model: O) => boolean | Promise<boolean>;
|
|
|
|
|
2020-06-27 14:36:50 +02:00
|
|
|
export type RelationDatabaseProperties = {
|
|
|
|
localKey: string;
|
|
|
|
foreignKey: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
export type PivotRelationDatabaseProperties = RelationDatabaseProperties & {
|
|
|
|
pivotTable: string;
|
|
|
|
localPivotKey: string;
|
|
|
|
foreignPivotKey: string;
|
|
|
|
};
|