Skip to content

Enhancement: Explore Polymorphism support for services and repositories #656

@cofin

Description

@cofin

Would it be possible to add Polymorphism support for services and repositories? Currently, I've only been able to make Polymorphism work by creating a service for each subclass. Ideally it would work something like this:

AllAlerts = with_polymorphic(Alert, (UnexpectedAlert, ExpectedAlert))

class AlertService(SQLAlchemyAsyncRepositoryService[AllAlerts]):

    class Repo(SQLAlchemyAsyncRepository[AllAlerts]):
        model_type = AllAlerts

    repository_type = Repo

AllAlerts is an AliasedClass that contains information about the Polymorphic relation. Just like SQLAlchemy, Advanced Alchemy typing would hint that it's methods return the abstract class.

alert: Alert = await alert_service.create(data)

But in practice, it really returns the full Polymorphic identity. I'm not sure how much additional work would be required, but this would be a great boon for services that provide many polymorphic identities.

Originally posted by @njz-cvm in #611 (comment)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions