Skip to content

Commit

Permalink
refactor: clean up new interfaces in change streams
Browse files Browse the repository at this point in the history
  • Loading branch information
baileympearson committed Jun 3, 2022
1 parent e1a6833 commit 010be9a
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 166 deletions.
231 changes: 65 additions & 166 deletions src/change_stream.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import type { UUID } from 'bson';
import Denque = require('denque');
import type { Readable } from 'stream';
import { setTimeout } from 'timers';

import type { Document, Long, Timestamp } from './bson';
import type { Binary, Document, Long, Timestamp } from './bson';
import { Collection } from './collection';
import { CHANGE, CLOSE, END, ERROR, INIT, MORE, RESPONSE, RESUME_TOKEN_CHANGED } from './constants';
import {
Expand Down Expand Up @@ -240,28 +239,46 @@ export interface ChangeStreamDocumentCommon {
lsid?: ServerSessionId;
}

/** @public */
export interface ChangeStreamDocumentCollectionUUID {
/**
* The UUID of the collection that the operation was performed on.
*
* Only present when the `showExpandedEvents` flag is enabled.
*
* **NOTE:** collectionUUID will only be a UUID when the promoteBuffers flag is to true.
*
* @since 6.1.0
*/
collectionUUID: Binary;
}

/** @public */
export interface ChangeStreamDocumentOperationDescription {
/**
* An description of the operation.
*
* Only present when the `showExpandedEvents` flag is enabled.
*
* @since 6.1.0
*/
operationDescription?: Document;
}

/**
* @public
* @see https://www.mongodb.com/docs/manual/reference/change-events/#insert-event
*/
export interface ChangeStreamInsertDocument<TSchema extends Document = Document>
extends ChangeStreamDocumentCommon,
ChangeStreamDocumentKey<TSchema> {
ChangeStreamDocumentKey<TSchema>,
ChangeStreamDocumentCollectionUUID {
/** Describes the type of operation represented in this change notification */
operationType: 'insert';
/** This key will contain the document being inserted */
fullDocument: TSchema;
/** Namespace the insert event occured on */
ns: ChangeStreamNameSpace;

/**
* The UUID of the collection that the operation was performed on.
*
* Only present when the `showExpandedEvents` flag is enabled.
*
* @since 6.1.0
*/
collectionUUID: UUID;
}

/**
Expand All @@ -270,7 +287,8 @@ export interface ChangeStreamInsertDocument<TSchema extends Document = Document>
*/
export interface ChangeStreamUpdateDocument<TSchema extends Document = Document>
extends ChangeStreamDocumentCommon,
ChangeStreamDocumentKey<TSchema> {
ChangeStreamDocumentKey<TSchema>,
ChangeStreamDocumentCollectionUUID {
/** Describes the type of operation represented in this change notification */
operationType: 'update';
/**
Expand All @@ -292,15 +310,6 @@ export interface ChangeStreamUpdateDocument<TSchema extends Document = Document>
* pre-image is unavailable, this will be explicitly set to null.
*/
fullDocumentBeforeChange?: TSchema;

/**
* The UUID of the collection that the operation was performed on.
*
* Only present when the `showExpandedEvents` flag is enabled.
*
* @since 6.1.0
*/
collectionUUID: UUID;
}

/**
Expand Down Expand Up @@ -332,7 +341,8 @@ export interface ChangeStreamReplaceDocument<TSchema extends Document = Document
*/
export interface ChangeStreamDeleteDocument<TSchema extends Document = Document>
extends ChangeStreamDocumentCommon,
ChangeStreamDocumentKey<TSchema> {
ChangeStreamDocumentKey<TSchema>,
ChangeStreamDocumentCollectionUUID {
/** Describes the type of operation represented in this change notification */
operationType: 'delete';
/** Namespace the delete event occured on */
Expand All @@ -345,56 +355,34 @@ export interface ChangeStreamDeleteDocument<TSchema extends Document = Document>
* pre-image is unavailable, this will be explicitly set to null.
*/
fullDocumentBeforeChange?: TSchema;

/**
* The UUID of the collection that the operation was performed on.
*
* Only present when the `showExpandedEvents` flag is enabled.
*
* @since 6.1.0
*/
collectionUUID: UUID;
}

/**
* @public
* @see https://www.mongodb.com/docs/manual/reference/change-events/#drop-event
*/
export interface ChangeStreamDropDocument extends ChangeStreamDocumentCommon {
export interface ChangeStreamDropDocument
extends ChangeStreamDocumentCommon,
ChangeStreamDocumentCollectionUUID {
/** Describes the type of operation represented in this change notification */
operationType: 'drop';
/** Namespace the drop event occured on */
ns: ChangeStreamNameSpace;

/**
* The UUID of the collection that the operation was performed on.
*
* Only present when the `showExpandedEvents` flag is enabled.
*
* @since 6.1.0
*/
collectionUUID: UUID;
}

/**
* @public
* @see https://www.mongodb.com/docs/manual/reference/change-events/#rename-event
*/
export interface ChangeStreamRenameDocument extends ChangeStreamDocumentCommon {
export interface ChangeStreamRenameDocument
extends ChangeStreamDocumentCommon,
ChangeStreamDocumentCollectionUUID {
/** Describes the type of operation represented in this change notification */
operationType: 'rename';
/** The new name for the `ns.coll` collection */
to: { db: string; coll: string };
/** The "from" namespace that the rename occured on */
ns: ChangeStreamNameSpace;
/**
* An description of the operation.
*
* Only present when the `showExpandedEvents` flag is enabled.
*
* @since 6.1.0
*/
operationDescription?: Document;
}

/**
Expand Down Expand Up @@ -422,173 +410,84 @@ export interface ChangeStreamInvalidateDocument extends ChangeStreamDocumentComm
* @public
* @see https://www.mongodb.com/docs/manual/reference/change-events/#invalidate-event
*/
export interface ChangeStreamCreateIndexDocument extends ChangeStreamDocumentCommon {
export interface ChangeStreamCreateIndexDocument
extends ChangeStreamDocumentCommon,
ChangeStreamDocumentCollectionUUID,
ChangeStreamDocumentOperationDescription {
/** Describes the type of operation represented in this change notification */
operationType: 'createIndexes';

/**
* The UUID of the collection that the operation was performed on.
*
* Only present when the `showExpandedEvents` flag is enabled.
*
* @since 6.1.0
*/
collectionUUID: UUID;

/**
* An description of the operation.
*
* Only present when the `showExpandedEvents` flag is enabled.
*
* @since 6.1.0
*/
operationDescription?: Document;
}

/**
* Only present when the `showExpandedEvents` flag is enabled.
* @public
* @see https://www.mongodb.com/docs/manual/reference/change-events/#invalidate-event
*/
export interface ChangeStreamDropIndexDocument extends ChangeStreamDocumentCommon {
export interface ChangeStreamDropIndexDocument
extends ChangeStreamDocumentCommon,
ChangeStreamDocumentCollectionUUID,
ChangeStreamDocumentOperationDescription {
/** Describes the type of operation represented in this change notification */
operationType: 'dropIndexes';

/**
* The UUID of the collection that the operation was performed on.
*
* Only present when the `showExpandedEvents` flag is enabled.
*
* @since 6.1.0
*/
collectionUUID: UUID;

/**
* An description of the operation.
*
* Only present when the `showExpandedEvents` flag is enabled.
*
* @since 6.1.0
*/
operationDescription?: Document;
}

/**
* Only present when the `showExpandedEvents` flag is enabled.
* @public
* @see https://www.mongodb.com/docs/manual/reference/change-events/#invalidate-event
*/
export interface ChangeStreamCollModDocument extends ChangeStreamDocumentCommon {
export interface ChangeStreamCollModDocument
extends ChangeStreamDocumentCommon,
ChangeStreamDocumentCollectionUUID {
/** Describes the type of operation represented in this change notification */
operationType: 'modify';

/**
* The UUID of the collection that the operation was performed on.
*
* Only present when the `showExpandedEvents` flag is enabled.
*
* @since 6.1.0
*/
collectionUUID: UUID;
}

/**
* @public
* @see https://www.mongodb.com/docs/manual/reference/change-events/#invalidate-event
*/
export interface ChangeStreamCreateDocument extends ChangeStreamDocumentCommon {
export interface ChangeStreamCreateDocument
extends ChangeStreamDocumentCommon,
ChangeStreamDocumentCollectionUUID {
/** Describes the type of operation represented in this change notification */
operationType: 'create';

/**
* The UUID of the collection that the operation was performed on.
*
* Only present when the `showExpandedEvents` flag is enabled.
*
* @since 6.1.0
*/
collectionUUID: UUID;
}

/**
* @public
* @see https://www.mongodb.com/docs/manual/reference/change-events/#invalidate-event
*/
export interface ChangeStreamShardCollectionDocument extends ChangeStreamDocumentCommon {
export interface ChangeStreamShardCollectionDocument
extends ChangeStreamDocumentCommon,
ChangeStreamDocumentCollectionUUID,
ChangeStreamDocumentOperationDescription {
/** Describes the type of operation represented in this change notification */
operationType: 'shardCollection';

/**
* The UUID of the collection that the operation was performed on.
*
* Only present when the `showExpandedEvents` flag is enabled.
*
* @since 6.1.0
*/
collectionUUID: UUID;

/**
* An description of the operation.
*
* Only present when the `showExpandedEvents` flag is enabled.
*
* @since 6.1.0
*/
operationDescription?: Document;
}

/**
* @public
* @see https://www.mongodb.com/docs/manual/reference/change-events/#invalidate-event
*/
export interface ChangeStreamReshardCollectionDocument extends ChangeStreamDocumentCommon {
export interface ChangeStreamReshardCollectionDocument
extends ChangeStreamDocumentCommon,
ChangeStreamDocumentCollectionUUID,
ChangeStreamDocumentOperationDescription {
/** Describes the type of operation represented in this change notification */
operationType: 'reshardCollection';

/**
* The UUID of the collection that the operation was performed on.
*
* Only present when the `showExpandedEvents` flag is enabled.
*
* @since 6.1.0
*/
collectionUUID: UUID;

/**
* An description of the operation.
*
* Only present when the `showExpandedEvents` flag is enabled.
*
* @since 6.1.0
*/
operationDescription?: Document;
}

/**
* @public
* @see https://www.mongodb.com/docs/manual/reference/change-events/#invalidate-event
*/
export interface ChangeStreamRefineCollectionShardKeyDocument extends ChangeStreamDocumentCommon {
export interface ChangeStreamRefineCollectionShardKeyDocument
extends ChangeStreamDocumentCommon,
ChangeStreamDocumentCollectionUUID,
ChangeStreamDocumentOperationDescription {
/** Describes the type of operation represented in this change notification */
operationType: 'refineCollectionShardKey';

/**
* The UUID of the collection that the operation was performed on.
*
* Only present when the `showExpandedEvents` flag is enabled.
*
* @since 6.1.0
*/
collectionUUID?: UUID;

/**
* An description of the operation.
*
* Only present when the `showExpandedEvents` flag is enabled.
*
* @since 6.1.0
*/
operationDescription?: Document;
}

/** @public */
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,10 @@ export type {
ChangeStreamCursorOptions,
ChangeStreamDeleteDocument,
ChangeStreamDocument,
ChangeStreamDocumentCollectionUUID,
ChangeStreamDocumentCommon,
ChangeStreamDocumentKey,
ChangeStreamDocumentOperationDescription,
ChangeStreamDropDatabaseDocument,
ChangeStreamDropDocument,
ChangeStreamDropIndexDocument,
Expand Down

0 comments on commit 010be9a

Please sign in to comment.