Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(NODE-3692): make change stream events typing more generic #3034

Merged
merged 6 commits into from Nov 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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