@@ -11,9 +11,9 @@ import {
1111 shellApiType ,
1212 ADMIN_DB ,
1313} from './enums' ;
14- import Database from './database' ;
14+ import { DatabaseImpl } from './database' ;
1515import Mongo from './mongo' ;
16- import Collection from './collection' ;
16+ import { CollectionImpl } from './collection' ;
1717import ChangeStreamCursor from './change-stream-cursor' ;
1818import Explainable from './explainable' ;
1919import type {
@@ -33,24 +33,26 @@ import {
3333 MongoshInvalidInputError ,
3434 MongoshRuntimeError ,
3535} from '@mongosh/errors' ;
36+ import type { StringKey } from './helpers' ;
3637
3738const sinonChai = require ( 'sinon-chai' ) ; // weird with import
3839
3940use ( sinonChai ) ;
4041describe ( 'Collection' , function ( ) {
4142 describe ( 'help' , function ( ) {
42- const apiClass = new Collection ( { } as any , { } as any , 'name' ) ;
43+ const apiClass = new CollectionImpl ( { } as any , { } as any , 'name' ) ;
4344 it ( 'calls help function' , async function ( ) {
4445 expect ( ( await toShellResult ( apiClass . help ( ) ) ) . type ) . to . equal ( 'Help' ) ;
4546 expect ( ( await toShellResult ( apiClass . help ) ) . type ) . to . equal ( 'Help' ) ;
4647 } ) ;
4748 } ) ;
4849 describe ( 'signatures' , function ( ) {
4950 it ( 'type' , function ( ) {
50- expect ( signatures . Collection . type ) . to . equal ( 'Collection' ) ;
51+ // TODO: do we want the signatures to be CollectionImpl or Collection?
52+ expect ( signatures . CollectionImpl . type ) . to . equal ( 'CollectionImpl' ) ;
5153 } ) ;
5254 it ( 'attributes' , function ( ) {
53- expect ( signatures . Collection . attributes ?. aggregate ) . to . deep . equal ( {
55+ expect ( signatures . CollectionImpl . attributes ?. aggregate ) . to . deep . equal ( {
5456 type : 'function' ,
5557 returnsPromise : true ,
5658 deprecated : false ,
@@ -68,10 +70,14 @@ describe('Collection', function () {
6870 describe ( 'metadata' , function ( ) {
6971 describe ( 'toShellResult' , function ( ) {
7072 const mongo = sinon . spy ( ) ;
71- const db = new Database ( mongo as any , 'myDB' ) ;
72- const coll = new Collection ( mongo as any , db , 'myCollection' ) ;
73+ const db = new DatabaseImpl ( mongo as any , 'myDB' ) ;
74+ const coll = new CollectionImpl (
75+ mongo as any ,
76+ db . _typeLaunder ( ) ,
77+ 'myCollection'
78+ ) ;
7379 it ( 'toShellResult' , async function ( ) {
74- expect ( ( await toShellResult ( coll ) ) . type ) . to . equal ( 'Collection ' ) ;
80+ expect ( ( await toShellResult ( coll ) ) . type ) . to . equal ( 'CollectionImpl ' ) ;
7581 expect ( ( await toShellResult ( coll ) ) . printable ) . to . equal (
7682 'myDB.myCollection'
7783 ) ;
@@ -80,21 +86,25 @@ describe('Collection', function () {
8086 } ) ;
8187 describe ( '.collections' , function ( ) {
8288 it ( 'allows to get a collection as property if is not one of the existing methods' , function ( ) {
83- const database = new Database (
89+ const database = new DatabaseImpl (
8490 { _instanceState : { emitApiCallWithArgs : ( ) : void => { } } } as any ,
8591 'db1'
8692 ) ;
87- const coll : any = new Collection ( { } as any , database , 'coll' ) ;
88- expect ( coll . someCollection ) . to . have . instanceOf ( Collection ) ;
93+ const coll : any = new CollectionImpl (
94+ { } as any ,
95+ database . _typeLaunder ( ) ,
96+ 'coll'
97+ ) ;
98+ expect ( coll . someCollection ) . to . have . instanceOf ( CollectionImpl ) ;
8999 expect ( coll . someCollection . _name ) . to . equal ( 'coll.someCollection' ) ;
90100 } ) ;
91101
92102 it ( 'reuses collections' , function ( ) {
93- const database : any = new Database (
103+ const database : any = new DatabaseImpl (
94104 { _instanceState : { emitApiCallWithArgs : ( ) : void => { } } } as any ,
95105 'db1'
96106 ) ;
97- const coll : any = new Collection ( { } as any , database , 'coll' ) ;
107+ const coll : any = new CollectionImpl ( { } as any , database , 'coll' ) ;
98108 expect ( coll . someCollection ) . to . equal (
99109 database . getCollection ( 'coll.someCollection' )
100110 ) ;
@@ -103,36 +113,47 @@ describe('Collection', function () {
103113
104114 it ( 'does not return a collection starting with _' , function ( ) {
105115 // this is the behaviour in the old shell
106- const database : any = new Database ( { } as any , 'db1' ) ;
107- const coll : any = new Collection ( { } as any , database , 'coll' ) ;
116+ const database : any = new DatabaseImpl ( { } as any , 'db1' ) ;
117+ const coll : any = new CollectionImpl ( { } as any , database , 'coll' ) ;
108118 expect ( coll . _someProperty ) . to . equal ( undefined ) ;
109119 } ) ;
110120
111121 it ( 'does not return a collection for symbols' , function ( ) {
112- const database : any = new Database ( { } as any , 'db1' ) ;
113- const coll : any = new Collection ( { } as any , database , 'coll' ) ;
122+ const database : any = new DatabaseImpl ( { } as any , 'db1' ) ;
123+ const coll : any = new CollectionImpl ( { } as any , database , 'coll' ) ;
114124 expect ( coll [ Symbol ( 'someProperty' ) ] ) . to . equal ( undefined ) ;
115125 } ) ;
116126
117127 it ( 'does not return a collection with invalid name' , function ( ) {
118- const database : any = new Database ( { } as any , 'db1' ) ;
119- const coll : any = new Collection ( { } as any , database , 'coll' ) ;
128+ const database : any = new DatabaseImpl ( { } as any , 'db1' ) ;
129+ const coll : any = new CollectionImpl ( { } as any , database , 'coll' ) ;
120130 expect ( coll . foo$bar ) . to . equal ( undefined ) ;
121131 } ) ;
122132
123133 it ( 'allows to access _name' , function ( ) {
124- const database : any = new Database ( { } as any , 'db1' ) ;
125- const coll : any = new Collection ( { } as any , database , 'coll' ) ;
134+ const database : any = new DatabaseImpl ( { } as any , 'db1' ) ;
135+ const coll : any = new CollectionImpl ( { } as any , database , 'coll' ) ;
126136 expect ( coll . _name ) . to . equal ( 'coll' ) ;
127137 } ) ;
128138 } ) ;
129139 describe ( 'commands' , function ( ) {
130- let mongo : Mongo ;
140+ type ServerSchema = {
141+ db1 : {
142+ coll1 : {
143+ schema : { } ;
144+ } ;
145+ } ;
146+ } ;
147+ let mongo : Mongo < ServerSchema > ;
131148 let serviceProvider : StubbedInstance < ServiceProvider > ;
132- let database : Database ;
149+ let database : DatabaseImpl < ServerSchema , ServerSchema [ 'db1' ] > ;
133150 let bus : StubbedInstance < EventEmitter > ;
134151 let instanceState : ShellInstanceState ;
135- let collection : Collection ;
152+ let collection : CollectionImpl <
153+ ServerSchema ,
154+ ServerSchema [ 'db1' ] ,
155+ ServerSchema [ 'db1' ] [ 'coll1' ]
156+ > ;
136157
137158 beforeEach ( function ( ) {
138159 bus = stubInterface < EventEmitter > ( ) ;
@@ -149,8 +170,15 @@ describe('Collection', function () {
149170 undefined ,
150171 serviceProvider
151172 ) ;
152- database = new Database ( mongo , 'db1' ) ;
153- collection = new Collection ( mongo , database , 'coll1' ) ;
173+ database = new DatabaseImpl < ServerSchema , ServerSchema [ 'db1' ] > (
174+ mongo ,
175+ 'db1' as StringKey < ServerSchema >
176+ ) ;
177+ collection = new CollectionImpl <
178+ ServerSchema ,
179+ ServerSchema [ 'db1' ] ,
180+ ServerSchema [ 'db1' ] [ 'coll1' ]
181+ > ( mongo , database . _typeLaunder ( ) , 'coll1' ) ;
154182 } ) ;
155183 describe ( 'aggregate' , function ( ) {
156184 let serviceProviderCursor : StubbedInstance < ServiceProviderAggregationCursor > ;
@@ -2802,13 +2830,24 @@ describe('Collection', function () {
28022830 } ) ;
28032831
28042832 describe ( 'fle2' , function ( ) {
2805- let mongo1 : Mongo ;
2806- let mongo2 : Mongo ;
2833+ type ServerSchema = {
2834+ db1 : {
2835+ collfle2 : {
2836+ schema : { } ;
2837+ } ;
2838+ } ;
2839+ } ;
2840+ let mongo1 : Mongo < ServerSchema > ;
2841+ let mongo2 : Mongo < ServerSchema > ;
28072842 let serviceProvider : StubbedInstance < ServiceProvider > ;
2808- let database : Database ;
2843+ let database : DatabaseImpl < ServerSchema , ServerSchema [ 'db1' ] > ;
28092844 let bus : StubbedInstance < EventEmitter > ;
28102845 let instanceState : ShellInstanceState ;
2811- let collection : Collection ;
2846+ let collection : CollectionImpl <
2847+ ServerSchema ,
2848+ ServerSchema [ 'db1' ] ,
2849+ ServerSchema [ 'db1' ] [ 'collfle2' ]
2850+ > ;
28122851 let keyId : any [ ] ;
28132852 beforeEach ( function ( ) {
28142853 bus = stubInterface < EventEmitter > ( ) ;
@@ -2821,7 +2860,8 @@ describe('Collection', function () {
28212860 keyId = [
28222861 { $binary : { base64 : 'oh3caogGQ4Sf34ugKnZ7Xw==' , subType : '04' } } ,
28232862 ] ;
2824- mongo1 = new Mongo (
2863+
2864+ mongo1 = new Mongo < ServerSchema > (
28252865 instanceState ,
28262866 undefined ,
28272867 {
@@ -2836,8 +2876,15 @@ describe('Collection', function () {
28362876 undefined ,
28372877 serviceProvider
28382878 ) ;
2839- database = new Database ( mongo1 , 'db1' ) ;
2840- collection = new Collection ( mongo1 , database , 'collfle2' ) ;
2879+ database = new DatabaseImpl < ServerSchema , ServerSchema [ 'db1' ] > (
2880+ mongo1 ,
2881+ 'db1' as StringKey < ServerSchema >
2882+ ) ;
2883+ collection = new CollectionImpl (
2884+ mongo1 ,
2885+ database . _typeLaunder ( ) ,
2886+ 'collfle2' as StringKey < ServerSchema [ 'db1' ] >
2887+ ) ;
28412888 mongo2 = new Mongo (
28422889 instanceState ,
28432890 undefined ,
@@ -2896,10 +2943,10 @@ describe('Collection', function () {
28962943 } ) ;
28972944 describe ( 'with session' , function ( ) {
28982945 let serviceProvider : StubbedInstance < ServiceProvider > ;
2899- let collection : Collection ;
2946+ let collection : CollectionImpl ;
29002947 let internalSession : StubbedInstance < ServiceProviderSession > ;
29012948 const exceptions : {
2902- [ key in keyof ( typeof Collection ) [ 'prototype' ] ] ?: {
2949+ [ key in keyof ( typeof CollectionImpl ) [ 'prototype' ] ] ?: {
29032950 a ?: any ;
29042951 m ?: string ;
29052952 i ?: number ;
@@ -2961,7 +3008,7 @@ describe('Collection', function () {
29613008 getSearchIndexes : { i : 3 } ,
29623009 checkMetadataConsistency : { m : 'runCursorCommand' , i : 2 } ,
29633010 } ;
2964- const ignore : ( keyof ( typeof Collection ) [ 'prototype' ] ) [ ] = [
3011+ const ignore : ( keyof ( typeof CollectionImpl ) [ 'prototype' ] ) [ ] = [
29653012 'getShardDistribution' ,
29663013 'stats' ,
29673014 'isCapped' ,
@@ -3034,8 +3081,8 @@ describe('Collection', function () {
30343081 } ) ;
30353082 context ( 'all commands that use the same command in sp' , function ( ) {
30363083 for ( const method of (
3037- Object . getOwnPropertyNames ( Collection . prototype ) as ( string &
3038- keyof ( typeof Collection ) [ 'prototype' ] ) [ ]
3084+ Object . getOwnPropertyNames ( CollectionImpl . prototype ) as ( string &
3085+ keyof ( typeof CollectionImpl ) [ 'prototype' ] ) [ ]
30393086 ) . filter (
30403087 ( k ) =>
30413088 typeof k === 'string' &&
@@ -3045,7 +3092,7 @@ describe('Collection', function () {
30453092 if (
30463093 ! method . startsWith ( '_' ) &&
30473094 ! method . startsWith ( 'print' ) &&
3048- Collection . prototype [ method ] . returnsPromise
3095+ CollectionImpl . prototype [ method ] . returnsPromise
30493096 ) {
30503097 it ( `passes the session through for ${ method } ` , async function ( ) {
30513098 try {
0 commit comments