1
1
import { expectAssignable , expectNotType , expectType } from 'tsd' ;
2
- import { FindCursor , FindOptions , MongoClient , Document } from '../../../../src' ;
2
+ import { FindCursor , FindOptions , MongoClient , Document , Collection , Db } from '../../../../src' ;
3
3
import type { Projection , ProjectionOperators } from '../../../../src' ;
4
4
import type { PropExists } from '../../utility_types' ;
5
5
@@ -9,7 +9,7 @@ const db = client.db('test');
9
9
const collection = db . collection ( 'test.find' ) ;
10
10
11
11
// Locate all the entries using find
12
- collection . find ( { } ) . toArray ( ( err , fields ) => {
12
+ collection . find ( { } ) . toArray ( ( _err , fields ) => {
13
13
expectType < Document [ ] | undefined > ( fields ) ;
14
14
} ) ;
15
15
@@ -22,7 +22,7 @@ interface TestModel {
22
22
}
23
23
24
24
const collectionT = db . collection < TestModel > ( 'testCollection' ) ;
25
- await collectionT . find ( {
25
+ collectionT . find ( {
26
26
$and : [ { numberField : { $gt : 0 } } , { numberField : { $lt : 100 } } ] ,
27
27
readonlyFruitTags : { $all : [ 'apple' , 'pear' ] }
28
28
} ) ;
@@ -74,7 +74,7 @@ const collectionBag = db.collection<Bag>('bag');
74
74
75
75
const cursor : FindCursor < Bag > = collectionBag . find ( { color : 'black' } ) ;
76
76
77
- cursor . toArray ( ( err , bags ) => {
77
+ cursor . toArray ( ( _err , bags ) => {
78
78
expectType < Bag [ ] | undefined > ( bags ) ;
79
79
} ) ;
80
80
@@ -198,3 +198,32 @@ expectType<FindOptions>(findOptions);
198
198
// This is just to check that we still export these type symbols
199
199
expectAssignable < Projection > ( { } ) ;
200
200
expectAssignable < ProjectionOperators > ( { } ) ;
201
+
202
+ // Ensure users can create a custom Db type that only contains specific
203
+ // collections (which are, in turn, strongly typed):
204
+ type Person = {
205
+ name : 'alice' | 'bob' ;
206
+ age : number ;
207
+ } ;
208
+
209
+ type Thing = {
210
+ location : 'shelf' | 'cupboard' ;
211
+ } ;
212
+
213
+ interface TypedDb extends Db {
214
+ collection ( name : 'people' ) : Collection < Person > ;
215
+ collection ( name : 'things' ) : Collection < Thing > ;
216
+ }
217
+
218
+ const typedDb = client . db ( 'test2' ) as TypedDb ;
219
+
220
+ const person = typedDb . collection ( 'people' ) . findOne ( { } ) ;
221
+ expectType < Promise < Person | undefined > > ( person ) ;
222
+
223
+ typedDb . collection ( 'people' ) . findOne ( { } , function ( _err , person ) {
224
+ expectType < Person | undefined > ( person ) ;
225
+ } ) ;
226
+
227
+ typedDb . collection ( 'things' ) . findOne ( { } , function ( _err , thing ) {
228
+ expectType < Thing | undefined > ( thing ) ;
229
+ } ) ;
0 commit comments