Skip to content</> asasvirtuais
Docs menuServer code
Docs/Core concepts

Server code

Reading or writing one table goes through the interface. Everything else is a server action.

KindWhat it doesWhere it livesIts authorization
Interface operationfind, list, create, update or remove on one tableapp/actions.ts → the table's middleware → app/db.tsthe table's middleware.ts
Server actioneverything else: an external API call, an LLM call, any computationactions.tsx of the component that calls itinside the action
Complex operationa mutation of two or more tables in one operationa server action of its owninside the action

An interface operation is one table#

An interface operation reads or writes the table it was called for, and nothing else. The table's middleware decides whether the caller may run it, and that is all it does: it doesn't write to other tables, call external APIs, or run business logic. A create answers with the row it created. That answer is what the client acts on next.

Server actions#

Server actions are for executing external APIs and calls, LLM calls, and any other computational operation. Each one does one thing and lives in the actions.tsx of the component that calls it, wrapped with action(...).

Server actions call app/db.ts without going through the middleware, because the middleware is for interface operations. So each one receives its own authorization logic. When that repeats, the repeated function goes into lib/.

When an action's result should be saved, the action returns it, and a form saves it through the interface.

Complex operations#

When there is a clearly defined need to mutate two tables in one operation, write a server action specific for it. Never put that logic into the table middleware, which is defined for the operations of one table.

These are not complex operations:

  • Steps the user takes. Each is its own form, and the result of one opens the next.
  • Something that follows a write. A request that creates a row answers with the row created. Most of the time the client starts what comes next after it has that response.
  • Keeping a status of a relation up to date. See below.

Derived state is read, not written#

A status that only notes the state of a table's relation with another table (whether there are related rows) is not a column, and no update writes it. The relational database already holds it: read the related rows, and show what they say in the front-end.