ModelRelation: deduplicate eagerloaded models
This commit is contained in:
parent
0e37014667
commit
b88f4e1b64
@ -70,7 +70,9 @@ export default abstract class ModelRelation<S extends Model, O extends Model, R
|
|||||||
protected abstract collectionToOutput(models: O[]): R;
|
protected abstract collectionToOutput(models: O[]): R;
|
||||||
|
|
||||||
public async eagerLoad(relations: ModelRelation<S, O, R>[], subRelations: string[] = []): Promise<ModelQueryResult<O>> {
|
public async eagerLoad(relations: ModelRelation<S, O, R>[], subRelations: string[] = []): Promise<ModelQueryResult<O>> {
|
||||||
const ids = relations.map(r => r.getModelID()).filter(id => id !== null && id !== undefined);
|
const ids = relations.map(r => r.getModelID())
|
||||||
|
.filter(id => id !== null && id !== undefined)
|
||||||
|
.reduce((array: O[], val) => val in array ? array : [...array, val], []);
|
||||||
if (ids.length === 0) return [];
|
if (ids.length === 0) return [];
|
||||||
|
|
||||||
const query = this.makeQuery();
|
const query = this.makeQuery();
|
||||||
@ -80,7 +82,8 @@ export default abstract class ModelRelation<S extends Model, O extends Model, R
|
|||||||
}
|
}
|
||||||
|
|
||||||
public async populate(models: ModelQueryResult<O>): Promise<void> {
|
public async populate(models: ModelQueryResult<O>): Promise<void> {
|
||||||
this.cachedModels = models.filter(m => m[this.dbProperties.foreignKey] === this.getModelID());
|
this.cachedModels = models.filter(m => m[this.dbProperties.foreignKey] === this.getModelID())
|
||||||
|
.reduce((array: O[], val) => array.find(v => v.equals(val)) ? array : [...array, val], []);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async count(): Promise<number> {
|
public async count(): Promise<number> {
|
||||||
@ -177,7 +180,8 @@ export class ManyThroughModelRelation<S extends Model, O extends Model> extends
|
|||||||
}
|
}
|
||||||
|
|
||||||
public async eagerLoad(relations: ModelRelation<S, O, O[]>[], subRelations: string[] = []): Promise<ModelQueryResult<O>> {
|
public async eagerLoad(relations: ModelRelation<S, O, O[]>[], subRelations: string[] = []): Promise<ModelQueryResult<O>> {
|
||||||
const ids = relations.map(r => r.getModelID());
|
const ids = relations.map(r => r.getModelID())
|
||||||
|
.reduce((array: O[], val) => val in array ? array : [...array, val], []);
|
||||||
if (ids.length === 0) return [];
|
if (ids.length === 0) return [];
|
||||||
|
|
||||||
const query = this.makeQuery();
|
const query = this.makeQuery();
|
||||||
@ -191,7 +195,8 @@ export class ManyThroughModelRelation<S extends Model, O extends Model> extends
|
|||||||
const ids = models.pivot!
|
const ids = models.pivot!
|
||||||
.filter(p => p[`pivot.${this.dbProperties.localPivotKey}`] === this.getModelID())
|
.filter(p => p[`pivot.${this.dbProperties.localPivotKey}`] === this.getModelID())
|
||||||
.map(p => p[`pivot.${this.dbProperties.foreignPivotKey}`]);
|
.map(p => p[`pivot.${this.dbProperties.foreignPivotKey}`]);
|
||||||
this.cachedModels = models.filter(m => ids.indexOf(m[this.dbProperties.foreignKey]) >= 0);
|
this.cachedModels = models.filter(m => ids.indexOf(m[this.dbProperties.foreignKey]) >= 0)
|
||||||
|
.reduce((array: O[], val) => array.find(v => v.equals(val)) ? array : [...array, val], []);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user