@@ -40,7 +40,7 @@ import { type RowType, type Table, type TableMethods } from '../lib/table';
4040import { hasOwn } from '../lib/util' ;
4141import { type AnonymousViewCtx , type ViewCtx } from './views' ;
4242import { isRowTypedQuery , makeQueryBuilder , toSql } from './query' ;
43- import type { DbView } from './db_view' ;
43+ import type { DbView , ReadonlyDbView } from './db_view' ;
4444import { getErrorConstructor , SenderError } from './errors' ;
4545import { Range , type Bound } from './range' ;
4646import { makeRandom , type Random } from './rng' ;
@@ -206,7 +206,7 @@ export const ReducerCtxImpl = class ReducerCtx<
206206 this . sender = sender ;
207207 this . timestamp = timestamp ;
208208 this . connectionId = connectionId ;
209- this . db = dbView ;
209+ this . db = dbView as unknown as DbView < SchemaDef > ;
210210 }
211211
212212 /** Reset the `ReducerCtx` to be used for a new transaction */
@@ -330,14 +330,19 @@ class ModuleHooksImpl implements ModuleHooks {
330330 }
331331
332332 get #dbView( ) {
333- return ( this . #dbView_ ??= freeze (
334- Object . fromEntries (
335- Object . values ( this . #schema. schemaType . tables ) . map ( table => [
336- table . accessorName ,
337- makeTableView ( this . #schema. typespace , table . tableDef ) ,
338- ] )
339- )
340- ) ) ;
333+ if ( this . #dbView_ !== undefined ) return this . #dbView_;
334+ const rootTables = Object . values ( this . #schema. schemaType . tables ) . map (
335+ table => [
336+ table . accessorName ,
337+ makeTableView ( this . #schema. typespace , table . tableDef ) ,
338+ ]
339+ ) ;
340+ const mountNs = this . #schema. mountedDispatchInfos . map ( dispatch => [
341+ dispatch . namespace ,
342+ buildDbViewForDispatch ( dispatch ) ,
343+ ] ) ;
344+ this . #dbView_ = freeze ( Object . fromEntries ( [ ...rootTables , ...mountNs ] ) ) as DbView < any > ;
345+ return this . #dbView_;
341346 }
342347
343348 #getMountDbView( mountIdx : number ) : DbView < any > {
@@ -348,7 +353,7 @@ class ModuleHooksImpl implements ModuleHooks {
348353 accessorName ,
349354 makeTableView ( m . typespace , tableDef ) ,
350355 ] )
351- )
356+ ) as DbView < any >
352357 ) ) ;
353358 }
354359
@@ -436,7 +441,7 @@ class ModuleHooksImpl implements ModuleHooks {
436441 // this is the non-readonly DbView, but the typing for the user will be
437442 // the readonly one, and if they do call mutating functions it will fail
438443 // at runtime
439- db : this . #dbView,
444+ db : this . #dbView as ReadonlyDbView < any > ,
440445 from : makeQueryBuilder ( moduleCtx . schemaType ) ,
441446 } ) ;
442447 const args = deserializeParams ( new BinaryReader ( argsBuf ) ) ;
@@ -460,7 +465,7 @@ class ModuleHooksImpl implements ModuleHooks {
460465 // this is the non-readonly DbView, but the typing for the user will be
461466 // the readonly one, and if they do call mutating functions it will fail
462467 // at runtime
463- db : this . #dbView,
468+ db : this . #dbView as ReadonlyDbView < any > ,
464469 from : makeQueryBuilder ( moduleCtx . schemaType ) ,
465470 } ) ;
466471 const args = deserializeParams ( new BinaryReader ( argsBuf ) ) ;
@@ -490,14 +495,26 @@ class ModuleHooksImpl implements ModuleHooks {
490495 ConnectionId . nullIfZero ( new ConnectionId ( connection_id ) ) ,
491496 new Timestamp ( timestamp ) ,
492497 args ,
493- ( ) => this . #dbView
498+ ( ) => this . #dbView as DbView < any >
494499 ) ;
495500 }
496501}
497502
498503const BINARY_WRITER = new BinaryWriter ( 0 ) ;
499504const BINARY_READER = new BinaryReader ( new Uint8Array ( ) ) ;
500505
506+ function buildDbViewForDispatch ( dispatch : MountedDispatchInfo ) : object {
507+ const tableEntries = dispatch . tables . map ( ( { accessorName, tableDef } ) => [
508+ accessorName ,
509+ makeTableView ( dispatch . typespace , tableDef ) ,
510+ ] ) ;
511+ const subNsEntries = dispatch . subDispatches . map ( sub => [
512+ sub . namespace ,
513+ buildDbViewForDispatch ( sub ) ,
514+ ] ) ;
515+ return freeze ( Object . fromEntries ( [ ...tableEntries , ...subNsEntries ] ) ) ;
516+ }
517+
501518function makeTableView (
502519 typespace : Typespace ,
503520 table : RawTableDefV10
0 commit comments