From b2632e2945fe20caecf46f16392142d5d88b7143 Mon Sep 17 00:00:00 2001 From: Tobe O Date: Sat, 17 Aug 2019 18:45:12 -0400 Subject: [PATCH] always generate a field for subquery joins --- .../lib/src/orm_generator.dart | 38 +++++++++++++------ 1 file changed, 26 insertions(+), 12 deletions(-) diff --git a/angel_orm_generator/lib/src/orm_generator.dart b/angel_orm_generator/lib/src/orm_generator.dart index 2eeb2441..05daa59b 100644 --- a/angel_orm_generator/lib/src/orm_generator.dart +++ b/angel_orm_generator/lib/src/orm_generator.dart @@ -293,18 +293,32 @@ class OrmGenerator extends GeneratorForAnnotation { .map(literalString) .toList(); - // Instead of passing the table as-is, we'll compile a subquery. - if (relation.type == RelationshipType.hasMany) { - var foreignQueryType = - foreign.buildContext.modelClassNameRecase.pascalCase + - 'Query'; - joinArgs.insert( - 0, - refer(foreignQueryType).newInstance( - [], {'trampoline': refer('trampoline')})); - } else { - joinArgs.insert(0, literalString(foreign.tableName)); - } + // In the past, we would either do a join on the table name + // itself, or create an instance of a query. + // + // From this point on, however, we will create a field for each + // join, so that users can customize the generated query. + // + // There'll be a private `_field`, and then a getter, named `field`, + // that returns the subqueryb object. + var foreignQueryType = refer( + foreign.buildContext.modelClassNameRecase.pascalCase + + 'Query'); + clazz + ..fields.add(Field((b) => b + ..name = '_$fieldName' + ..type = foreignQueryType)) + ..methods.add(Method((b) => b + ..name = fieldName + ..type = MethodType.getter + ..returns = foreignQueryType + ..body = refer('_$fieldName').returned.statement)); + + // Assign a value to `_field`. + var queryInstantiation = foreignQueryType + .newInstance([], {'trampoline': refer('trampoline')}); + joinArgs.insert( + 0, queryInstantiation.assign(refer('_$fieldName'))); b.addExpression(refer('leftJoin').call(joinArgs, { 'additionalFields':