From 673fbc84f8080add287cc32debbd6b77c386012b Mon Sep 17 00:00:00 2001 From: garrettmills Date: Sun, 16 Aug 2020 12:21:33 -0500 Subject: [PATCH] Add support for eager-loading nested relations --- orm/src/model/ModelResultOperator.ts | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/orm/src/model/ModelResultOperator.ts b/orm/src/model/ModelResultOperator.ts index f3abb23..bf8d23f 100644 --- a/orm/src/model/ModelResultOperator.ts +++ b/orm/src/model/ModelResultOperator.ts @@ -29,9 +29,19 @@ export default class ModelResultOperator> 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)