always generate a field for subquery joins

This commit is contained in:
Tobe O 2019-08-17 18:45:12 -04:00
parent eabd89aff5
commit b2632e2945

View file

@ -293,18 +293,32 @@ class OrmGenerator extends GeneratorForAnnotation<Orm> {
.map(literalString) .map(literalString)
.toList(); .toList();
// Instead of passing the table as-is, we'll compile a subquery. // In the past, we would either do a join on the table name
if (relation.type == RelationshipType.hasMany) { // itself, or create an instance of a query.
var foreignQueryType = //
foreign.buildContext.modelClassNameRecase.pascalCase + // From this point on, however, we will create a field for each
'Query'; // join, so that users can customize the generated query.
joinArgs.insert( //
0, // There'll be a private `_field`, and then a getter, named `field`,
refer(foreignQueryType).newInstance( // that returns the subqueryb object.
[], {'trampoline': refer('trampoline')})); var foreignQueryType = refer(
} else { foreign.buildContext.modelClassNameRecase.pascalCase +
joinArgs.insert(0, literalString(foreign.tableName)); '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, { b.addExpression(refer('leftJoin').call(joinArgs, {
'additionalFields': 'additionalFields':