@@ -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`
345385export 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:
377420export 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