Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions src/change_stream.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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