@@ -45,10 +45,20 @@ const kClosed = Symbol('closed');
45
45
/** @internal */
46
46
const kMode = Symbol ( 'mode' ) ;
47
47
48
- const CHANGE_STREAM_OPTIONS = [ 'resumeAfter' , 'startAfter' , 'startAtOperationTime' , 'fullDocument' ] ;
49
- const CURSOR_OPTIONS = [ 'batchSize' , 'maxAwaitTimeMS' , 'collation' , 'readPreference' ] . concat (
50
- CHANGE_STREAM_OPTIONS
51
- ) ;
48
+ const CHANGE_STREAM_OPTIONS = [
49
+ 'resumeAfter' ,
50
+ 'startAfter' ,
51
+ 'startAtOperationTime' ,
52
+ 'fullDocument'
53
+ ] as const ;
54
+
55
+ const CURSOR_OPTIONS = [
56
+ 'batchSize' ,
57
+ 'maxAwaitTimeMS' ,
58
+ 'collation' ,
59
+ 'readPreference' ,
60
+ ...CHANGE_STREAM_OPTIONS
61
+ ] as const ;
52
62
53
63
const CHANGE_DOMAIN_TYPES = {
54
64
COLLECTION : Symbol ( 'Collection' ) ,
@@ -68,6 +78,8 @@ export interface ResumeOptions {
68
78
maxAwaitTimeMS ?: number ;
69
79
collation ?: CollationOptions ;
70
80
readPreference ?: ReadPreference ;
81
+ resumeAfter ?: ResumeToken ;
82
+ startAfter ?: ResumeToken ;
71
83
}
72
84
73
85
/**
@@ -94,7 +106,7 @@ export interface PipeOptions {
94
106
* @public
95
107
*/
96
108
export interface ChangeStreamOptions extends AggregateOptions {
97
- /** Allowed values: ‘default’, ‘ updateLookup’ . When set to ‘ updateLookup’ , the change stream will include both a delta describing the changes to the document, as well as a copy of the entire document that was changed from some time after the change occurred. */
109
+ /** Allowed values: ' updateLookup' . When set to ' updateLookup' , the change stream will include both a delta describing the changes to the document, as well as a copy of the entire document that was changed from some time after the change occurred. */
98
110
fullDocument ?: string ;
99
111
/** The maximum amount of time for the server to wait on new documents to satisfy a change stream query. */
100
112
maxAwaitTimeMS ?: number ;
@@ -446,22 +458,18 @@ export class ChangeStreamCursor<TSchema extends Document = Document> extends Abs
446
458
}
447
459
448
460
get resumeOptions ( ) : ResumeOptions {
449
- const result = { } as ResumeOptions ;
450
- for ( const optionName of CURSOR_OPTIONS ) {
451
- if ( Reflect . has ( this . options , optionName ) ) {
452
- Reflect . set ( result , optionName , Reflect . get ( this . options , optionName ) ) ;
453
- }
454
- }
461
+ const result : ResumeOptions = applyKnownOptions ( this . options , CURSOR_OPTIONS ) ;
455
462
456
463
if ( this . resumeToken || this . startAtOperationTime ) {
457
- [ 'resumeAfter' , 'startAfter' , 'startAtOperationTime' ] . forEach ( key =>
458
- Reflect . deleteProperty ( result , key )
459
- ) ;
464
+ for ( const key of [ 'resumeAfter' , 'startAfter' , 'startAtOperationTime' ] ) {
465
+ Reflect . deleteProperty ( result , key ) ;
466
+ }
460
467
461
468
if ( this . resumeToken ) {
462
469
const resumeKey =
463
470
this . options . startAfter && ! this . hasReceived ? 'startAfter' : 'resumeAfter' ;
464
- Reflect . set ( result , resumeKey , this . resumeToken ) ;
471
+
472
+ result [ resumeKey ] = this . resumeToken ;
465
473
} else if ( this . startAtOperationTime && maxWireVersion ( this . server ) >= 7 ) {
466
474
result . startAtOperationTime = this . startAtOperationTime ;
467
475
}
@@ -568,25 +576,25 @@ function setIsIterator<TSchema>(changeStream: ChangeStream<TSchema>): void {
568
576
}
569
577
changeStream [ kMode ] = 'iterator' ;
570
578
}
579
+
571
580
/**
572
581
* Create a new change stream cursor based on self's configuration
573
582
* @internal
574
583
*/
575
584
function createChangeStreamCursor < TSchema > (
576
585
changeStream : ChangeStream < TSchema > ,
577
- options : ChangeStreamOptions
586
+ options : ChangeStreamOptions | ResumeOptions
578
587
) : ChangeStreamCursor < TSchema > {
579
- const changeStreamStageOptions : Document = { fullDocument : options . fullDocument || 'default' } ;
580
- applyKnownOptions ( changeStreamStageOptions , options , CHANGE_STREAM_OPTIONS ) ;
588
+ const changeStreamStageOptions = applyKnownOptions ( options , CHANGE_STREAM_OPTIONS ) ;
581
589
if ( changeStream . type === CHANGE_DOMAIN_TYPES . CLUSTER ) {
582
590
changeStreamStageOptions . allChangesForCluster = true ;
583
591
}
584
-
585
592
const pipeline = [ { $changeStream : changeStreamStageOptions } as Document ] . concat (
586
593
changeStream . pipeline
587
594
) ;
588
595
589
- const cursorOptions = applyKnownOptions ( { } , options , CURSOR_OPTIONS ) ;
596
+ const cursorOptions : ChangeStreamCursorOptions = applyKnownOptions ( options , CURSOR_OPTIONS ) ;
597
+
590
598
const changeStreamCursor = new ChangeStreamCursor < TSchema > (
591
599
getTopology ( changeStream . parent ) ,
592
600
changeStream . namespace ,
@@ -605,16 +613,17 @@ function createChangeStreamCursor<TSchema>(
605
613
return changeStreamCursor ;
606
614
}
607
615
608
- function applyKnownOptions ( target : Document , source : Document , optionNames : string [ ] ) {
609
- optionNames . forEach ( name => {
610
- if ( source [ name ] ) {
611
- target [ name ] = source [ name ] ;
616
+ function applyKnownOptions ( source : Document , options : ReadonlyArray < string > ) {
617
+ const result : Document = { } ;
618
+
619
+ for ( const option of options ) {
620
+ if ( source [ option ] ) {
621
+ result [ option ] = source [ option ] ;
612
622
}
613
- } ) ;
623
+ }
614
624
615
- return target ;
625
+ return result ;
616
626
}
617
-
618
627
interface TopologyWaitOptions {
619
628
start ?: number ;
620
629
timeout ?: number ;
0 commit comments