@@ -15,20 +15,18 @@ import type {
15
15
ChangeStreamInvalidateDocument ,
16
16
ChangeStreamNameSpace ,
17
17
ChangeStreamOptions ,
18
+ ChangeStreamRefineCollectionShardKeyDocument ,
18
19
ChangeStreamRenameDocument ,
19
20
ChangeStreamReplaceDocument ,
21
+ ChangeStreamReshardCollectionDocument ,
22
+ ChangeStreamShardCollectionDocument ,
20
23
ChangeStreamUpdateDocument ,
21
24
Collection ,
22
25
Document ,
23
26
ResumeToken ,
24
27
ServerSessionId ,
25
28
Timestamp ,
26
29
UpdateDescription
27
- } from '../../src' ;
28
- import type {
29
- ChangeStreamRefineCollectionShardKeyDocument ,
30
- ChangeStreamReshardCollectionDocument ,
31
- ChangeStreamShardCollectionDocument
32
30
} from '../mongodb' ;
33
31
34
32
declare const changeStreamOptions : ChangeStreamOptions ;
@@ -181,8 +179,8 @@ switch (change.operationType) {
181
179
// New fields can be added with $addFields, but you have to use TChange to type it
182
180
expectError ( change . randomKeyAlwaysAccessibleBecauseOfPipelineFlexibilty ) ;
183
181
184
- declare const collection : Collection < Schema > ;
185
- const pipelineChangeStream = collection . watch <
182
+ declare const collectionWithSchema : Collection < Schema > ;
183
+ const pipelineChangeStream = collectionWithSchema . watch <
186
184
Schema ,
187
185
ChangeStreamInsertDocument < Schema > & { comment : string }
188
186
> ( [ { $addFields : { comment : 'big changes' } } , { $match : { operationType : 'insert' } } ] ) ;
@@ -193,28 +191,52 @@ pipelineChangeStream.on('change', change => {
193
191
expectType < Schema > ( change . fullDocument ) ;
194
192
} ) ;
195
193
196
- collection . watch ( ) . on ( 'change' , change => expectType < ChangeStreamDocument < Schema > > ( change ) ) ;
194
+ collectionWithSchema
195
+ . watch ( )
196
+ . on ( 'change' , change => expectType < ChangeStreamDocument < Schema > > ( change ) ) ;
197
197
198
198
// Just overriding the schema provides a typed changestream OF that schema
199
- collection
199
+ collectionWithSchema
200
200
. watch < Document > ( )
201
201
. on ( 'change' , change => expectType < ChangeStreamDocument < Document > > ( change ) ) ;
202
202
203
- // both schema and Tchange can be made as flexible as possible (Document)
204
- collection . watch < Document , Document > ( ) . on ( 'change' , change => expectType < Document > ( change ) ) ;
203
+ // both schema and TChange can be made as flexible as possible (Document)
204
+ collectionWithSchema
205
+ . watch < Document , Document > ( )
206
+ . on ( 'change' , change => expectType < Document > ( change ) ) ;
205
207
206
208
// first argument does not stop you from making second more generic
207
- collection . watch < { a : number } , Document > ( ) . on ( 'change' , change => expectType < Document > ( change ) ) ;
209
+ collectionWithSchema
210
+ . watch < { a : number } , Document > ( )
211
+ . on ( 'change' , change => expectType < Document > ( change ) ) ;
208
212
209
213
// Arguments must be objects
210
- expectError ( collection . watch < Document , number > ( ) ) ;
211
- expectError ( collection . watch < number , number > ( ) ) ;
214
+ expectError ( collectionWithSchema . watch < Document , number > ( ) ) ;
215
+ expectError ( collectionWithSchema . watch < number , number > ( ) ) ;
212
216
213
217
// First argument no longer relates to second
214
- collection
218
+ collectionWithSchema
215
219
. watch < { a : number } , { b : boolean } > ( )
216
220
. on ( 'change' , change => expectType < { b : boolean } > ( change ) ) ;
217
221
218
222
expectType < AsyncGenerator < ChangeStreamDocument < Schema > , void , void > > (
219
- collection . watch ( ) [ Symbol . asyncIterator ] ( )
223
+ collectionWithSchema . watch ( ) [ Symbol . asyncIterator ] ( )
224
+ ) ;
225
+
226
+ // Change type returned to user is equivalent across next/tryNext/on/once/addListener
227
+ const changeStream = collectionWithSchema . watch ( ) ;
228
+ expectType < ChangeStreamDocument < Schema > | null > ( await changeStream . tryNext ( ) ) ;
229
+ expectType < ChangeStreamDocument < Schema > > ( await changeStream . next ( ) ) ;
230
+ changeStream . on ( 'change' , change => expectType < ChangeStreamDocument < Schema > > ( change ) ) ;
231
+ changeStream . once ( 'change' , change => expectType < ChangeStreamDocument < Schema > > ( change ) ) ;
232
+ changeStream . addListener ( 'change' , change => expectType < ChangeStreamDocument < Schema > > ( change ) ) ;
233
+
234
+ declare const noSchemaCollection : Collection ;
235
+ const changeStreamNoSchema = noSchemaCollection . watch ( ) ;
236
+ expectType < ChangeStreamDocument < Document > | null > ( await changeStreamNoSchema . tryNext ( ) ) ;
237
+ expectType < ChangeStreamDocument < Document > > ( await changeStreamNoSchema . next ( ) ) ;
238
+ changeStreamNoSchema . on ( 'change' , change => expectType < ChangeStreamDocument < Document > > ( change ) ) ;
239
+ changeStreamNoSchema . once ( 'change' , change => expectType < ChangeStreamDocument < Document > > ( change ) ) ;
240
+ changeStreamNoSchema . addListener ( 'change' , change =>
241
+ expectType < ChangeStreamDocument < Document > > ( change )
220
242
) ;
0 commit comments