generate:resource
Psychic considers a resource to be something that is both a database model and an entity that is accessible via an endpoint. Not all of the models you generate may need to have API endpoints for accessing them, so you will not need resource generators for these, and can instead lean on the model generators.
See the resource generator documentation for details. The CLI documentation follows:
Usage
pnpm psy g:resource [options] <path> <modelName> [columnsWithTypes...]
Arguments
-
<path>: URL path from root domain. Specify nesting resource with{}, e.g.tickets/{}/comments. -
<modelName>: The name of the model to create, e.g.PostorSettings/CommunicationPreferences. -
[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
uuid,uuid[]: a column optimized for storing UUIDscitext,citext[]: case insensitive text (indexes and queries are automatically case insensitive)encrypted: encrypted text (used in conjunction with the@deco.Encrypteddecorator)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[]time,time[]timetz,timetz[]integer,integer[]decimal,decimal[]: precision,scale 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
<path>, <modelName>, and --owning-model are independent. A nested route path such as v1/host/places/{}/rooms does not mean the model should be namespaced under Place; the model name should still describe what the record is. Nested {} paths must pass --owning-model so the generated controller can query and create through the parent association.
The generator includes id, created_at, updated_at, deleted_at, and @SoftDelete() by default. Use --no-soft-delete only when hard deletion is an intentional part of the model design.
Options
--singular: Generates a "resource" route instead of "resources", along with the necessary controller and spec changes.--only <onlyActions>: Comma separated list of resourceful endpoints (e.g.--only=create,show); any of:indexcreateshowupdatedelete
--sti-base-serializer: Generates an STI-aware base serializer that child serializers can extend. Use this when the generated model is an STI parent.--owning-model <modelName>: The model class of the object thatassociationQuery/createAssociationwill be performed on in the created controller and spec (e.g., "Host", "Guest", "Ticketing/Ticket"). Defaults to the current user for non-admin/internal namespaced controllers. For admin/internal namespaced controllers, this defaults to null, meaning every admin/internal user can access the model.--connection-name <connectionName>: The name of the db connection you would like to use for your model. Defaults to "default" (default: "default").--model-name <modelName>: Explicit model class name to use instead of the auto-generated one (e.g.--model-name=KitchenforRoom/Kitchen).-h, --help: Display help for command.