Functions
The function kind controls what its handler can read, write, and run.
A function is a query, mutation, or action with a handler that receives ctx first.
- Runs queries
- Runs queries and mutations
- Emits events and schedules calls
- Runs any function
- Gets services
/** Price a stay at the property's nightly rate. */
export const const priceStay: Fn<"query", {
propertyId: string;
nights: number;
}, {
total: number;
}, false, {
scope: {
accountId: string;
};
}, z.ZodObject<{
propertyId: z.ZodUUID;
nights: z.ZodNumber;
}, z.core.$strip>, undefined>
Price a stay at the property's nightly rate.priceStay = const query: <z.ZodObject<{
propertyId: z.ZodUUID;
nights: z.ZodNumber;
}, z.core.$strip>, undefined, {
total: number;
}, Policy<{
accountId: string;
}>, never, undefined>(def: Def<{
scope: {
accountId: string;
};
}, "query", z.ZodObject<{
propertyId: z.ZodUUID;
nights: z.ZodNumber;
}, z.core.$strip>, undefined, {
total: number;
}, Policy<{
accountId: string;
}>, never, undefined>) => Fn<"query", {
propertyId: string;
nights: number;
}, {
total: number;
}, false, {
scope: {
accountId: string;
};
}, z.ZodObject<{
propertyId: z.ZodUUID;
nights: z.ZodNumber;
}, z.core.$strip>, undefined>
Reads data. Can run other queries. No writes, no events, no services.query({
input?: z.ZodObject<{
propertyId: z.ZodUUID;
nights: z.ZodNumber;
}, z.core.$strip> | undefined
What callers pass, validated on every call. Leave it out when the function takes nothing.input: import zz.function object<{
propertyId: z.ZodUUID;
nights: z.ZodNumber;
}>(shape?: {
propertyId: z.ZodUUID;
nights: z.ZodNumber;
} | undefined, params?: string | {
error?: string | z.core.$ZodErrorMap<NonNullable<z.core.$ZodIssueInvalidType<unknown> | z.core.$ZodIssueUnrecognizedKeys>> | undefined;
message?: string | undefined | undefined;
} | undefined): z.ZodObject<{
propertyId: z.ZodUUID;
nights: z.ZodNumber;
}, z.core.$strip>
object({ propertyId: z.ZodUUIDpropertyId: import zz.function uuid(params?: string | z.core.$ZodUUIDParams): z.ZodUUIDuuid(), nights: z.ZodNumbernights: import zz.function number(params?: string | z.core.$ZodNumberParams): z.ZodNumbernumber()._ZodNumber<$ZodNumberInternals<number>>.int(params?: string | z.core.$ZodCheckNumberFormatParams): z.ZodNumberConsider `z.int()` instead. This API is considered *legacy*; it will never be removed but a better alternative exists.int()._ZodNumber<$ZodNumberInternals<number>>.positive(params?: string | z.core.$ZodCheckGreaterThanParams): z.ZodNumberpositive() }),
async handler: (ctx: QueryCtx<{
scope: {
accountId: string;
};
}, {
accountId: string;
}>, input: {
propertyId: string;
nights: number;
}) => Promise<{
total: number;
}>
handler(ctx: QueryCtx<{
scope: {
accountId: string;
};
}, {
accountId: string;
}>
ctx, { propertyId: stringpropertyId, nights: numbernights }) {
const const property: {
nightlyRate: number;
}
property = await ctx: QueryCtx<{
scope: {
accountId: string;
};
}, {
accountId: string;
}>
ctx.QueryCtx<{ scope: { accountId: string; }; }, { accountId: string; }>.run: <Fn<"query", {
propertyId: string;
}, {
nightlyRate: number;
}, false, {
scope: {
accountId: string;
};
}, z.ZodObject<{
propertyId: z.ZodUUID;
}, z.core.$strip>, undefined>>(fn: Fn<"query", {
propertyId: string;
}, {
nightlyRate: number;
}, false, {
scope: {
accountId: string;
};
}, z.ZodObject<{
propertyId: z.ZodUUID;
}, z.core.$strip>, undefined>, input: {
propertyId: string;
}, options?: RunOptions<{
accountId: string;
}> | undefined) => Promise<{
nightlyRate: number;
}>
Call another query, with its policy, validation and output check: `await ctx.run(pricing.quote, { nights })`.run(const properties: {
get: Fn<"query", {
propertyId: string;
}, {
nightlyRate: number;
}, false, {
scope: {
accountId: string;
};
}, z.ZodObject<{
propertyId: z.ZodUUID;
}, z.core.$strip>, undefined>;
}
properties.get: Fn<"query", {
propertyId: string;
}, {
nightlyRate: number;
}, false, {
scope: {
accountId: string;
};
}, z.ZodObject<{
propertyId: z.ZodUUID;
}, z.core.$strip>, undefined>
get, { propertyId: stringpropertyId });
return { total: numbertotal: nights: numbernights * const property: {
nightlyRate: number;
}
property.nightlyRate: numbernightlyRate };
},
});
- Context. See context members. Read
ctx.now()so tests can set the clock. - Database. Declare
db: DrizzleDb<typeof db>in theinitconfig. A query’sctx.dbreads only. A mutation’sctx.dbis its transaction. - Cancellation. The registry checks a call’s signal before starting. Pass
ctx.signalto external work in an action.
Input
Omit input for a function without input. Its input is undefined. Run it with run(fn).
Use Input<typeof fn> and Output<typeof fn> for a function or its schema. Derive form fields when the shapes match.
const fields = properties.create.input.pick({ name: true, nightlyRate: true });
run parses input itself. If parsing at the entry changes the input type, pass the original input. Use a separate schema and translate if the entry accepts different values.
Helpers and nested calls
Put ctx first in helpers. A QueryCtx helper can read through a mutation’s ctx.
Run other functions with ctx.run, not directly, so their policy, validation, and wrap apply.
Import across modules through their indexes. Use events to break write cycles. The registry reports the call path above 64 levels.
Private functions
A private function is a function that the module’s index does not export. Only ctx.run can run it.
Use a private function for a mutation that needs its action’s checks. Put private functions shared across files in data.ts.
The call ID is the module name with (private), or with the function’s id. You cannot schedule a private function or run it from a registry or test caller. Test it through the definition that runs it.
Internal functions
An internal function is a registered function that you do not publish. Other functions, listeners, and verified callbacks run it. Without authorize, the oRPC and AI integrations refuse to publish it.