morphOne<Parent extends RelationalModel<Parent> , Child extends RelationalModel<Child> > function
Relation<Parent, Child>
morphOne<Parent extends RelationalModel<Parent> , Child extends RelationalModel<Child> >(
- RelationalModel<
Child> child, - String foreignKey,
- String typeColumn,
- String typeValue, [
- String? localKey,
- RelationScope? scope,
Implementation
Relation<Parent, Child> morphOne<Parent extends RelationalModel<Parent>,Child extends RelationalModel<Child>>(
RelationalModel<Child> child,
String foreignKey, // e.g. "subject_uuid"
String typeColumn, // e.g. "subject_type"
String typeValue, // e.g. "publisher"
[
String? localKey,
RelationScope? scope
]
) {
// auto scope for the morph
final RelationScope autoScope;
autoScope = (q) => q.where(typeColumn, typeValue);
// Merge autoScope + userScope
RelationScope? finalScope;
if (scope != null) {
finalScope = (q) => scope(autoScope(q)); // autoScope FIRST → then user scope
} else {
finalScope = autoScope;
}
return hasOne<Parent, Child>(
child,
foreignKey,
localKey,
finalScope, // automatically applied
);
}