Diagram components
Static diagrams you can edit directly in MDX.
Wrap each example in Diagram to give it a canvas and an optional caption. Text in a node wraps. On a narrow canvas (under 560px), a Flow turns vertical and a Fan puts its source above its targets. The components are available in MDX without imports.
Fan
Put the source node in the from slot and list the target nodes in branch order. Fan numbers the branches from top to bottom. A node accepts a title, an optional subtitle and icon glyph, and a tone of default, accent, or muted. Use Check for each checklist row.
- Policy passed
- Input is valid
- One commit
- Sent after commit
<Diagram caption="A successful mutation commits before its events are delivered.">
<Fan>
<Node slot="from" title="Caller" subtitle="Runs a mutation" icon="›_" />
<Node title="Transaction" icon="▤" tone="accent">
<Check>Policy passed</Check>
<Check>Input is valid</Check>
<Check>One commit</Check>
</Node>
<Node title="Events" icon="↗">
<Check>Sent after commit</Check>
</Node>
</Fan>
</Diagram>
Flow and Group
Flow adds arrows between adjacent items. Insert Arrow between two items to add a number, a label, or both. Group puts a labelled outline around a flow, a stack, or a column of nodes.
<Diagram caption="A release moves from draft through review to ready.">
<Group label="Release">
<Flow>
<Node title="Draft" tone="muted" />
<Arrow n={1} label="submit" />
<Node title="Review" />
<Node title="Ready" tone="accent" />
</Flow>
</Group>
</Diagram>
Stack
Stack puts its children in a column and adds down arrows between them.
<Diagram>
<Stack>
<Node title="Write" />
<Node title="Review" />
<Node title="Publish" />
</Stack>
</Diagram>
Type-checked code
A ts twoslash fence checks the snippet against the workspace SDK and shows inferred types on hover. Hover a name to see its type. Don’t pin types with ^?: the popup covers the text below it.
import { function init<C extends Config & OnlyKnownKeys<C>>(): {
createRegistry: <const Mods extends Record<string, object>, T = ScopeOf<C>>(options: TypedOptions<C, Mods, T>) => Registry<ScopeOf<C>, Mods, ConfigOf<C>, ServicesOf<C>, AppError<`${string extends keyof Mods ? keyof Mods & string : { [M in keyof Mods & string]: { [N in keyof Mods[M] & string]: Mods[M][N] extends Fn<Kind, any, any, any, any, any, infer Id extends string | undefined> ? `${M}.${Id extends string ? Id : N}` : never; }[keyof Mods[M] & string]; }[keyof Mods & string] | (keyof Mods & string)}.forbidden`, undefined> | AppError<`${string extends keyof Mods ? keyof Mods & string : { [M in keyof Mods & string]: { [N in keyof Mods[M] & string]: Mods[M][N] extends Fn<Kind, any, any, any, any, any, infer Id extends string | undefined> ? `${M}.${Id extends string ? Id : N}` : never; }[keyof Mods[M] & string]; }[keyof Mods & string]}.invalidInput`, {
issues: {
message: string;
path?: PropertyKey[];
}[];
}> | AppError<`${string extends keyof Mods ? keyof Mods & string : keyof Mods & string}.notFound`, undefined> | { [M in keyof Mods & string]: { [N in keyof Mods[M] & string]: Mods[M][N] extends ErrorDef<any, infer O, infer Id extends string | undefined> ? AppError<`${M}.${Id extends string ? Id : N}`, O> : never; }[keyof Mods[M] & string]; }[keyof Mods & string]>;
query: Constructor<C, "query">;
mutation: Constructor<C, "mutation">;
action: Constructor<C, "action">;
event: <S extends Schema, const Id extends string | undefined = undefined>(def: {
id?: Id;
payload: S;
}) => EventDef<In<S>, Out<S>, Id, S>;
error: <S extends Schema | undefined = undefined, const Id extends string | undefined = undefined>(def?: {
id?: Id;
kind?: Exclude<ErrorKind, "invalid">;
details?: S;
message?: string | ((details: S extends Schema ? Out<S> : undefined) => string);
meta?: Readonly<Record<string, unknown>>;
}) => ErrorDef<S extends Schema ? In<S> : undefined, S extends Schema ? Out<S> : undefined, Id>;
cron: <const Sc extends ScopeOf<C>>(def: CronDef<...>) => Cron<C>;
listener: <O>(def: {
id?: string;
event: () => EventDef<unknown, O>;
attempts?: number;
delay?: number;
when?: (payload: O) => boolean;
key?: (payload: O) => string;
unique?: (payload: O) => string;
scope?: (payload: O) => ScopeOf<C>;
timeout?: number;
queue?: string;
meta?: MetaOf<C>;
handler: (ctx: ListenerCtx<C, O>, payload: O) => Promise<unknown>;
retry?: (error: AppError) => boolean;
failed?: (ctx: ListenerCtx<C, O>, payload: O, error: unknown) => Promise<unknown>;
}) => Listener<O, C>;
}
Typed constructors for your app:
```ts
export const { query, mutation, action, event, listener, createRegistry } = init<Config>();
```init } from "logic-sdk";
import { import zz } from "zod";
const { const query: Constructor<{
scope: {
actor: string | null;
};
}, "query">
Reads data. Can run other queries. No writes, no events, no services.query } = init<{
scope: {
actor: string | null;
};
}>(): {
createRegistry: <Mods, T>(options: TypedOptions<{
scope: {
actor: string | null;
};
}, Mods, T>) => Registry<{
actor: string | null;
}, Mods, Record<string, never>, {}, AppError<`${string extends keyof Mods ? keyof Mods & string : (keyof Mods & string) | { [M in keyof Mods & string]: { [N in keyof Mods[M] & string]: Mods[M][N] extends Fn<Kind, any, any, any, any, any, infer Id extends string | undefined> ? `${M}.${Id extends string ? Id : N}` : never; }[keyof Mods[M] & string]; }[keyof Mods & string]}.forbidden`, undefined> | AppError<...> | AppError<...> | { [M in keyof Mods & string]: { [N in keyof Mods[M] & string]: Mods[M][N] extends ErrorDef<...> ? AppError<...> : never; }[keyof Mods[M] & string]; }[keyof Mods & string]>;
... 6 more ...;
listener: <O>(def: {
...;
}) => Listener<...>;
}
Typed constructors for your app:
```ts
export const { query, mutation, action, event, listener, createRegistry } = init<Config>();
```init<{ scope: {
actor: string | null;
}
scope: { actor: string | nullactor: string | null } }>();
export const const ping: Fn<"query", {
name: string;
}, string, false, {
scope: {
actor: string | null;
};
}, z.ZodObject<{
name: z.ZodString;
}, z.core.$strip>, undefined>
ping = const query: <z.ZodObject<{
name: z.ZodString;
}, z.core.$strip>, undefined, string, Policy<{
actor: string | null;
}>, never, undefined>(def: Def<{
scope: {
actor: string | null;
};
}, "query", z.ZodObject<{
name: z.ZodString;
}, z.core.$strip>, undefined, string, Policy<{
actor: string | null;
}>, never, undefined>) => Fn<"query", {
name: string;
}, string, false, {
scope: {
actor: string | null;
};
}, z.ZodObject<{
name: z.ZodString;
}, z.core.$strip>, undefined>
Reads data. Can run other queries. No writes, no events, no services.query({
input?: z.ZodObject<{
name: z.ZodString;
}, z.core.$strip> | undefined
What callers pass, validated on every call. Leave it out when the function takes nothing.input: import zz.function object<{
name: z.ZodString;
}>(shape?: {
name: z.ZodString;
} | undefined, params?: string | {
error?: string | z.core.$ZodErrorMap<NonNullable<z.core.$ZodIssueInvalidType<unknown> | z.core.$ZodIssueUnrecognizedKeys>> | undefined;
message?: string | undefined | undefined;
} | undefined): z.ZodObject<{
name: z.ZodString;
}, z.core.$strip>
object({ name: z.ZodStringname: import zz.function string(params?: string | z.core.$ZodStringParams): z.ZodString (+1 overload)string() }),
handler: (ctx: QueryCtx<{
scope: {
actor: string | null;
};
}, {
actor: string | null;
}>, input: {
name: string;
}) => Promise<string>
handler: async (_ctx: QueryCtx<{
scope: {
actor: string | null;
};
}, {
actor: string | null;
}>
_ctx, { name: stringname }) => `hi ${name: stringname}`,
});
Generated from the source
modulesMods
One entry per module: `{ bookings, availability }` with `import * as bookings from "./bookings"`.
Modsdatabase?DatabaseOptions
`database: { adapter: drizzleAdapter(db) }`, with `transactionAttempts` when 3 isn't right.
DatabaseOptionsservices?object
objectconfig?object
Settings every function reads as `ctx.config`. Plain data: it's copied and frozen.
objectjobs?JobsOptions
Background work: `jobs: { adapter: databaseQueue(), scope: { store, load } }`. Default: in-process, scope as-is (nothing waits).
JobsOptionswrap?(call: Call, next: () => Promise<unknown>) => Promise<unknown>
Wraps every call, including ones that fail validation or authorization: logging, tracing, audit.
(call: Call, next: () => Promise<unknown>) => Promise<unknown>waitUntil?(promise: Promise<unknown>) => void
Serverless: let listeners finish after the response. Pass the platform's global: `import { waitUntil } from "cloudflare:workers"` or `import { waitUntil } from "@vercel/functions"`. Without it, `run` waits for listeners, unless a worker in this process takes them (`work(registry)` on Node or Bun, with `databaseQueue`): then the request answers at once.
(promise: Promise<unknown>) => voidonError?(error: unknown, info: FailureInfo) => void
Failures no caller waits for: jobs, and the work after commit that runs them. They never fail the caller.
(error: unknown, info: FailureInfo) => voidnow?() => Date
() => Date