Skip to content

Commit

Permalink
feat(NODE-3692): make change stream events typing more generic (#3034)
Browse files Browse the repository at this point in the history
Co-authored-by: Brian Evans <53117772+mrbrianevans@users.noreply.github.com>
  • Loading branch information
durran and mrbrianevans committed Nov 10, 2021
1 parent 53b3164 commit d5ae78e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
14 changes: 7 additions & 7 deletions src/change_stream.ts
Expand Up @@ -184,23 +184,23 @@ export interface UpdateDescription<TSchema extends Document = Document> {
}

/** @public */
export type ChangeStreamEvents = {
export type ChangeStreamEvents<TSchema extends Document = Document> = {
resumeTokenChanged(token: ResumeToken): void;
init(response: Document): void;
more(response?: Document | undefined): void;
init(response: TSchema): void;
more(response?: TSchema | undefined): void;
response(): void;
end(): void;
error(error: Error): void;
change(change: ChangeStreamDocument): void;
change(change: ChangeStreamDocument<TSchema>): void;
} & AbstractCursorEvents;

/**
* Creates a new Change Stream instance. Normally created using {@link Collection#watch|Collection.watch()}.
* @public
*/
export class ChangeStream<
TSchema extends Document = Document
> extends TypedEventEmitter<ChangeStreamEvents> {
export class ChangeStream<TSchema extends Document = Document> extends TypedEventEmitter<
ChangeStreamEvents<TSchema>
> {
pipeline: Document[];
options: ChangeStreamOptions;
parent: MongoClient | Db | Collection;
Expand Down
19 changes: 18 additions & 1 deletion test/types/mongodb.test-d.ts
Expand Up @@ -3,6 +3,7 @@ import { MongoClient } from '../../src/mongo_client';
import { Collection } from '../../src/collection';
import { AggregationCursor } from '../../src/cursor/aggregation_cursor';
import type { FindCursor } from '../../src/cursor/find_cursor';
import type { ChangeStreamDocument } from '../../src/change_stream';
import type { Document } from 'bson';
import { Db } from '../../src';
import { Topology } from '../../src/sdam/topology';
Expand All @@ -19,9 +20,14 @@ expectDeprecated(Db.prototype.unref);
expectDeprecated(MongoDBDriver.ObjectID);
expectNotDeprecated(MongoDBDriver.ObjectId);

interface TSchema extends Document {
name: string;
}

// test mapped cursor types
const client = new MongoClient('');
const coll = client.db('test').collection('test');
const db = client.db('test');
const coll = db.collection('test');
const findCursor = coll.find();
expectType<Document | null>(await findCursor.next());
const mappedFind = findCursor.map<number>(obj => Object.keys(obj).length);
Expand All @@ -38,6 +44,17 @@ const composedMap = mappedAgg.map<string>(x => x.toString());
expectType<AggregationCursor<string>>(composedMap);
expectType<string | null>(await composedMap.next());
expectType<string[]>(await composedMap.toArray());
const tschemaColl = db.collection<TSchema>('test');
const changeStream = tschemaColl.watch();
changeStream.on('init', doc => {
expectType<TSchema>(doc);
});
changeStream.on('more', doc => {
expectType<TSchema | undefined>(doc);
});
changeStream.on('change', doc => {
expectType<ChangeStreamDocument<TSchema>>(doc);
});

const builtCursor = coll.aggregate();
// should allow string values for the out helper
Expand Down

0 comments on commit d5ae78e

Please sign in to comment.