Config and services
The registry supplies settings and services that handlers use.
Config holds the app’s settings. Services hold clients that do external work.
env
Read at the entry
createRegistry
Receives config and services
ctx.config
Available in every handler
ctx.services
Actions, listeners, and crons
const { const query: Constructor<{
scope: {
actor: string | null;
};
config: {
bookings: {
maxItems: number;
};
};
services: {
mailer: typeof mailer;
};
}, "query">
Reads data. Can run other queries. No writes, no events, no services.query, const createRegistry: <const Mods extends Record<string, object>, T = {
actor: string | null;
}>(options: TypedOptions<{
scope: {
actor: string | null;
};
config: {
bookings: {
maxItems: number;
};
};
services: {
mailer: typeof mailer;
};
}, Mods, T>) => Registry<{
actor: string | null;
}, Mods, {
bookings: {
maxItems: number;
};
}, {
mailer: typeof mailer;
}, 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<...> ? `${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]>
Create the registry once at startup: `createRegistry({ modules, database, services })`.createRegistry } = init<{
scope: {
actor: string | null;
};
config: {
bookings: {
maxItems: number;
};
};
services: {
mailer: typeof mailer;
};
}>(): {
createRegistry: <Mods, T>(options: TypedOptions<{
scope: {
actor: string | null;
};
config: {
bookings: {
maxItems: number;
};
};
services: {
mailer: typeof mailer;
};
}, Mods, T>) => Registry<{
actor: string | null;
}, Mods, {
bookings: {
maxItems: number;
};
}, {
mailer: typeof mailer;
}, 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<...> ? `${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 };
config: {
bookings: {
maxItems: number;
};
}
config: { bookings: {
maxItems: number;
}
bookings: { maxItems: numbermaxItems: number } };
services: {
mailer: typeof mailer;
}
services: { mailer: {
sendConfirmation: (bookingId: string) => Promise<void>;
}
mailer: typeof const mailer: {
sendConfirmation: (bookingId: string) => Promise<void>;
}
mailer };
}>();
const const maxItems: Fn<"query", undefined, number, false, {
scope: {
actor: string | null;
};
config: {
bookings: {
maxItems: number;
};
};
services: {
mailer: typeof mailer;
};
}, undefined, undefined>
maxItems = const query: <NoInput, undefined, number, Policy<{
actor: string | null;
}>, never, undefined>(def: Def<{
scope: {
actor: string | null;
};
config: {
bookings: {
maxItems: number;
};
};
services: {
mailer: typeof mailer;
};
}, "query", NoInput, undefined, number, Policy<{
actor: string | null;
}>, never, undefined>) => Fn<"query", undefined, number, false, {
scope: {
actor: string | null;
};
config: {
bookings: {
maxItems: number;
};
};
services: {
mailer: typeof mailer;
};
}, undefined, undefined>
Reads data. Can run other queries. No writes, no events, no services.query({
async handler: (ctx: QueryCtx<{
scope: {
actor: string | null;
};
config: {
bookings: {
maxItems: number;
};
};
services: {
mailer: typeof mailer;
};
}, {
actor: string | null;
}>, input: undefined) => Promise<number>
handler(ctx: QueryCtx<{
scope: {
actor: string | null;
};
config: {
bookings: {
maxItems: number;
};
};
services: {
mailer: typeof mailer;
};
}, {
actor: string | null;
}>
ctx) {
return ctx: QueryCtx<{
scope: {
actor: string | null;
};
config: {
bookings: {
maxItems: number;
};
};
services: {
mailer: typeof mailer;
};
}, {
actor: string | null;
}>
ctx.BaseCtx<{ scope: { actor: string | null; }; config: { bookings: { maxItems: number; }; }; services: { mailer: typeof mailer; }; }, { actor: string | null; }>.config: {
readonly bookings: {
readonly maxItems: number;
};
}
Your settings from `createRegistry({ config })`. Read-only; tests override parts of it.config.bookings: {
readonly maxItems: number;
}
bookings.maxItems: numbermaxItems;
},
});
export const const registry: Registry<{
actor: string | null;
}, {
readonly bookings: {
readonly maxItems: Fn<"query", undefined, number, false, {
scope: {
actor: string | null;
};
config: {
bookings: {
maxItems: number;
};
};
services: {
mailer: typeof mailer;
};
}, undefined, undefined>;
};
}, {
bookings: {
maxItems: number;
};
}, {
mailer: typeof mailer;
}, AppError<"bookings.forbidden" | "bookings.maxItems.forbidden", undefined> | AppError<"bookings.maxItems.invalidInput", {
issues: {
message: string;
path?: PropertyKey[];
}[];
}> | AppError<...>>
registry = const createRegistry: <{
readonly bookings: {
readonly maxItems: Fn<"query", undefined, number, false, {
scope: {
actor: string | null;
};
config: {
bookings: {
maxItems: number;
};
};
services: {
mailer: typeof mailer;
};
}, undefined, undefined>;
};
}, {
actor: string | null;
}>(options: TypedOptions<{
scope: {
actor: string | null;
};
config: {
bookings: {
maxItems: number;
};
};
services: {
mailer: typeof mailer;
};
}, {
readonly bookings: {
readonly maxItems: Fn<"query", undefined, number, false, {
scope: {
actor: string | null;
};
config: {
bookings: {
maxItems: number;
};
};
services: {
mailer: typeof mailer;
};
}, undefined, undefined>;
};
}, {
actor: string | null;
}>) => Registry<...>
Create the registry once at startup: `createRegistry({ modules, database, services })`.createRegistry({
modules: {
readonly bookings: {
readonly maxItems: Fn<"query", undefined, number, false, {
scope: {
actor: string | null;
};
config: {
bookings: {
maxItems: number;
};
};
services: {
mailer: typeof mailer;
};
}, undefined, undefined>;
};
} & OwnModules<{
scope: {
actor: string | null;
};
config: {
bookings: {
maxItems: number;
};
};
services: {
mailer: typeof mailer;
};
}, {
readonly bookings: {
readonly maxItems: Fn<"query", undefined, number, false, {
scope: {
actor: string | null;
};
config: {
bookings: {
maxItems: number;
};
};
services: {
mailer: typeof mailer;
};
}, undefined, undefined>;
};
}>
One entry per module: `{ bookings, availability }` with `import * as bookings from "./bookings"`.modules: { bookings: {
readonly maxItems: Fn<"query", undefined, number, false, {
scope: {
actor: string | null;
};
config: {
bookings: {
maxItems: number;
};
};
services: {
mailer: typeof mailer;
};
}, undefined, undefined>;
} & {
readonly maxItems: Fn<"query", undefined, number, false, {
scope: {
actor: string | null;
};
config: {
bookings: {
maxItems: number;
};
};
services: {
mailer: typeof mailer;
};
}, undefined, undefined>;
}
bookings: { maxItems: Fn<"query", undefined, number, false, {
scope: {
actor: string | null;
};
config: {
bookings: {
maxItems: number;
};
};
services: {
mailer: typeof mailer;
};
}, undefined, undefined>
maxItems } },
config: object & {
bookings: {
maxItems: number;
};
}
Settings every function reads as `ctx.config`. Plain data: it's copied and frozen.config: { bookings: {
maxItems: number;
}
bookings: { maxItems: numbermaxItems: var Number: NumberConstructor
(value?: any) => number
An object that represents a number of any kind. All JavaScript numbers are 64-bit floating-point numbers.Number(const env: {
MAX_ITEMS?: string;
}
env.MAX_ITEMS?: string | undefinedMAX_ITEMS ?? 20) } },
services: object & {
mailer: typeof mailer;
}
services: { mailer: {
sendConfirmation: (bookingId: string) => Promise<void>;
}
mailer },
});
- Entry. Read env and build clients at the entry. Declare shapes in
init. Supply required fields tocreateRegistry. - Config (1). Each handler and
jobs.scope.loadreadsctx.configfrom its registry. - Services (2). Only actions, listeners, and crons get
ctx.services. Keep client secrets inside the service. - Plain data. Use strings, finite numbers, booleans, null, arrays, and plain objects. The registry reports the path of invalid values, such as a
Date. - Read-only. The registry copies and freezes config at creation. Test overrides merge object sections and replace arrays in the test registry.
- Inspection. See CLI show for config keys.
Feature flags
Choose where to store a flag by who needs it and when its value must be current.
| Location | Use it for |
|---|---|
| Scope | A per-account or per-actor flag you resolve at the entry. Policies read flags here. |
| Service | A flag from an external provider. Resolve it in an action, listener, or cron. |
| Database | A flag that must be read with the rest of the transaction. Filter by account. |
| Config | A flag shared by every caller that changes when you create a new registry. |
For a job to use the original scope flag, save it in jobs.scope.store and carry it through load. To use the current value, resolve it again in load or read it from a service in the listener.
Pass a service flag to a query or mutation through input only if the function is internal. Clients can set the input of published functions.