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

Commit cec0ae4

Browse files
committed
feat: sqlite
1 parent 0a8dd91 commit cec0ae4

File tree

17 files changed

+702
-111
lines changed

17 files changed

+702
-111
lines changed

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

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

106
/**
117
* Action function returned by Worker connections and handles.
@@ -27,11 +23,10 @@ export type WorkerActionFunction<
2723
* Maps action methods from worker definition to typed function signatures.
2824
*/
2925
export type WorkerDefinitionActions<AD extends AnyWorkerDefinition> =
30-
AD extends WorkerDefinition<any, any, any, any, infer R>
26+
AD extends WorkerDefinition<any, any, any, any, infer R, any>
3127
? {
3228
[K in keyof R]: R[K] extends (...args: infer Args) => infer Return
3329
? WorkerActionFunction<Args, Return>
3430
: never;
3531
}
3632
: never;
37-

packages/core/src/registry/config.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export type WorkerPeerConfig = z.infer<typeof WorkerPeerConfigSchema>;
4343

4444
export const WorkersSchema = z.record(
4545
z.string(),
46-
z.custom<WorkerDefinition<any, any, any, any, any>>(),
46+
z.custom<WorkerDefinition<any, any, any, any, any, any>>(),
4747
);
4848
export type Workers = z.infer<typeof WorkersSchema>;
4949

@@ -75,10 +75,11 @@ export const RegistryConfigSchema = z.object({
7575
inspector: InspectorConfigSchema.optional().default({ enabled: false }),
7676

7777
// TODO: Find a better way of passing around the test config
78-
/**
78+
/**
7979
* Test configuration.
8080
*
8181
* DO NOT MANUALLY ENABLE. THIS IS USED INTERNALLY.
82+
* @internal
8283
**/
8384
test: TestConfigSchema.optional().default({ enabled: false }),
8485
});

packages/core/src/worker/action.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ import { WorkerContext } from "./context";
1313
*
1414
* @typeParam A Worker this action belongs to
1515
*/
16-
export class ActionContext<S, CP, CS, V> {
17-
#workerContext: WorkerContext<S, CP, CS, V>;
16+
export class ActionContext<S, CP, CS, V, DB> {
17+
#workerContext: WorkerContext<S, CP, CS, V, DB>;
1818

1919
/**
2020
* Should not be called directly.
@@ -23,8 +23,8 @@ export class ActionContext<S, CP, CS, V> {
2323
* @param conn - The connection associated with the action
2424
*/
2525
constructor(
26-
workerContext: WorkerContext<S, CP, CS, V>,
27-
public readonly conn: Conn<S, CP, CS, V>,
26+
workerContext: WorkerContext<S, CP, CS, V, DB>,
27+
public readonly conn: Conn<S, CP, CS, V, DB>,
2828
) {
2929
this.#workerContext = workerContext;
3030
}
@@ -95,10 +95,17 @@ export class ActionContext<S, CP, CS, V> {
9595
/**
9696
* Gets the map of connections.
9797
*/
98-
get conns(): Map<ConnId, Conn<S, CP, CS, V>> {
98+
get conns(): Map<ConnId, Conn<S, CP, CS, V, DB>> {
9999
return this.#workerContext.conns;
100100
}
101101

102+
/**
103+
* @experimental
104+
*/
105+
get db(): DB {
106+
return this.#workerContext.db;
107+
}
108+
102109
/**
103110
* Forces the state to get saved.
104111
*/

packages/core/src/worker/config.ts

Lines changed: 59 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export const WorkerConfigSchema = z
2323
connState: z.any().optional(),
2424
createConnState: z.function().optional(),
2525
vars: z.any().optional(),
26+
db: z.any().optional(),
2627
createVars: z.function().optional(),
2728
options: z
2829
.object({
@@ -95,11 +96,11 @@ export interface OnConnectOptions<CP> {
9596
// Creates state config
9697
//
9798
// This must have only one or the other or else S will not be able to be inferred
98-
type CreateState<S, CP, CS, V> =
99+
type CreateState<S, CP, CS, V, DB> =
99100
| { state: S }
100101
| {
101102
createState: (
102-
c: WorkerContext<undefined, undefined, undefined, undefined>,
103+
c: WorkerContext<undefined, undefined, undefined, undefined, undefined>,
103104
opts: CreateStateOptions,
104105
) => S | Promise<S>;
105106
}
@@ -108,11 +109,11 @@ type CreateState<S, CP, CS, V> =
108109
// Creates connection state config
109110
//
110111
// This must have only one or the other or else S will not be able to be inferred
111-
type CreateConnState<S, CP, CS, V> =
112+
type CreateConnState<S, CP, CS, V, DB> =
112113
| { connState: CS }
113114
| {
114115
createConnState: (
115-
c: WorkerContext<undefined, undefined, undefined, undefined>,
116+
c: WorkerContext<undefined, undefined, undefined, undefined, undefined>,
116117
opts: OnConnectOptions<CP>,
117118
) => CS | Promise<CS>;
118119
}
@@ -124,7 +125,7 @@ type CreateConnState<S, CP, CS, V> =
124125
/**
125126
* @experimental
126127
*/
127-
type CreateVars<S, CP, CS, V> =
128+
type CreateVars<S, CP, CS, V, DB> =
128129
| {
129130
/**
130131
* @experimental
@@ -136,30 +137,47 @@ type CreateVars<S, CP, CS, V> =
136137
* @experimental
137138
*/
138139
createVars: (
139-
c: WorkerContext<undefined, undefined, undefined, undefined>,
140+
c: WorkerContext<undefined, undefined, undefined, undefined, undefined>,
140141
driverCtx: unknown,
141142
) => V | Promise<V>;
142143
}
143144
| Record<never, never>;
144145

145-
export interface Actions<S, CP, CS, V> {
146-
[Action: string]: (c: ActionContext<S, CP, CS, V>, ...args: any[]) => any;
146+
/**
147+
* @experimental
148+
*/
149+
type DatabaseConfig<DB> = {
150+
/**
151+
* @experimental
152+
*/
153+
db?: DB;
154+
};
155+
156+
export interface Actions<S, CP, CS, V, DB> {
157+
[Action: string]: (c: ActionContext<S, CP, CS, V, DB>, ...args: any[]) => any;
147158
}
148159

149160
//export type WorkerConfig<S, CP, CS, V> = BaseWorkerConfig<S, CP, CS, V> &
150161
// WorkerConfigLifecycle<S, CP, CS, V> &
151162
// CreateState<S, CP, CS, V> &
152163
// CreateConnState<S, CP, CS, V>;
153164

154-
interface BaseWorkerConfig<S, CP, CS, V, R extends Actions<S, CP, CS, V>> {
165+
interface BaseWorkerConfig<
166+
S,
167+
CP,
168+
CS,
169+
V,
170+
DB,
171+
R extends Actions<S, CP, CS, V, DB>,
172+
> {
155173
/**
156174
* Called when the worker is first initialized.
157175
*
158176
* Use this hook to initialize your worker's state.
159177
* This is called before any other lifecycle hooks.
160178
*/
161179
onCreate?: (
162-
c: WorkerContext<S, CP, CS, V>,
180+
c: WorkerContext<S, CP, CS, V, DB>,
163181
opts: OnCreateOptions,
164182
) => void | Promise<void>;
165183

@@ -171,7 +189,7 @@ interface BaseWorkerConfig<S, CP, CS, V, R extends Actions<S, CP, CS, V>> {
171189
*
172190
* @returns Void or a Promise that resolves when startup is complete
173191
*/
174-
onStart?: (c: WorkerContext<S, CP, CS, V>) => void | Promise<void>;
192+
onStart?: (c: WorkerContext<S, CP, CS, V, DB>) => void | Promise<void>;
175193

176194
/**
177195
* Called when the worker's state changes.
@@ -181,7 +199,7 @@ interface BaseWorkerConfig<S, CP, CS, V, R extends Actions<S, CP, CS, V>> {
181199
*
182200
* @param newState The updated state
183201
*/
184-
onStateChange?: (c: WorkerContext<S, CP, CS, V>, newState: S) => void;
202+
onStateChange?: (c: WorkerContext<S, CP, CS, V, DB>, newState: S) => void;
185203

186204
/**
187205
* Called before a client connects to the worker.
@@ -194,7 +212,7 @@ interface BaseWorkerConfig<S, CP, CS, V, R extends Actions<S, CP, CS, V>> {
194212
* @throws Throw an error to reject the connection
195213
*/
196214
onBeforeConnect?: (
197-
c: WorkerContext<S, CP, CS, V>,
215+
c: WorkerContext<S, CP, CS, V, DB>,
198216
opts: OnConnectOptions<CP>,
199217
) => void | Promise<void>;
200218

@@ -208,8 +226,8 @@ interface BaseWorkerConfig<S, CP, CS, V, R extends Actions<S, CP, CS, V>> {
208226
* @returns Void or a Promise that resolves when connection handling is complete
209227
*/
210228
onConnect?: (
211-
c: WorkerContext<S, CP, CS, V>,
212-
conn: Conn<S, CP, CS, V>,
229+
c: WorkerContext<S, CP, CS, V, DB>,
230+
conn: Conn<S, CP, CS, V, DB>,
213231
) => void | Promise<void>;
214232

215233
/**
@@ -222,8 +240,8 @@ interface BaseWorkerConfig<S, CP, CS, V, R extends Actions<S, CP, CS, V>> {
222240
* @returns Void or a Promise that resolves when disconnect handling is complete
223241
*/
224242
onDisconnect?: (
225-
c: WorkerContext<S, CP, CS, V>,
226-
conn: Conn<S, CP, CS, V>,
243+
c: WorkerContext<S, CP, CS, V, DB>,
244+
conn: Conn<S, CP, CS, V, DB>,
227245
) => void | Promise<void>;
228246

229247
/**
@@ -239,7 +257,7 @@ interface BaseWorkerConfig<S, CP, CS, V, R extends Actions<S, CP, CS, V>> {
239257
* @returns The modified output to send to the client
240258
*/
241259
onBeforeActionResponse?: <Out>(
242-
c: WorkerContext<S, CP, CS, V>,
260+
c: WorkerContext<S, CP, CS, V, DB>,
243261
name: string,
244262
args: unknown[],
245263
output: Out,
@@ -251,7 +269,7 @@ interface BaseWorkerConfig<S, CP, CS, V, R extends Actions<S, CP, CS, V>> {
251269
// 1. Infer schema
252270
// 2. Omit keys that we'll manually define (because of generics)
253271
// 3. Define our own types that have generic constraints
254-
export type WorkerConfig<S, CP, CS, V> = Omit<
272+
export type WorkerConfig<S, CP, CS, V, DB> = Omit<
255273
z.infer<typeof WorkerConfigSchema>,
256274
| "actions"
257275
| "onCreate"
@@ -268,18 +286,20 @@ export type WorkerConfig<S, CP, CS, V> = Omit<
268286
| "vars"
269287
| "createVars"
270288
> &
271-
BaseWorkerConfig<S, CP, CS, V, Actions<S, CP, CS, V>> &
272-
CreateState<S, CP, CS, V> &
273-
CreateConnState<S, CP, CS, V> &
274-
CreateVars<S, CP, CS, V>;
289+
BaseWorkerConfig<S, CP, CS, V, DB, Actions<S, CP, CS, V, DB>> &
290+
CreateState<S, CP, CS, V, DB> &
291+
CreateConnState<S, CP, CS, V, DB> &
292+
CreateVars<S, CP, CS, V, DB> &
293+
DatabaseConfig<DB>;
275294

276295
// See description on `WorkerConfig`
277296
export type WorkerConfigInput<
278297
S,
279298
CP,
280299
CS,
281300
V,
282-
R extends Actions<S, CP, CS, V>,
301+
DB,
302+
R extends Actions<S, CP, CS, V, DB>,
283303
> = Omit<
284304
z.input<typeof WorkerConfigSchema>,
285305
| "actions"
@@ -297,16 +317,23 @@ export type WorkerConfigInput<
297317
| "vars"
298318
| "createVars"
299319
> &
300-
BaseWorkerConfig<S, CP, CS, V, R> &
301-
CreateState<S, CP, CS, V> &
302-
CreateConnState<S, CP, CS, V> &
303-
CreateVars<S, CP, CS, V>;
320+
BaseWorkerConfig<S, CP, CS, V, DB, R> &
321+
CreateState<S, CP, CS, V, DB> &
322+
CreateConnState<S, CP, CS, V, DB> &
323+
CreateVars<S, CP, CS, V, DB> &
324+
DatabaseConfig<DB>;
304325

305326
// For testing type definitions:
306-
export function test<S, CP, CS, V, R extends Actions<S, CP, CS, V>>(
307-
input: WorkerConfigInput<S, CP, CS, V, R>,
308-
): WorkerConfig<S, CP, CS, V> {
309-
const config = WorkerConfigSchema.parse(input) as WorkerConfig<S, CP, CS, V>;
327+
export function test<S, CP, CS, V, DB, R extends Actions<S, CP, CS, V, DB>>(
328+
input: WorkerConfigInput<S, CP, CS, V, DB, R>,
329+
): WorkerConfig<S, CP, CS, V, DB> {
330+
const config = WorkerConfigSchema.parse(input) as WorkerConfig<
331+
S,
332+
CP,
333+
CS,
334+
V,
335+
DB
336+
>;
310337
return config;
311338
}
312339

packages/core/src/worker/connection.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export function generateConnToken(): string {
1717

1818
export type ConnId = string;
1919

20-
export type AnyConn = Conn<any, any, any, any>;
20+
export type AnyConn = Conn<any, any, any, any, any>;
2121

2222
/**
2323
* Represents a client connection to a worker.
@@ -26,13 +26,13 @@ export type AnyConn = Conn<any, any, any, any>;
2626
*
2727
* @see {@link https://rivet.gg/docs/connections|Connection Documentation}
2828
*/
29-
export class Conn<S, CP, CS, V> {
29+
export class Conn<S, CP, CS, V, DB> {
3030
subscriptions: Set<string> = new Set<string>();
3131

3232
#stateEnabled: boolean;
3333

3434
// TODO: Remove this cyclical reference
35-
#worker: WorkerInstance<S, CP, CS, V>;
35+
#worker: WorkerInstance<S, CP, CS, V, DB>;
3636

3737
/**
3838
* The proxied state that notifies of changes automatically.
@@ -99,7 +99,7 @@ export class Conn<S, CP, CS, V> {
9999
* @protected
100100
*/
101101
public constructor(
102-
worker: WorkerInstance<S, CP, CS, V>,
102+
worker: WorkerInstance<S, CP, CS, V, DB>,
103103
persist: PersistedConn<CP, CS>,
104104
driver: ConnDriver,
105105
stateEnabled: boolean,
@@ -153,6 +153,11 @@ export class Conn<S, CP, CS, V> {
153153
* @param reason - The reason for disconnection.
154154
*/
155155
public async disconnect(reason?: string) {
156-
await this.#driver.disconnect(this.#worker, this, this.__persist.ds, reason);
156+
await this.#driver.disconnect(
157+
this.#worker,
158+
this,
159+
this.__persist.ds,
160+
reason,
161+
);
157162
}
158163
}

packages/core/src/worker/context.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ import { Schedule } from "./schedule";
88
/**
99
* WorkerContext class that provides access to worker methods and state
1010
*/
11-
export class WorkerContext<S, CP, CS, V> {
12-
#worker: WorkerInstance<S, CP, CS, V>;
11+
export class WorkerContext<S, CP, CS, V, DB> {
12+
#worker: WorkerInstance<S, CP, CS, V, DB>;
1313

14-
constructor(worker: WorkerInstance<S, CP, CS, V>) {
14+
constructor(worker: WorkerInstance<S, CP, CS, V, DB>) {
1515
this.#worker = worker;
1616
}
1717

@@ -84,10 +84,17 @@ export class WorkerContext<S, CP, CS, V> {
8484
/**
8585
* Gets the map of connections.
8686
*/
87-
get conns(): Map<ConnId, Conn<S, CP, CS, V>> {
87+
get conns(): Map<ConnId, Conn<S, CP, CS, V, DB>> {
8888
return this.#worker.conns;
8989
}
9090

91+
/**
92+
* Gets the database.
93+
*/
94+
get db(): DB {
95+
return this.#worker.db;
96+
}
97+
9198
/**
9299
* Forces the state to get saved.
93100
*

0 commit comments

Comments
 (0)