Skip to content

Commit 9c1f665

Browse files
fix: re-add applyKnownOptions
1 parent 7c2df8d commit 9c1f665

File tree

1 file changed

+22
-37
lines changed

1 file changed

+22
-37
lines changed

src/change_stream.ts

Lines changed: 22 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ export interface ResumeOptions {
7878
maxAwaitTimeMS?: number;
7979
collation?: CollationOptions;
8080
readPreference?: ReadPreference;
81+
resumeAfter?: ResumeToken;
82+
startAfter?: ResumeToken;
8183
}
8284

8385
/**
@@ -104,7 +106,7 @@ export interface PipeOptions {
104106
* @public
105107
*/
106108
export interface ChangeStreamOptions extends AggregateOptions {
107-
/** 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. */
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. */
108110
fullDocument?: string;
109111
/** The maximum amount of time for the server to wait on new documents to satisfy a change stream query. */
110112
maxAwaitTimeMS?: number;
@@ -456,22 +458,18 @@ export class ChangeStreamCursor<TSchema extends Document = Document> extends Abs
456458
}
457459

458460
get resumeOptions(): ResumeOptions {
459-
const result = {} as ResumeOptions;
460-
for (const optionName of CURSOR_OPTIONS) {
461-
if (Reflect.has(this.options, optionName)) {
462-
Reflect.set(result, optionName, Reflect.get(this.options, optionName));
463-
}
464-
}
461+
const result: ResumeOptions = applyKnownOptions(this.options, CURSOR_OPTIONS);
465462

466463
if (this.resumeToken || this.startAtOperationTime) {
467-
['resumeAfter', 'startAfter', 'startAtOperationTime'].forEach(key =>
468-
Reflect.deleteProperty(result, key)
469-
);
464+
for (const key of ['resumeAfter', 'startAfter', 'startAtOperationTime']) {
465+
Reflect.deleteProperty(result, key);
466+
}
470467

471468
if (this.resumeToken) {
472469
const resumeKey =
473470
this.options.startAfter && !this.hasReceived ? 'startAfter' : 'resumeAfter';
474-
Reflect.set(result, resumeKey, this.resumeToken);
471+
472+
result[resumeKey] = this.resumeToken;
475473
} else if (this.startAtOperationTime && maxWireVersion(this.server) >= 7) {
476474
result.startAtOperationTime = this.startAtOperationTime;
477475
}
@@ -579,48 +577,35 @@ function setIsIterator<TSchema>(changeStream: ChangeStream<TSchema>): void {
579577
changeStream[kMode] = 'iterator';
580578
}
581579

582-
function createChangeStreamStageOptions<TSchema>(
583-
changeStream: ChangeStream<TSchema>,
584-
changeStreamOptions: ChangeStreamOptions
585-
) {
586-
const changeStreamStageOptions: Document = {
587-
fullDocument: changeStreamOptions.fullDocument
588-
};
580+
function applyKnownOptions(source: Document, options: ReadonlyArray<string>) {
581+
const result: Document = {};
589582

590-
for (const optionName of CHANGE_STREAM_OPTIONS) {
591-
if (changeStreamOptions[optionName]) {
592-
changeStreamStageOptions[optionName] = changeStreamOptions[optionName];
583+
for (const option of options) {
584+
if (source[option]) {
585+
result[option] = source[option];
593586
}
594587
}
595588

596-
if (changeStream.type === CHANGE_DOMAIN_TYPES.CLUSTER) {
597-
changeStreamStageOptions.allChangesForCluster = true;
598-
}
599-
600-
return changeStreamStageOptions;
589+
return result;
601590
}
591+
602592
/**
603593
* Create a new change stream cursor based on self's configuration
604594
* @internal
605595
*/
606596
function createChangeStreamCursor<TSchema>(
607597
changeStream: ChangeStream<TSchema>,
608-
changeStreamOptions: ChangeStreamOptions
598+
changeStreamOptions: ChangeStreamOptions | ResumeOptions
609599
): ChangeStreamCursor<TSchema> {
610-
const changeStreamStageOptions = createChangeStreamStageOptions(
611-
changeStream,
612-
changeStreamOptions
613-
);
600+
const changeStreamStageOptions = applyKnownOptions(changeStreamOptions, CHANGE_STREAM_OPTIONS);
614601
const pipeline = [{ $changeStream: changeStreamStageOptions } as Document].concat(
615602
changeStream.pipeline
616603
);
617604

618-
const cursorOptions: Document = {};
619-
for (const optionName of CURSOR_OPTIONS) {
620-
if (changeStreamOptions[optionName]) {
621-
cursorOptions[optionName] = changeStreamOptions[optionName];
622-
}
623-
}
605+
const cursorOptions: ChangeStreamCursorOptions = applyKnownOptions(
606+
changeStreamOptions,
607+
CURSOR_OPTIONS
608+
);
624609

625610
const changeStreamCursor = new ChangeStreamCursor<TSchema>(
626611
getTopology(changeStream.parent),

0 commit comments

Comments
 (0)