@brickkit/core 0.11.0 → 0.13.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/chunks/client.d.ts +52 -2769
- package/dist/chunks/client.js +397 -101
- package/dist/chunks/client.js.map +1 -1
- package/dist/chunks/server.d.ts +444 -0
- package/dist/chunks/server.js +4018 -0
- package/dist/chunks/server.js.map +1 -0
- package/dist/chunks/types.d.ts +2505 -0
- package/dist/client.d.ts +3 -2
- package/dist/client.js +1 -1
- package/dist/index.d.ts +332 -717
- package/dist/index.js +612 -4959
- package/dist/index.js.map +1 -1
- package/dist/pg.d.ts +0 -2
- package/dist/tanstack-query.d.ts +2 -3
- package/dist/tanstack-query.js +1 -2
- package/dist/tanstack-query.js.map +1 -1
- package/dist/testing.d.ts +31 -0
- package/dist/testing.js +46 -0
- package/dist/testing.js.map +1 -0
- package/dist/workflow.d.ts +74 -0
- package/dist/workflow.js +93 -0
- package/dist/workflow.js.map +1 -0
- package/package.json +32 -17
- package/dist/chunks/client.d.ts.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/pg.d.ts.map +0 -1
- package/dist/tanstack-query.d.ts.map +0 -1
|
@@ -0,0 +1,2505 @@
|
|
|
1
|
+
import { Static, TIntersect, TNumber, TObject, TSchema, TString, TUnion } from "@sinclair/typebox";
|
|
2
|
+
import { Table } from "drizzle-orm";
|
|
3
|
+
import { SQLiteColumnBuilderBase, blob, foreignKey, index as index$1, integer as integer$1, primaryKey, real as real$1, sqliteTable as sqliteTable$1, text as text$1, uniqueIndex } from "drizzle-orm/sqlite-core";
|
|
4
|
+
import { BunSQLiteDatabase, BunSQLiteDatabase as BunSQLiteDatabase$1 } from "drizzle-orm/bun-sqlite";
|
|
5
|
+
import { Sql } from "postgres";
|
|
6
|
+
import { PostgresJsDatabase } from "drizzle-orm/postgres-js";
|
|
7
|
+
import { Database } from "bun:sqlite";
|
|
8
|
+
/** The HTTP methods an action, endpoint or plugin route can use. */
|
|
9
|
+
declare const HTTP_METHODS: readonly ["GET", "POST", "PUT", "PATCH", "DELETE"];
|
|
10
|
+
type HttpMethod = (typeof HTTP_METHODS)[number];
|
|
11
|
+
declare const portType: unique symbol;
|
|
12
|
+
/** A typed dependency slot, declared with `port<T>()` and filled by `brick({ deps })`. */
|
|
13
|
+
interface Port<T> {
|
|
14
|
+
readonly [portType]?: T;
|
|
15
|
+
}
|
|
16
|
+
/** The dependency slots of a service: `{ carrier: port<Carrier>() }`. */
|
|
17
|
+
type Ports = Record<string, Port<unknown>>;
|
|
18
|
+
/** The values a set of ports is filled with: `{ carrier: Carrier }`. */
|
|
19
|
+
type DepsOf<P> = { [K in keyof P]: P[K] extends Port<infer T> ? T : never; };
|
|
20
|
+
/**
|
|
21
|
+
* Declare a dependency a service needs, without creating it. The app provides
|
|
22
|
+
* the value with `brick({ deps })`, and the service's code reads it as
|
|
23
|
+
* `ctx.deps.<name>` (or `useDeps()` in listeners and workflow steps).
|
|
24
|
+
*
|
|
25
|
+
* @returns A slot typed as `T`. It holds nothing at runtime.
|
|
26
|
+
* @example
|
|
27
|
+
* import { defineService, port } from "@brickkit/core";
|
|
28
|
+
*
|
|
29
|
+
* export const orders = defineService("orders", {
|
|
30
|
+
* database,
|
|
31
|
+
* deps: { carrier: port<Carrier>() },
|
|
32
|
+
* });
|
|
33
|
+
*
|
|
34
|
+
* // app.ts
|
|
35
|
+
* export default brick({ services: [orders], deps: { carrier: createCarrier() } });
|
|
36
|
+
*/
|
|
37
|
+
declare function port<T>(): Port<T>;
|
|
38
|
+
/**
|
|
39
|
+
* The deps of the app that is running the current listener or workflow step.
|
|
40
|
+
* Actions, tools and socket handlers read `ctx.deps` instead.
|
|
41
|
+
*
|
|
42
|
+
* A workflow step is a plain function the Workflow SDK calls by name, so it
|
|
43
|
+
* cannot close over the app that started it. Brick runs every listener and every
|
|
44
|
+
* step inside its app, and this returns that app's `brick({ deps })`.
|
|
45
|
+
*
|
|
46
|
+
* @param ports - Optional: the ports to type the result by (the same object a
|
|
47
|
+
* service passes as its `deps` option). Otherwise pass the type: `useDeps<{ carrier: Carrier }>()`.
|
|
48
|
+
* @returns The app's deps.
|
|
49
|
+
* @throws When called outside a listener or workflow step of a Brick app.
|
|
50
|
+
* @example
|
|
51
|
+
* export const ordersDeps = { carrier: port<Carrier>() };
|
|
52
|
+
*
|
|
53
|
+
* async function bookCarrier(orderId: string) {
|
|
54
|
+
* "use step";
|
|
55
|
+
* return useDeps(ordersDeps).carrier.book(orderId);
|
|
56
|
+
* }
|
|
57
|
+
*/
|
|
58
|
+
declare function useDeps<const P extends Ports>(ports: P): DepsOf<P>;
|
|
59
|
+
declare function useDeps<T = Record<string, unknown>>(): T;
|
|
60
|
+
/** A service's declared events: payload schema by event name. */
|
|
61
|
+
type EventCatalog = Record<string, TSchema>;
|
|
62
|
+
/**
|
|
63
|
+
* One declared event, as `service.events.<name>`. Pass it to `defineListener({ on })`
|
|
64
|
+
* for a typed payload, or to `emitEvent` for a checked one.
|
|
65
|
+
*/
|
|
66
|
+
interface EventRef<TPayload = unknown> {
|
|
67
|
+
/** The event name on the wire: `<service>.<event>`, such as `orders.shipped`. */
|
|
68
|
+
readonly name: string;
|
|
69
|
+
readonly service: string;
|
|
70
|
+
readonly schema: TSchema;
|
|
71
|
+
/** The plugin that declared it, for events a `definePlugin` adds. */
|
|
72
|
+
readonly plugin?: string;
|
|
73
|
+
/** Type-level only: the payload this event carries. */
|
|
74
|
+
readonly __payload?: TPayload;
|
|
75
|
+
}
|
|
76
|
+
/** `service.events`: one `EventRef` per catalog entry. */
|
|
77
|
+
type EventRefs<C extends EventCatalog> = { readonly [K in keyof C & string]: EventRef<Static<C[K]>>; };
|
|
78
|
+
/** The writes a resource's CRUD events report. Payloads are `CrudEventPayload`. */
|
|
79
|
+
declare const CRUD_EVENT_OPERATIONS: readonly ["create", "update", "delete"];
|
|
80
|
+
type CrudEventOperation = (typeof CRUD_EVENT_OPERATIONS)[number];
|
|
81
|
+
/** The event a CRUD write records: `<resource>.<operation>`. */
|
|
82
|
+
declare const crudEventName: (resource: string, operation: CrudEventOperation) => string;
|
|
83
|
+
/** The `source` of a resource's events: `<service>.<resource>`. */
|
|
84
|
+
declare const crudEventSource: (service: string, resource: string) => string;
|
|
85
|
+
/** Whether `name` is the CRUD event of one of `resources`. */
|
|
86
|
+
declare function isCrudEvent(name: string, resources: readonly string[]): boolean;
|
|
87
|
+
/**
|
|
88
|
+
* An event, as it was recorded. It is the same for every listener of it.
|
|
89
|
+
*/
|
|
90
|
+
interface EventEnvelope<TPayload = unknown> {
|
|
91
|
+
/** Unique per event. Every listener's delivery of the event shares it. */
|
|
92
|
+
id: string;
|
|
93
|
+
/** The event name, such as `user.create`. */
|
|
94
|
+
name: string;
|
|
95
|
+
/** What happened. JSON data. */
|
|
96
|
+
payload: TPayload;
|
|
97
|
+
/** When it happened, as an ISO timestamp. */
|
|
98
|
+
occurredAt: string;
|
|
99
|
+
/** The trace of the request that caused it, so a listener continues the same trace. */
|
|
100
|
+
traceId?: string;
|
|
101
|
+
/** What emitted it, such as `identity.user` for a generated CRUD write. */
|
|
102
|
+
source?: string;
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* One listener's delivery of an event: the envelope, plus which listener it is for
|
|
106
|
+
* and which attempt this is.
|
|
107
|
+
*/
|
|
108
|
+
interface EventDelivery<TPayload = unknown> extends EventEnvelope<TPayload> {
|
|
109
|
+
/** The name of the listener this delivery is for. */
|
|
110
|
+
listener: string;
|
|
111
|
+
/**
|
|
112
|
+
* `<event id>:<listener>`. It is the same on every retry, so a listener can use
|
|
113
|
+
* it to avoid doing the work twice: delivery is at least once.
|
|
114
|
+
*/
|
|
115
|
+
deliveryId: string;
|
|
116
|
+
/** 1 on the first try, 2 on the first retry, and so on. */
|
|
117
|
+
attempt: number;
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* The CRUD events of a resource called `user`, by operation. Listeners of
|
|
121
|
+
* `user.create`, `user.update` and `user.delete` receive these payloads.
|
|
122
|
+
*/
|
|
123
|
+
type CrudEventPayload<TRow = Record<string, unknown>> = {
|
|
124
|
+
id: unknown;
|
|
125
|
+
row: TRow;
|
|
126
|
+
} | {
|
|
127
|
+
id: unknown;
|
|
128
|
+
before: TRow;
|
|
129
|
+
after: TRow;
|
|
130
|
+
};
|
|
131
|
+
/**
|
|
132
|
+
* What `retry` receives: one member per error the listener declares, or the
|
|
133
|
+
* fallback for anything else it threw.
|
|
134
|
+
*/
|
|
135
|
+
type ListenerFailure<TErrors extends Record<string, ActionErrorDefinition>> = { [K in keyof TErrors & string]: {
|
|
136
|
+
code: K;
|
|
137
|
+
message: string;
|
|
138
|
+
details: ErrorDetails<TErrors[K]>;
|
|
139
|
+
}; }[keyof TErrors & string] | {
|
|
140
|
+
code: "UNKNOWN";
|
|
141
|
+
message: string;
|
|
142
|
+
cause: unknown;
|
|
143
|
+
};
|
|
144
|
+
/** What a listener's `retry` decides: try again later, or park the delivery as dead now. */
|
|
145
|
+
type RetryDecision = "retry" | "dead";
|
|
146
|
+
/**
|
|
147
|
+
* A listener: code that runs when an event it listens for has been recorded.
|
|
148
|
+
*/
|
|
149
|
+
interface EventListener<TPayload = any> {
|
|
150
|
+
/** Unique among the app's listeners. It is part of each delivery's id. */
|
|
151
|
+
readonly name: string;
|
|
152
|
+
/**
|
|
153
|
+
* The event names to listen for. A trailing `*` matches a prefix (`user.*`),
|
|
154
|
+
* and `*` alone matches every event.
|
|
155
|
+
*/
|
|
156
|
+
readonly on: readonly string[];
|
|
157
|
+
/**
|
|
158
|
+
* A handler that runs in the dispatching process for each delivery. Throwing
|
|
159
|
+
* retries the delivery with a growing delay. A listener has this or `workflow`.
|
|
160
|
+
*/
|
|
161
|
+
readonly run?: (delivery: EventDelivery<TPayload>) => void | Promise<void>;
|
|
162
|
+
/** The typed errors a handler can fail with; `retry` receives them as a `ListenerFailure`. */
|
|
163
|
+
readonly errors?: Record<string, ActionErrorDefinition>;
|
|
164
|
+
/**
|
|
165
|
+
* Decides what a failed delivery does next. Without it, every failure retries
|
|
166
|
+
* until `maxAttempts`. It cannot extend the attempts: `"retry"` still parks the
|
|
167
|
+
* delivery as dead once they run out.
|
|
168
|
+
*/
|
|
169
|
+
readonly retry?: (error: ListenerFailure<any>) => RetryDecision;
|
|
170
|
+
/**
|
|
171
|
+
* A durable workflow that runs for each delivery, on the app's `events.world`.
|
|
172
|
+
* The delivery counts as done once its run has started; the workflow retries
|
|
173
|
+
* its own steps from there. A listener has this or `run`.
|
|
174
|
+
*/
|
|
175
|
+
readonly workflow?: EventWorkflow<TPayload>;
|
|
176
|
+
/** Attempts before the delivery is parked as dead. Defaults to the dispatcher's. */
|
|
177
|
+
readonly maxAttempts?: number;
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* What a workflow listener's workflow receives: the event and which listener it
|
|
181
|
+
* is for. It has no `attempt`, because one delivery starts one run however many
|
|
182
|
+
* times it is tried.
|
|
183
|
+
*/
|
|
184
|
+
type WorkflowEvent<TPayload = any> = Omit<EventDelivery<TPayload>, "attempt">;
|
|
185
|
+
/**
|
|
186
|
+
* A function marked `"use workflow"`, run once per delivery of the listener it is
|
|
187
|
+
* given to.
|
|
188
|
+
*/
|
|
189
|
+
type EventWorkflow<TPayload = any> = (event: WorkflowEvent<TPayload>) => Promise<unknown>;
|
|
190
|
+
/**
|
|
191
|
+
* The world workflow listeners run on: the queue and storage behind the Workflow
|
|
192
|
+
* SDK. `createWorld()` from `@brickkit/redis-world` or `@brickkit/bullmq-world`
|
|
193
|
+
* makes one. Brick starts it with the app's events and closes it when they stop.
|
|
194
|
+
*/
|
|
195
|
+
interface EventsWorld {
|
|
196
|
+
/**
|
|
197
|
+
* Names a run after its key so that starting it twice makes one run. A world
|
|
198
|
+
* without it would start a second run when an event is delivered again, so
|
|
199
|
+
* Brick refuses such a world.
|
|
200
|
+
*/
|
|
201
|
+
createRunId?(options?: Readonly<Record<string, unknown>>): string;
|
|
202
|
+
start?(): Promise<void>;
|
|
203
|
+
close?(): Promise<void>;
|
|
204
|
+
registerHandler?(prefix: any, handler: (request: Request) => Promise<Response>): void;
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* Where the workflows of an app are built from. The CLI (`brick dev`,
|
|
208
|
+
* `brick start`) writes the bundle to `<root>/.well-known/workflow/v1` before
|
|
209
|
+
* events start, and events read it from there.
|
|
210
|
+
*/
|
|
211
|
+
interface WorkflowsOptions {
|
|
212
|
+
/** The project root: where the Workflow SDK is installed. Default: the working directory. */
|
|
213
|
+
root?: string;
|
|
214
|
+
}
|
|
215
|
+
/**
|
|
216
|
+
* How the dispatcher delivers recorded events.
|
|
217
|
+
*/
|
|
218
|
+
interface DispatchOptions {
|
|
219
|
+
/**
|
|
220
|
+
* Whether to look for due deliveries on a timer, and right after a commit.
|
|
221
|
+
* Default `true`. With `false`, deliveries run only when something calls
|
|
222
|
+
* `app.events.drain()`: a cron job, a worker you drive yourself, or a test.
|
|
223
|
+
*/
|
|
224
|
+
timer?: boolean;
|
|
225
|
+
/** How often to look for deliveries that are due, in milliseconds. Default 1000. */
|
|
226
|
+
intervalMs?: number;
|
|
227
|
+
/** Most deliveries claimed at once. Default 50. */
|
|
228
|
+
batchSize?: number;
|
|
229
|
+
/** Attempts before a delivery is parked as dead. Default 8. */
|
|
230
|
+
maxAttempts?: number;
|
|
231
|
+
/** How long a claimed delivery may run before another dispatcher takes it, in milliseconds. Default 60000. */
|
|
232
|
+
claimTimeoutMs?: number;
|
|
233
|
+
/** Longest wait before a retry, in milliseconds. Default 300000. */
|
|
234
|
+
maxBackoffMs?: number;
|
|
235
|
+
/** How long delivered rows are kept, in milliseconds. Default 86400000 (one day). 0 keeps none. */
|
|
236
|
+
retainDeliveredMs?: number;
|
|
237
|
+
}
|
|
238
|
+
/**
|
|
239
|
+
* The `events` option of `brick()`.
|
|
240
|
+
*/
|
|
241
|
+
interface EventsOptions {
|
|
242
|
+
/** The listeners of the app. */
|
|
243
|
+
listeners?: readonly EventListener[];
|
|
244
|
+
/**
|
|
245
|
+
* Whether this process delivers recorded events. `true` (the default) starts a
|
|
246
|
+
* dispatcher with the default settings, an object tunes it, and `false` only
|
|
247
|
+
* records events, for an app whose events are delivered by another process.
|
|
248
|
+
*/
|
|
249
|
+
dispatch?: boolean | DispatchOptions;
|
|
250
|
+
/**
|
|
251
|
+
* The world that runs `workflow` listeners. Required when any listener has one.
|
|
252
|
+
* Brick starts it when the app's events start and closes it when they stop.
|
|
253
|
+
*/
|
|
254
|
+
world?: EventsWorld;
|
|
255
|
+
/** Where the workflow bundle lives. Only used with workflow listeners. */
|
|
256
|
+
workflows?: WorkflowsOptions;
|
|
257
|
+
}
|
|
258
|
+
/**
|
|
259
|
+
* Records an event inside the transaction it was given. The event is committed
|
|
260
|
+
* with the write, or not at all.
|
|
261
|
+
*/
|
|
262
|
+
type EventRecorder = (name: string, payload: unknown) => Promise<void>;
|
|
263
|
+
/**
|
|
264
|
+
* What a service with events offers the code that writes to its database. A
|
|
265
|
+
* service has it once `brick({ events })` is set up and its database holds an
|
|
266
|
+
* outbox table.
|
|
267
|
+
*/
|
|
268
|
+
interface ServiceEvents {
|
|
269
|
+
/** Whether any listener wants the event, so a write can skip recording one. */
|
|
270
|
+
wants(name: string): boolean;
|
|
271
|
+
/**
|
|
272
|
+
* Run `fn` in a database transaction. Events it records are committed with
|
|
273
|
+
* whatever else it wrote, or not at all, and are delivered after the commit.
|
|
274
|
+
*/
|
|
275
|
+
transaction<T>(db: unknown, context: {
|
|
276
|
+
traceId?: string;
|
|
277
|
+
source?: string;
|
|
278
|
+
}, fn: (tx: any, record: EventRecorder) => Promise<T>): Promise<T>;
|
|
279
|
+
/**
|
|
280
|
+
* Record an event in a transaction the caller already has open. It is
|
|
281
|
+
* committed with that transaction. Nothing tells Brick when the caller commits,
|
|
282
|
+
* so delivery waits for the dispatcher's next poll.
|
|
283
|
+
*/
|
|
284
|
+
record(tx: unknown, context: {
|
|
285
|
+
traceId?: string;
|
|
286
|
+
source?: string;
|
|
287
|
+
}, name: string, payload: unknown): Promise<void>;
|
|
288
|
+
}
|
|
289
|
+
interface HookBase<C> {
|
|
290
|
+
ctx: C;
|
|
291
|
+
/** The write's open transaction: write other tables through it. */
|
|
292
|
+
tx: any;
|
|
293
|
+
}
|
|
294
|
+
/** The arguments each host CRUD hook receives, by hook name. */
|
|
295
|
+
interface PluginHookArgs<C = ActionContext> {
|
|
296
|
+
beforeCreate: HookBase<C> & {
|
|
297
|
+
data: Record<string, unknown>;
|
|
298
|
+
};
|
|
299
|
+
afterCreate: HookBase<C> & {
|
|
300
|
+
row: Record<string, unknown>;
|
|
301
|
+
};
|
|
302
|
+
beforeUpdate: HookBase<C> & {
|
|
303
|
+
id: unknown;
|
|
304
|
+
data: Record<string, unknown>;
|
|
305
|
+
};
|
|
306
|
+
afterUpdate: HookBase<C> & {
|
|
307
|
+
id: unknown;
|
|
308
|
+
row: Record<string, unknown>;
|
|
309
|
+
};
|
|
310
|
+
beforeDelete: HookBase<C> & {
|
|
311
|
+
id: unknown;
|
|
312
|
+
};
|
|
313
|
+
afterDelete: HookBase<C> & {
|
|
314
|
+
id: unknown;
|
|
315
|
+
};
|
|
316
|
+
afterCommit: {
|
|
317
|
+
operation: "create" | "update" | "delete";
|
|
318
|
+
id: unknown;
|
|
319
|
+
row?: Record<string, unknown>;
|
|
320
|
+
ctx: C;
|
|
321
|
+
};
|
|
322
|
+
}
|
|
323
|
+
/**
|
|
324
|
+
* Subscribe to the host's CRUD writes (resources with `crud()`). `before*` and
|
|
325
|
+
* `after*` run inside the write's transaction, after the host's own crud hooks;
|
|
326
|
+
* a hook that throws rolls the write back. `afterCommit` runs after the commit.
|
|
327
|
+
*/
|
|
328
|
+
type PluginHooks<C = ActionContext> = { [K in keyof PluginHookArgs<C>]: (hook: (args: PluginHookArgs<C>[K]) => unknown) => void; };
|
|
329
|
+
type HostHooks = { [K in keyof PluginHookArgs]?: ((args: PluginHookArgs[K]) => unknown)[]; };
|
|
330
|
+
/** The plugin hooks installed on a service or resource, by hook name. */
|
|
331
|
+
declare function getHostHooks(host: object): HostHooks | undefined;
|
|
332
|
+
declare const sqliteBuilders: {
|
|
333
|
+
/** A table. Its name gets the plugin's prefix (`page_comments`). */
|
|
334
|
+
table: <C extends Record<string, SQLiteColumnBuilderBase>>(name: string, columns: C) => import("drizzle-orm/sqlite-core").SQLiteTableWithColumns<{
|
|
335
|
+
name: string;
|
|
336
|
+
schema: undefined;
|
|
337
|
+
columns: { [Key in keyof C]: import("drizzle-orm/sqlite-core").SQLiteColumn<{
|
|
338
|
+
name: (Omit<C[Key]["_"], "name"> & {
|
|
339
|
+
name: C[Key]["_"]["name"] extends "" ? import("drizzle-orm").Assume<Key, string> : C[Key]["_"]["name"];
|
|
340
|
+
})["name"];
|
|
341
|
+
tableName: string;
|
|
342
|
+
dataType: (Omit<C[Key]["_"], "name"> & {
|
|
343
|
+
name: C[Key]["_"]["name"] extends "" ? import("drizzle-orm").Assume<Key, string> : C[Key]["_"]["name"];
|
|
344
|
+
})["dataType"];
|
|
345
|
+
columnType: (Omit<C[Key]["_"], "name"> & {
|
|
346
|
+
name: C[Key]["_"]["name"] extends "" ? import("drizzle-orm").Assume<Key, string> : C[Key]["_"]["name"];
|
|
347
|
+
})["columnType"];
|
|
348
|
+
data: Omit<C[Key]["_"], "name"> & {
|
|
349
|
+
name: C[Key]["_"]["name"] extends "" ? import("drizzle-orm").Assume<Key, string> : C[Key]["_"]["name"];
|
|
350
|
+
} extends (infer T) ? T extends Omit<C[Key]["_"], "name"> & {
|
|
351
|
+
name: C[Key]["_"]["name"] extends "" ? import("drizzle-orm").Assume<Key, string> : C[Key]["_"]["name"];
|
|
352
|
+
} ? T extends {
|
|
353
|
+
$type: infer U;
|
|
354
|
+
} ? U : T["data"] : never : never;
|
|
355
|
+
driverParam: (Omit<C[Key]["_"], "name"> & {
|
|
356
|
+
name: C[Key]["_"]["name"] extends "" ? import("drizzle-orm").Assume<Key, string> : C[Key]["_"]["name"];
|
|
357
|
+
})["driverParam"];
|
|
358
|
+
notNull: Omit<C[Key]["_"], "name"> & {
|
|
359
|
+
name: C[Key]["_"]["name"] extends "" ? import("drizzle-orm").Assume<Key, string> : C[Key]["_"]["name"];
|
|
360
|
+
} extends (infer T_1) ? T_1 extends Omit<C[Key]["_"], "name"> & {
|
|
361
|
+
name: C[Key]["_"]["name"] extends "" ? import("drizzle-orm").Assume<Key, string> : C[Key]["_"]["name"];
|
|
362
|
+
} ? T_1 extends {
|
|
363
|
+
notNull: true;
|
|
364
|
+
} ? true : false : never : never;
|
|
365
|
+
hasDefault: Omit<C[Key]["_"], "name"> & {
|
|
366
|
+
name: C[Key]["_"]["name"] extends "" ? import("drizzle-orm").Assume<Key, string> : C[Key]["_"]["name"];
|
|
367
|
+
} extends (infer T_2) ? T_2 extends Omit<C[Key]["_"], "name"> & {
|
|
368
|
+
name: C[Key]["_"]["name"] extends "" ? import("drizzle-orm").Assume<Key, string> : C[Key]["_"]["name"];
|
|
369
|
+
} ? T_2 extends {
|
|
370
|
+
hasDefault: true;
|
|
371
|
+
} ? true : false : never : never;
|
|
372
|
+
isPrimaryKey: Omit<C[Key]["_"], "name"> & {
|
|
373
|
+
name: C[Key]["_"]["name"] extends "" ? import("drizzle-orm").Assume<Key, string> : C[Key]["_"]["name"];
|
|
374
|
+
} extends (infer T_3) ? T_3 extends Omit<C[Key]["_"], "name"> & {
|
|
375
|
+
name: C[Key]["_"]["name"] extends "" ? import("drizzle-orm").Assume<Key, string> : C[Key]["_"]["name"];
|
|
376
|
+
} ? T_3 extends {
|
|
377
|
+
isPrimaryKey: true;
|
|
378
|
+
} ? true : false : never : never;
|
|
379
|
+
isAutoincrement: Omit<C[Key]["_"], "name"> & {
|
|
380
|
+
name: C[Key]["_"]["name"] extends "" ? import("drizzle-orm").Assume<Key, string> : C[Key]["_"]["name"];
|
|
381
|
+
} extends (infer T_4) ? T_4 extends Omit<C[Key]["_"], "name"> & {
|
|
382
|
+
name: C[Key]["_"]["name"] extends "" ? import("drizzle-orm").Assume<Key, string> : C[Key]["_"]["name"];
|
|
383
|
+
} ? T_4 extends {
|
|
384
|
+
isAutoincrement: true;
|
|
385
|
+
} ? true : false : never : never;
|
|
386
|
+
hasRuntimeDefault: Omit<C[Key]["_"], "name"> & {
|
|
387
|
+
name: C[Key]["_"]["name"] extends "" ? import("drizzle-orm").Assume<Key, string> : C[Key]["_"]["name"];
|
|
388
|
+
} extends (infer T_5) ? T_5 extends Omit<C[Key]["_"], "name"> & {
|
|
389
|
+
name: C[Key]["_"]["name"] extends "" ? import("drizzle-orm").Assume<Key, string> : C[Key]["_"]["name"];
|
|
390
|
+
} ? T_5 extends {
|
|
391
|
+
hasRuntimeDefault: true;
|
|
392
|
+
} ? true : false : never : never;
|
|
393
|
+
enumValues: (Omit<C[Key]["_"], "name"> & {
|
|
394
|
+
name: C[Key]["_"]["name"] extends "" ? import("drizzle-orm").Assume<Key, string> : C[Key]["_"]["name"];
|
|
395
|
+
})["enumValues"];
|
|
396
|
+
baseColumn: Omit<C[Key]["_"], "name"> & {
|
|
397
|
+
name: C[Key]["_"]["name"] extends "" ? import("drizzle-orm").Assume<Key, string> : C[Key]["_"]["name"];
|
|
398
|
+
} extends (infer T_6) ? T_6 extends Omit<C[Key]["_"], "name"> & {
|
|
399
|
+
name: C[Key]["_"]["name"] extends "" ? import("drizzle-orm").Assume<Key, string> : C[Key]["_"]["name"];
|
|
400
|
+
} ? T_6 extends {
|
|
401
|
+
baseBuilder: infer U_1 extends import("drizzle-orm").ColumnBuilderBase;
|
|
402
|
+
} ? import("drizzle-orm").Column<{
|
|
403
|
+
name: U_1["_"]["name"];
|
|
404
|
+
tableName: string;
|
|
405
|
+
dataType: U_1["_"]["dataType"];
|
|
406
|
+
columnType: U_1["_"]["columnType"];
|
|
407
|
+
data: U_1["_"] extends (infer T_7) ? T_7 extends U_1["_"] ? T_7 extends {
|
|
408
|
+
$type: infer U;
|
|
409
|
+
} ? U : T_7["data"] : never : never;
|
|
410
|
+
driverParam: U_1["_"]["driverParam"];
|
|
411
|
+
notNull: U_1["_"] extends (infer T_8) ? T_8 extends U_1["_"] ? T_8 extends {
|
|
412
|
+
notNull: true;
|
|
413
|
+
} ? true : false : never : never;
|
|
414
|
+
hasDefault: U_1["_"] extends (infer T_9) ? T_9 extends U_1["_"] ? T_9 extends {
|
|
415
|
+
hasDefault: true;
|
|
416
|
+
} ? true : false : never : never;
|
|
417
|
+
isPrimaryKey: U_1["_"] extends (infer T_10) ? T_10 extends U_1["_"] ? T_10 extends {
|
|
418
|
+
isPrimaryKey: true;
|
|
419
|
+
} ? true : false : never : never;
|
|
420
|
+
isAutoincrement: U_1["_"] extends (infer T_11) ? T_11 extends U_1["_"] ? T_11 extends {
|
|
421
|
+
isAutoincrement: true;
|
|
422
|
+
} ? true : false : never : never;
|
|
423
|
+
hasRuntimeDefault: U_1["_"] extends (infer T_12) ? T_12 extends U_1["_"] ? T_12 extends {
|
|
424
|
+
hasRuntimeDefault: true;
|
|
425
|
+
} ? true : false : never : never;
|
|
426
|
+
enumValues: U_1["_"]["enumValues"];
|
|
427
|
+
baseColumn: U_1["_"] extends (infer T_13) ? T_13 extends U_1["_"] ? T_13 extends {
|
|
428
|
+
baseBuilder: infer U_1 extends import("drizzle-orm").ColumnBuilderBase;
|
|
429
|
+
} ? import("drizzle-orm").Column< /*elided*/ any, {}, Omit<U_1["_"], "baseColumn" | "brand" | "columnType" | "data" | "dataType" | "dialect" | "driverParam" | "enumValues" | "generated" | "hasDefault" | "hasRuntimeDefault" | "identity" | "isAutoincrement" | "isPrimaryKey" | "name" | "notNull" | "tableName"> extends (infer T_14) ? { [K in keyof T_14]: T_14[K]; } : never> : never : never : never;
|
|
430
|
+
identity: U_1["_"] extends (infer T_15) ? T_15 extends U_1["_"] ? T_15 extends {
|
|
431
|
+
identity: 'always';
|
|
432
|
+
} ? "always" : T_15 extends {
|
|
433
|
+
identity: 'byDefault';
|
|
434
|
+
} ? "byDefault" : undefined : never : never;
|
|
435
|
+
generated: U_1["_"] extends (infer T_16) ? T_16 extends U_1["_"] ? T_16 extends {
|
|
436
|
+
generated: infer G;
|
|
437
|
+
} ? unknown extends G ? undefined : G extends undefined ? undefined : G : undefined : never : never;
|
|
438
|
+
}, {}, Omit<U_1["_"], "baseColumn" | "brand" | "columnType" | "data" | "dataType" | "dialect" | "driverParam" | "enumValues" | "generated" | "hasDefault" | "hasRuntimeDefault" | "identity" | "isAutoincrement" | "isPrimaryKey" | "name" | "notNull" | "tableName"> extends (infer T_14) ? { [K in keyof T_14]: T_14[K]; } : never> : never : never : never;
|
|
439
|
+
identity: Omit<C[Key]["_"], "name"> & {
|
|
440
|
+
name: C[Key]["_"]["name"] extends "" ? import("drizzle-orm").Assume<Key, string> : C[Key]["_"]["name"];
|
|
441
|
+
} extends (infer T_17) ? T_17 extends Omit<C[Key]["_"], "name"> & {
|
|
442
|
+
name: C[Key]["_"]["name"] extends "" ? import("drizzle-orm").Assume<Key, string> : C[Key]["_"]["name"];
|
|
443
|
+
} ? T_17 extends {
|
|
444
|
+
identity: 'always';
|
|
445
|
+
} ? "always" : T_17 extends {
|
|
446
|
+
identity: 'byDefault';
|
|
447
|
+
} ? "byDefault" : undefined : never : never;
|
|
448
|
+
generated: Omit<C[Key]["_"], "name"> & {
|
|
449
|
+
name: C[Key]["_"]["name"] extends "" ? import("drizzle-orm").Assume<Key, string> : C[Key]["_"]["name"];
|
|
450
|
+
} extends (infer T_18) ? T_18 extends Omit<C[Key]["_"], "name"> & {
|
|
451
|
+
name: C[Key]["_"]["name"] extends "" ? import("drizzle-orm").Assume<Key, string> : C[Key]["_"]["name"];
|
|
452
|
+
} ? T_18 extends {
|
|
453
|
+
generated: infer G;
|
|
454
|
+
} ? unknown extends G ? undefined : G extends undefined ? undefined : G : undefined : never : never;
|
|
455
|
+
}, {}, Omit<Omit<C[Key]["_"], "name"> & {
|
|
456
|
+
name: C[Key]["_"]["name"] extends "" ? import("drizzle-orm").Assume<Key, string> : C[Key]["_"]["name"];
|
|
457
|
+
}, "baseColumn" | "brand" | "columnType" | "data" | "dataType" | "dialect" | "driverParam" | "enumValues" | "generated" | "hasDefault" | "hasRuntimeDefault" | "identity" | "isAutoincrement" | "isPrimaryKey" | "name" | "notNull" | "tableName"> extends (infer T_19) ? { [K in keyof T_19]: T_19[K]; } : never>; };
|
|
458
|
+
dialect: 'sqlite';
|
|
459
|
+
}>;
|
|
460
|
+
text: (name: string) => import("drizzle-orm/sqlite-core").SQLiteTextBuilderInitial<string, [string, ...string[]], number | undefined>;
|
|
461
|
+
integer: (name: string) => import("drizzle-orm/sqlite-core").SQLiteIntegerBuilderInitial<string>;
|
|
462
|
+
real: (name: string) => import("drizzle-orm/sqlite-core").SQLiteRealBuilderInitial<string>;
|
|
463
|
+
boolean: (name: string) => import("drizzle-orm/sqlite-core").SQLiteBooleanBuilderInitial<string>;
|
|
464
|
+
/** A `Date`, stored as epoch milliseconds or `timestamptz`. */
|
|
465
|
+
timestamp: (name: string) => import("drizzle-orm/sqlite-core").SQLiteTimestampBuilderInitial<string>;
|
|
466
|
+
json: (name: string) => import("drizzle-orm/sqlite-core").SQLiteTextJsonBuilderInitial<string>;
|
|
467
|
+
};
|
|
468
|
+
/**
|
|
469
|
+
* Engine-neutral column builders for `tables`. Brick passes the SQLite or
|
|
470
|
+
* Postgres ones to match the host database; the types follow SQLite's, and the
|
|
471
|
+
* JavaScript values are the same on both (a `Date` for `timestamp`).
|
|
472
|
+
*/
|
|
473
|
+
type PluginTableBuilders = typeof sqliteBuilders & {
|
|
474
|
+
/** The host database's engine. */
|
|
475
|
+
readonly engine: "sqlite" | "postgres";
|
|
476
|
+
};
|
|
477
|
+
/**
|
|
478
|
+
* Type-safe secrets & environment variables.
|
|
479
|
+
*
|
|
480
|
+
* Declare each secret once at module scope instead of scattering untyped
|
|
481
|
+
* `process.env.FOO` reads:
|
|
482
|
+
*
|
|
483
|
+
* const port = defineSecret("PORT").default("3333").transform(Number).require();
|
|
484
|
+
* const baseUrl = defineSecret("BASE_URL").url().require();
|
|
485
|
+
* const key = defineSecret("STRIPE_KEY").validate((v) =>
|
|
486
|
+
* v.startsWith("sk_") || "must start with sk_"
|
|
487
|
+
* ).require();
|
|
488
|
+
*
|
|
489
|
+
* Values resolve from the environment (Bun populates `process.env` from
|
|
490
|
+
* `.env` locally; in prod the same env source applies — a future
|
|
491
|
+
* `SecretSource` such as AWS can slot in without touching call sites).
|
|
492
|
+
*
|
|
493
|
+
* Resolution order per secret: test/session override → source (`process.env`
|
|
494
|
+
* by default) → declared `default`. `resolveSecrets()` validates every
|
|
495
|
+
* registered secret at boot and throws listing all missing required ones.
|
|
496
|
+
*/
|
|
497
|
+
interface SecretSource {
|
|
498
|
+
get(name: string): string | undefined;
|
|
499
|
+
}
|
|
500
|
+
/** Default source: `process.env` (Bun loads `.env` into it automatically). */
|
|
501
|
+
declare const envSource: SecretSource;
|
|
502
|
+
interface SecretOptions {
|
|
503
|
+
/** Fallback when the source has no value. Secrets without a default are required. */
|
|
504
|
+
default?: string;
|
|
505
|
+
}
|
|
506
|
+
/** A validator: return `true`/`void` when ok, `false`/reason/`throw` when not. */
|
|
507
|
+
type SecretValidator<T> = (value: T) => void | boolean | string;
|
|
508
|
+
declare class SecretRef<T = string> {
|
|
509
|
+
readonly name: string;
|
|
510
|
+
readonly options: SecretOptions;
|
|
511
|
+
private transformFn?;
|
|
512
|
+
private validators;
|
|
513
|
+
constructor(name: string, options?: SecretOptions);
|
|
514
|
+
/** True when a default was declared (i.e. the secret is optional). */
|
|
515
|
+
get hasDefault(): boolean;
|
|
516
|
+
/** Fallback used when the source has no value. Makes the secret optional. */
|
|
517
|
+
default(value: string): this;
|
|
518
|
+
/**
|
|
519
|
+
* Convert the raw string (e.g. `Number`, `JSON.parse`, `v === "true"`).
|
|
520
|
+
* Runs before validators; a throwing transform fails resolution.
|
|
521
|
+
*/
|
|
522
|
+
transform<U>(fn: (raw: string) => U): SecretRef<U>;
|
|
523
|
+
/** Run a custom check on the (transformed) value at every resolution. */
|
|
524
|
+
validate(fn: SecretValidator<T>): this;
|
|
525
|
+
/** Shorthand validator: value must parse as a URL. String refs only. */
|
|
526
|
+
url(this: SecretRef<string>): SecretRef<string>;
|
|
527
|
+
private resolveRaw;
|
|
528
|
+
private coerce;
|
|
529
|
+
/** Resolved (transformed, validated) value or `undefined`. Never throws on missing. */
|
|
530
|
+
value(source?: SecretSource): T | undefined;
|
|
531
|
+
/** Resolved value, or throws naming the missing/invalid secret. */
|
|
532
|
+
require(source?: SecretSource): T;
|
|
533
|
+
/** True when a value would resolve (override, source, or default). */
|
|
534
|
+
isSet(source?: SecretSource): boolean;
|
|
535
|
+
}
|
|
536
|
+
/**
|
|
537
|
+
* Declare a secret. Calling it again for the name returns the same ref. From
|
|
538
|
+
* the same line (a module evaluated again) it starts the declaration over, so
|
|
539
|
+
* its builder calls replace the earlier ones instead of adding to them.
|
|
540
|
+
*/
|
|
541
|
+
declare function defineSecret(name: string, options?: SecretOptions): SecretRef<string>;
|
|
542
|
+
/**
|
|
543
|
+
* Validate every registered secret at boot. Throws a single error listing
|
|
544
|
+
* all missing required secrets. Returns the resolved values.
|
|
545
|
+
*/
|
|
546
|
+
declare function resolveSecrets(opts?: {
|
|
547
|
+
source?: SecretSource;
|
|
548
|
+
}): Record<string, unknown>;
|
|
549
|
+
/** Metadata for introspection (values never exposed — future dashboard use). */
|
|
550
|
+
declare function listSecrets(opts?: {
|
|
551
|
+
source?: SecretSource;
|
|
552
|
+
}): Array<{
|
|
553
|
+
name: string;
|
|
554
|
+
hasDefault: boolean;
|
|
555
|
+
isSet: boolean;
|
|
556
|
+
}>;
|
|
557
|
+
/** Test/session override for one secret (`undefined` clears the override). */
|
|
558
|
+
declare function overrideSecret(name: string, value: string | undefined): void;
|
|
559
|
+
/** Clear registry and overrides. Test-only. */
|
|
560
|
+
declare function resetSecrets(): void;
|
|
561
|
+
type Secrets = Record<string, SecretRef<any>>;
|
|
562
|
+
type OptionsOf<O> = O extends TSchema ? Static<O> : {};
|
|
563
|
+
/**
|
|
564
|
+
* The context of a plugin's actions, endpoints and hooks: the host's request
|
|
565
|
+
* context, loosely typed, with `deps` set to the plugin's own declared deps.
|
|
566
|
+
*/
|
|
567
|
+
type PluginContext<P extends Ports = {}> = BaseContext<any> & PluginServiceContext<P>;
|
|
568
|
+
/** A plugin's view of its host's service context: loose, except `deps`, which is the plugin's. */
|
|
569
|
+
type PluginServiceContext<P extends Ports = {}> = {
|
|
570
|
+
[key: string]: any;
|
|
571
|
+
/** The values passed for the plugin's `deps` when it was installed. */
|
|
572
|
+
deps: DepsOf<P>;
|
|
573
|
+
};
|
|
574
|
+
/** What every setup callback receives. */
|
|
575
|
+
interface PluginSetupContext<O, S, T, E extends EventCatalog, P extends Ports = {}> {
|
|
576
|
+
/** The validated options, defaults filled in. */
|
|
577
|
+
options: O;
|
|
578
|
+
/** The declared secrets, to read when they are needed. */
|
|
579
|
+
secrets: S;
|
|
580
|
+
/** The tables this plugin owns, by the keys `tables` returned. */
|
|
581
|
+
tables: T;
|
|
582
|
+
/** The plugin's events, as `emitEvent` and `defineListener` take them. */
|
|
583
|
+
events: EventRefs<E>;
|
|
584
|
+
/** The values passed for `deps` when the plugin was installed. */
|
|
585
|
+
deps: DepsOf<P>;
|
|
586
|
+
/** The host service. Actions registered on it read the plugin's deps as `ctx.deps`. */
|
|
587
|
+
service: Service<any, PluginServiceContext<P>>;
|
|
588
|
+
routes: PluginRouteRegistrar<PluginContext<P>>;
|
|
589
|
+
/** The host's CRUD writes: on a service, those of all its resources. */
|
|
590
|
+
hooks: PluginHooks<PluginContext<P>>;
|
|
591
|
+
}
|
|
592
|
+
/** The lifecycle context of `on("start")` and `on("stop")`. */
|
|
593
|
+
interface PluginLifecycleContext<O, S, P extends Ports = {}> {
|
|
594
|
+
options: O;
|
|
595
|
+
secrets: S;
|
|
596
|
+
deps: DepsOf<P>;
|
|
597
|
+
}
|
|
598
|
+
/** What `app` receives: the app-wide part of a plugin. */
|
|
599
|
+
interface PluginAppContext<O, S, P extends Ports = {}> {
|
|
600
|
+
options: O;
|
|
601
|
+
secrets: S;
|
|
602
|
+
/** The values passed for `deps` when the plugin was installed. */
|
|
603
|
+
deps: DepsOf<P>;
|
|
604
|
+
/**
|
|
605
|
+
* Run when the app starts (`app.start()`, which `listen`, `brick dev` and
|
|
606
|
+
* `brick start` call) in install order, and when it stops in reverse order.
|
|
607
|
+
*/
|
|
608
|
+
on(event: "start" | "stop", handler: (context: PluginLifecycleContext<O, S, P>) => void | Promise<void>): void;
|
|
609
|
+
/**
|
|
610
|
+
* Run before every request is routed. Return a `Response` (or any value) to
|
|
611
|
+
* answer it here; return nothing to let it through.
|
|
612
|
+
*/
|
|
613
|
+
middleware(handler: (context: {
|
|
614
|
+
request: Request;
|
|
615
|
+
set: {
|
|
616
|
+
status?: number | string;
|
|
617
|
+
headers: Record<string, string>;
|
|
618
|
+
};
|
|
619
|
+
}) => unknown): void;
|
|
620
|
+
/**
|
|
621
|
+
* Event listeners to add to the app's `events`. `useDeps()` in their `run`,
|
|
622
|
+
* and in the steps of their `workflow`, returns the plugin's deps.
|
|
623
|
+
*/
|
|
624
|
+
listeners(...listeners: EventListener[]): void;
|
|
625
|
+
}
|
|
626
|
+
interface PluginManifest<O extends TSchema | undefined = undefined, S extends Secrets = {}, T extends Record<string, Table> = {}, E extends EventCatalog = {}, RA extends object = {}, SA = unknown, P extends Ports = {}> {
|
|
627
|
+
/** The npm package name, scoped: `@acme/brick-comments`. */
|
|
628
|
+
name: string;
|
|
629
|
+
description?: string;
|
|
630
|
+
/** The Brick versions it supports, as a semver range: `^0.12.0`. */
|
|
631
|
+
brick?: string;
|
|
632
|
+
/** Plugins that must be installed on the same host first, by name. */
|
|
633
|
+
requires?: readonly string[];
|
|
634
|
+
/** A TypeBox schema for the options; defaults fill what the user leaves out. */
|
|
635
|
+
options?: O;
|
|
636
|
+
/** Secrets it reads, from `defineSecret`. */
|
|
637
|
+
secrets?: S;
|
|
638
|
+
/**
|
|
639
|
+
* What it needs from the app, declared with `port<T>()`. The user passes the
|
|
640
|
+
* values when installing: `notify(options, { deps: { mailer } })`. Setup
|
|
641
|
+
* parts read them as `deps`, the plugin's actions as `ctx.deps`.
|
|
642
|
+
*/
|
|
643
|
+
deps?: P;
|
|
644
|
+
/**
|
|
645
|
+
* Tables it owns. They are created in the host service's database for its
|
|
646
|
+
* engine, named `<host>_<table>` (or with the install's `prefix`), and join
|
|
647
|
+
* schema sync and migrations through `database.tables`.
|
|
648
|
+
*/
|
|
649
|
+
tables?: (builders: PluginTableBuilders) => T;
|
|
650
|
+
/** Events it emits; they join the host catalog as `<plugin>.<event>`. */
|
|
651
|
+
events?: E;
|
|
652
|
+
/** Installed with `resource.use()`; returns what the resource gains. */
|
|
653
|
+
resource?: (context: PluginSetupContext<OptionsOf<O>, S, T, E, P> & {
|
|
654
|
+
resource: Resource<any, PluginContext<P>, any>;
|
|
655
|
+
}) => RA & (RA extends PromiseLike<unknown> ? never : unknown);
|
|
656
|
+
/** Installed with `service.use()`; returns what `use` hands back. */
|
|
657
|
+
service?: (context: PluginSetupContext<OptionsOf<O>, S, T, E, P>) => SA & (SA extends PromiseLike<unknown> ? never : unknown);
|
|
658
|
+
/**
|
|
659
|
+
* The app-wide part: lifecycle, middleware and listeners. Runs once per
|
|
660
|
+
* plugin instance when `brick()` creates the app, whether the plugin was
|
|
661
|
+
* installed with `brick({ plugins })`, `service.use()` or `resource.use()`.
|
|
662
|
+
*/
|
|
663
|
+
app?: (context: PluginAppContext<OptionsOf<O>, S, P>) => void;
|
|
664
|
+
/**
|
|
665
|
+
* A dashboard extension module (`defineDashboard`), relative to the file
|
|
666
|
+
* that calls `definePlugin`. `brick dev` merges it with the app's own.
|
|
667
|
+
*/
|
|
668
|
+
dashboard?: string;
|
|
669
|
+
}
|
|
670
|
+
/** Where the plugin is installed, besides its options. */
|
|
671
|
+
interface PluginInstallOptions {
|
|
672
|
+
/** Table name prefix instead of `<host>_`, such as `"blog_"`; `""` for none. */
|
|
673
|
+
prefix?: string;
|
|
674
|
+
}
|
|
675
|
+
/** What the plugin's capabilities are, read from its manifest. */
|
|
676
|
+
interface PluginInfo {
|
|
677
|
+
readonly name: string;
|
|
678
|
+
readonly description?: string;
|
|
679
|
+
readonly brick?: string;
|
|
680
|
+
readonly requires: readonly string[];
|
|
681
|
+
readonly options?: TSchema;
|
|
682
|
+
readonly secrets: readonly string[];
|
|
683
|
+
/** The names of the deps it declares, passed when installing. */
|
|
684
|
+
readonly deps: readonly string[];
|
|
685
|
+
readonly capabilities: readonly ("tables" | "events" | "resource" | "service" | "app" | "dashboard")[];
|
|
686
|
+
}
|
|
687
|
+
/** A plugin ready to install: `resource.use(p)`, `service.use(p)` or `brick({ plugins: [p] })`. */
|
|
688
|
+
type PluginInstance<RA extends object = {}, SA = unknown> = ResourcePlugin<any, any, RA> & ServicePlugin<any, SA> & {
|
|
689
|
+
readonly info: PluginInfo;
|
|
690
|
+
};
|
|
691
|
+
/** `definePlugin`'s result: call it with options to get an instance. */
|
|
692
|
+
type PluginFactory<O extends TSchema | undefined, RA extends object, SA, P extends Ports = {}> = ((...args: {} extends P ? [options?: Partial<OptionsOf<O>>, install?: PluginInstallOptions] : [options: Partial<OptionsOf<O>> | undefined, install: PluginInstallOptions & {
|
|
693
|
+
deps: DepsOf<P>;
|
|
694
|
+
}]) => PluginInstance<RA, SA>) & {
|
|
695
|
+
readonly info: PluginInfo;
|
|
696
|
+
};
|
|
697
|
+
/**
|
|
698
|
+
* Define a plugin: a manifest Brick checks, plus the capabilities the plugin
|
|
699
|
+
* brings. Install its instances with `resource.use()`, `service.use()` or
|
|
700
|
+
* `brick({ plugins })`; scope comes from where it is installed.
|
|
701
|
+
*
|
|
702
|
+
* @param manifest - `name`, `brick` (semver range), `requires`, `options`
|
|
703
|
+
* (TypeBox), `secrets`, `tables`, `events`, the `resource`, `service` and `app`
|
|
704
|
+
* parts, and a `dashboard` extension.
|
|
705
|
+
* @returns A factory: `comments({ maxLength: 500 })` validates the options and
|
|
706
|
+
* the Brick version and returns an instance to install.
|
|
707
|
+
* @throws From the factory, when the running Brick is outside `brick` or an
|
|
708
|
+
* option is invalid; when installed, when a required plugin is missing or the
|
|
709
|
+
* host does not suit the plugin.
|
|
710
|
+
* @example
|
|
711
|
+
* export const audit = definePlugin({
|
|
712
|
+
* name: "@acme/brick-audit",
|
|
713
|
+
* brick: "^0.12.0",
|
|
714
|
+
* requires: ["@brickkit/crud"],
|
|
715
|
+
* options: t.Object({ table: t.String({ default: "audit" }) }),
|
|
716
|
+
* tables: ({ table, text, timestamp }) => ({
|
|
717
|
+
* log: table("log", { id: text("id").primaryKey(), at: timestamp("at").notNull() }),
|
|
718
|
+
* }),
|
|
719
|
+
* resource: ({ tables, hooks }) => {
|
|
720
|
+
* hooks.afterCreate(({ row, tx }) =>
|
|
721
|
+
* tx.insert(tables.log).values({ id: String(row.id), at: new Date() }));
|
|
722
|
+
* return {};
|
|
723
|
+
* },
|
|
724
|
+
* });
|
|
725
|
+
*
|
|
726
|
+
* page.use(audit());
|
|
727
|
+
*/
|
|
728
|
+
declare function definePlugin<O extends TSchema | undefined = undefined, S extends Secrets = {}, T extends Record<string, Table> = {}, const E extends EventCatalog = {}, RA extends object = {}, SA = unknown, P extends Ports = {}>(manifest: PluginManifest<O, S, T, E, RA, SA, P>): PluginFactory<O, RA, SA, P>;
|
|
729
|
+
declare const OUTBOX_STATUSES: readonly ["pending", "claimed", "delivered", "dead"];
|
|
730
|
+
/** What the outbox rows are in, by state. */
|
|
731
|
+
type OutboxStatus = (typeof OUTBOX_STATUSES)[number];
|
|
732
|
+
declare function sqliteOutbox(name: string): import("drizzle-orm/sqlite-core").SQLiteTableWithColumns<{
|
|
733
|
+
name: string;
|
|
734
|
+
schema: undefined;
|
|
735
|
+
columns: {
|
|
736
|
+
seq: import("drizzle-orm/sqlite-core").SQLiteColumn<{
|
|
737
|
+
name: "seq";
|
|
738
|
+
tableName: string;
|
|
739
|
+
dataType: "number";
|
|
740
|
+
columnType: "SQLiteInteger";
|
|
741
|
+
data: number;
|
|
742
|
+
driverParam: number;
|
|
743
|
+
notNull: true;
|
|
744
|
+
hasDefault: true;
|
|
745
|
+
isPrimaryKey: true;
|
|
746
|
+
isAutoincrement: false;
|
|
747
|
+
hasRuntimeDefault: false;
|
|
748
|
+
enumValues: undefined;
|
|
749
|
+
baseColumn: never;
|
|
750
|
+
identity: undefined;
|
|
751
|
+
generated: undefined;
|
|
752
|
+
}, {}, {}>;
|
|
753
|
+
id: import("drizzle-orm/sqlite-core").SQLiteColumn<{
|
|
754
|
+
name: "id";
|
|
755
|
+
tableName: string;
|
|
756
|
+
dataType: "string";
|
|
757
|
+
columnType: "SQLiteText";
|
|
758
|
+
data: string;
|
|
759
|
+
driverParam: string;
|
|
760
|
+
notNull: true;
|
|
761
|
+
hasDefault: false;
|
|
762
|
+
isPrimaryKey: false;
|
|
763
|
+
isAutoincrement: false;
|
|
764
|
+
hasRuntimeDefault: false;
|
|
765
|
+
enumValues: [string, ...string[]];
|
|
766
|
+
baseColumn: never;
|
|
767
|
+
identity: undefined;
|
|
768
|
+
generated: undefined;
|
|
769
|
+
}, {}, {
|
|
770
|
+
length: number | undefined;
|
|
771
|
+
}>;
|
|
772
|
+
eventId: import("drizzle-orm/sqlite-core").SQLiteColumn<{
|
|
773
|
+
name: "event_id";
|
|
774
|
+
tableName: string;
|
|
775
|
+
dataType: "string";
|
|
776
|
+
columnType: "SQLiteText";
|
|
777
|
+
data: string;
|
|
778
|
+
driverParam: string;
|
|
779
|
+
notNull: true;
|
|
780
|
+
hasDefault: false;
|
|
781
|
+
isPrimaryKey: false;
|
|
782
|
+
isAutoincrement: false;
|
|
783
|
+
hasRuntimeDefault: false;
|
|
784
|
+
enumValues: [string, ...string[]];
|
|
785
|
+
baseColumn: never;
|
|
786
|
+
identity: undefined;
|
|
787
|
+
generated: undefined;
|
|
788
|
+
}, {}, {
|
|
789
|
+
length: number | undefined;
|
|
790
|
+
}>;
|
|
791
|
+
listener: import("drizzle-orm/sqlite-core").SQLiteColumn<{
|
|
792
|
+
name: "listener";
|
|
793
|
+
tableName: string;
|
|
794
|
+
dataType: "string";
|
|
795
|
+
columnType: "SQLiteText";
|
|
796
|
+
data: string;
|
|
797
|
+
driverParam: string;
|
|
798
|
+
notNull: true;
|
|
799
|
+
hasDefault: false;
|
|
800
|
+
isPrimaryKey: false;
|
|
801
|
+
isAutoincrement: false;
|
|
802
|
+
hasRuntimeDefault: false;
|
|
803
|
+
enumValues: [string, ...string[]];
|
|
804
|
+
baseColumn: never;
|
|
805
|
+
identity: undefined;
|
|
806
|
+
generated: undefined;
|
|
807
|
+
}, {}, {
|
|
808
|
+
length: number | undefined;
|
|
809
|
+
}>;
|
|
810
|
+
name: import("drizzle-orm/sqlite-core").SQLiteColumn<{
|
|
811
|
+
name: "name";
|
|
812
|
+
tableName: string;
|
|
813
|
+
dataType: "string";
|
|
814
|
+
columnType: "SQLiteText";
|
|
815
|
+
data: string;
|
|
816
|
+
driverParam: string;
|
|
817
|
+
notNull: true;
|
|
818
|
+
hasDefault: false;
|
|
819
|
+
isPrimaryKey: false;
|
|
820
|
+
isAutoincrement: false;
|
|
821
|
+
hasRuntimeDefault: false;
|
|
822
|
+
enumValues: [string, ...string[]];
|
|
823
|
+
baseColumn: never;
|
|
824
|
+
identity: undefined;
|
|
825
|
+
generated: undefined;
|
|
826
|
+
}, {}, {
|
|
827
|
+
length: number | undefined;
|
|
828
|
+
}>;
|
|
829
|
+
payload: import("drizzle-orm/sqlite-core").SQLiteColumn<{
|
|
830
|
+
name: "payload";
|
|
831
|
+
tableName: string;
|
|
832
|
+
dataType: "json";
|
|
833
|
+
columnType: "SQLiteTextJson";
|
|
834
|
+
data: unknown;
|
|
835
|
+
driverParam: string;
|
|
836
|
+
notNull: true;
|
|
837
|
+
hasDefault: false;
|
|
838
|
+
isPrimaryKey: false;
|
|
839
|
+
isAutoincrement: false;
|
|
840
|
+
hasRuntimeDefault: false;
|
|
841
|
+
enumValues: undefined;
|
|
842
|
+
baseColumn: never;
|
|
843
|
+
identity: undefined;
|
|
844
|
+
generated: undefined;
|
|
845
|
+
}, {}, {}>;
|
|
846
|
+
traceId: import("drizzle-orm/sqlite-core").SQLiteColumn<{
|
|
847
|
+
name: "trace_id";
|
|
848
|
+
tableName: string;
|
|
849
|
+
dataType: "string";
|
|
850
|
+
columnType: "SQLiteText";
|
|
851
|
+
data: string;
|
|
852
|
+
driverParam: string;
|
|
853
|
+
notNull: false;
|
|
854
|
+
hasDefault: false;
|
|
855
|
+
isPrimaryKey: false;
|
|
856
|
+
isAutoincrement: false;
|
|
857
|
+
hasRuntimeDefault: false;
|
|
858
|
+
enumValues: [string, ...string[]];
|
|
859
|
+
baseColumn: never;
|
|
860
|
+
identity: undefined;
|
|
861
|
+
generated: undefined;
|
|
862
|
+
}, {}, {
|
|
863
|
+
length: number | undefined;
|
|
864
|
+
}>;
|
|
865
|
+
source: import("drizzle-orm/sqlite-core").SQLiteColumn<{
|
|
866
|
+
name: "source";
|
|
867
|
+
tableName: string;
|
|
868
|
+
dataType: "string";
|
|
869
|
+
columnType: "SQLiteText";
|
|
870
|
+
data: string;
|
|
871
|
+
driverParam: string;
|
|
872
|
+
notNull: false;
|
|
873
|
+
hasDefault: false;
|
|
874
|
+
isPrimaryKey: false;
|
|
875
|
+
isAutoincrement: false;
|
|
876
|
+
hasRuntimeDefault: false;
|
|
877
|
+
enumValues: [string, ...string[]];
|
|
878
|
+
baseColumn: never;
|
|
879
|
+
identity: undefined;
|
|
880
|
+
generated: undefined;
|
|
881
|
+
}, {}, {
|
|
882
|
+
length: number | undefined;
|
|
883
|
+
}>;
|
|
884
|
+
occurredAt: import("drizzle-orm/sqlite-core").SQLiteColumn<{
|
|
885
|
+
name: string;
|
|
886
|
+
tableName: string;
|
|
887
|
+
dataType: "date";
|
|
888
|
+
columnType: "SQLiteTimestamp";
|
|
889
|
+
data: Date;
|
|
890
|
+
driverParam: number;
|
|
891
|
+
notNull: true;
|
|
892
|
+
hasDefault: false;
|
|
893
|
+
isPrimaryKey: false;
|
|
894
|
+
isAutoincrement: false;
|
|
895
|
+
hasRuntimeDefault: false;
|
|
896
|
+
enumValues: undefined;
|
|
897
|
+
baseColumn: never;
|
|
898
|
+
identity: undefined;
|
|
899
|
+
generated: undefined;
|
|
900
|
+
}, {}, {}>;
|
|
901
|
+
status: import("drizzle-orm/sqlite-core").SQLiteColumn<{
|
|
902
|
+
name: "status";
|
|
903
|
+
tableName: string;
|
|
904
|
+
dataType: "string";
|
|
905
|
+
columnType: "SQLiteText";
|
|
906
|
+
data: "claimed" | "dead" | "delivered" | "pending";
|
|
907
|
+
driverParam: string;
|
|
908
|
+
notNull: true;
|
|
909
|
+
hasDefault: false;
|
|
910
|
+
isPrimaryKey: false;
|
|
911
|
+
isAutoincrement: false;
|
|
912
|
+
hasRuntimeDefault: false;
|
|
913
|
+
enumValues: [string, ...string[]];
|
|
914
|
+
baseColumn: never;
|
|
915
|
+
identity: undefined;
|
|
916
|
+
generated: undefined;
|
|
917
|
+
}, {}, {
|
|
918
|
+
length: number | undefined;
|
|
919
|
+
$type: "claimed" | "dead" | "delivered" | "pending";
|
|
920
|
+
}>;
|
|
921
|
+
attempts: import("drizzle-orm/sqlite-core").SQLiteColumn<{
|
|
922
|
+
name: "attempts";
|
|
923
|
+
tableName: string;
|
|
924
|
+
dataType: "number";
|
|
925
|
+
columnType: "SQLiteInteger";
|
|
926
|
+
data: number;
|
|
927
|
+
driverParam: number;
|
|
928
|
+
notNull: true;
|
|
929
|
+
hasDefault: true;
|
|
930
|
+
isPrimaryKey: false;
|
|
931
|
+
isAutoincrement: false;
|
|
932
|
+
hasRuntimeDefault: false;
|
|
933
|
+
enumValues: undefined;
|
|
934
|
+
baseColumn: never;
|
|
935
|
+
identity: undefined;
|
|
936
|
+
generated: undefined;
|
|
937
|
+
}, {}, {}>;
|
|
938
|
+
lastError: import("drizzle-orm/sqlite-core").SQLiteColumn<{
|
|
939
|
+
name: "last_error";
|
|
940
|
+
tableName: string;
|
|
941
|
+
dataType: "string";
|
|
942
|
+
columnType: "SQLiteText";
|
|
943
|
+
data: string;
|
|
944
|
+
driverParam: string;
|
|
945
|
+
notNull: false;
|
|
946
|
+
hasDefault: false;
|
|
947
|
+
isPrimaryKey: false;
|
|
948
|
+
isAutoincrement: false;
|
|
949
|
+
hasRuntimeDefault: false;
|
|
950
|
+
enumValues: [string, ...string[]];
|
|
951
|
+
baseColumn: never;
|
|
952
|
+
identity: undefined;
|
|
953
|
+
generated: undefined;
|
|
954
|
+
}, {}, {
|
|
955
|
+
length: number | undefined;
|
|
956
|
+
}>;
|
|
957
|
+
availableAt: import("drizzle-orm/sqlite-core").SQLiteColumn<{
|
|
958
|
+
name: string;
|
|
959
|
+
tableName: string;
|
|
960
|
+
dataType: "date";
|
|
961
|
+
columnType: "SQLiteTimestamp";
|
|
962
|
+
data: Date;
|
|
963
|
+
driverParam: number;
|
|
964
|
+
notNull: true;
|
|
965
|
+
hasDefault: false;
|
|
966
|
+
isPrimaryKey: false;
|
|
967
|
+
isAutoincrement: false;
|
|
968
|
+
hasRuntimeDefault: false;
|
|
969
|
+
enumValues: undefined;
|
|
970
|
+
baseColumn: never;
|
|
971
|
+
identity: undefined;
|
|
972
|
+
generated: undefined;
|
|
973
|
+
}, {}, {}>;
|
|
974
|
+
claimedAt: import("drizzle-orm/sqlite-core").SQLiteColumn<{
|
|
975
|
+
name: string;
|
|
976
|
+
tableName: string;
|
|
977
|
+
dataType: "date";
|
|
978
|
+
columnType: "SQLiteTimestamp";
|
|
979
|
+
data: Date;
|
|
980
|
+
driverParam: number;
|
|
981
|
+
notNull: false;
|
|
982
|
+
hasDefault: false;
|
|
983
|
+
isPrimaryKey: false;
|
|
984
|
+
isAutoincrement: false;
|
|
985
|
+
hasRuntimeDefault: false;
|
|
986
|
+
enumValues: undefined;
|
|
987
|
+
baseColumn: never;
|
|
988
|
+
identity: undefined;
|
|
989
|
+
generated: undefined;
|
|
990
|
+
}, {}, {}>;
|
|
991
|
+
deliveredAt: import("drizzle-orm/sqlite-core").SQLiteColumn<{
|
|
992
|
+
name: string;
|
|
993
|
+
tableName: string;
|
|
994
|
+
dataType: "date";
|
|
995
|
+
columnType: "SQLiteTimestamp";
|
|
996
|
+
data: Date;
|
|
997
|
+
driverParam: number;
|
|
998
|
+
notNull: false;
|
|
999
|
+
hasDefault: false;
|
|
1000
|
+
isPrimaryKey: false;
|
|
1001
|
+
isAutoincrement: false;
|
|
1002
|
+
hasRuntimeDefault: false;
|
|
1003
|
+
enumValues: undefined;
|
|
1004
|
+
baseColumn: never;
|
|
1005
|
+
identity: undefined;
|
|
1006
|
+
generated: undefined;
|
|
1007
|
+
}, {}, {}>;
|
|
1008
|
+
};
|
|
1009
|
+
dialect: 'sqlite';
|
|
1010
|
+
}>;
|
|
1011
|
+
declare function postgresOutbox(name: string): import("drizzle-orm/pg-core").PgTableWithColumns<{
|
|
1012
|
+
name: string;
|
|
1013
|
+
schema: undefined;
|
|
1014
|
+
columns: {
|
|
1015
|
+
seq: import("drizzle-orm/pg-core").PgColumn<{
|
|
1016
|
+
name: "seq";
|
|
1017
|
+
tableName: string;
|
|
1018
|
+
dataType: "number";
|
|
1019
|
+
columnType: "PgBigSerial53";
|
|
1020
|
+
data: number;
|
|
1021
|
+
driverParam: number;
|
|
1022
|
+
notNull: true;
|
|
1023
|
+
hasDefault: true;
|
|
1024
|
+
isPrimaryKey: true;
|
|
1025
|
+
isAutoincrement: false;
|
|
1026
|
+
hasRuntimeDefault: false;
|
|
1027
|
+
enumValues: undefined;
|
|
1028
|
+
baseColumn: never;
|
|
1029
|
+
identity: undefined;
|
|
1030
|
+
generated: undefined;
|
|
1031
|
+
}, {}, {}>;
|
|
1032
|
+
id: import("drizzle-orm/pg-core").PgColumn<{
|
|
1033
|
+
name: "id";
|
|
1034
|
+
tableName: string;
|
|
1035
|
+
dataType: "string";
|
|
1036
|
+
columnType: "PgText";
|
|
1037
|
+
data: string;
|
|
1038
|
+
driverParam: string;
|
|
1039
|
+
notNull: true;
|
|
1040
|
+
hasDefault: false;
|
|
1041
|
+
isPrimaryKey: false;
|
|
1042
|
+
isAutoincrement: false;
|
|
1043
|
+
hasRuntimeDefault: false;
|
|
1044
|
+
enumValues: [string, ...string[]];
|
|
1045
|
+
baseColumn: never;
|
|
1046
|
+
identity: undefined;
|
|
1047
|
+
generated: undefined;
|
|
1048
|
+
}, {}, {}>;
|
|
1049
|
+
eventId: import("drizzle-orm/pg-core").PgColumn<{
|
|
1050
|
+
name: "event_id";
|
|
1051
|
+
tableName: string;
|
|
1052
|
+
dataType: "string";
|
|
1053
|
+
columnType: "PgText";
|
|
1054
|
+
data: string;
|
|
1055
|
+
driverParam: string;
|
|
1056
|
+
notNull: true;
|
|
1057
|
+
hasDefault: false;
|
|
1058
|
+
isPrimaryKey: false;
|
|
1059
|
+
isAutoincrement: false;
|
|
1060
|
+
hasRuntimeDefault: false;
|
|
1061
|
+
enumValues: [string, ...string[]];
|
|
1062
|
+
baseColumn: never;
|
|
1063
|
+
identity: undefined;
|
|
1064
|
+
generated: undefined;
|
|
1065
|
+
}, {}, {}>;
|
|
1066
|
+
listener: import("drizzle-orm/pg-core").PgColumn<{
|
|
1067
|
+
name: "listener";
|
|
1068
|
+
tableName: string;
|
|
1069
|
+
dataType: "string";
|
|
1070
|
+
columnType: "PgText";
|
|
1071
|
+
data: string;
|
|
1072
|
+
driverParam: string;
|
|
1073
|
+
notNull: true;
|
|
1074
|
+
hasDefault: false;
|
|
1075
|
+
isPrimaryKey: false;
|
|
1076
|
+
isAutoincrement: false;
|
|
1077
|
+
hasRuntimeDefault: false;
|
|
1078
|
+
enumValues: [string, ...string[]];
|
|
1079
|
+
baseColumn: never;
|
|
1080
|
+
identity: undefined;
|
|
1081
|
+
generated: undefined;
|
|
1082
|
+
}, {}, {}>;
|
|
1083
|
+
name: import("drizzle-orm/pg-core").PgColumn<{
|
|
1084
|
+
name: "name";
|
|
1085
|
+
tableName: string;
|
|
1086
|
+
dataType: "string";
|
|
1087
|
+
columnType: "PgText";
|
|
1088
|
+
data: string;
|
|
1089
|
+
driverParam: string;
|
|
1090
|
+
notNull: true;
|
|
1091
|
+
hasDefault: false;
|
|
1092
|
+
isPrimaryKey: false;
|
|
1093
|
+
isAutoincrement: false;
|
|
1094
|
+
hasRuntimeDefault: false;
|
|
1095
|
+
enumValues: [string, ...string[]];
|
|
1096
|
+
baseColumn: never;
|
|
1097
|
+
identity: undefined;
|
|
1098
|
+
generated: undefined;
|
|
1099
|
+
}, {}, {}>;
|
|
1100
|
+
payload: import("drizzle-orm/pg-core").PgColumn<{
|
|
1101
|
+
name: "payload";
|
|
1102
|
+
tableName: string;
|
|
1103
|
+
dataType: "json";
|
|
1104
|
+
columnType: "PgJsonb";
|
|
1105
|
+
data: unknown;
|
|
1106
|
+
driverParam: unknown;
|
|
1107
|
+
notNull: true;
|
|
1108
|
+
hasDefault: false;
|
|
1109
|
+
isPrimaryKey: false;
|
|
1110
|
+
isAutoincrement: false;
|
|
1111
|
+
hasRuntimeDefault: false;
|
|
1112
|
+
enumValues: undefined;
|
|
1113
|
+
baseColumn: never;
|
|
1114
|
+
identity: undefined;
|
|
1115
|
+
generated: undefined;
|
|
1116
|
+
}, {}, {}>;
|
|
1117
|
+
traceId: import("drizzle-orm/pg-core").PgColumn<{
|
|
1118
|
+
name: "trace_id";
|
|
1119
|
+
tableName: string;
|
|
1120
|
+
dataType: "string";
|
|
1121
|
+
columnType: "PgText";
|
|
1122
|
+
data: string;
|
|
1123
|
+
driverParam: string;
|
|
1124
|
+
notNull: false;
|
|
1125
|
+
hasDefault: false;
|
|
1126
|
+
isPrimaryKey: false;
|
|
1127
|
+
isAutoincrement: false;
|
|
1128
|
+
hasRuntimeDefault: false;
|
|
1129
|
+
enumValues: [string, ...string[]];
|
|
1130
|
+
baseColumn: never;
|
|
1131
|
+
identity: undefined;
|
|
1132
|
+
generated: undefined;
|
|
1133
|
+
}, {}, {}>;
|
|
1134
|
+
source: import("drizzle-orm/pg-core").PgColumn<{
|
|
1135
|
+
name: "source";
|
|
1136
|
+
tableName: string;
|
|
1137
|
+
dataType: "string";
|
|
1138
|
+
columnType: "PgText";
|
|
1139
|
+
data: string;
|
|
1140
|
+
driverParam: string;
|
|
1141
|
+
notNull: false;
|
|
1142
|
+
hasDefault: false;
|
|
1143
|
+
isPrimaryKey: false;
|
|
1144
|
+
isAutoincrement: false;
|
|
1145
|
+
hasRuntimeDefault: false;
|
|
1146
|
+
enumValues: [string, ...string[]];
|
|
1147
|
+
baseColumn: never;
|
|
1148
|
+
identity: undefined;
|
|
1149
|
+
generated: undefined;
|
|
1150
|
+
}, {}, {}>;
|
|
1151
|
+
occurredAt: import("drizzle-orm/pg-core").PgColumn<{
|
|
1152
|
+
name: string;
|
|
1153
|
+
tableName: string;
|
|
1154
|
+
dataType: "date";
|
|
1155
|
+
columnType: "PgTimestamp";
|
|
1156
|
+
data: Date;
|
|
1157
|
+
driverParam: string;
|
|
1158
|
+
notNull: true;
|
|
1159
|
+
hasDefault: false;
|
|
1160
|
+
isPrimaryKey: false;
|
|
1161
|
+
isAutoincrement: false;
|
|
1162
|
+
hasRuntimeDefault: false;
|
|
1163
|
+
enumValues: undefined;
|
|
1164
|
+
baseColumn: never;
|
|
1165
|
+
identity: undefined;
|
|
1166
|
+
generated: undefined;
|
|
1167
|
+
}, {}, {}>;
|
|
1168
|
+
status: import("drizzle-orm/pg-core").PgColumn<{
|
|
1169
|
+
name: "status";
|
|
1170
|
+
tableName: string;
|
|
1171
|
+
dataType: "string";
|
|
1172
|
+
columnType: "PgText";
|
|
1173
|
+
data: "claimed" | "dead" | "delivered" | "pending";
|
|
1174
|
+
driverParam: string;
|
|
1175
|
+
notNull: true;
|
|
1176
|
+
hasDefault: false;
|
|
1177
|
+
isPrimaryKey: false;
|
|
1178
|
+
isAutoincrement: false;
|
|
1179
|
+
hasRuntimeDefault: false;
|
|
1180
|
+
enumValues: [string, ...string[]];
|
|
1181
|
+
baseColumn: never;
|
|
1182
|
+
identity: undefined;
|
|
1183
|
+
generated: undefined;
|
|
1184
|
+
}, {}, {
|
|
1185
|
+
$type: "claimed" | "dead" | "delivered" | "pending";
|
|
1186
|
+
}>;
|
|
1187
|
+
attempts: import("drizzle-orm/pg-core").PgColumn<{
|
|
1188
|
+
name: "attempts";
|
|
1189
|
+
tableName: string;
|
|
1190
|
+
dataType: "number";
|
|
1191
|
+
columnType: "PgInteger";
|
|
1192
|
+
data: number;
|
|
1193
|
+
driverParam: string | number;
|
|
1194
|
+
notNull: true;
|
|
1195
|
+
hasDefault: true;
|
|
1196
|
+
isPrimaryKey: false;
|
|
1197
|
+
isAutoincrement: false;
|
|
1198
|
+
hasRuntimeDefault: false;
|
|
1199
|
+
enumValues: undefined;
|
|
1200
|
+
baseColumn: never;
|
|
1201
|
+
identity: undefined;
|
|
1202
|
+
generated: undefined;
|
|
1203
|
+
}, {}, {}>;
|
|
1204
|
+
lastError: import("drizzle-orm/pg-core").PgColumn<{
|
|
1205
|
+
name: "last_error";
|
|
1206
|
+
tableName: string;
|
|
1207
|
+
dataType: "string";
|
|
1208
|
+
columnType: "PgText";
|
|
1209
|
+
data: string;
|
|
1210
|
+
driverParam: string;
|
|
1211
|
+
notNull: false;
|
|
1212
|
+
hasDefault: false;
|
|
1213
|
+
isPrimaryKey: false;
|
|
1214
|
+
isAutoincrement: false;
|
|
1215
|
+
hasRuntimeDefault: false;
|
|
1216
|
+
enumValues: [string, ...string[]];
|
|
1217
|
+
baseColumn: never;
|
|
1218
|
+
identity: undefined;
|
|
1219
|
+
generated: undefined;
|
|
1220
|
+
}, {}, {}>;
|
|
1221
|
+
availableAt: import("drizzle-orm/pg-core").PgColumn<{
|
|
1222
|
+
name: string;
|
|
1223
|
+
tableName: string;
|
|
1224
|
+
dataType: "date";
|
|
1225
|
+
columnType: "PgTimestamp";
|
|
1226
|
+
data: Date;
|
|
1227
|
+
driverParam: string;
|
|
1228
|
+
notNull: true;
|
|
1229
|
+
hasDefault: false;
|
|
1230
|
+
isPrimaryKey: false;
|
|
1231
|
+
isAutoincrement: false;
|
|
1232
|
+
hasRuntimeDefault: false;
|
|
1233
|
+
enumValues: undefined;
|
|
1234
|
+
baseColumn: never;
|
|
1235
|
+
identity: undefined;
|
|
1236
|
+
generated: undefined;
|
|
1237
|
+
}, {}, {}>;
|
|
1238
|
+
claimedAt: import("drizzle-orm/pg-core").PgColumn<{
|
|
1239
|
+
name: string;
|
|
1240
|
+
tableName: string;
|
|
1241
|
+
dataType: "date";
|
|
1242
|
+
columnType: "PgTimestamp";
|
|
1243
|
+
data: Date;
|
|
1244
|
+
driverParam: string;
|
|
1245
|
+
notNull: false;
|
|
1246
|
+
hasDefault: false;
|
|
1247
|
+
isPrimaryKey: false;
|
|
1248
|
+
isAutoincrement: false;
|
|
1249
|
+
hasRuntimeDefault: false;
|
|
1250
|
+
enumValues: undefined;
|
|
1251
|
+
baseColumn: never;
|
|
1252
|
+
identity: undefined;
|
|
1253
|
+
generated: undefined;
|
|
1254
|
+
}, {}, {}>;
|
|
1255
|
+
deliveredAt: import("drizzle-orm/pg-core").PgColumn<{
|
|
1256
|
+
name: string;
|
|
1257
|
+
tableName: string;
|
|
1258
|
+
dataType: "date";
|
|
1259
|
+
columnType: "PgTimestamp";
|
|
1260
|
+
data: Date;
|
|
1261
|
+
driverParam: string;
|
|
1262
|
+
notNull: false;
|
|
1263
|
+
hasDefault: false;
|
|
1264
|
+
isPrimaryKey: false;
|
|
1265
|
+
isAutoincrement: false;
|
|
1266
|
+
hasRuntimeDefault: false;
|
|
1267
|
+
enumValues: undefined;
|
|
1268
|
+
baseColumn: never;
|
|
1269
|
+
identity: undefined;
|
|
1270
|
+
generated: undefined;
|
|
1271
|
+
}, {}, {}>;
|
|
1272
|
+
};
|
|
1273
|
+
dialect: 'pg';
|
|
1274
|
+
}>;
|
|
1275
|
+
/** The outbox table of a SQLite database. */
|
|
1276
|
+
type SQLiteOutboxTable = ReturnType<typeof sqliteOutbox>;
|
|
1277
|
+
/** The outbox table of a Postgres database. */
|
|
1278
|
+
type PostgresOutboxTable = ReturnType<typeof postgresOutbox>;
|
|
1279
|
+
/** An outbox table, made by `outboxTable`. */
|
|
1280
|
+
type OutboxTable = SQLiteOutboxTable | PostgresOutboxTable;
|
|
1281
|
+
/**
|
|
1282
|
+
* The outbox table: where recorded events wait, one row per listener, until they
|
|
1283
|
+
* are delivered. A write and the events it causes are inserted in one transaction,
|
|
1284
|
+
* so an event is neither lost when the process dies after the write nor sent for a
|
|
1285
|
+
* write that was rolled back.
|
|
1286
|
+
*
|
|
1287
|
+
* Brick does not create tables for you. Put this in the schema of the database
|
|
1288
|
+
* whose writes emit events, and your migration tool (such as drizzle-kit) creates
|
|
1289
|
+
* it. A service whose database holds the table records events; one without it does
|
|
1290
|
+
* not.
|
|
1291
|
+
*
|
|
1292
|
+
* @param engine - The database engine, `"sqlite"` or `"postgres"`.
|
|
1293
|
+
* @param options - `name` to call the table something other than `brick_outbox`.
|
|
1294
|
+
* @returns A Drizzle table.
|
|
1295
|
+
* @example
|
|
1296
|
+
* import { defineDatabase, outboxTable } from "@brickkit/core";
|
|
1297
|
+
*
|
|
1298
|
+
* export const outbox = outboxTable("postgres");
|
|
1299
|
+
* export const database = defineDatabase({
|
|
1300
|
+
* engine: "postgres",
|
|
1301
|
+
* tables: { users, outbox },
|
|
1302
|
+
* });
|
|
1303
|
+
*/
|
|
1304
|
+
declare function outboxTable(engine: "sqlite", options?: {
|
|
1305
|
+
name?: string;
|
|
1306
|
+
}): SQLiteOutboxTable;
|
|
1307
|
+
declare function outboxTable(engine: "postgres", options?: {
|
|
1308
|
+
name?: string;
|
|
1309
|
+
}): PostgresOutboxTable;
|
|
1310
|
+
/**
|
|
1311
|
+
* What core's own modules use of a service, beyond the public `Service`. Every
|
|
1312
|
+
* service `defineService` makes is one.
|
|
1313
|
+
*/
|
|
1314
|
+
interface ServicePort<TDb = any, TServiceContext extends Record<string, any> = Record<string, any>> extends Service<TDb, TServiceContext> {
|
|
1315
|
+
/** The values of this service's `deps`. Set by `brick({ deps })`. */
|
|
1316
|
+
readonly deps?: Readonly<Record<string, unknown>>;
|
|
1317
|
+
/** Whether the service declares an event catalog. */
|
|
1318
|
+
readonly hasCatalog: boolean;
|
|
1319
|
+
setDeps(deps: Readonly<Record<string, unknown>>): void;
|
|
1320
|
+
/** `resolveContext`, for an action or socket a plugin install created: it sees `pluginDeps`. */
|
|
1321
|
+
resolveContext(callCtx?: Partial<BaseContext<TDb> & TServiceContext>, pluginDeps?: Readonly<Record<string, unknown>>): Promise<BaseContext<TDb> & TServiceContext>;
|
|
1322
|
+
setRecorder(recorder: ServiceEvents | undefined): void;
|
|
1323
|
+
getOutbox(): OutboxTable | undefined;
|
|
1324
|
+
/** @throws When the event is not in the catalog or its payload does not match. */
|
|
1325
|
+
checkEvent(name: string, payload: unknown): void;
|
|
1326
|
+
/** Adds events to the catalog (a plugin's). */
|
|
1327
|
+
addEvents(refs: Readonly<Record<string, EventRef>>): void;
|
|
1328
|
+
registerTable(table: unknown): this;
|
|
1329
|
+
listPreparations(): PreparationContribution[];
|
|
1330
|
+
prepareContributions(): void;
|
|
1331
|
+
/** Records the declarations; the returned function puts them back (plugin rollback). */
|
|
1332
|
+
snapshot(): () => void;
|
|
1333
|
+
/** Marks the definitions changed, so cached architecture and OpenAPI are rebuilt. */
|
|
1334
|
+
touch(): void;
|
|
1335
|
+
}
|
|
1336
|
+
/** Set on actions declared with `resource.action(...)`. */
|
|
1337
|
+
interface ResourceActionInfo {
|
|
1338
|
+
resource: string;
|
|
1339
|
+
/** The declared name, without the `resource.` prefix. */
|
|
1340
|
+
name: string;
|
|
1341
|
+
record: boolean;
|
|
1342
|
+
/** A record action's own access rule. */
|
|
1343
|
+
access?: ResourceActionAccess;
|
|
1344
|
+
}
|
|
1345
|
+
interface ActionAccessInfo {
|
|
1346
|
+
hasAuthorize: boolean;
|
|
1347
|
+
hasScope: boolean;
|
|
1348
|
+
policy: ActionAccessPolicy;
|
|
1349
|
+
}
|
|
1350
|
+
/** What a plugin action adds to the validation of `routes.action`. */
|
|
1351
|
+
interface PluginActionChecks {
|
|
1352
|
+
/** Checked instead of `output` at runtime, while `output` stays the published contract (client types, OpenAPI). */
|
|
1353
|
+
runtimeOutput?: TSchema;
|
|
1354
|
+
/** Runs before input validation, in-process and over HTTP; the errors it returns reject the input with a 400. */
|
|
1355
|
+
checkInput?: (input: unknown) => ValidationErrorItem[] | undefined;
|
|
1356
|
+
}
|
|
1357
|
+
type PluginRouteMethod = HttpMethod;
|
|
1358
|
+
/** Where a plugin route mounts: under the app prefix, its service, or its resource. */
|
|
1359
|
+
declare const PLUGIN_ROUTE_SCOPES: readonly ["app", "service", "resource"];
|
|
1360
|
+
type PluginRouteScope = (typeof PLUGIN_ROUTE_SCOPES)[number];
|
|
1361
|
+
type PluginRoutes<S extends PluginRouteScope = PluginRouteScope> = false | Readonly<Record<string, false | {
|
|
1362
|
+
path?: string;
|
|
1363
|
+
scope?: S;
|
|
1364
|
+
}>>;
|
|
1365
|
+
type ContextOf<S> = S extends Service<infer D, infer C> ? BaseContext<D> & C : never;
|
|
1366
|
+
interface PluginEndpointContext<C extends ActionContext = ActionContext> {
|
|
1367
|
+
params: Record<string, string>;
|
|
1368
|
+
query: Record<string, string | undefined>;
|
|
1369
|
+
body: unknown;
|
|
1370
|
+
headers: Record<string, string>;
|
|
1371
|
+
request: Request;
|
|
1372
|
+
set: {
|
|
1373
|
+
status?: number;
|
|
1374
|
+
headers: Record<string, string>;
|
|
1375
|
+
};
|
|
1376
|
+
ctx: C;
|
|
1377
|
+
}
|
|
1378
|
+
interface PluginRouteRegistrar<C extends ActionContext = ActionContext, S extends PluginRouteScope = PluginRouteScope> {
|
|
1379
|
+
action<I extends TSchema | undefined = undefined, O extends TSchema | undefined = undefined, E extends Record<string, ActionErrorDefinition> = Record<string, ActionErrorDefinition>>(config: ActionConfigWithAuthorize<I, O, E, C> & PluginActionChecks & {
|
|
1380
|
+
path: string;
|
|
1381
|
+
method: PluginRouteMethod;
|
|
1382
|
+
scope?: S;
|
|
1383
|
+
}): Action<I, O, E, C>;
|
|
1384
|
+
action<I extends TSchema | undefined = undefined, O extends TSchema | undefined = undefined, E extends Record<string, ActionErrorDefinition> = Record<string, ActionErrorDefinition>>(config: ActionConfigWithoutAuthorize<I, O, E, C> & PluginActionChecks & {
|
|
1385
|
+
path: string;
|
|
1386
|
+
method: PluginRouteMethod;
|
|
1387
|
+
scope?: S;
|
|
1388
|
+
}): Action<I, O, E, C>;
|
|
1389
|
+
endpoint(config: {
|
|
1390
|
+
name: string;
|
|
1391
|
+
path: string;
|
|
1392
|
+
method: PluginRouteMethod;
|
|
1393
|
+
scope?: S;
|
|
1394
|
+
handler: (context: PluginEndpointContext<C>) => unknown | Promise<unknown>;
|
|
1395
|
+
summary?: string;
|
|
1396
|
+
description?: string;
|
|
1397
|
+
tags?: string[];
|
|
1398
|
+
}): void;
|
|
1399
|
+
/**
|
|
1400
|
+
* Reserve a URL segment under the resource, so no resource action takes it,
|
|
1401
|
+
* for a route the plugin may add later. Resource-scoped routes claim their
|
|
1402
|
+
* first segment themselves.
|
|
1403
|
+
*/
|
|
1404
|
+
claim(segment: string): void;
|
|
1405
|
+
}
|
|
1406
|
+
interface ServicePluginContext<S extends Service<any, any> = Service<any, any>> {
|
|
1407
|
+
service: S;
|
|
1408
|
+
routes: PluginRouteRegistrar<ContextOf<S>, "app" | "service">;
|
|
1409
|
+
}
|
|
1410
|
+
interface ResourcePluginContext<S extends Service<any, any> = Service<any, any>, R extends Resource<any, any, any> = Resource<any, ContextOf<S>, any>> {
|
|
1411
|
+
service: S;
|
|
1412
|
+
resource: R;
|
|
1413
|
+
routes: PluginRouteRegistrar<ContextOf<S>>;
|
|
1414
|
+
}
|
|
1415
|
+
/** Plugins may specialize their returned API for the installation host. */
|
|
1416
|
+
interface ResourcePluginTypes {
|
|
1417
|
+
readonly table: unknown;
|
|
1418
|
+
readonly context: unknown;
|
|
1419
|
+
readonly resource: unknown;
|
|
1420
|
+
readonly api: object;
|
|
1421
|
+
}
|
|
1422
|
+
type ResourcePluginAPI<P, T, C, R = unknown> = P extends {
|
|
1423
|
+
readonly types: infer H extends ResourcePluginTypes;
|
|
1424
|
+
} ? (H & {
|
|
1425
|
+
readonly table: T;
|
|
1426
|
+
readonly context: C;
|
|
1427
|
+
readonly resource: R;
|
|
1428
|
+
})["api"] : P extends PluginInstance<infer A extends object, any> ? A : P extends ResourcePlugin<any, any, infer A> ? A : never;
|
|
1429
|
+
/** Which host a plugin descriptor installs on. Process-global, so descriptors survive a second core copy. */
|
|
1430
|
+
declare const PLUGIN_KIND: unique symbol;
|
|
1431
|
+
interface ServicePlugin<S extends Service<any, any>, A> {
|
|
1432
|
+
readonly [PLUGIN_KIND]: "service" | "plugin";
|
|
1433
|
+
readonly name: string;
|
|
1434
|
+
readonly routes?: PluginRoutes<"app" | "service">;
|
|
1435
|
+
readonly setup: (context: ServicePluginContext<S>) => A;
|
|
1436
|
+
}
|
|
1437
|
+
interface ResourcePlugin<S extends Service<any, any>, R extends Resource<any, any, any>, A extends object> {
|
|
1438
|
+
readonly [PLUGIN_KIND]: "resource" | "plugin";
|
|
1439
|
+
readonly name: string;
|
|
1440
|
+
readonly routes?: PluginRoutes;
|
|
1441
|
+
readonly setup: (context: ResourcePluginContext<S, R>) => A;
|
|
1442
|
+
}
|
|
1443
|
+
/** A plugin as `app.definition.plugins` lists it. */
|
|
1444
|
+
interface InstalledPlugin {
|
|
1445
|
+
readonly name: string;
|
|
1446
|
+
readonly brick?: string;
|
|
1447
|
+
readonly scope: "app" | "service" | "resource";
|
|
1448
|
+
readonly service?: string;
|
|
1449
|
+
readonly resource?: string;
|
|
1450
|
+
/** The plugin's dashboard extension, an absolute path. */
|
|
1451
|
+
readonly dashboard?: string;
|
|
1452
|
+
}
|
|
1453
|
+
interface SQLiteDatabaseConfig<TSchema extends Record<string, unknown> = Record<string, unknown>> {
|
|
1454
|
+
/** Database file path or ':memory:' (default: ':memory:') */
|
|
1455
|
+
path?: string;
|
|
1456
|
+
/** Alias for path */
|
|
1457
|
+
filename?: string;
|
|
1458
|
+
/** Whether to enable WAL journal mode for file-based DBs (default: true) */
|
|
1459
|
+
wal?: boolean;
|
|
1460
|
+
/** Whether to enforce foreign key constraints (default: true) */
|
|
1461
|
+
foreignKeys?: boolean;
|
|
1462
|
+
/** Optional existing bun:sqlite Database instance */
|
|
1463
|
+
client?: Database;
|
|
1464
|
+
/** Optional Drizzle schema object for typed relational queries */
|
|
1465
|
+
schema?: TSchema;
|
|
1466
|
+
}
|
|
1467
|
+
type BrickSQLiteDatabase<TSchema extends Record<string, unknown> = Record<string, unknown>> = BunSQLiteDatabase<TSchema> & {
|
|
1468
|
+
$client: Database;
|
|
1469
|
+
};
|
|
1470
|
+
/**
|
|
1471
|
+
* Creates a zero-config SQLite database using Bun's native `bun:sqlite` engine
|
|
1472
|
+
* and Drizzle ORM. Defaults to an ultra-fast in-memory database (`:memory:`).
|
|
1473
|
+
*
|
|
1474
|
+
* Bun-only: the driver is loaded lazily so importing `@brickkit/core` on
|
|
1475
|
+
* other runtimes (e.g. Node) stays safe until this function is actually
|
|
1476
|
+
* called.
|
|
1477
|
+
*/
|
|
1478
|
+
declare function createSQLiteDatabase<TSchema extends Record<string, unknown> = Record<string, unknown>>(config?: SQLiteDatabaseConfig<TSchema>): BunSQLiteDatabase<TSchema>;
|
|
1479
|
+
interface PostgresDatabaseConfig<TSchema extends Record<string, unknown> = Record<string, unknown>> {
|
|
1480
|
+
/** Existing postgres-js client. Connection options are ignored; the caller owns its lifecycle. */
|
|
1481
|
+
client?: Sql;
|
|
1482
|
+
/** Database connection URL (e.g. postgresql://user:pass@host:5432/dbname) */
|
|
1483
|
+
url?: string;
|
|
1484
|
+
/** Connection string alias */
|
|
1485
|
+
connectionString?: string;
|
|
1486
|
+
host?: string;
|
|
1487
|
+
port?: number;
|
|
1488
|
+
user?: string;
|
|
1489
|
+
password?: string;
|
|
1490
|
+
database?: string;
|
|
1491
|
+
ssl?: boolean | "require" | "allow" | "prefer" | "verify-full";
|
|
1492
|
+
max?: number;
|
|
1493
|
+
/**
|
|
1494
|
+
* Disable prepared statements (required for transaction-mode poolers such
|
|
1495
|
+
* as Supabase's Supavisor on :6543, which cannot hold named prepared
|
|
1496
|
+
* statements across transactions).
|
|
1497
|
+
*/
|
|
1498
|
+
prepare?: boolean;
|
|
1499
|
+
/** Optional Drizzle schema for relational queries */
|
|
1500
|
+
schema?: TSchema;
|
|
1501
|
+
/** Custom schema name for Encore-style isolated schemas */
|
|
1502
|
+
schemaName?: string;
|
|
1503
|
+
}
|
|
1504
|
+
type PostgresDatabase<_TSchema extends Record<string, unknown> = Record<string, unknown>> = any;
|
|
1505
|
+
/**
|
|
1506
|
+
* Creates a PostgreSQL Drizzle instance using postgres-js.
|
|
1507
|
+
* Static imports so bundlers (Nitro/Rollup) trace and ship the driver.
|
|
1508
|
+
* `bun:sqlite` stays lazy in `sqlite.ts` since it only exists under Bun.
|
|
1509
|
+
*/
|
|
1510
|
+
declare function createPostgresDatabase<TSchema extends Record<string, unknown> = Record<string, unknown>>(config?: PostgresDatabaseConfig<TSchema>): PostgresDatabase<TSchema>;
|
|
1511
|
+
type EnvInput<T> = T | SecretRef<T>;
|
|
1512
|
+
type EnvOptions<T> = { [K in keyof T]: EnvInput<Exclude<T[K], undefined>>; };
|
|
1513
|
+
interface DefinitionOptions<TSchema extends Record<string, unknown>> {
|
|
1514
|
+
name?: string;
|
|
1515
|
+
/** Drizzle tables retained for schema tooling. Never applied at runtime. */
|
|
1516
|
+
tables?: (TSchema & Record<string, Table>) | readonly Table[];
|
|
1517
|
+
schema?: TSchema;
|
|
1518
|
+
}
|
|
1519
|
+
type SQLiteDefinition<TSchema extends Record<string, unknown> = Record<string, unknown>> = DefinitionOptions<TSchema> & EnvOptions<Omit<SQLiteDatabaseConfig<TSchema>, "schema" | "client">> & {
|
|
1520
|
+
engine?: "sqlite";
|
|
1521
|
+
client?: SQLiteDatabaseConfig<TSchema>["client"];
|
|
1522
|
+
url?: never;
|
|
1523
|
+
connectionString?: never;
|
|
1524
|
+
};
|
|
1525
|
+
type PostgresDefinition<TSchema extends Record<string, unknown> = Record<string, unknown>> = DefinitionOptions<TSchema> & EnvOptions<Omit<PostgresDatabaseConfig<TSchema>, "schema" | "client">> & {
|
|
1526
|
+
engine: "postgres";
|
|
1527
|
+
path?: never;
|
|
1528
|
+
filename?: never;
|
|
1529
|
+
wal?: never;
|
|
1530
|
+
foreignKeys?: never;
|
|
1531
|
+
client?: PostgresDatabaseConfig<TSchema>["client"];
|
|
1532
|
+
};
|
|
1533
|
+
type DatabaseDefinition<TSchema extends Record<string, unknown> = Record<string, unknown>> = SQLiteDefinition<TSchema> | PostgresDefinition<TSchema>;
|
|
1534
|
+
interface DatabaseHandle<TDb, TSchema extends Record<string, unknown> = Record<string, unknown>> {
|
|
1535
|
+
readonly kind: "brick-database";
|
|
1536
|
+
readonly name?: string;
|
|
1537
|
+
readonly engine: "sqlite" | "postgres";
|
|
1538
|
+
readonly tables: Readonly<Record<string, Table>>;
|
|
1539
|
+
readonly schema: TSchema;
|
|
1540
|
+
/**
|
|
1541
|
+
* Add a table after definition, as `definePlugin` does for the tables a
|
|
1542
|
+
* plugin owns. Before `getDb()` it also joins the relational schema.
|
|
1543
|
+
*/
|
|
1544
|
+
addTable?(key: string, table: Table): void;
|
|
1545
|
+
getDb(): TDb;
|
|
1546
|
+
close?(): void | Promise<void>;
|
|
1547
|
+
}
|
|
1548
|
+
/**
|
|
1549
|
+
* Whether a value is a database handle made by `defineDatabase`.
|
|
1550
|
+
*
|
|
1551
|
+
* @param value - Anything.
|
|
1552
|
+
* @returns `true` for a handle, narrowing its type.
|
|
1553
|
+
*/
|
|
1554
|
+
declare function isDatabaseHandle(value: unknown): value is DatabaseHandle<unknown>;
|
|
1555
|
+
/**
|
|
1556
|
+
* Declare a service's database: its engine, its Drizzle tables, and how to
|
|
1557
|
+
* reach it. Nothing connects until the handle is used (`getDb()`), so defining
|
|
1558
|
+
* one has no side effects.
|
|
1559
|
+
*
|
|
1560
|
+
* SQLite is the default and needs no options. Pass `engine: "postgres"` with a
|
|
1561
|
+
* `url` for Postgres. Options that belong to the other engine (such as `url` for
|
|
1562
|
+
* SQLite, or `path` for Postgres) are refused.
|
|
1563
|
+
*
|
|
1564
|
+
* @param options - `engine` (`"sqlite"` or `"postgres"`), `tables`, an optional
|
|
1565
|
+
* `name`, and the engine's connection settings.
|
|
1566
|
+
* @returns A handle to give to `defineService({ database })`. `getDb()` returns
|
|
1567
|
+
* the Drizzle database.
|
|
1568
|
+
* @throws When the engine is unsupported, or an option does not suit the engine.
|
|
1569
|
+
* @example
|
|
1570
|
+
* import { defineDatabase } from "@brickkit/core";
|
|
1571
|
+
* import { sqliteTable, text } from "drizzle-orm/sqlite-core";
|
|
1572
|
+
*
|
|
1573
|
+
* const pages = sqliteTable("pages", {
|
|
1574
|
+
* id: text("id").primaryKey(),
|
|
1575
|
+
* title: text("title").notNull(),
|
|
1576
|
+
* });
|
|
1577
|
+
*
|
|
1578
|
+
* export const database = defineDatabase({ tables: { pages } });
|
|
1579
|
+
*/
|
|
1580
|
+
declare function defineDatabase<TSchema extends Record<string, unknown> = Record<string, unknown>>(options?: SQLiteDefinition<TSchema>): DatabaseHandle<BunSQLiteDatabase$1<TSchema>, TSchema>;
|
|
1581
|
+
declare function defineDatabase<TSchema extends Record<string, unknown> = Record<string, unknown>>(options: PostgresDefinition<TSchema>): DatabaseHandle<PostgresJsDatabase<TSchema> & {
|
|
1582
|
+
$client: Sql;
|
|
1583
|
+
}, TSchema>;
|
|
1584
|
+
/** The codes every action, listener and socket handler can raise, with their status and default message. */
|
|
1585
|
+
declare const STANDARD_ERRORS: {
|
|
1586
|
+
readonly BAD_REQUEST: {
|
|
1587
|
+
readonly status: 400;
|
|
1588
|
+
readonly message: "Bad Request";
|
|
1589
|
+
};
|
|
1590
|
+
readonly UNAUTHORIZED: {
|
|
1591
|
+
readonly status: 401;
|
|
1592
|
+
readonly message: "Unauthorized";
|
|
1593
|
+
};
|
|
1594
|
+
readonly FORBIDDEN: {
|
|
1595
|
+
readonly status: 403;
|
|
1596
|
+
readonly message: "Forbidden";
|
|
1597
|
+
};
|
|
1598
|
+
readonly NOT_FOUND: {
|
|
1599
|
+
readonly status: 404;
|
|
1600
|
+
readonly message: "Not Found";
|
|
1601
|
+
};
|
|
1602
|
+
readonly CONFLICT: {
|
|
1603
|
+
readonly status: 409;
|
|
1604
|
+
readonly message: "Conflict";
|
|
1605
|
+
};
|
|
1606
|
+
readonly INTERNAL_SERVER_ERROR: {
|
|
1607
|
+
readonly status: 500;
|
|
1608
|
+
readonly message: "Internal Server Error";
|
|
1609
|
+
};
|
|
1610
|
+
};
|
|
1611
|
+
type StandardErrorCode = keyof typeof STANDARD_ERRORS;
|
|
1612
|
+
/** An error as JSON: the body of a failed HTTP call, and the data of a socket `$error` (with its `event`). */
|
|
1613
|
+
interface WireError {
|
|
1614
|
+
name: string;
|
|
1615
|
+
code: string;
|
|
1616
|
+
message: string;
|
|
1617
|
+
status: number;
|
|
1618
|
+
details?: unknown;
|
|
1619
|
+
errors?: ValidationErrorItem[];
|
|
1620
|
+
}
|
|
1621
|
+
/** Event names starting with `$` are reserved for control messages. */
|
|
1622
|
+
declare const isControlEvent: (event: string) => boolean;
|
|
1623
|
+
/** How many outbox rows are in each state. */
|
|
1624
|
+
type OutboxCounts = Record<OutboxStatus, number>;
|
|
1625
|
+
/**
|
|
1626
|
+
* Delivers the events recorded in one outbox table to the app's listeners.
|
|
1627
|
+
*
|
|
1628
|
+
* It claims a batch of due rows, runs each listener, and marks the row delivered.
|
|
1629
|
+
* A listener that throws is retried later with a growing delay, and parked as
|
|
1630
|
+
* dead after its attempts run out. Several dispatchers can share a table: on
|
|
1631
|
+
* Postgres they claim with `FOR UPDATE SKIP LOCKED`, and a claim that is not
|
|
1632
|
+
* finished within `claimTimeoutMs` (the process died) is taken by another.
|
|
1633
|
+
* Delivery is at least once, so a listener should tolerate running twice for the
|
|
1634
|
+
* same `deliveryId`.
|
|
1635
|
+
*/
|
|
1636
|
+
declare class Dispatcher {
|
|
1637
|
+
private readonly db;
|
|
1638
|
+
private readonly table;
|
|
1639
|
+
private readonly listeners;
|
|
1640
|
+
private readonly engine;
|
|
1641
|
+
private readonly options;
|
|
1642
|
+
private readonly inFlight;
|
|
1643
|
+
private timer?;
|
|
1644
|
+
private tickQueue?;
|
|
1645
|
+
private lastPurge;
|
|
1646
|
+
constructor(db: any, table: OutboxTable, listeners: (name: string) => Deliverable | undefined, options?: DispatchOptions);
|
|
1647
|
+
/** Start looking for due deliveries on a timer. Does nothing if it already is. */
|
|
1648
|
+
start(): void;
|
|
1649
|
+
/** Stop the timer and wait for the deliveries that are running to finish. */
|
|
1650
|
+
stop(): Promise<void>;
|
|
1651
|
+
/** Look for work soon, without waiting for the timer. Safe to call often. */
|
|
1652
|
+
notify(): void;
|
|
1653
|
+
/** Claim what is due and run it, once. @returns How many deliveries it started. */
|
|
1654
|
+
private tick;
|
|
1655
|
+
/**
|
|
1656
|
+
* Deliver everything that is due now and wait until those deliveries are done.
|
|
1657
|
+
* Deliveries that fail and are rescheduled are not waited for again.
|
|
1658
|
+
*
|
|
1659
|
+
* @returns How many deliveries it ran.
|
|
1660
|
+
*/
|
|
1661
|
+
drain(): Promise<number>;
|
|
1662
|
+
counts(): Promise<OutboxCounts>;
|
|
1663
|
+
/**
|
|
1664
|
+
* Put parked deliveries back in line, with their attempts reset.
|
|
1665
|
+
*
|
|
1666
|
+
* @param deliveryId - One delivery to retry. Omit it to retry every dead one.
|
|
1667
|
+
* @returns How many deliveries were put back.
|
|
1668
|
+
*/
|
|
1669
|
+
retryDead(deliveryId?: string): Promise<number>;
|
|
1670
|
+
private claimAndRun;
|
|
1671
|
+
private claim;
|
|
1672
|
+
private deliver;
|
|
1673
|
+
private markDelivered;
|
|
1674
|
+
private fail;
|
|
1675
|
+
private park;
|
|
1676
|
+
private logError;
|
|
1677
|
+
private purgeDelivered;
|
|
1678
|
+
}
|
|
1679
|
+
/**
|
|
1680
|
+
* Stop every events hub started in this process, by any copy of `@brickkit/core`,
|
|
1681
|
+
* and wait for their deliveries. A hub that fails to stop does not cut the wait
|
|
1682
|
+
* for the others: the first failure is thrown once every hub is done.
|
|
1683
|
+
*/
|
|
1684
|
+
declare function stopAllEvents(): Promise<void>;
|
|
1685
|
+
/**
|
|
1686
|
+
* What an app's `events` option describes, as plain data. It goes in the app
|
|
1687
|
+
* definition, which is sent to the dev dashboard, so it holds no functions.
|
|
1688
|
+
*/
|
|
1689
|
+
interface EventsDefinition {
|
|
1690
|
+
listeners: {
|
|
1691
|
+
name: string;
|
|
1692
|
+
on: string[];
|
|
1693
|
+
kind: "handler" | "workflow";
|
|
1694
|
+
}[];
|
|
1695
|
+
dispatch: boolean;
|
|
1696
|
+
/** Names of the services whose writes record events. */
|
|
1697
|
+
recorders: string[];
|
|
1698
|
+
}
|
|
1699
|
+
/** What the dispatcher runs for a delivery. */
|
|
1700
|
+
interface Deliverable {
|
|
1701
|
+
readonly maxAttempts?: number;
|
|
1702
|
+
readonly errors?: EventListener["errors"];
|
|
1703
|
+
readonly retry?: EventListener["retry"];
|
|
1704
|
+
run(delivery: EventDelivery): void | Promise<void>;
|
|
1705
|
+
}
|
|
1706
|
+
/**
|
|
1707
|
+
* The events of one app: who listens, where events are recorded, and the
|
|
1708
|
+
* dispatchers that deliver them. `brick({ events })` makes one and puts it on
|
|
1709
|
+
* `app.events`.
|
|
1710
|
+
*/
|
|
1711
|
+
declare class EventsHub {
|
|
1712
|
+
private readonly deps;
|
|
1713
|
+
private readonly byName;
|
|
1714
|
+
private readonly dispatchers;
|
|
1715
|
+
private readonly deliverables;
|
|
1716
|
+
private readonly dispatchOptions;
|
|
1717
|
+
private readonly runtime;
|
|
1718
|
+
private readonly recorderNames;
|
|
1719
|
+
private started;
|
|
1720
|
+
private stopping;
|
|
1721
|
+
/**
|
|
1722
|
+
* @param options - The `events` option.
|
|
1723
|
+
* @param deps - The app's `brick({ deps })`, which `useDeps()` returns in its
|
|
1724
|
+
* listeners and workflow steps.
|
|
1725
|
+
*/
|
|
1726
|
+
constructor(options: EventsOptions, deps?: Record<string, unknown>);
|
|
1727
|
+
/**
|
|
1728
|
+
* Give each service whose database holds an outbox table a recorder, and a
|
|
1729
|
+
* dispatcher for that table. Call it before the first request: a service has no
|
|
1730
|
+
* recorder until then.
|
|
1731
|
+
*
|
|
1732
|
+
* @param services - The app's services.
|
|
1733
|
+
* @returns The hub, for chaining.
|
|
1734
|
+
*/
|
|
1735
|
+
attach(services: readonly Service<any, any>[]): this;
|
|
1736
|
+
matching(name: string): EventListener[];
|
|
1737
|
+
describe(): EventsDefinition;
|
|
1738
|
+
/**
|
|
1739
|
+
* Where the workflow bundle is read from, the project it is built from, and
|
|
1740
|
+
* the world the workflows run on. Only set when a listener runs a workflow.
|
|
1741
|
+
* The CLI builds the bundle there before events start, and shows the world's
|
|
1742
|
+
* runs in the dev dashboard.
|
|
1743
|
+
*/
|
|
1744
|
+
get workflows(): {
|
|
1745
|
+
root: string;
|
|
1746
|
+
dir: string;
|
|
1747
|
+
world: EventsWorld;
|
|
1748
|
+
} | undefined;
|
|
1749
|
+
/**
|
|
1750
|
+
* Resume the workflow that waits on a webhook. The server mounts this on the
|
|
1751
|
+
* route the Workflow SDK puts in webhook URLs.
|
|
1752
|
+
*/
|
|
1753
|
+
webhook(request: Request): Promise<Response>;
|
|
1754
|
+
/**
|
|
1755
|
+
* Start delivering recorded events from this process, and start the world that
|
|
1756
|
+
* runs workflow listeners. Does nothing if this process only records events
|
|
1757
|
+
* (`dispatch: false`), already started, or a stop is still waiting.
|
|
1758
|
+
*/
|
|
1759
|
+
start(): void;
|
|
1760
|
+
/**
|
|
1761
|
+
* Stop delivering, wait for the deliveries that are running, and close the
|
|
1762
|
+
* world. A world cannot be started again, so workflow listeners stop for good.
|
|
1763
|
+
* A stop that arrives while another runs waits for that one.
|
|
1764
|
+
*/
|
|
1765
|
+
stop(): Promise<void>;
|
|
1766
|
+
private shutDown;
|
|
1767
|
+
/**
|
|
1768
|
+
* Deliver everything that is due now and wait for it. For tests and for
|
|
1769
|
+
* workers that drive delivery themselves.
|
|
1770
|
+
*
|
|
1771
|
+
* @returns How many deliveries ran.
|
|
1772
|
+
*/
|
|
1773
|
+
drain(): Promise<number>;
|
|
1774
|
+
/** How many rows are in each state, across the app's outbox tables. */
|
|
1775
|
+
counts(): Promise<OutboxCounts>;
|
|
1776
|
+
/**
|
|
1777
|
+
* Put parked deliveries back in line.
|
|
1778
|
+
*
|
|
1779
|
+
* @param deliveryId - One delivery. Omit it to retry every dead one.
|
|
1780
|
+
* @returns How many deliveries were put back.
|
|
1781
|
+
*/
|
|
1782
|
+
retryDead(deliveryId?: string): Promise<number>;
|
|
1783
|
+
private deliverable;
|
|
1784
|
+
private bind;
|
|
1785
|
+
}
|
|
1786
|
+
type SocketSchemas = Record<string, TSchema>;
|
|
1787
|
+
type SocketPayloads<S extends SocketSchemas> = { [K in keyof S]: Static<S[K]>; };
|
|
1788
|
+
type SocketEventArgs<Events> = { [K in keyof Events & string]: [event: K, payload: Events[K]]; }[keyof Events & string];
|
|
1789
|
+
interface BrickSocketSender<Events> {
|
|
1790
|
+
/** Locally accepts a message. Does not acknowledge remote delivery. */
|
|
1791
|
+
send(...args: SocketEventArgs<Events>): void;
|
|
1792
|
+
close(): void;
|
|
1793
|
+
}
|
|
1794
|
+
interface BrickSocketConfig<I extends SocketSchemas, O extends SocketSchemas, Ctx, E extends Record<string, ActionErrorDefinition> = {}> {
|
|
1795
|
+
name: string;
|
|
1796
|
+
path?: string;
|
|
1797
|
+
description?: string;
|
|
1798
|
+
input: I;
|
|
1799
|
+
output: O;
|
|
1800
|
+
/** Defaults to the upgrade request's origin. */
|
|
1801
|
+
allowedOrigins?: readonly string[];
|
|
1802
|
+
allowMissingOrigin?: boolean;
|
|
1803
|
+
authorize?: (params: {
|
|
1804
|
+
ctx: Ctx;
|
|
1805
|
+
}) => boolean | Promise<boolean>;
|
|
1806
|
+
/** Rooms the connection is put in when it opens. */
|
|
1807
|
+
rooms?: (params: {
|
|
1808
|
+
ctx: Ctx;
|
|
1809
|
+
}) => readonly string[] | Promise<readonly string[]>;
|
|
1810
|
+
/** Decides each room a client asks to join. Without it, client joins are refused. */
|
|
1811
|
+
join?: (params: {
|
|
1812
|
+
ctx: Ctx;
|
|
1813
|
+
room: string;
|
|
1814
|
+
}) => boolean | Promise<boolean>;
|
|
1815
|
+
/** Rooms one connection can be in at once (default 100). */
|
|
1816
|
+
maxRooms?: number;
|
|
1817
|
+
/** Keep recent emits per room so a reconnecting client gets what it missed. Off by default. */
|
|
1818
|
+
replay?: SocketReplay;
|
|
1819
|
+
/**
|
|
1820
|
+
* Errors handlers may raise with `error.CODE()`, as an action declares them.
|
|
1821
|
+
* A raised error, with a declared or standard code, reaches the client's
|
|
1822
|
+
* `onHandlerError` and the connection stays open; anything else a handler
|
|
1823
|
+
* throws, or any error deciding a join, closes it with `1011` and is logged.
|
|
1824
|
+
*/
|
|
1825
|
+
errors?: E;
|
|
1826
|
+
handlers: { [K in keyof I]: (params: {
|
|
1827
|
+
input: Static<I[K]>;
|
|
1828
|
+
ctx: Ctx;
|
|
1829
|
+
socket: BrickSocketSender<SocketPayloads<O>>;
|
|
1830
|
+
error: ErrorBuilder<E>;
|
|
1831
|
+
}) => void | Promise<void>; };
|
|
1832
|
+
open?: (params: {
|
|
1833
|
+
ctx: Ctx;
|
|
1834
|
+
socket: BrickSocketSender<SocketPayloads<O>>;
|
|
1835
|
+
}) => void | Promise<void>;
|
|
1836
|
+
close?: (params: {
|
|
1837
|
+
ctx: Ctx;
|
|
1838
|
+
socket: BrickSocketSender<SocketPayloads<O>>;
|
|
1839
|
+
code: number;
|
|
1840
|
+
reason: string;
|
|
1841
|
+
}) => void | Promise<void>;
|
|
1842
|
+
}
|
|
1843
|
+
interface SocketEmitOptions {
|
|
1844
|
+
/** A transaction handle from `runInTransaction`: send on commit, drop on rollback. */
|
|
1845
|
+
after?: unknown;
|
|
1846
|
+
}
|
|
1847
|
+
type SocketEmitArgs<Events> = { [K in keyof Events & string]: [event: K, payload: Events[K], options?: SocketEmitOptions]; }[keyof Events & string];
|
|
1848
|
+
/** Returned by `service.socket()`. Emits and kicks reach every process through the app's broker. */
|
|
1849
|
+
interface BrickSocketHandle<Output> {
|
|
1850
|
+
readonly name: string;
|
|
1851
|
+
/** Validates and serializes once, then sends to every connection in the rooms. At most once, unacknowledged. */
|
|
1852
|
+
emit(rooms: string | readonly string[], ...args: SocketEmitArgs<Output>): void;
|
|
1853
|
+
/** Removes connections from a room; `also` limits it to connections also in that room. */
|
|
1854
|
+
kick(room: string, options?: {
|
|
1855
|
+
also?: string;
|
|
1856
|
+
reason?: string;
|
|
1857
|
+
}): void;
|
|
1858
|
+
}
|
|
1859
|
+
interface SocketReplay {
|
|
1860
|
+
/** Emits kept per room. */
|
|
1861
|
+
keep: number;
|
|
1862
|
+
/** How long a room's history lasts without new emits. */
|
|
1863
|
+
maxAgeMs: number;
|
|
1864
|
+
}
|
|
1865
|
+
type SocketBrokerMessage =
|
|
1866
|
+
/** `seq` is set by the broker for sockets with replay: `<number>-<number>`, rising per room. */
|
|
1867
|
+
{
|
|
1868
|
+
type: "emit";
|
|
1869
|
+
topic: string;
|
|
1870
|
+
frame: string;
|
|
1871
|
+
seq?: string;
|
|
1872
|
+
} | {
|
|
1873
|
+
type: "kick";
|
|
1874
|
+
topic: string;
|
|
1875
|
+
also?: string;
|
|
1876
|
+
reason?: string;
|
|
1877
|
+
};
|
|
1878
|
+
/** Carries emits and kicks between processes. Topics are `<service>.<socket>:<room>`. */
|
|
1879
|
+
interface SocketBroker {
|
|
1880
|
+
/**
|
|
1881
|
+
* Sends to every subscriber of `message.topic`, in every process, this one
|
|
1882
|
+
* included. With `replay`, also keeps the emit and gives it a `seq`.
|
|
1883
|
+
*/
|
|
1884
|
+
publish(message: SocketBrokerMessage, replay?: SocketReplay): void;
|
|
1885
|
+
/** The current position of a room, below every later `seq`. Needed for replay. */
|
|
1886
|
+
head?(topic: string): string | Promise<string>;
|
|
1887
|
+
/** Kept emits after `since`, or undefined when the history no longer covers it. Needed for replay. */
|
|
1888
|
+
history?(topic: string, since: string, replay: SocketReplay): SocketBrokerMessage[] | undefined | Promise<SocketBrokerMessage[] | undefined>;
|
|
1889
|
+
/** Called once per local holder of a topic; returns its unsubscribe. */
|
|
1890
|
+
subscribe(topic: string, deliver: (message: SocketBrokerMessage) => void): () => void;
|
|
1891
|
+
/** Resolves once messages for a topic just subscribed reach this process. */
|
|
1892
|
+
ready?(topic: string): Promise<void>;
|
|
1893
|
+
close?(): void | Promise<void>;
|
|
1894
|
+
}
|
|
1895
|
+
interface BrickSocketReconnectOptions {
|
|
1896
|
+
/** Default 500. */
|
|
1897
|
+
minDelayMs?: number;
|
|
1898
|
+
/** Default 30 000. */
|
|
1899
|
+
maxDelayMs?: number;
|
|
1900
|
+
/** Random spread around each delay, 0 to 1. Default 0.2. */
|
|
1901
|
+
jitter?: number;
|
|
1902
|
+
}
|
|
1903
|
+
type BrickSocketStatus = "connecting" | "open" | "reconnecting" | "closed";
|
|
1904
|
+
interface BrickSocketConnectOptions {
|
|
1905
|
+
signal?: AbortSignal;
|
|
1906
|
+
/** Limits each opening attempt. */
|
|
1907
|
+
timeoutMs?: number;
|
|
1908
|
+
/** Called on every connect and reconnect; sent as the `brick.token.<token>` subprotocol. */
|
|
1909
|
+
token?: () => string | Promise<string>;
|
|
1910
|
+
/** On by default. `false` ends the client at the first close. */
|
|
1911
|
+
reconnect?: false | BrickSocketReconnectOptions;
|
|
1912
|
+
}
|
|
1913
|
+
declare class BrickSocketError extends Error {
|
|
1914
|
+
readonly kind: "connection" | "protocol" | "backpressure" | "refused";
|
|
1915
|
+
constructor(kind: "connection" | "protocol" | "backpressure" | "refused", message: string, options?: ErrorOptions);
|
|
1916
|
+
}
|
|
1917
|
+
/** An error a socket handler raised with `error.CODE()`, for the message `event` it was handling. */
|
|
1918
|
+
type BrickSocketHandlerError<Input = unknown, Errors = {}> = ActionExecutionError & {
|
|
1919
|
+
event: keyof Input & string;
|
|
1920
|
+
code: Extract<keyof Errors, string> | StandardErrorCode;
|
|
1921
|
+
};
|
|
1922
|
+
interface BrickSocketClient<Input, Output, Errors = {}> extends BrickSocketSender<Input> {
|
|
1923
|
+
/** Resolves on the first open; rejects if the client closes before it. */
|
|
1924
|
+
readonly ready: Promise<void>;
|
|
1925
|
+
readonly status: BrickSocketStatus;
|
|
1926
|
+
onStatus(listener: (status: BrickSocketStatus) => void): () => void;
|
|
1927
|
+
/** After each reconnect. Messages sent while offline are not replayed. */
|
|
1928
|
+
onReconnect(listener: () => void): () => void;
|
|
1929
|
+
on<K extends keyof Output & string>(event: K, listener: (payload: Output[K]) => void): () => void;
|
|
1930
|
+
/** Resolves when the server accepts the room; rejects with kind `refused` otherwise. */
|
|
1931
|
+
join(room: string): Promise<void>;
|
|
1932
|
+
leave(room: string): void;
|
|
1933
|
+
/** The server removed this connection from a room. */
|
|
1934
|
+
onLeave(listener: (room: string, reason?: string) => void): () => void;
|
|
1935
|
+
/** Replay could not cover what this client missed in a room while away: refetch it. */
|
|
1936
|
+
onGap(listener: (room: string) => void): () => void;
|
|
1937
|
+
onError(listener: (error: BrickSocketError) => void): () => void;
|
|
1938
|
+
/** A handler raised a declared error for a message this client sent. The connection stays open. */
|
|
1939
|
+
onHandlerError(listener: (error: BrickSocketHandlerError<Input, Errors>) => void): () => void;
|
|
1940
|
+
onClose(listener: (info: {
|
|
1941
|
+
code: number;
|
|
1942
|
+
reason: string;
|
|
1943
|
+
}) => void): () => void;
|
|
1944
|
+
}
|
|
1945
|
+
/** Browser metadata. Schemas retain TypeBox kinds as JSON-safe annotations. */
|
|
1946
|
+
interface BrickClientSocket<Input = unknown, Output = unknown, Errors = {}> {
|
|
1947
|
+
kind: "socket";
|
|
1948
|
+
name: string;
|
|
1949
|
+
serviceName: string;
|
|
1950
|
+
config: {
|
|
1951
|
+
path: string;
|
|
1952
|
+
};
|
|
1953
|
+
output: Record<string, unknown>;
|
|
1954
|
+
readonly __types?: {
|
|
1955
|
+
input: Input;
|
|
1956
|
+
output: Output;
|
|
1957
|
+
errors: Errors;
|
|
1958
|
+
};
|
|
1959
|
+
}
|
|
1960
|
+
interface BrickSocketSchema {
|
|
1961
|
+
name: string;
|
|
1962
|
+
path?: string;
|
|
1963
|
+
description?: string;
|
|
1964
|
+
input: string[];
|
|
1965
|
+
output: string[];
|
|
1966
|
+
hasAuthorize: boolean;
|
|
1967
|
+
}
|
|
1968
|
+
interface SocketSender {
|
|
1969
|
+
readonly isOpen: boolean;
|
|
1970
|
+
send(event: string, payload: unknown): void;
|
|
1971
|
+
close(): void;
|
|
1972
|
+
}
|
|
1973
|
+
interface SocketSession {
|
|
1974
|
+
/** Server rooms, resolved from the handshake context. */
|
|
1975
|
+
rooms: readonly string[];
|
|
1976
|
+
open(socket: SocketSender): Promise<void>;
|
|
1977
|
+
message(event: string, data: unknown, socket: SocketSender): Promise<void>;
|
|
1978
|
+
join(room: string): Promise<boolean>;
|
|
1979
|
+
close(socket: SocketSender, code: number, reason: string): Promise<void>;
|
|
1980
|
+
}
|
|
1981
|
+
interface RegisteredSocket {
|
|
1982
|
+
schema: BrickSocketSchema;
|
|
1983
|
+
input: SocketSchemas;
|
|
1984
|
+
output: SocketSchemas;
|
|
1985
|
+
/** The errors its handlers declare, for the client contract. */
|
|
1986
|
+
errors?: Record<string, ActionErrorDefinition>;
|
|
1987
|
+
allowedOrigins?: readonly string[];
|
|
1988
|
+
allowMissingOrigin?: boolean;
|
|
1989
|
+
maxRooms: number;
|
|
1990
|
+
replay?: SocketReplay;
|
|
1991
|
+
/** Brokers of the apps this socket is mounted in. */
|
|
1992
|
+
brokers: Set<SocketBroker>;
|
|
1993
|
+
handle: BrickSocketHandle<any>;
|
|
1994
|
+
topicPrefix: string;
|
|
1995
|
+
topic(room: string): string;
|
|
1996
|
+
connect(request: Request): Promise<SocketSession>;
|
|
1997
|
+
/** A plugin's deps, for a socket its setup declared. */
|
|
1998
|
+
deps?: Readonly<Record<string, unknown>>;
|
|
1999
|
+
}
|
|
2000
|
+
/**
|
|
2001
|
+
* The logger on action, tool, and endpoint contexts. While a dashboard is on
|
|
2002
|
+
* (`brick dev`, or `brick start` with the `dashboard` option) a line logged
|
|
2003
|
+
* during a traced request appears in the dashboard with that request, instead
|
|
2004
|
+
* of on the console (as does a plain `console.log`); anywhere else it prints
|
|
2005
|
+
* to the console.
|
|
2006
|
+
*/
|
|
2007
|
+
interface Logger {
|
|
2008
|
+
info: (...args: any[]) => void;
|
|
2009
|
+
warn: (...args: any[]) => void;
|
|
2010
|
+
error: (...args: any[]) => void;
|
|
2011
|
+
debug: (...args: any[]) => void;
|
|
2012
|
+
}
|
|
2013
|
+
/**
|
|
2014
|
+
* Base framework context supplied to all service resolvers and action executions.
|
|
2015
|
+
*/
|
|
2016
|
+
interface BaseContext<TDb = any> {
|
|
2017
|
+
request: Request;
|
|
2018
|
+
db: TDb;
|
|
2019
|
+
logger: Logger;
|
|
2020
|
+
traceId: string;
|
|
2021
|
+
requestId?: string;
|
|
2022
|
+
caller?: string;
|
|
2023
|
+
tenantId?: string;
|
|
2024
|
+
services?: Record<string, any>;
|
|
2025
|
+
metadata?: Record<string, unknown>;
|
|
2026
|
+
}
|
|
2027
|
+
/**
|
|
2028
|
+
* Default fallback service context for untyped or simple services.
|
|
2029
|
+
*/
|
|
2030
|
+
interface DefaultServiceContext {
|
|
2031
|
+
user?: any;
|
|
2032
|
+
session?: any;
|
|
2033
|
+
[key: string]: any;
|
|
2034
|
+
}
|
|
2035
|
+
/**
|
|
2036
|
+
* The combined execution context: BaseContext & TServiceContext.
|
|
2037
|
+
*/
|
|
2038
|
+
type ActionContext<TDb = any, TServiceContext = DefaultServiceContext> = BaseContext<TDb> & TServiceContext;
|
|
2039
|
+
/**
|
|
2040
|
+
* Type utility that automatically narrows ctx.user from User | null to User
|
|
2041
|
+
* in execute() when authorize() is declared on the action.
|
|
2042
|
+
*/
|
|
2043
|
+
type NarrowAuthorizedContext<TCtx> = TCtx extends {
|
|
2044
|
+
user: infer U;
|
|
2045
|
+
} ? [U] extends [null | undefined] ? TCtx : Omit<TCtx, "user"> & {
|
|
2046
|
+
user: NonNullable<U>;
|
|
2047
|
+
} : TCtx;
|
|
2048
|
+
interface ActionErrorDefinition {
|
|
2049
|
+
status?: number;
|
|
2050
|
+
message: string;
|
|
2051
|
+
/** Schema of the `details` the error carries. Checked in types only, not at runtime. */
|
|
2052
|
+
details?: TSchema;
|
|
2053
|
+
}
|
|
2054
|
+
/** The `details` type of a declared error: its schema's type, or `unknown` when it has none. */
|
|
2055
|
+
type ErrorDetails<TDef> = TDef extends {
|
|
2056
|
+
details: infer D extends TSchema;
|
|
2057
|
+
} ? Static<D> : unknown;
|
|
2058
|
+
interface ValidationErrorItem {
|
|
2059
|
+
path: string;
|
|
2060
|
+
message: string;
|
|
2061
|
+
value?: unknown;
|
|
2062
|
+
type?: number;
|
|
2063
|
+
}
|
|
2064
|
+
declare class ValidationError extends Error {
|
|
2065
|
+
readonly code = "VALIDATION_FAILED";
|
|
2066
|
+
readonly status: number;
|
|
2067
|
+
readonly errors: ValidationErrorItem[];
|
|
2068
|
+
constructor(message: string, errors?: ValidationErrorItem[], status?: number);
|
|
2069
|
+
toJSON(): {
|
|
2070
|
+
name: string;
|
|
2071
|
+
code: string;
|
|
2072
|
+
message: string;
|
|
2073
|
+
status: number;
|
|
2074
|
+
errors: ValidationErrorItem[];
|
|
2075
|
+
};
|
|
2076
|
+
}
|
|
2077
|
+
declare class ActionExecutionError extends Error {
|
|
2078
|
+
readonly code: string;
|
|
2079
|
+
readonly status: number;
|
|
2080
|
+
readonly details?: unknown;
|
|
2081
|
+
constructor(code: string, message: string, status?: number, details?: unknown, options?: ErrorOptions);
|
|
2082
|
+
toJSON(): {
|
|
2083
|
+
name: string;
|
|
2084
|
+
code: string;
|
|
2085
|
+
message: string;
|
|
2086
|
+
status: number;
|
|
2087
|
+
details: unknown;
|
|
2088
|
+
};
|
|
2089
|
+
}
|
|
2090
|
+
/**
|
|
2091
|
+
* Two declarations claim one name or URL segment. Startup throws it; `brick lint`
|
|
2092
|
+
* reports it as `unique-names`. Compare `name`, since a second copy of core
|
|
2093
|
+
* throws its own class.
|
|
2094
|
+
*/
|
|
2095
|
+
declare class DuplicateNameError extends Error {
|
|
2096
|
+
constructor(message: string);
|
|
2097
|
+
}
|
|
2098
|
+
interface StandardErrorMap {
|
|
2099
|
+
BAD_REQUEST: (overrideMessage?: string, details?: unknown, options?: ErrorOptions) => never;
|
|
2100
|
+
UNAUTHORIZED: (overrideMessage?: string, details?: unknown, options?: ErrorOptions) => never;
|
|
2101
|
+
FORBIDDEN: (overrideMessage?: string, details?: unknown, options?: ErrorOptions) => never;
|
|
2102
|
+
NOT_FOUND: (overrideMessage?: string, details?: unknown, options?: ErrorOptions) => never;
|
|
2103
|
+
CONFLICT: (overrideMessage?: string, details?: unknown, options?: ErrorOptions) => never;
|
|
2104
|
+
INTERNAL_SERVER_ERROR: (overrideMessage?: string, details?: unknown, options?: ErrorOptions) => never;
|
|
2105
|
+
[key: string]: ((overrideMessage?: string, details?: unknown, options?: ErrorOptions) => never) | undefined;
|
|
2106
|
+
}
|
|
2107
|
+
type ErrorBuilder<TErrors extends Record<string, ActionErrorDefinition>> = StandardErrorMap & { [K in keyof TErrors]: TErrors[K] extends {
|
|
2108
|
+
details: TSchema;
|
|
2109
|
+
} ? (overrideMessage: string | undefined, details: ErrorDetails<TErrors[K]>, options?: ErrorOptions) => never : (overrideMessage?: string, details?: unknown, options?: ErrorOptions) => never; };
|
|
2110
|
+
interface BaseActionConfig<TInputSchema extends TSchema | undefined = undefined, TOutputSchema extends TSchema | undefined = undefined, TErrors extends Record<string, ActionErrorDefinition> = Record<string, ActionErrorDefinition>> {
|
|
2111
|
+
name: string;
|
|
2112
|
+
description?: string;
|
|
2113
|
+
/**
|
|
2114
|
+
* HTTP path relative to the action's owner (`/api/<service>` for a service
|
|
2115
|
+
* action, `/api/<service>/<resource>` for a resource action); replaces the
|
|
2116
|
+
* default `/<action>` segment. Path parameters (`/:id/summary`) are merged
|
|
2117
|
+
* into the input.
|
|
2118
|
+
*/
|
|
2119
|
+
path?: string;
|
|
2120
|
+
/** Single HTTP method exposed for this action (default POST). */
|
|
2121
|
+
method?: HttpMethod;
|
|
2122
|
+
input?: TInputSchema;
|
|
2123
|
+
output?: TOutputSchema;
|
|
2124
|
+
errors?: TErrors;
|
|
2125
|
+
emits?: string[];
|
|
2126
|
+
tags?: string[];
|
|
2127
|
+
}
|
|
2128
|
+
interface ActionConfigWithAuthorize<TInputSchema extends TSchema | undefined = undefined, TOutputSchema extends TSchema | undefined = undefined, TErrors extends Record<string, ActionErrorDefinition> = Record<string, ActionErrorDefinition>, TContext extends ActionContext = ActionContext> extends BaseActionConfig<TInputSchema, TOutputSchema, TErrors> {
|
|
2129
|
+
authorize: (params: {
|
|
2130
|
+
user?: TContext extends {
|
|
2131
|
+
user: infer U;
|
|
2132
|
+
} ? U : any;
|
|
2133
|
+
input: TInputSchema extends TSchema ? Static<TInputSchema> : any;
|
|
2134
|
+
ctx: TContext;
|
|
2135
|
+
}) => boolean | Promise<boolean>;
|
|
2136
|
+
execute: (params: {
|
|
2137
|
+
input: TInputSchema extends TSchema ? Static<TInputSchema> : any;
|
|
2138
|
+
ctx: NarrowAuthorizedContext<TContext>;
|
|
2139
|
+
error: ErrorBuilder<TErrors>;
|
|
2140
|
+
}) => Promise<TOutputSchema extends TSchema ? Static<TOutputSchema> : any> | (TOutputSchema extends TSchema ? Static<TOutputSchema> : any);
|
|
2141
|
+
}
|
|
2142
|
+
interface ActionConfigWithoutAuthorize<TInputSchema extends TSchema | undefined = undefined, TOutputSchema extends TSchema | undefined = undefined, TErrors extends Record<string, ActionErrorDefinition> = Record<string, ActionErrorDefinition>, TContext extends ActionContext = ActionContext> extends BaseActionConfig<TInputSchema, TOutputSchema, TErrors> {
|
|
2143
|
+
authorize?: undefined;
|
|
2144
|
+
execute: (params: {
|
|
2145
|
+
input: TInputSchema extends TSchema ? Static<TInputSchema> : any;
|
|
2146
|
+
ctx: TContext;
|
|
2147
|
+
error: ErrorBuilder<TErrors>;
|
|
2148
|
+
}) => Promise<TOutputSchema extends TSchema ? Static<TOutputSchema> : any> | (TOutputSchema extends TSchema ? Static<TOutputSchema> : any);
|
|
2149
|
+
}
|
|
2150
|
+
type ActionConfig<TInputSchema extends TSchema | undefined = any, TOutputSchema extends TSchema | undefined = any, TErrors extends Record<string, ActionErrorDefinition> = Record<string, ActionErrorDefinition>, TContext extends ActionContext = ActionContext> = ActionConfigWithAuthorize<TInputSchema, TOutputSchema, TErrors, TContext> | ActionConfigWithoutAuthorize<TInputSchema, TOutputSchema, TErrors, TContext>;
|
|
2151
|
+
interface Action<TInputSchema extends TSchema | undefined = any, TOutputSchema extends TSchema | undefined = any, TErrors extends Record<string, ActionErrorDefinition> = Record<string, ActionErrorDefinition>, TContext extends ActionContext = ActionContext> {
|
|
2152
|
+
(params: {
|
|
2153
|
+
input?: TInputSchema extends TSchema ? Static<TInputSchema> : any;
|
|
2154
|
+
ctx?: Partial<TContext>;
|
|
2155
|
+
}): Promise<TOutputSchema extends TSchema ? Static<TOutputSchema> : any>;
|
|
2156
|
+
name: string;
|
|
2157
|
+
serviceName?: string;
|
|
2158
|
+
config: ActionConfig<TInputSchema, TOutputSchema, TErrors, TContext>;
|
|
2159
|
+
run(input?: TInputSchema extends TSchema ? Static<TInputSchema> : any, ctx?: Partial<TContext>): Promise<TOutputSchema extends TSchema ? Static<TOutputSchema> : any>;
|
|
2160
|
+
execute(params: {
|
|
2161
|
+
input?: TInputSchema extends TSchema ? Static<TInputSchema> : any;
|
|
2162
|
+
ctx?: Partial<TContext>;
|
|
2163
|
+
}): Promise<TOutputSchema extends TSchema ? Static<TOutputSchema> : any>;
|
|
2164
|
+
validateInput(input: unknown): {
|
|
2165
|
+
success: true;
|
|
2166
|
+
data: any;
|
|
2167
|
+
} | {
|
|
2168
|
+
success: false;
|
|
2169
|
+
errors: ValidationErrorItem[];
|
|
2170
|
+
};
|
|
2171
|
+
validateOutput(output: unknown): {
|
|
2172
|
+
success: true;
|
|
2173
|
+
data: any;
|
|
2174
|
+
} | {
|
|
2175
|
+
success: false;
|
|
2176
|
+
errors: ValidationErrorItem[];
|
|
2177
|
+
};
|
|
2178
|
+
errorHelpers?: ErrorBuilder<TErrors>;
|
|
2179
|
+
}
|
|
2180
|
+
/** MCP tool metadata, kept with the executable definition. */
|
|
2181
|
+
interface ToolAnnotations {
|
|
2182
|
+
title?: string;
|
|
2183
|
+
readOnlyHint?: boolean;
|
|
2184
|
+
destructiveHint?: boolean;
|
|
2185
|
+
idempotentHint?: boolean;
|
|
2186
|
+
openWorldHint?: boolean;
|
|
2187
|
+
}
|
|
2188
|
+
interface ToolOptions {
|
|
2189
|
+
title?: string;
|
|
2190
|
+
annotations?: ToolAnnotations;
|
|
2191
|
+
/** Also register HTTP routes and OpenAPI. Defaults to false. */
|
|
2192
|
+
http?: boolean;
|
|
2193
|
+
}
|
|
2194
|
+
type ToolConfigWithAuthorize<I extends TSchema | undefined = undefined, O extends TSchema | undefined = undefined, E extends Record<string, ActionErrorDefinition> = Record<string, ActionErrorDefinition>, C extends ActionContext = ActionContext> = ActionConfigWithAuthorize<I, O, E, C> & ToolOptions;
|
|
2195
|
+
type ToolConfigWithoutAuthorize<I extends TSchema | undefined = undefined, O extends TSchema | undefined = undefined, E extends Record<string, ActionErrorDefinition> = Record<string, ActionErrorDefinition>, C extends ActionContext = ActionContext> = ActionConfigWithoutAuthorize<I, O, E, C> & ToolOptions;
|
|
2196
|
+
interface Tool<I extends TSchema | undefined = any, O extends TSchema | undefined = any, E extends Record<string, ActionErrorDefinition> = Record<string, ActionErrorDefinition>, C extends ActionContext = ActionContext> extends Action<I, O, E, C> {
|
|
2197
|
+
readonly kind: "tool";
|
|
2198
|
+
config: ToolConfigWithAuthorize<I, O, E, C> | ToolConfigWithoutAuthorize<I, O, E, C>;
|
|
2199
|
+
}
|
|
2200
|
+
/** A resource declares identity and a table; plugins and its own `action`/`command` supply behavior. */
|
|
2201
|
+
interface ResourceConfig<TTable = any, _TCtx = any> {
|
|
2202
|
+
name: string;
|
|
2203
|
+
table: TTable;
|
|
2204
|
+
id?: import("drizzle-orm").Column;
|
|
2205
|
+
/**
|
|
2206
|
+
* Child tables of this resource, by name. A command can load them with its
|
|
2207
|
+
* row (`load: { children: ["items"] }`) and insert, update and delete their
|
|
2208
|
+
* rows in the same transaction.
|
|
2209
|
+
*/
|
|
2210
|
+
children?: Record<string, ResourceChild>;
|
|
2211
|
+
}
|
|
2212
|
+
/** A child table: rows that belong to one row of their resource. */
|
|
2213
|
+
interface ResourceChild<TTable = any> {
|
|
2214
|
+
table: TTable;
|
|
2215
|
+
/** The child's column that holds the parent row's id. */
|
|
2216
|
+
parent: import("drizzle-orm").Column;
|
|
2217
|
+
/** The child's id column. Defaults to its single primary key. */
|
|
2218
|
+
id?: import("drizzle-orm").Column;
|
|
2219
|
+
}
|
|
2220
|
+
type InferTableRow<TTable> = TTable extends {
|
|
2221
|
+
$inferSelect: infer S;
|
|
2222
|
+
} ? S : any;
|
|
2223
|
+
type InferTableInsert<TTable> = TTable extends {
|
|
2224
|
+
$inferInsert: infer S;
|
|
2225
|
+
} ? S : any;
|
|
2226
|
+
type ResourceTypeOptions = Pick<ResourceConfig, "name" | "id" | "children">;
|
|
2227
|
+
type PrimaryColumn<T> = T extends {
|
|
2228
|
+
_: {
|
|
2229
|
+
columns: infer C;
|
|
2230
|
+
};
|
|
2231
|
+
} ? { [K in keyof C]: C[K] extends {
|
|
2232
|
+
_: {
|
|
2233
|
+
isPrimaryKey: true;
|
|
2234
|
+
};
|
|
2235
|
+
} ? C[K] : never; }[keyof C] : import("drizzle-orm").Column;
|
|
2236
|
+
type ResourceIdentity<T, O> = O & {
|
|
2237
|
+
id: O extends {
|
|
2238
|
+
id: infer I extends import("drizzle-orm").Column;
|
|
2239
|
+
} ? I : PrimaryColumn<T>;
|
|
2240
|
+
};
|
|
2241
|
+
/** Generic preparation declarations consumed by the application compiler. */
|
|
2242
|
+
interface PreparationContribution {
|
|
2243
|
+
readonly name: string;
|
|
2244
|
+
describe(): unknown;
|
|
2245
|
+
prepare(): void;
|
|
2246
|
+
}
|
|
2247
|
+
interface Resource<TTable = any, TCtx extends ActionContext = ActionContext, TOptions = {}, TEvents extends EventCatalog = EventCatalog> {
|
|
2248
|
+
use<P extends ResourcePlugin<Service<TCtx["db"], Omit<TCtx, keyof BaseContext>>, this, any>>(plugin: P): this & ResourcePluginAPI<P, TTable, TCtx, this>;
|
|
2249
|
+
readonly name: string;
|
|
2250
|
+
readonly serviceName: string;
|
|
2251
|
+
readonly table: TTable;
|
|
2252
|
+
readonly id: TOptions extends {
|
|
2253
|
+
id: infer I extends import("drizzle-orm").Column;
|
|
2254
|
+
} ? I : import("drizzle-orm").Column;
|
|
2255
|
+
readonly idField: string;
|
|
2256
|
+
readonly config: ResourceConfig<TTable, TCtx> & TOptions;
|
|
2257
|
+
/**
|
|
2258
|
+
* Declare an action owned by this resource, registered as
|
|
2259
|
+
* `<resource>.<name>`. It mounts at `/api/<service>/<resource>/<name>`, or at
|
|
2260
|
+
* `/api/<service>/<resource>/:id/<name>` with `record: true`.
|
|
2261
|
+
*/
|
|
2262
|
+
action<TIn extends TSchema | undefined = undefined, TOut extends TSchema | undefined = undefined, TErr extends Record<string, ActionErrorDefinition> = Record<string, ActionErrorDefinition>, const TRecord extends boolean = false>(config: ResourceActionConfig<TIn, TOut, TErr, TCtx, InferTableRow<TTable>, TRecord>): Action<TRecord extends true ? TIntersect<[TIn extends TSchema ? TIn : TObject<{}>, TObject<{
|
|
2263
|
+
id: TUnion<[TString, TNumber]>;
|
|
2264
|
+
}>]> : TIn, TOut, TErr, TCtx>;
|
|
2265
|
+
/**
|
|
2266
|
+
* Declare a command: a record action whose body is the pure `decide`. Brick
|
|
2267
|
+
* loads the row (and the child rows it asks for) in a transaction, runs
|
|
2268
|
+
* `decide`, applies the changes it returns, records the events it emits in
|
|
2269
|
+
* the same transaction, and commits. Registered as `<resource>.<name>` at
|
|
2270
|
+
* `POST /api/<service>/<resource>/:id/<name>`.
|
|
2271
|
+
*/
|
|
2272
|
+
command<TIn extends TSchema | undefined = undefined, TErr extends Record<string, ActionErrorDefinition> = {}, const TLoad extends readonly ChildNames<TOptions>[] = [], TOut extends TSchema | undefined = undefined>(name: string, config: CommandConfig<TIn, TOut, TErr, TCtx, CommandRow<TTable, TOptions, TLoad[number]>, CommandChildren<TOptions>, TEvents, TLoad>): Command<TIn, TOut, TErr, TCtx, CommandRow<TTable, TOptions, TLoad[number]>, CommandChildren<TOptions>, TEvents>;
|
|
2273
|
+
}
|
|
2274
|
+
type ChildNames<TOptions> = TOptions extends {
|
|
2275
|
+
children: infer C extends Record<string, ResourceChild>;
|
|
2276
|
+
} ? keyof C & string : never;
|
|
2277
|
+
/** The child tables of a resource, by name. */
|
|
2278
|
+
type CommandChildren<TOptions> = TOptions extends {
|
|
2279
|
+
children: infer C extends Record<string, ResourceChild>;
|
|
2280
|
+
} ? { [K in keyof C]: C[K]["table"]; } : {};
|
|
2281
|
+
/** The row `decide` receives: the resource's row with the children it loads. */
|
|
2282
|
+
type CommandRow<TTable, TOptions, TLoaded extends string> = InferTableRow<TTable> & { [K in TLoaded & keyof CommandChildren<TOptions>]: InferTableRow<CommandChildren<TOptions>[K]>[]; };
|
|
2283
|
+
/** The changes to one child table. Updates and deletes name rows by child id. */
|
|
2284
|
+
interface ChildChanges<TChild = any> {
|
|
2285
|
+
/** New rows. Their parent column is set to the parent's id. */
|
|
2286
|
+
insert?: Partial<InferTableInsert<TChild>>[];
|
|
2287
|
+
/** Partial rows, each with the child id; only rows of this parent change. */
|
|
2288
|
+
update?: Partial<InferTableRow<TChild>>[];
|
|
2289
|
+
/** Child ids to delete; only rows of this parent are deleted. */
|
|
2290
|
+
delete?: (string | number)[];
|
|
2291
|
+
}
|
|
2292
|
+
/** What `decide` returns when the command goes ahead. */
|
|
2293
|
+
interface CommandDecision<TRow = any, TChildren = {}, TEvents extends EventCatalog = EventCatalog> {
|
|
2294
|
+
/** Columns of the parent row to change. */
|
|
2295
|
+
set?: Partial<Omit<TRow, keyof TChildren>>;
|
|
2296
|
+
/** Inserts, updates and deletes per child table. */
|
|
2297
|
+
children?: { [K in keyof TChildren]?: ChildChanges<TChildren[K]>; };
|
|
2298
|
+
/** Events to record with the write, by catalog name and payload. */
|
|
2299
|
+
emit?: { [K in keyof TEvents]?: Static<TEvents[K]>; };
|
|
2300
|
+
}
|
|
2301
|
+
/**
|
|
2302
|
+
* The `error` helpers `decide` receives. Unlike an action's, they return the
|
|
2303
|
+
* rejection instead of throwing, so `decide` stays a pure function.
|
|
2304
|
+
*/
|
|
2305
|
+
type CommandErrors<TErrors extends Record<string, ActionErrorDefinition>> = { [K in keyof TErrors | StandardErrorCode]: (overrideMessage?: string, details?: unknown) => ActionExecutionError; };
|
|
2306
|
+
/** `resource.command(name, config)`. */
|
|
2307
|
+
interface CommandConfig<TIn extends TSchema | undefined = undefined, TOut extends TSchema | undefined = undefined, TErr extends Record<string, ActionErrorDefinition> = {}, TCtx extends ActionContext = ActionContext, TRow = any, TChildren = {}, TEvents extends EventCatalog = EventCatalog, TLoad extends readonly string[] = readonly string[]> {
|
|
2308
|
+
description?: string;
|
|
2309
|
+
tags?: string[];
|
|
2310
|
+
/** The command's own input; `id` from the path joins it. */
|
|
2311
|
+
input?: TIn;
|
|
2312
|
+
/**
|
|
2313
|
+
* The reply: the updated row (with loaded children), cut down to this
|
|
2314
|
+
* schema's properties. Without it the reply is `{ id }`.
|
|
2315
|
+
*/
|
|
2316
|
+
output?: TOut;
|
|
2317
|
+
errors?: TErr;
|
|
2318
|
+
/** Which child tables to load with the row, by their name on the resource. */
|
|
2319
|
+
load?: {
|
|
2320
|
+
children?: TLoad;
|
|
2321
|
+
};
|
|
2322
|
+
/** For a resource without `crud`: which rows the command may load. */
|
|
2323
|
+
access?: ResourceActionAccess<TCtx>;
|
|
2324
|
+
authorize?: (params: {
|
|
2325
|
+
user?: TCtx extends {
|
|
2326
|
+
user: infer U;
|
|
2327
|
+
} ? U : any;
|
|
2328
|
+
input: ResourceActionInput<TIn, true>;
|
|
2329
|
+
ctx: TCtx;
|
|
2330
|
+
}) => boolean | Promise<boolean>;
|
|
2331
|
+
/**
|
|
2332
|
+
* The rules: a pure function of the loaded row and the input. Return
|
|
2333
|
+
* `error.CODE()` to reject (nothing is written), or the changes to make.
|
|
2334
|
+
*/
|
|
2335
|
+
decide: (row: TRow, input: ResourceActionInput<TIn, true>, helpers: {
|
|
2336
|
+
error: CommandErrors<TErr>;
|
|
2337
|
+
}) => CommandDecision<TRow, TChildren, TEvents> | ActionExecutionError;
|
|
2338
|
+
}
|
|
2339
|
+
/** A command: an action with its `decide`, callable without a database. */
|
|
2340
|
+
type Command<TIn extends TSchema | undefined = undefined, TOut extends TSchema | undefined = undefined, TErr extends Record<string, ActionErrorDefinition> = {}, TCtx extends ActionContext = ActionContext, TRow = any, TChildren = {}, TEvents extends EventCatalog = EventCatalog> = Action<TIntersect<[TIn extends TSchema ? TIn : TObject<{}>, TObject<{
|
|
2341
|
+
id: TUnion<[TString, TNumber]>;
|
|
2342
|
+
}>]>, TOut extends TSchema ? TOut : TObject<{
|
|
2343
|
+
id: TUnion<[TString, TNumber]>;
|
|
2344
|
+
}>, TErr, TCtx> & {
|
|
2345
|
+
/**
|
|
2346
|
+
* Run the rules alone, with the standard and declared error helpers: for
|
|
2347
|
+
* unit tests that need no database.
|
|
2348
|
+
*/
|
|
2349
|
+
decide(row: TRow, input: ResourceActionInput<TIn, true>): CommandDecision<TRow, TChildren, TEvents> | ActionExecutionError;
|
|
2350
|
+
};
|
|
2351
|
+
/**
|
|
2352
|
+
* Which rows a record action may load when its resource has no `crud`:
|
|
2353
|
+
* `"public"` loads any row by id, `scope` adds a SQL condition (such as the
|
|
2354
|
+
* caller's tenant). With `crud` installed, its `access` always applies instead.
|
|
2355
|
+
*/
|
|
2356
|
+
type ResourceActionAccess<TCtx = any> = "public" | {
|
|
2357
|
+
scope: (args: {
|
|
2358
|
+
ctx: TCtx;
|
|
2359
|
+
}) => import("drizzle-orm").SQL | undefined | Promise<import("drizzle-orm").SQL | undefined>;
|
|
2360
|
+
};
|
|
2361
|
+
/** A record action's input: its own fields and the `id` from the path. */
|
|
2362
|
+
type ResourceActionInput<TIn extends TSchema | undefined, TRecord extends boolean> = TRecord extends true ? (TIn extends TSchema ? Static<TIn> : {}) & {
|
|
2363
|
+
id: string | number;
|
|
2364
|
+
} : TIn extends TSchema ? Static<TIn> : any;
|
|
2365
|
+
/** `resource.action(config)`: an action owned by a resource. */
|
|
2366
|
+
interface ResourceActionConfig<TIn extends TSchema | undefined = undefined, TOut extends TSchema | undefined = undefined, TErr extends Record<string, ActionErrorDefinition> = Record<string, ActionErrorDefinition>, TCtx extends ActionContext = ActionContext, TRow = any, TRecord extends boolean = false> extends BaseActionConfig<TIn, TOut, TErr> {
|
|
2367
|
+
/**
|
|
2368
|
+
* `true` makes it a record action at `/:id/<name>`: `id` joins the input,
|
|
2369
|
+
* the row loads through the resource's access, and a missing or hidden row
|
|
2370
|
+
* answers 404 before `execute` runs.
|
|
2371
|
+
*/
|
|
2372
|
+
record?: TRecord;
|
|
2373
|
+
/** For a record action on a resource without `crud`: which rows it may load. */
|
|
2374
|
+
access?: ResourceActionAccess<TCtx>;
|
|
2375
|
+
authorize?: (params: {
|
|
2376
|
+
user?: TCtx extends {
|
|
2377
|
+
user: infer U;
|
|
2378
|
+
} ? U : any;
|
|
2379
|
+
input: ResourceActionInput<TIn, TRecord>;
|
|
2380
|
+
ctx: TCtx;
|
|
2381
|
+
}) => boolean | Promise<boolean>;
|
|
2382
|
+
execute: (params: {
|
|
2383
|
+
input: ResourceActionInput<TIn, TRecord>;
|
|
2384
|
+
ctx: TCtx;
|
|
2385
|
+
error: ErrorBuilder<TErr>;
|
|
2386
|
+
/** The loaded row of a record action; `undefined` otherwise. */
|
|
2387
|
+
row: TRecord extends true ? TRow : undefined;
|
|
2388
|
+
}) => Promise<TOut extends TSchema ? Static<TOut> : any> | (TOut extends TSchema ? Static<TOut> : any);
|
|
2389
|
+
}
|
|
2390
|
+
interface ServiceOptions<TServiceContext extends Record<string, any> = Record<string, any>, TDb = any> {
|
|
2391
|
+
database?: DatabaseHandle<TDb>;
|
|
2392
|
+
/**
|
|
2393
|
+
* What the service needs from the app, as typed slots: `{ carrier: port<Carrier>() }`.
|
|
2394
|
+
* `brick({ deps })` provides the values, and actions read them as `ctx.deps`.
|
|
2395
|
+
*/
|
|
2396
|
+
deps?: Ports;
|
|
2397
|
+
context?: (baseCtx: BaseContext<TDb> & {
|
|
2398
|
+
user?: unknown;
|
|
2399
|
+
}) => Promise<TServiceContext> | TServiceContext;
|
|
2400
|
+
/**
|
|
2401
|
+
* The service's event catalog: payload schema by event name. Each becomes
|
|
2402
|
+
* `service.events.<name>`, recorded as `<service>.<name>`.
|
|
2403
|
+
*/
|
|
2404
|
+
events?: EventCatalog;
|
|
2405
|
+
}
|
|
2406
|
+
type ActionAccessPolicy = "public" | "authorized" | "scoped";
|
|
2407
|
+
interface ActionSchema {
|
|
2408
|
+
name: string;
|
|
2409
|
+
/** Present only when this action has a registered HTTP route. */
|
|
2410
|
+
method?: HttpMethod;
|
|
2411
|
+
path?: string;
|
|
2412
|
+
serviceName?: string;
|
|
2413
|
+
/** Owning plugin descriptor name for plugin actions (e.g. `@brickkit/crud`). */
|
|
2414
|
+
plugin?: string;
|
|
2415
|
+
/** Owning resource name for resource actions (e.g. `page`). */
|
|
2416
|
+
resource?: string;
|
|
2417
|
+
description?: string;
|
|
2418
|
+
inputSchema?: unknown;
|
|
2419
|
+
outputSchema?: unknown;
|
|
2420
|
+
hasAuthorize: boolean;
|
|
2421
|
+
hasScope?: boolean;
|
|
2422
|
+
accessPolicy?: ActionAccessPolicy;
|
|
2423
|
+
errors?: Record<string, ActionErrorDefinition>;
|
|
2424
|
+
emits?: string[];
|
|
2425
|
+
tags?: string[];
|
|
2426
|
+
}
|
|
2427
|
+
interface ServiceSchema {
|
|
2428
|
+
name: string;
|
|
2429
|
+
/** The service options as JSON: functions, such as `context` and hooks, are left out. */
|
|
2430
|
+
options: Record<string, unknown>;
|
|
2431
|
+
hasDatabase: boolean;
|
|
2432
|
+
databaseConfig?: {
|
|
2433
|
+
name?: string;
|
|
2434
|
+
engine: "sqlite" | "postgres";
|
|
2435
|
+
tables: string[];
|
|
2436
|
+
/** The plugin that owns each table it contributed, by table key. */
|
|
2437
|
+
plugins?: Record<string, string>;
|
|
2438
|
+
};
|
|
2439
|
+
actions: ActionSchema[];
|
|
2440
|
+
sockets?: BrickSocketSchema[];
|
|
2441
|
+
/** MCP tools. A tool with an HTTP route is also listed under `actions`. */
|
|
2442
|
+
tools?: {
|
|
2443
|
+
name: string;
|
|
2444
|
+
}[];
|
|
2445
|
+
/** The event catalog: each event's wire name and payload JSON Schema. */
|
|
2446
|
+
events?: {
|
|
2447
|
+
name: string;
|
|
2448
|
+
schema: unknown;
|
|
2449
|
+
plugin?: string;
|
|
2450
|
+
}[];
|
|
2451
|
+
resources?: {
|
|
2452
|
+
name: string;
|
|
2453
|
+
}[];
|
|
2454
|
+
}
|
|
2455
|
+
interface ArchitectureSchema {
|
|
2456
|
+
version: string;
|
|
2457
|
+
services: ServiceSchema[];
|
|
2458
|
+
/** Present when the app has an `events` option. */
|
|
2459
|
+
events?: EventsDefinition;
|
|
2460
|
+
/** Every `definePlugin` installation, with its dashboard extension. */
|
|
2461
|
+
plugins?: InstalledPlugin[];
|
|
2462
|
+
}
|
|
2463
|
+
interface Service<TDb = BunSQLiteDatabase$1<Record<string, unknown>>, TServiceContext extends Record<string, any> = Record<string, any>, TEvents extends EventCatalog = EventCatalog> {
|
|
2464
|
+
readonly name: string;
|
|
2465
|
+
/** The declared events, as handles for `defineListener` and `emitEvent`. */
|
|
2466
|
+
readonly events: EventRefs<TEvents>;
|
|
2467
|
+
readonly options: ServiceOptions<TServiceContext, TDb>;
|
|
2468
|
+
/** The tables of the service's resources and plugins, by table name. */
|
|
2469
|
+
readonly tables: Map<string, any>;
|
|
2470
|
+
readonly db?: TDb;
|
|
2471
|
+
readonly isBuilt?: boolean;
|
|
2472
|
+
use<A>(plugin: ServicePlugin<this, A>): A;
|
|
2473
|
+
build(): this;
|
|
2474
|
+
socket<const I extends SocketSchemas, const O extends SocketSchemas, const E extends Record<string, ActionErrorDefinition> = {}>(config: BrickSocketConfig<I, O, BaseContext<TDb> & TServiceContext, E>): BrickSocketHandle<SocketPayloads<O>>;
|
|
2475
|
+
listSockets(): RegisteredSocket[];
|
|
2476
|
+
action<TIn extends TSchema | undefined = undefined, TOut extends TSchema | undefined = undefined, TErr extends Record<string, ActionErrorDefinition> = Record<string, ActionErrorDefinition>>(action: Action<TIn, TOut, TErr, BaseContext<TDb> & TServiceContext>): this;
|
|
2477
|
+
action<TIn extends TSchema | undefined = undefined, TOut extends TSchema | undefined = undefined, TErr extends Record<string, ActionErrorDefinition> = Record<string, ActionErrorDefinition>>(config: ActionConfigWithAuthorize<TIn, TOut, TErr, BaseContext<TDb> & TServiceContext>): Action<TIn, TOut, TErr, BaseContext<TDb> & TServiceContext>;
|
|
2478
|
+
action<TIn extends TSchema | undefined = undefined, TOut extends TSchema | undefined = undefined, TErr extends Record<string, ActionErrorDefinition> = Record<string, ActionErrorDefinition>>(config: ActionConfigWithoutAuthorize<TIn, TOut, TErr, BaseContext<TDb> & TServiceContext>): Action<TIn, TOut, TErr, BaseContext<TDb> & TServiceContext>;
|
|
2479
|
+
tool<TIn extends TSchema | undefined = undefined, TOut extends TSchema | undefined = undefined, TErr extends Record<string, ActionErrorDefinition> = Record<string, ActionErrorDefinition>>(action: Tool<TIn, TOut, TErr, BaseContext<TDb> & TServiceContext>): this;
|
|
2480
|
+
tool<TIn extends TSchema | undefined = undefined, TOut extends TSchema | undefined = undefined, TErr extends Record<string, ActionErrorDefinition> = Record<string, ActionErrorDefinition>>(config: ToolConfigWithAuthorize<TIn, TOut, TErr, BaseContext<TDb> & TServiceContext>): Tool<TIn, TOut, TErr, BaseContext<TDb> & TServiceContext>;
|
|
2481
|
+
tool<TIn extends TSchema | undefined = undefined, TOut extends TSchema | undefined = undefined, TErr extends Record<string, ActionErrorDefinition> = Record<string, ActionErrorDefinition>>(config: ToolConfigWithoutAuthorize<TIn, TOut, TErr, BaseContext<TDb> & TServiceContext>): Tool<TIn, TOut, TErr, BaseContext<TDb> & TServiceContext>;
|
|
2482
|
+
resource<TTable = any, const TOptions extends ResourceTypeOptions = ResourceTypeOptions>(config: ResourceConfig<TTable, BaseContext<TDb> & TServiceContext> & TOptions & Record<Exclude<keyof TOptions, keyof ResourceConfig>, never>): Resource<TTable, BaseContext<TDb> & TServiceContext, ResourceIdentity<TTable, TOptions>, TEvents>;
|
|
2483
|
+
contributePreparation(contribution: PreparationContribution): void;
|
|
2484
|
+
getAction(name: string): Action<any, any, any, any> | undefined;
|
|
2485
|
+
listActions(): Action<any, any, any, any>[];
|
|
2486
|
+
getTool(name: string): Tool<any, any, any, any> | undefined;
|
|
2487
|
+
listTools(): Tool<any, any, any, any>[];
|
|
2488
|
+
getResource(name: string): Resource<any, BaseContext<TDb> & TServiceContext> | undefined;
|
|
2489
|
+
listResources(): Resource<any, BaseContext<TDb> & TServiceContext>[];
|
|
2490
|
+
getDb(): TDb | undefined;
|
|
2491
|
+
/** The handle from `defineDatabase`, without connecting. */
|
|
2492
|
+
getDatabaseHandle(): DatabaseHandle<TDb> | undefined;
|
|
2493
|
+
setDb(database: DatabaseHandle<TDb>): void;
|
|
2494
|
+
/** What a write needs to record events. Set by `brick({ events })`; absent otherwise. */
|
|
2495
|
+
readonly recorder?: ServiceEvents;
|
|
2496
|
+
resolveContext(callCtx?: Partial<BaseContext<TDb> & TServiceContext>): Promise<BaseContext<TDb> & TServiceContext>;
|
|
2497
|
+
introspect(): ServiceSchema;
|
|
2498
|
+
}
|
|
2499
|
+
interface ProxyOptions {
|
|
2500
|
+
/** Name of the service creating this proxy. Populates ctx.caller on the target's envelope. */
|
|
2501
|
+
from?: string;
|
|
2502
|
+
context?: Partial<ActionContext>;
|
|
2503
|
+
}
|
|
2504
|
+
export { BrickSocketError as $, CrudEventOperation as $n, PluginRoutes as $t, Resource as A, listSecrets as An, PostgresDatabase as At, ServiceSchema as B, DispatchOptions as Bn, integer$1 as Bt, ErrorDetails as C, PluginSetupContext as Cn, DatabaseDefinition as Ct, NarrowAuthorizedContext as D, SecretSource as Dn, SQLiteDefinition as Dt, Logger as E, SecretRef as En, PostgresDefinition as Et, ResourceConfig as F, HostHooks as Fn, SQLiteDatabaseConfig as Ft, ToolConfigWithoutAuthorize as G, EventWorkflow as Gn, uniqueIndex as Gt, Tool as H, EventEnvelope as Hn, real$1 as Ht, ResourceIdentity as I, PluginHookArgs as In, blob as It, ValidationErrorItem as J, ListenerFailure as Jn, PLUGIN_KIND as Jt, ToolOptions as K, EventsOptions as Kn, ContextOf as Kt, ResourceTypeOptions as L, PluginHooks as Ln, createSQLiteDatabase as Lt, ResourceActionConfig as M, resetSecrets as Mn, createPostgresDatabase as Mt, ResourceActionInput as N, resolveSecrets as Nn, BrickSQLiteDatabase as Nt, PreparationContribution as O, defineSecret as On, defineDatabase as Ot, ResourceChild as P, PluginTableBuilders as Pn, BunSQLiteDatabase$1 as Pt, BrickSocketConnectOptions as Q, WorkflowsOptions as Qn, PluginRouteScope as Qt, Service as R, getHostHooks as Rn, foreignKey as Rt, ErrorBuilder as S, PluginServiceContext as Sn, isControlEvent as St, InferTableRow as T, SecretOptions as Tn, EnvInput as Tt, ToolAnnotations as U, EventListener as Un, sqliteTable$1 as Ut, StandardErrorMap as V, EventDelivery as Vn, primaryKey as Vt, ToolConfigWithAuthorize as W, EventRecorder as Wn, text$1 as Wt, BrickSocketClient as X, ServiceEvents as Xn, PluginRouteMethod as Xt, BrickClientSocket as Y, RetryDecision as Yn, PluginEndpointContext as Yt, BrickSocketConfig as Z, WorkflowEvent as Zn, PluginRouteRegistrar as Zt, CommandDecision as _, PluginInfo as _n, Dispatcher as _t, ActionConfigWithoutAuthorize as a, ServicePluginContext as an, isCrudEvent as ar, BrickSocketStatus as at, DefaultServiceContext as b, PluginLifecycleContext as bn, StandardErrorCode as bt, ActionExecutionError as c, ServicePort as cn, Ports as cr, SocketEmitArgs as ct, BaseActionConfig as d, PostgresOutboxTable as dn, HttpMethod as dr, SocketPayloads as dt, ResourcePlugin as en, EventCatalog as er, BrickSocketHandle as et, BaseContext as f, SQLiteOutboxTable as fn, SocketReplay as ft, CommandConfig as g, PluginFactory as gn, stopAllEvents as gt, CommandChildren as h, PluginContext as hn, EventsHub as ht, ActionConfigWithAuthorize as i, ServicePlugin as in, crudEventSource as ir, BrickSocketSender as it, ResourceActionAccess as j, overrideSecret as jn, PostgresDatabaseConfig as jt, ProxyOptions as k, envSource as kn, isDatabaseHandle as kt, ActionSchema as l, OutboxStatus as ln, port as lr, SocketEmitOptions as lt, Command as m, PluginAppContext as mn, EventsDefinition as mt, ActionAccessPolicy as n, ResourcePluginContext as nn, EventRefs as nr, BrickSocketReconnectOptions as nt, ActionContext as o, ActionAccessInfo as on, DepsOf as or, SocketBroker as ot, ChildChanges as p, outboxTable as pn, SocketSchemas as pt, ValidationError as q, EventsWorld as qn, InstalledPlugin as qt, ActionConfig as r, ResourcePluginTypes as rn, crudEventName as rr, BrickSocketSchema as rt, ActionErrorDefinition as s, ResourceActionInfo as sn, Port as sr, SocketBrokerMessage as st, Action as t, ResourcePluginAPI as tn, EventRef as tr, BrickSocketHandlerError as tt, ArchitectureSchema as u, OutboxTable as un, useDeps as ur, SocketEventArgs as ut, CommandErrors as v, PluginInstallOptions as vn, OutboxCounts as vt, InferTableInsert as w, definePlugin as wn, DatabaseHandle as wt, DuplicateNameError as x, PluginManifest as xn, WireError as xt, CommandRow as y, PluginInstance as yn, STANDARD_ERRORS as yt, ServiceOptions as z, CrudEventPayload as zn, index$1 as zt };
|
|
2505
|
+
//# sourceMappingURL=types.d.ts.map
|