Skip to content

Commit 010be9a

Browse files
refactor: clean up new interfaces in change streams
1 parent e1a6833 commit 010be9a

File tree

2 files changed

+67
-166
lines changed

2 files changed

+67
-166
lines changed

src/change_stream.ts

Lines changed: 65 additions & 166 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import type { UUID } from 'bson';
21
import Denque = require('denque');
32
import type { Readable } from 'stream';
43
import { setTimeout } from 'timers';
54

6-
import type { Document, Long, Timestamp } from './bson';
5+
import type { Binary, Document, Long, Timestamp } from './bson';
76
import { Collection } from './collection';
87
import { CHANGE, CLOSE, END, ERROR, INIT, MORE, RESPONSE, RESUME_TOKEN_CHANGED } from './constants';
98
import {
@@ -240,28 +239,46 @@ export interface ChangeStreamDocumentCommon {
240239
lsid?: ServerSessionId;
241240
}
242241

242+
/** @public */
243+
export interface ChangeStreamDocumentCollectionUUID {
244+
/**
245+
* The UUID of the collection that the operation was performed on.
246+
*
247+
* Only present when the `showExpandedEvents` flag is enabled.
248+
*
249+
* **NOTE:** collectionUUID will only be a UUID when the promoteBuffers flag is to true.
250+
*
251+
* @since 6.1.0
252+
*/
253+
collectionUUID: Binary;
254+
}
255+
256+
/** @public */
257+
export interface ChangeStreamDocumentOperationDescription {
258+
/**
259+
* An description of the operation.
260+
*
261+
* Only present when the `showExpandedEvents` flag is enabled.
262+
*
263+
* @since 6.1.0
264+
*/
265+
operationDescription?: Document;
266+
}
267+
243268
/**
244269
* @public
245270
* @see https://www.mongodb.com/docs/manual/reference/change-events/#insert-event
246271
*/
247272
export interface ChangeStreamInsertDocument<TSchema extends Document = Document>
248273
extends ChangeStreamDocumentCommon,
249-
ChangeStreamDocumentKey<TSchema> {
274+
ChangeStreamDocumentKey<TSchema>,
275+
ChangeStreamDocumentCollectionUUID {
250276
/** Describes the type of operation represented in this change notification */
251277
operationType: 'insert';
252278
/** This key will contain the document being inserted */
253279
fullDocument: TSchema;
254280
/** Namespace the insert event occured on */
255281
ns: ChangeStreamNameSpace;
256-
257-
/**
258-
* The UUID of the collection that the operation was performed on.
259-
*
260-
* Only present when the `showExpandedEvents` flag is enabled.
261-
*
262-
* @since 6.1.0
263-
*/
264-
collectionUUID: UUID;
265282
}
266283

267284
/**
@@ -270,7 +287,8 @@ export interface ChangeStreamInsertDocument<TSchema extends Document = Document>
270287
*/
271288
export interface ChangeStreamUpdateDocument<TSchema extends Document = Document>
272289
extends ChangeStreamDocumentCommon,
273-
ChangeStreamDocumentKey<TSchema> {
290+
ChangeStreamDocumentKey<TSchema>,
291+
ChangeStreamDocumentCollectionUUID {
274292
/** Describes the type of operation represented in this change notification */
275293
operationType: 'update';
276294
/**
@@ -292,15 +310,6 @@ export interface ChangeStreamUpdateDocument<TSchema extends Document = Document>
292310
* pre-image is unavailable, this will be explicitly set to null.
293311
*/
294312
fullDocumentBeforeChange?: TSchema;
295-
296-
/**
297-
* The UUID of the collection that the operation was performed on.
298-
*
299-
* Only present when the `showExpandedEvents` flag is enabled.
300-
*
301-
* @since 6.1.0
302-
*/
303-
collectionUUID: UUID;
304313
}
305314

306315
/**
@@ -332,7 +341,8 @@ export interface ChangeStreamReplaceDocument<TSchema extends Document = Document
332341
*/
333342
export interface ChangeStreamDeleteDocument<TSchema extends Document = Document>
334343
extends ChangeStreamDocumentCommon,
335-
ChangeStreamDocumentKey<TSchema> {
344+
ChangeStreamDocumentKey<TSchema>,
345+
ChangeStreamDocumentCollectionUUID {
336346
/** Describes the type of operation represented in this change notification */
337347
operationType: 'delete';
338348
/** Namespace the delete event occured on */
@@ -345,56 +355,34 @@ export interface ChangeStreamDeleteDocument<TSchema extends Document = Document>
345355
* pre-image is unavailable, this will be explicitly set to null.
346356
*/
347357
fullDocumentBeforeChange?: TSchema;
348-
349-
/**
350-
* The UUID of the collection that the operation was performed on.
351-
*
352-
* Only present when the `showExpandedEvents` flag is enabled.
353-
*
354-
* @since 6.1.0
355-
*/
356-
collectionUUID: UUID;
357358
}
358359

359360
/**
360361
* @public
361362
* @see https://www.mongodb.com/docs/manual/reference/change-events/#drop-event
362363
*/
363-
export interface ChangeStreamDropDocument extends ChangeStreamDocumentCommon {
364+
export interface ChangeStreamDropDocument
365+
extends ChangeStreamDocumentCommon,
366+
ChangeStreamDocumentCollectionUUID {
364367
/** Describes the type of operation represented in this change notification */
365368
operationType: 'drop';
366369
/** Namespace the drop event occured on */
367370
ns: ChangeStreamNameSpace;
368-
369-
/**
370-
* The UUID of the collection that the operation was performed on.
371-
*
372-
* Only present when the `showExpandedEvents` flag is enabled.
373-
*
374-
* @since 6.1.0
375-
*/
376-
collectionUUID: UUID;
377371
}
378372

379373
/**
380374
* @public
381375
* @see https://www.mongodb.com/docs/manual/reference/change-events/#rename-event
382376
*/
383-
export interface ChangeStreamRenameDocument extends ChangeStreamDocumentCommon {
377+
export interface ChangeStreamRenameDocument
378+
extends ChangeStreamDocumentCommon,
379+
ChangeStreamDocumentCollectionUUID {
384380
/** Describes the type of operation represented in this change notification */
385381
operationType: 'rename';
386382
/** The new name for the `ns.coll` collection */
387383
to: { db: string; coll: string };
388384
/** The "from" namespace that the rename occured on */
389385
ns: ChangeStreamNameSpace;
390-
/**
391-
* An description of the operation.
392-
*
393-
* Only present when the `showExpandedEvents` flag is enabled.
394-
*
395-
* @since 6.1.0
396-
*/
397-
operationDescription?: Document;
398386
}
399387

400388
/**
@@ -422,173 +410,84 @@ export interface ChangeStreamInvalidateDocument extends ChangeStreamDocumentComm
422410
* @public
423411
* @see https://www.mongodb.com/docs/manual/reference/change-events/#invalidate-event
424412
*/
425-
export interface ChangeStreamCreateIndexDocument extends ChangeStreamDocumentCommon {
413+
export interface ChangeStreamCreateIndexDocument
414+
extends ChangeStreamDocumentCommon,
415+
ChangeStreamDocumentCollectionUUID,
416+
ChangeStreamDocumentOperationDescription {
426417
/** Describes the type of operation represented in this change notification */
427418
operationType: 'createIndexes';
428-
429-
/**
430-
* The UUID of the collection that the operation was performed on.
431-
*
432-
* Only present when the `showExpandedEvents` flag is enabled.
433-
*
434-
* @since 6.1.0
435-
*/
436-
collectionUUID: UUID;
437-
438-
/**
439-
* An description of the operation.
440-
*
441-
* Only present when the `showExpandedEvents` flag is enabled.
442-
*
443-
* @since 6.1.0
444-
*/
445-
operationDescription?: Document;
446419
}
447420

448421
/**
449422
* Only present when the `showExpandedEvents` flag is enabled.
450423
* @public
451424
* @see https://www.mongodb.com/docs/manual/reference/change-events/#invalidate-event
452425
*/
453-
export interface ChangeStreamDropIndexDocument extends ChangeStreamDocumentCommon {
426+
export interface ChangeStreamDropIndexDocument
427+
extends ChangeStreamDocumentCommon,
428+
ChangeStreamDocumentCollectionUUID,
429+
ChangeStreamDocumentOperationDescription {
454430
/** Describes the type of operation represented in this change notification */
455431
operationType: 'dropIndexes';
456-
457-
/**
458-
* The UUID of the collection that the operation was performed on.
459-
*
460-
* Only present when the `showExpandedEvents` flag is enabled.
461-
*
462-
* @since 6.1.0
463-
*/
464-
collectionUUID: UUID;
465-
466-
/**
467-
* An description of the operation.
468-
*
469-
* Only present when the `showExpandedEvents` flag is enabled.
470-
*
471-
* @since 6.1.0
472-
*/
473-
operationDescription?: Document;
474432
}
475433

476434
/**
477435
* Only present when the `showExpandedEvents` flag is enabled.
478436
* @public
479437
* @see https://www.mongodb.com/docs/manual/reference/change-events/#invalidate-event
480438
*/
481-
export interface ChangeStreamCollModDocument extends ChangeStreamDocumentCommon {
439+
export interface ChangeStreamCollModDocument
440+
extends ChangeStreamDocumentCommon,
441+
ChangeStreamDocumentCollectionUUID {
482442
/** Describes the type of operation represented in this change notification */
483443
operationType: 'modify';
484-
485-
/**
486-
* The UUID of the collection that the operation was performed on.
487-
*
488-
* Only present when the `showExpandedEvents` flag is enabled.
489-
*
490-
* @since 6.1.0
491-
*/
492-
collectionUUID: UUID;
493444
}
494445

495446
/**
496447
* @public
497448
* @see https://www.mongodb.com/docs/manual/reference/change-events/#invalidate-event
498449
*/
499-
export interface ChangeStreamCreateDocument extends ChangeStreamDocumentCommon {
450+
export interface ChangeStreamCreateDocument
451+
extends ChangeStreamDocumentCommon,
452+
ChangeStreamDocumentCollectionUUID {
500453
/** Describes the type of operation represented in this change notification */
501454
operationType: 'create';
502-
503-
/**
504-
* The UUID of the collection that the operation was performed on.
505-
*
506-
* Only present when the `showExpandedEvents` flag is enabled.
507-
*
508-
* @since 6.1.0
509-
*/
510-
collectionUUID: UUID;
511455
}
512456

513457
/**
514458
* @public
515459
* @see https://www.mongodb.com/docs/manual/reference/change-events/#invalidate-event
516460
*/
517-
export interface ChangeStreamShardCollectionDocument extends ChangeStreamDocumentCommon {
461+
export interface ChangeStreamShardCollectionDocument
462+
extends ChangeStreamDocumentCommon,
463+
ChangeStreamDocumentCollectionUUID,
464+
ChangeStreamDocumentOperationDescription {
518465
/** Describes the type of operation represented in this change notification */
519466
operationType: 'shardCollection';
520-
521-
/**
522-
* The UUID of the collection that the operation was performed on.
523-
*
524-
* Only present when the `showExpandedEvents` flag is enabled.
525-
*
526-
* @since 6.1.0
527-
*/
528-
collectionUUID: UUID;
529-
530-
/**
531-
* An description of the operation.
532-
*
533-
* Only present when the `showExpandedEvents` flag is enabled.
534-
*
535-
* @since 6.1.0
536-
*/
537-
operationDescription?: Document;
538467
}
539468

540469
/**
541470
* @public
542471
* @see https://www.mongodb.com/docs/manual/reference/change-events/#invalidate-event
543472
*/
544-
export interface ChangeStreamReshardCollectionDocument extends ChangeStreamDocumentCommon {
473+
export interface ChangeStreamReshardCollectionDocument
474+
extends ChangeStreamDocumentCommon,
475+
ChangeStreamDocumentCollectionUUID,
476+
ChangeStreamDocumentOperationDescription {
545477
/** Describes the type of operation represented in this change notification */
546478
operationType: 'reshardCollection';
547-
548-
/**
549-
* The UUID of the collection that the operation was performed on.
550-
*
551-
* Only present when the `showExpandedEvents` flag is enabled.
552-
*
553-
* @since 6.1.0
554-
*/
555-
collectionUUID: UUID;
556-
557-
/**
558-
* An description of the operation.
559-
*
560-
* Only present when the `showExpandedEvents` flag is enabled.
561-
*
562-
* @since 6.1.0
563-
*/
564-
operationDescription?: Document;
565479
}
566480

567481
/**
568482
* @public
569483
* @see https://www.mongodb.com/docs/manual/reference/change-events/#invalidate-event
570484
*/
571-
export interface ChangeStreamRefineCollectionShardKeyDocument extends ChangeStreamDocumentCommon {
485+
export interface ChangeStreamRefineCollectionShardKeyDocument
486+
extends ChangeStreamDocumentCommon,
487+
ChangeStreamDocumentCollectionUUID,
488+
ChangeStreamDocumentOperationDescription {
572489
/** Describes the type of operation represented in this change notification */
573490
operationType: 'refineCollectionShardKey';
574-
575-
/**
576-
* The UUID of the collection that the operation was performed on.
577-
*
578-
* Only present when the `showExpandedEvents` flag is enabled.
579-
*
580-
* @since 6.1.0
581-
*/
582-
collectionUUID?: UUID;
583-
584-
/**
585-
* An description of the operation.
586-
*
587-
* Only present when the `showExpandedEvents` flag is enabled.
588-
*
589-
* @since 6.1.0
590-
*/
591-
operationDescription?: Document;
592491
}
593492

594493
/** @public */

src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,10 @@ export type {
178178
ChangeStreamCursorOptions,
179179
ChangeStreamDeleteDocument,
180180
ChangeStreamDocument,
181+
ChangeStreamDocumentCollectionUUID,
181182
ChangeStreamDocumentCommon,
182183
ChangeStreamDocumentKey,
184+
ChangeStreamDocumentOperationDescription,
183185
ChangeStreamDropDatabaseDocument,
184186
ChangeStreamDropDocument,
185187
ChangeStreamDropIndexDocument,

0 commit comments

Comments
 (0)