Skip to content
This repository was archived by the owner on Oct 22, 2025. It is now read-only.

Commit 4512d9d

Browse files
committed
feat: sqlite
1 parent f0bf4f5 commit 4512d9d

File tree

17 files changed

+344
-116
lines changed

17 files changed

+344
-116
lines changed

packages/core/src/client/worker-common.ts

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,6 @@ import type {
22
AnyWorkerDefinition,
33
WorkerDefinition,
44
} from "@/worker/definition";
5-
import type * as protoHttpResolve from "@/worker/protocol/http/resolve";
6-
import type { Encoding } from "@/worker/protocol/serde";
7-
import type { WorkerQuery } from "@/manager/protocol/query";
8-
import { logger } from "./log";
9-
import * as errors from "./errors";
10-
import { sendHttpRequest } from "./utils";
11-
import {
12-
HEADER_WORKER_QUERY,
13-
HEADER_ENCODING,
14-
} from "@/worker/router-endpoints";
155

166
/**
177
* Action function returned by Worker connections and handles.
@@ -33,7 +23,8 @@ export type WorkerActionFunction<
3323
* Maps action methods from worker definition to typed function signatures.
3424
*/
3525
export type WorkerDefinitionActions<AD extends AnyWorkerDefinition> =
36-
AD extends WorkerDefinition<any, any, any, any, any, any, infer R>
26+
// biome-ignore lint/suspicious/noExplicitAny: safe to use any here
27+
AD extends WorkerDefinition<any, any, any, any, infer R, any, any, any>
3728
? {
3829
[K in keyof R]: R[K] extends (...args: infer Args) => infer Return
3930
? WorkerActionFunction<Args, Return>

packages/core/src/registry/config.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { z } from "zod";
55

66
export const WorkersSchema = z.record(
77
z.string(),
8-
z.custom<WorkerDefinition<any, any, any, any, any, any, any>>(),
8+
z.custom<WorkerDefinition<any, any, any, any, any, any, any, any>>(),
99
);
1010
export type RegistryWorkers = z.infer<typeof WorkersSchema>;
1111

@@ -21,6 +21,7 @@ export const RegistryConfigSchema = z.object({
2121
* Test configuration.
2222
*
2323
* DO NOT MANUALLY ENABLE. THIS IS USED INTERNALLY.
24+
* @internal
2425
**/
2526
test: TestConfigSchema.optional().default({ enabled: false }),
2627
});

packages/core/src/worker/action.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
1-
import type { AnyWorkerInstance } from "./instance";
21
import type { Conn } from "./connection";
32
import type { Logger } from "@/common/log";
43
import type { WorkerKey } from "@/common/utils";
54
import type { Schedule } from "./schedule";
65
import type { ConnId } from "./connection";
76
import type { SaveStateOptions } from "./instance";
8-
import { Actions } from "./config";
9-
import { WorkerContext } from "./context";
7+
import type { WorkerContext } from "./context";
108

119
/**
1210
* Context for a remote procedure call.
1311
*
1412
* @typeParam A Worker this action belongs to
1513
*/
16-
export class ActionContext<S, CP, CS, V, I, AD> {
17-
#workerContext: WorkerContext<S, CP, CS, V, I, AD>;
14+
export class ActionContext<S, CP, CS, V, I, AD, DB> {
15+
#workerContext: WorkerContext<S, CP, CS, V, I, AD, DB>;
1816

1917
/**
2018
* Should not be called directly.
@@ -23,8 +21,8 @@ export class ActionContext<S, CP, CS, V, I, AD> {
2321
* @param conn - The connection associated with the action
2422
*/
2523
constructor(
26-
workerContext: WorkerContext<S, CP, CS, V, I, AD>,
27-
public readonly conn: Conn<S, CP, CS, V, I, AD>,
24+
workerContext: WorkerContext<S, CP, CS, V, I, AD, DB>,
25+
public readonly conn: Conn<S, CP, CS, V, I, AD, DB>,
2826
) {
2927
this.#workerContext = workerContext;
3028
}
@@ -95,10 +93,17 @@ export class ActionContext<S, CP, CS, V, I, AD> {
9593
/**
9694
* Gets the map of connections.
9795
*/
98-
get conns(): Map<ConnId, Conn<S, CP, CS, V, I, AD>> {
96+
get conns(): Map<ConnId, Conn<S, CP, CS, V, I, AD, DB>> {
9997
return this.#workerContext.conns;
10098
}
10199

100+
/**
101+
* @experimental
102+
*/
103+
get db(): DB {
104+
return this.#workerContext.db;
105+
}
106+
102107
/**
103108
* Forces the state to get saved.
104109
*/

packages/core/src/worker/config.ts

Lines changed: 77 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export const WorkerConfigSchema = z
2424
connState: z.any().optional(),
2525
createConnState: z.function().optional(),
2626
vars: z.any().optional(),
27+
db: z.any().optional(),
2728
createVars: z.function().optional(),
2829
options: z
2930
.object({
@@ -98,11 +99,19 @@ export interface OnConnectOptions<CP> {
9899
// This must have only one or the other or else S will not be able to be inferred
99100
//
100101
// Data returned from this handler will be available on `c.state`.
101-
type CreateState<S, CP, CS, V, I, AD> =
102+
type CreateState<S, CP, CS, V, I, AD, DB> =
102103
| { state: S }
103104
| {
104105
createState: (
105-
c: WorkerContext<undefined, undefined, undefined, undefined, undefined, undefined>,
106+
c: WorkerContext<
107+
undefined,
108+
undefined,
109+
undefined,
110+
undefined,
111+
undefined,
112+
undefined,
113+
undefined
114+
>,
106115
opts: CreateStateOptions<I>,
107116
) => S | Promise<S>;
108117
}
@@ -113,11 +122,19 @@ type CreateState<S, CP, CS, V, I, AD> =
113122
// This must have only one or the other or else S will not be able to be inferred
114123
//
115124
// Data returned from this handler will be available on `c.conn.state`.
116-
type CreateConnState<S, CP, CS, V, I, AD> =
125+
type CreateConnState<S, CP, CS, V, I, AD, DB> =
117126
| { connState: CS }
118127
| {
119128
createConnState: (
120-
c: WorkerContext<undefined, undefined, undefined, undefined, undefined, undefined>,
129+
c: WorkerContext<
130+
undefined,
131+
undefined,
132+
undefined,
133+
undefined,
134+
undefined,
135+
undefined,
136+
undefined
137+
>,
121138
opts: OnConnectOptions<CP>,
122139
) => CS | Promise<CS>;
123140
}
@@ -129,7 +146,7 @@ type CreateConnState<S, CP, CS, V, I, AD> =
129146
/**
130147
* @experimental
131148
*/
132-
type CreateVars<S, CP, CS, V, I, AD> =
149+
type CreateVars<S, CP, CS, V, I, AD, DB> =
133150
| {
134151
/**
135152
* @experimental
@@ -141,15 +158,23 @@ type CreateVars<S, CP, CS, V, I, AD> =
141158
* @experimental
142159
*/
143160
createVars: (
144-
c: WorkerContext<undefined, undefined, undefined, undefined, undefined, undefined>,
161+
c: WorkerContext<
162+
undefined,
163+
undefined,
164+
undefined,
165+
undefined,
166+
undefined,
167+
undefined,
168+
undefined
169+
>,
145170
driverCtx: unknown,
146171
) => V | Promise<V>;
147172
}
148173
| Record<never, never>;
149174

150-
export interface Actions<S, CP, CS, V, I, AD> {
175+
export interface Actions<S, CP, CS, V, I, AD, DB> {
151176
[Action: string]: (
152-
c: ActionContext<S, CP, CS, V, I, AD>,
177+
c: ActionContext<S, CP, CS, V, I, AD, DB>,
153178
...args: any[]
154179
) => any;
155180
}
@@ -180,7 +205,8 @@ interface BaseWorkerConfig<
180205
V,
181206
I,
182207
AD,
183-
R extends Actions<S, CP, CS, V, I, AD>,
208+
DB,
209+
R extends Actions<S, CP, CS, V, I, AD, DB>,
184210
> {
185211
/**
186212
* Called on the HTTP server before clients can interact with the worker.
@@ -216,7 +242,7 @@ interface BaseWorkerConfig<
216242
* This is called before any other lifecycle hooks.
217243
*/
218244
onCreate?: (
219-
c: WorkerContext<S, CP, CS, V, I, AD>,
245+
c: WorkerContext<S, CP, CS, V, I, AD, DB>,
220246
opts: OnCreateOptions<I>,
221247
) => void | Promise<void>;
222248

@@ -228,7 +254,7 @@ interface BaseWorkerConfig<
228254
*
229255
* @returns Void or a Promise that resolves when startup is complete
230256
*/
231-
onStart?: (c: WorkerContext<S, CP, CS, V, I, AD>) => void | Promise<void>;
257+
onStart?: (c: WorkerContext<S, CP, CS, V, I, AD, DB>) => void | Promise<void>;
232258

233259
/**
234260
* Called when the worker's state changes.
@@ -238,7 +264,10 @@ interface BaseWorkerConfig<
238264
*
239265
* @param newState The updated state
240266
*/
241-
onStateChange?: (c: WorkerContext<S, CP, CS, V, I, AD>, newState: S) => void;
267+
onStateChange?: (
268+
c: WorkerContext<S, CP, CS, V, I, AD, DB>,
269+
newState: S,
270+
) => void;
242271

243272
/**
244273
* Called before a client connects to the worker.
@@ -261,7 +290,7 @@ interface BaseWorkerConfig<
261290
* @throws Throw an error to reject the connection
262291
*/
263292
onBeforeConnect?: (
264-
c: WorkerContext<S, CP, CS, V, I, AD>,
293+
c: WorkerContext<S, CP, CS, V, I, AD, DB>,
265294
opts: OnConnectOptions<CP>,
266295
) => void | Promise<void>;
267296

@@ -275,8 +304,8 @@ interface BaseWorkerConfig<
275304
* @returns Void or a Promise that resolves when connection handling is complete
276305
*/
277306
onConnect?: (
278-
c: WorkerContext<S, CP, CS, V, I, AD>,
279-
conn: Conn<S, CP, CS, V, I, AD>,
307+
c: WorkerContext<S, CP, CS, V, I, AD, DB>,
308+
conn: Conn<S, CP, CS, V, I, AD, DB>,
280309
) => void | Promise<void>;
281310

282311
/**
@@ -289,8 +318,8 @@ interface BaseWorkerConfig<
289318
* @returns Void or a Promise that resolves when disconnect handling is complete
290319
*/
291320
onDisconnect?: (
292-
c: WorkerContext<S, CP, CS, V, I, AD>,
293-
conn: Conn<S, CP, CS, V, I, AD>,
321+
c: WorkerContext<S, CP, CS, V, I, AD, DB>,
322+
conn: Conn<S, CP, CS, V, I, AD, DB>,
294323
) => void | Promise<void>;
295324

296325
/**
@@ -306,7 +335,7 @@ interface BaseWorkerConfig<
306335
* @returns The modified output to send to the client
307336
*/
308337
onBeforeActionResponse?: <Out>(
309-
c: WorkerContext<S, CP, CS, V, I, AD>,
338+
c: WorkerContext<S, CP, CS, V, I, AD, DB>,
310339
name: string,
311340
args: unknown[],
312341
output: Out,
@@ -315,10 +344,19 @@ interface BaseWorkerConfig<
315344
actions: R;
316345
}
317346

347+
type WorkerDatabaseConfig<DB> =
348+
| {
349+
/**
350+
* @experimental
351+
*/
352+
db: DB;
353+
}
354+
| Record<never, never>;
355+
318356
// 1. Infer schema
319357
// 2. Omit keys that we'll manually define (because of generics)
320358
// 3. Define our own types that have generic constraints
321-
export type WorkerConfig<S, CP, CS, V, I, AD> = Omit<
359+
export type WorkerConfig<S, CP, CS, V, I, AD, DB> = Omit<
322360
z.infer<typeof WorkerConfigSchema>,
323361
| "actions"
324362
| "onAuth"
@@ -335,11 +373,13 @@ export type WorkerConfig<S, CP, CS, V, I, AD> = Omit<
335373
| "createConnState"
336374
| "vars"
337375
| "createVars"
376+
| "db"
338377
> &
339-
BaseWorkerConfig<S, CP, CS, V, I, AD, Actions<S, CP, CS, V, I, AD>> &
340-
CreateState<S, CP, CS, V, I, AD> &
341-
CreateConnState<S, CP, CS, V, I, AD> &
342-
CreateVars<S, CP, CS, V, I, AD>;
378+
BaseWorkerConfig<S, CP, CS, V, I, AD, DB, Actions<S, CP, CS, V, I, AD, DB>> &
379+
CreateState<S, CP, CS, V, I, AD, DB> &
380+
CreateConnState<S, CP, CS, V, I, AD, DB> &
381+
CreateVars<S, CP, CS, V, I, AD, DB> &
382+
WorkerDatabaseConfig<DB>;
343383

344384
// See description on `WorkerConfig`
345385
export type WorkerConfigInput<
@@ -349,7 +389,8 @@ export type WorkerConfigInput<
349389
V,
350390
I,
351391
AD,
352-
R extends Actions<S, CP, CS, V, I, AD>,
392+
DB,
393+
R extends Actions<S, CP, CS, V, I, AD, DB>,
353394
> = Omit<
354395
z.input<typeof WorkerConfigSchema>,
355396
| "actions"
@@ -367,11 +408,13 @@ export type WorkerConfigInput<
367408
| "createConnState"
368409
| "vars"
369410
| "createVars"
411+
| "db"
370412
> &
371-
BaseWorkerConfig<S, CP, CS, V, I, AD, R> &
372-
CreateState<S, CP, CS, V, I, AD> &
373-
CreateConnState<S, CP, CS, V, I, AD> &
374-
CreateVars<S, CP, CS, V, I, AD>;
413+
BaseWorkerConfig<S, CP, CS, V, I, AD, DB, R> &
414+
CreateState<S, CP, CS, V, I, AD, DB> &
415+
CreateConnState<S, CP, CS, V, I, AD, DB> &
416+
CreateVars<S, CP, CS, V, I, AD, DB> &
417+
WorkerDatabaseConfig<DB>;
375418

376419
// For testing type definitions:
377420
export function test<
@@ -381,17 +424,19 @@ export function test<
381424
V,
382425
I,
383426
AD,
384-
R extends Actions<S, CP, CS, V, I, AD>,
427+
DB,
428+
R extends Actions<S, CP, CS, V, I, AD, DB>,
385429
>(
386-
input: WorkerConfigInput<S, CP, CS, V, I, AD, R>,
387-
): WorkerConfig<S, CP, CS, V, I, AD> {
430+
input: WorkerConfigInput<S, CP, CS, V, I, AD, DB, R>,
431+
): WorkerConfig<S, CP, CS, V, I, AD, DB> {
388432
const config = WorkerConfigSchema.parse(input) as WorkerConfig<
389433
S,
390434
CP,
391435
CS,
392436
V,
393437
I,
394-
AD
438+
AD,
439+
DB
395440
>;
396441
return config;
397442
}

0 commit comments

Comments
 (0)