Introduction
Write business logic once and run it from any caller.
Logic SDK holds the business logic of a TypeScript app: the layer between your routes and your database. You write each rule one time, as a function. Then a route, an AI agent, a job, a script or a test runs it the same way.
Caller
Route, agent, job, test
- Runs as a scope
mutation
One transaction
- Policy passed
- Input is valid
Listeners
Run after commit
- Email, sync, AI
/** Confirm a pending booking */
export const confirm = mutation({
input: z.object({ bookingId: z.uuid() }),
authorize: staff,
async handler(ctx, { bookingId }) {
const booking = await findBooking(ctx, bookingId);
if (booking.status !== "pending") throw ctx.error(notPending);
// another module, in the same transaction
await ctx.run(availability.hold, rangeOf(booking));
// listeners get it after commit
ctx.emit(confirmed, { bookingId });
return setStatus(ctx, booking.id, "confirmed");
},
});
// a route, an agent, a job or a test: bind who calls, then run
await registry.as(scope).run(bookings.confirm, { bookingId });
What you get
- One transaction across modules. Nested mutations commit together. A failure anywhere rolls back all of them.
- Side effects after commit. A mutation emits events. Listeners send the email only when the data is safe.
- A policy on every call.
authorizeruns for a route, an agent, a job and a nested call. - Typed errors. A caller switches on error codes, and TypeScript checks each code.
- Tests without mocks. Each test runs on a real database, in a transaction that rolls back.
- Any runtime. Node, Bun and Cloudflare Workers, with any framework. No decorators, classes or code generation.
The five kinds
| Kind | Use it for | Transaction |
|---|---|---|
query |
Read data | The caller’s, if any |
mutation |
Change data | One, shared by nested mutations |
action |
Payments, email, AI, any network call | None |
listener |
React to an event | None, runs after commit |
cron |
Work on a schedule | None |
Services (a mailer, a payment client) reach only actions, listeners and crons. A mutation can roll back, and an email cannot come back.