morphOne<Parent extends RelationalModel<Parent>, Child extends RelationalModel<Child>> function

Relation<Parent, Child> morphOne<Parent extends RelationalModel<Parent>, Child extends RelationalModel<Child>>(
  1. RelationalModel<Child> child,
  2. String foreignKey,
  3. String typeColumn,
  4. String typeValue, [
  5. String? localKey,
  6. 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
  );
}