Skip to content

"Interface" model support. #2842

Open

Description

@SandPod

Is your feature request related to a problem? Please describe.
As an alternative to inheritance, there might be fields that I would like to share between different unrelated models.

A use case for this could be audit fields that standardizes the format of when a record was created or updated.

By supporting interfaces, we would enable multiple definitions of standardized fields that could be added to a model.

Describe the solution you'd like
Add a new model type called interface that can be implemented by other models.

The definition for an Interface could look like this:

interface: AuditLog
fields:
  createdAt: Datetime, default=now
  updatedAt: Datetime, default=now

And then be used in Class model like so:

class: MyUser
implements: AuditLog
fields:
    name: String
    age: Int

The fields createdAt and updatedAt are then accessible for the user.

Additional context

  • It should be possible to implement multiple interfaces.
  • SerializableExceptions should also have support for implementing interfaces.

Activity

  1. added
    enhancement
    area: models
    on Oct 10, 2024
  2. moved this to Later 🥳 in Serverpod Frameworkson Oct 10, 2024
  3. BenAuerDev commented on Jan 24, 2025

    @BenAuerDev
    Contributor

    I'll look into this :) 🚀

  4. vlidholt commented on Jan 24, 2025

    @vlidholt
    Collaborator

    @BenAuerDev, this is awesome! 🥳

  5. dkbast commented on May 21, 2025

    @dkbast
    Contributor

    It would be great if this would also allow the user to implement a Dart class interface - this would allow the user to use the objects which are passed over the wire in more scenarios without the need to map them into an object which would ducktype to the same fields.

  6. borutjures commented on Aug 26, 2025

    @borutjures
    Contributor

    It would be nice to be able to determine where the interface fields are inserted in the database table.

    interface: AddressInterface
    fields:
      address: String
      city: String
    
    interface: AuditLog
    fields:
      createdAt: Datetime, default=now
      updatedAt: Datetime, default=now
    
    class: MyUser
    implements: PositionedInterface, AuditLog
    fields:
      name: String
      _addressInterface: AddressInterface
      age: Int
      _auditLog: AuditLog
    

    p.s.
    The current PR already adds a great feature even without allowing to specify the exact position of the interface fields.


    Another nice feature that extends on the above is to allow the use of the same interface more than once in the same table. This would require specifying a fields name prefix for each use.

    class: MyUser
    implements: AddressInterface
    fields:
      name: String
      age: Int
      _homeAddressInterface: AddressInterface
      workAddressIsSameAsHome: Boolean
      _workAddressInterface: AddressInterface
    

    In the above example the fields prefix would be determined by removing the interface name (_homeAddressInterface).

  7. moved this from In Progress 🔥 to Contributor In Progress 👩‍💻 in Serverpod Frameworkson Sep 30, 2025
  8. moved this from Contributor In Progress 👩‍💻 to Parked Pull-Requests in Serverpod Frameworkson Feb 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area: modelsenhancement

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions