@@ -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`
277296export 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
0 commit comments