Add support for eager-loading nested relations

This commit is contained in:
Garrett Mills 2020-08-16 12:21:33 -05:00
parent dc6ba53380
commit 673fbc84f8
Signed by: garrettmills
GPG Key ID: D2BF5FBA8298F246

View File

@ -29,9 +29,19 @@ export default class ModelResultOperator<T extends Model<T>> extends ResultOpera
const eagers = query.eager_relations
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 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()
results.each(result => {
const result_relation = result.get_relation(rel_name)