Add support for eager-loading nested relations
This commit is contained in:
parent
dc6ba53380
commit
673fbc84f8
@ -29,9 +29,19 @@ export default class ModelResultOperator<T extends Model<T>> extends ResultOpera
|
|||||||
const eagers = query.eager_relations
|
const eagers = query.eager_relations
|
||||||
const model = new this.ModelClass()
|
const model = new this.ModelClass()
|
||||||
|
|
||||||
for ( const rel_name of eagers ) {
|
for ( const full_rel_name of eagers ) {
|
||||||
|
const rel_name = full_rel_name.split('.')[0]
|
||||||
|
const child_eagers = eagers.filter((x: string) => x.startsWith(`${rel_name}.`)).map((x: string) => {
|
||||||
|
return x.split('.').slice(1).join('.')
|
||||||
|
})
|
||||||
|
|
||||||
const relation = model.get_relation(rel_name)
|
const relation = model.get_relation(rel_name)
|
||||||
const select = await relation.build_eager_query(query, results)
|
const select = await relation.build_eager_query(query, results)
|
||||||
|
|
||||||
|
for ( const child_eager of child_eagers ) {
|
||||||
|
select.with(child_eager)
|
||||||
|
}
|
||||||
|
|
||||||
const all_related = await select.results()
|
const all_related = await select.results()
|
||||||
results.each(result => {
|
results.each(result => {
|
||||||
const result_relation = result.get_relation(rel_name)
|
const result_relation = result.get_relation(rel_name)
|
||||||
|
Loading…
Reference in New Issue
Block a user