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 = //
// 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 + foreign.buildContext.modelClassNameRecase.pascalCase +
'Query'; '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( joinArgs.insert(
0, 0, queryInstantiation.assign(refer('_$fieldName')));
refer(foreignQueryType).newInstance(
[], {'trampoline': refer('trampoline')}));
} else {
joinArgs.insert(0, literalString(foreign.tableName));
}
b.addExpression(refer('leftJoin').call(joinArgs, { b.addExpression(refer('leftJoin').call(joinArgs, {
'additionalFields': 'additionalFields':