Allow specifying join type

This commit is contained in:
Tobe O 2019-10-09 11:26:27 -04:00
parent fdac775e41
commit 69106dd0e7
3 changed files with 18 additions and 3 deletions

View file

@ -236,6 +236,16 @@ Future<OrmBuildContext> buildOrmContext(
localKey ??= '${rcc.snakeCase}_$foreignKey';
}
// Figure out the join type.
var joinType = JoinType.left;
var joinTypeRdr = cr.peek('joinType')?.objectValue;
if (joinTypeRdr != null) {
var idx = joinTypeRdr.getField('index')?.toIntValue();
if (idx != null) {
joinType = JoinType.values[idx];
}
}
var relation = RelationshipReader(
type,
localKey: localKey,
@ -245,6 +255,7 @@ Future<OrmBuildContext> buildOrmContext(
through: through,
foreign: foreign,
throughContext: throughContext,
joinType: joinType ?? JoinType.left,
);
// print('Relation on ${buildCtx.originalClassName}.${field.name} => '

View file

@ -34,6 +34,7 @@ class RelationshipReader {
final DartType through;
final OrmBuildContext foreign;
final OrmBuildContext throughContext;
final JoinType joinType;
const RelationshipReader(this.type,
{this.localKey,
@ -42,11 +43,14 @@ class RelationshipReader {
this.cascadeOnDelete,
this.through,
this.foreign,
this.throughContext});
this.throughContext,
this.joinType = JoinType.left});
bool get isManyToMany =>
type == RelationshipType.hasMany && throughContext != null;
String get joinTypeString => joinType.toString().replaceAll('JoinType.', '');
FieldElement findLocalField(OrmBuildContext ctx) {
return ctx.effectiveFields.firstWhere(
(f) => ctx.buildContext.resolveFieldName(f.name) == localKey,

View file

@ -9,10 +9,10 @@ part 'book.g.dart';
@serializable
@orm
class _Book extends Model {
@belongsTo
@BelongsTo(joinType: JoinType.inner)
_Author author;
@BelongsTo(localKey: "partner_author_id")
@BelongsTo(localKey: "partner_author_id", joinType: JoinType.inner)
_Author partnerAuthor;
String name;