STI Child Model Generator
The STI (Single Table Inheritance) child generator creates new Dream models that extend another Dream model, enabling you to leverage single table inheritance patterns. This generator creates complete models, just like the regular model generator, but with inheritance relationships.
Prerequisites: Creating the STI Parent
Before generating STI children, you must first create the parent model, and it is recommended to use the --sti-base-serializer flag when doing so:
pnpm psy g:resource --sti-base-serializer --owning-model=Place v1/host/places/\{\}/rooms Room type:enum:room_types:Bathroom,Bedroom,Kitchen,Den,LivingRoom Place:belongs_to position:integer:optional deleted_at:datetime:optional
Why --sti-base-serializer? This flag creates the base serializer in a format that child serializers can properly extend, ensuring the inheritance hierarchy works correctly.
Usage
You can also create an STI parent using the model generator:
pnpm psy g:model --sti-base-serializer Room type:enum:room_types:Bathroom,Bedroom,Kitchen,Den,LivingRoom Place:belongs_to position:integer:optional deleted_at:datetime:optional
pnpm psy g:sti-child <childModelName> extends <parentModelName> [columnsWithTypes...]
Arguments
-
<childModelName>: The name of the model to create, e.g.PostorSettings/CommunicationPreferences. -
extends: Just the word "extends". -
<parentModelName>: Fully qualified name of the parent model, e.g.:- To extend the Room model in
src/app/models/Room:Room - To extend the Coach model in
src/app/models/Health/Coach:Health/Coach
- To extend the Room model in
-
[columnsWithTypes...]: Space separated snake-case (except for belongs_to model name) properties like this:title:citext subtitle:string body_markdown:text style:enum:post_styles:formal,informal User:belongs_toAll properties default to not nullable; null can be allowed by appending
:optional:subtitle:string:optionalSupported types
citext,citext[]: case insensitive text (indexes and queries are automatically case insensitive)string,string[]: varchar; allowed length defaults to 255, but may be customized, e.g.:subtitle:string:128orsubtitle:string:128:optionaltext,text[]date,date[]datetime,datetime[]integer,integer[]decimal,decimal[]: scale,precision is required, e.g.:volume:decimal:3,2orvolume:decimal:3,2:optional- Leveraging arrays, add the "[]" suffix, e.g.:
volume:decimal[]:3,2
- Leveraging arrays, add the "[]" suffix, e.g.:
enum,enum[]: include the enum name to automatically create the enum:type:enum:room_types:bathroom,kitchen,bedroomortype:enum:room_types:bathroom,kitchen,bedroom:optional- Omit the enum values to leverage an existing enum (omits the enum type creation):
type:enum:room_typesortype:enum:room_types:optional - Leveraging arrays, add the "[]" suffix, e.g.:
type:enum[]:room_types:bathroom,kitchen,bedroom
- Omit the enum values to leverage an existing enum (omits the enum type creation):
belongs_to: not only adds a foreign key to the migration, but also adds a BelongsTo association to the generated model:- Include the fully qualified model name, e.g., if the Coach model is in
src/app/models/Health/Coach:Health/Coach:belongs_to
- Include the fully qualified model name, e.g., if the Coach model is in
Options
--no-serializer: Skip generating a serializer for the child model.--connection-name <connectionName>: The db connection you want this model attached to (defaults to the default connection).-h, --help: Display help for command.
STI Example
This example creates an STI model for various rooms.
STI Base
Create the base Room model using the resource generator with the --sti-base-serializer flag:
pnpm psy g:resource --sti-base-serializer --owning-model=Place v1/host/places/\{\}/rooms Room type:enum:room_types:Bathroom,Bedroom,Kitchen,Den,LivingRoom Place:belongs_to position:integer:optional deleted_at:datetime:optional
STI Child
Then create an STI child:
pnpm psy g:sti-child Room/Kitchen extends Room appliances:enum[]:appliance_types:stove,oven,microwave,dishwasher
Supported Column Types
STI child models support the same column types as the model generator.