Polymorphic Models
🔋 ZenStack vs Prisma
Polymorphic models is a major feature that sets ZenStack apart from Prisma.
ZenStack natively supports polymorphic models. As we have seen in the Polymorphism section in the data modeling part, the ZModel language allows you to define models with Object-Oriented style inheritance. This section will describe the ORM runtime behavior of polymorphic models.
CRUD behavior​
Polymorphic models' CRUD behavior is similar to that of regular models, with two major differences:
- Base model entities cannot be created directly as they cannot exist without an associated concrete model entity.
- When querying a base model (either top-level or nested), the result will include all fields of the associated concrete model (unless fields are explicitly selected). The result's type is a discriminated union, so you can use TypeScript's type narrowing to access the concrete model's specific fields.
The ORM query API hides all the complexity of managing polymorphic models for you:
- When creating a concrete model entity, its base entity is automatically created.
- When querying a base entity, the ORM fetches the associated concrete entity and merges the results.
- When deleting a base or concrete entity, the ORM automatically deletes the counterpart entity.
Samples​
The schema used in the sample involves a base model and three concrete models:
zenstack/schema.zmodel
Loading...
- Interactive Sample
- Plain Code
Click here to pop out if the embed doesn't load an interactive terminal.
main.ts
Loading...