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'; 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( var relation = RelationshipReader(
type, type,
localKey: localKey, localKey: localKey,
@ -245,6 +255,7 @@ Future<OrmBuildContext> buildOrmContext(
through: through, through: through,
foreign: foreign, foreign: foreign,
throughContext: throughContext, throughContext: throughContext,
joinType: joinType ?? JoinType.left,
); );
// print('Relation on ${buildCtx.originalClassName}.${field.name} => ' // print('Relation on ${buildCtx.originalClassName}.${field.name} => '

View file

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

View file

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