Generating a Product resource

The command, the prompts, what each flag controls.

7 mineasy

Let's generate a Product resource. By the end of this lesson you will have eight new files on disk, wired into routes, with a working admin page.

The command

Terminal
$grit generate resource Product \
$ --field "name:string:required" \
$ --field "price:decimal:required" \
$ --field "description:text" \
$ --field "stockQuantity:int:default=0"

Run that from the project root. You'll see:

āœ“ Wrote apps/api/internal/models/product.go
āœ“ Wrote apps/api/internal/services/product.go
āœ“ Wrote apps/api/internal/handlers/product.go
āœ“ Injected routes into apps/api/internal/routes/routes.go
āœ“ Wrote packages/shared/src/schemas/product.ts
āœ“ Wrote packages/shared/src/types/product.ts
āœ“ Wrote apps/web/hooks/use-products.ts
āœ“ Wrote apps/admin/app/resources/products/page.tsx
Next steps:
grit migrate # adds the products table
grit start # restart dev servers to pick up the new code

Field types

The generator understands a small but solid set:

Field typeGoTypeScriptAdmin input
stringstringstringtext input
textstringstringtextarea
intintnumbernumber input
decimaldecimal.Decimalstringmoney input
boolboolbooleanswitch
datetime.Timestringdate picker
uuiduuid.UUIDstringFK select
jsondatatypes.JSONunknownJSON editor

Field modifiers

  • required — NOT NULL + Zod .required()
  • unique — DB unique index + Zod refine
  • default=value — column default + Zod default
  • belongs_to=Customer — foreign key + relation
You can also run grit generate resource Product without flags. It prompts you for fields one at a time — useful when you're not sure what to call them yet.

What happened to the database?

Nothing yet. The generator writes the Go struct (the GORM model) but doesn't create the table. Run grit migrate after every generate — it AutoMigrate's the new model, creating the table + columns + indexes.

Quick check

You generated a Product resource but `GET /api/products` returns 500. What did you forget?

Try it

Generate the Product resource on your machine:

Terminal
$grit generate resource Product \
$ --field "name:string:required" \
$ --field "price:decimal:required" \
$ --field "stockQuantity:int:default=0"
$grit migrate
# restart dev servers if they don't hot reload

Then hit GET /api/products and paste the response (the empty paginated list) in notes.md.

What's next

Eight files showed up. Next lesson is the tour — open every one, understand what each does, see how they connect.

Spot a typo? Have an idea?

Help us improve this lesson. One click opens a GitHub issue with the lesson URL pre-filled — suggest clearer wording, report a bug, or request more depth. The course keeps improving thanks to learners like you.

Suggest an improvement on GitHub