Skip to main content

generating

The psy cli provides three commands for generating Dream models and their accompanying migrations and serializers:

yarn psy g:model  --help
yarn psy g:sti-child --help
yarn psy g:resource --help

The resource generator is used when a model will be displayed and/or manipulated via an API endpoint. The model generator is used to generate a model without an API endpoint. And, as the name indicates, the STI child generator is used to generate STI children.

Resource generator commands can be found in the resource generator documentation, and the sti-child has its own, dedicated documentation, but here are some examples of the model and sti-child generator:

yarn psy g:model --no-serializer User email:citext first_name:citext last_name:citext

yarn psy g:model Guest User:belongs_to

yarn 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

# use the --help flag to inspect all of the options

enum types

As demonstrated above, you can use the enum type as a column type when specifying your column, provided you give a little bit more information about the enum you are designing. Looking closer at the type field generated for the Room model in the above example, we see some extra information being provided:

type:enum:room_types:Bathroom,Bedroom,Kitchen,Den,LivingRoom

the room_types segment signals to Dream to create a new enum called room_types_enum within your db. The segment following the room_types segment informs Dream of what values you want to allow for your enum column. In this case, it will produce the following values for your enum:

[
'Bathroom',
'Bedroom',
'Kitchen',
'Den',
'LivingRoom'
]

enum arrays

In some cases, you may need an array of enum values for your column. In this case, simply provide enum[] as the column type segment, instead of enum, like so:

type:enum[]:room_types:Bathroom,Bedroom,Kitchen,Den,LivingRoom

Doing this will produce the same enum type in postgres, but the column definition will expect an array of enum values instead of a single value. Dream will also set the default value for an enum[] column to be a blank array.

snake_casing column, table, and enum names

Since sql is a case-insensitive language, it is encouraged that you write your columns and tables using snake case. Dream leverages a Kysely setting to automatically convert snake case to camel case so that your Typescript will use the conventional camel case while the database retains the database convention of snake case.