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

Relation<Parent, Child> morphMany<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> morphMany<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,      // e.g. 'uuid'
    RelationScope? scope,  // optional extra filtering
  ]
) {
  // Base polymorphic match
  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 hasMany<Parent, Child>(
    child,
    foreignKey,
    localKey,
    finalScope,
  );
}