11import type Mongo from './mongo' ;
2- import Collection from './collection' ;
2+ import type Collection from './collection' ;
3+ import { CollectionImpl } from './collection' ;
34import {
45 returnsPromise ,
56 returnType ,
@@ -70,26 +71,37 @@ type AuthDoc = {
7071 mechanism ?: string ;
7172} ;
7273
74+ export type Database <
75+ M extends GenericServerSideSchema = GenericServerSideSchema ,
76+ D extends GenericDatabaseSchema = GenericDatabaseSchema
77+ > = DatabaseImpl < M , D > & {
78+ [ k in StringKey < D > ] : Collection < M , D , D [ k ] , k > ;
79+ } ;
80+
7381@shellApiClassDefault
74- export default class Database <
82+ export class DatabaseImpl <
7583 M extends GenericServerSideSchema = GenericServerSideSchema ,
76- D extends GenericDatabaseSchema = M [ keyof M ]
84+ D extends GenericDatabaseSchema = GenericDatabaseSchema
7785> extends ShellApiWithMongoClass {
7886 _mongo : Mongo < M > ;
7987 _name : StringKey < M > ;
80- _collections : Record < string , Collection > ;
88+ _collections : Record < string , Collection < M , D > > ;
8189 _session : Session | undefined ;
8290 _cachedCollectionNames : StringKey < D > [ ] = [ ] ;
8391 _cachedHello : Document | null = null ;
8492
93+ _typeLaunder ( ) : Database < M , D > {
94+ return this as Database < M , D > ;
95+ }
96+
8597 constructor ( mongo : Mongo < M > , name : StringKey < M > , session ?: Session ) {
8698 super ( ) ;
8799 this . _mongo = mongo ;
88100 this . _name = name ;
89- const collections : Record < string , Collection > = Object . create ( null ) ;
101+ const collections : Record < string , Collection < M , D > > = Object . create ( null ) ;
90102 this . _collections = collections ;
91103 this . _session = session ;
92- const proxy = new Proxy ( this , {
104+ const proxy = new Proxy ( this . _typeLaunder ( ) , {
93105 get : ( target , prop ) : any => {
94106 if ( prop in target ) {
95107 return ( target as any ) [ prop ] ;
@@ -104,7 +116,11 @@ export default class Database<
104116 }
105117
106118 if ( ! collections [ prop ] ) {
107- collections [ prop ] = new Collection ( mongo , proxy , prop ) ;
119+ collections [ prop ] = new CollectionImpl (
120+ mongo ,
121+ proxy ,
122+ prop
123+ ) . _typeLaunder ( ) ;
108124 }
109125
110126 return collections [ prop ] ;
@@ -449,13 +465,13 @@ export default class Database<
449465 assertArgsDefinedType ( [ db ] , [ 'string' ] , 'Database.getSiblingDB' ) ;
450466 this . _emitDatabaseApiCall ( 'getSiblingDB' , { db } ) ;
451467 if ( this . _session ) {
452- return this . _session . getDatabase ( db ) ;
468+ return this . _session . getDatabase ( db ) as Database < M , M [ K ] > ;
453469 }
454470 return this . _mongo . _getDb ( db ) ;
455471 }
456472
457473 @returnType ( 'Collection' )
458- getCollection < K extends StringKey < D > > ( coll : K ) : Collection < M , D , D [ K ] > {
474+ getCollection < K extends StringKey < D > > ( coll : K ) : Collection < M , D , D [ K ] , K > {
459475 assertArgsDefinedType ( [ coll ] , [ 'string' ] , 'Database.getColl' ) ;
460476 this . _emitDatabaseApiCall ( 'getCollection' , { coll } ) ;
461477 if ( ! isValidCollectionName ( coll ) ) {
@@ -465,13 +481,17 @@ export default class Database<
465481 ) ;
466482 }
467483
468- const collections : Record < string , Collection > = this . _collections ;
484+ const collections : Record < string , Collection < M , D > > = this . _collections ;
469485
470486 if ( ! collections [ coll ] ) {
471- collections [ coll ] = new Collection ( this . _mongo , this , coll ) ;
487+ collections [ coll ] = new CollectionImpl (
488+ this . _mongo ,
489+ this . _typeLaunder ( ) ,
490+ coll
491+ ) . _typeLaunder ( ) ;
472492 }
473493
474- return collections [ coll ] ;
494+ return collections [ coll ] as Collection < M , D , D [ K ] , K > ;
475495 }
476496
477497 @returnsPromise
@@ -1471,7 +1491,7 @@ export default class Database<
14711491 async printShardingStatus ( verbose = false ) : Promise < CommandResult > {
14721492 this . _emitDatabaseApiCall ( 'printShardingStatus' , { verbose } ) ;
14731493 const result = await getPrintableShardStatus (
1474- await getConfigDB ( this ) ,
1494+ await getConfigDB ( this . _typeLaunder ( ) ) ,
14751495 verbose
14761496 ) ;
14771497 return new CommandResult ( 'StatsResult' , result ) ;
@@ -1760,3 +1780,5 @@ export default class Database<
17601780 } ) ;
17611781 }
17621782}
1783+
1784+ export default Database ;
0 commit comments