Skip to content

Commit

Permalink
add 3 missing new change stream types
Browse files Browse the repository at this point in the history
  • Loading branch information
baileympearson committed May 19, 2022
1 parent 1f03496 commit a945d02
Show file tree
Hide file tree
Showing 3 changed files with 207 additions and 20 deletions.
193 changes: 178 additions & 15 deletions src/change_stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,15 @@ export interface ChangeStreamInsertDocument<TSchema extends Document = Document>
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 Down Expand Up @@ -288,6 +297,15 @@ 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,6 +350,15 @@ 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;
}

/**
Expand All @@ -343,6 +370,15 @@ export interface ChangeStreamDropDocument extends ChangeStreamDocumentCommon {
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;
}

/**
Expand All @@ -357,24 +393,13 @@ export interface ChangeStreamRenameDocument extends ChangeStreamDocumentCommon {
/** The "from" namespace that the rename occured on */
ns: ChangeStreamNameSpace;
/**
* An operation description representing the changes in a `rename` event.
* An description of the operation.
*
* Only present when the `showExpandedEvents` flag is enabled.
*
* @since 6.0.0
* @since 6.1.0
*/
operationDescription?: {
/**
* Contains two fields: "db" and "coll" containing the database and
* collection name in which the change happened.
*/
to?: { db: string; coll: string };

/**
* The uuid of the target collection that was dropped.
*/
dropTarget?: UUID; // TODO - confirm that this value is optional
};
operationDescription?: Document;
}

/**
Expand Down Expand Up @@ -405,6 +430,24 @@ export interface ChangeStreamInvalidateDocument extends ChangeStreamDocumentComm
export interface ChangeStreamCreateIndexDocument extends ChangeStreamDocumentCommon {
/** 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;
}

/**
Expand All @@ -415,6 +458,24 @@ export interface ChangeStreamCreateIndexDocument extends ChangeStreamDocumentCom
export interface ChangeStreamDropIndexDocument extends ChangeStreamDocumentCommon {
/** 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;
}

/**
Expand All @@ -425,6 +486,15 @@ export interface ChangeStreamDropIndexDocument extends ChangeStreamDocumentCommo
export interface ChangeStreamCollModDocument extends ChangeStreamDocumentCommon {
/** 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;
}

/**
Expand All @@ -434,6 +504,96 @@ export interface ChangeStreamCollModDocument extends ChangeStreamDocumentCommon
export interface ChangeStreamCreateDocument extends ChangeStreamDocumentCommon {
/** 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 {
/** 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 {
/** 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 {
/** 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 All @@ -449,7 +609,10 @@ export type ChangeStreamDocument<TSchema extends Document = Document> =
| ChangeStreamCreateIndexDocument
| ChangeStreamCreateDocument
| ChangeStreamCollModDocument
| ChangeStreamDropIndexDocument;
| ChangeStreamDropIndexDocument
| ChangeStreamShardCollectionDocument
| ChangeStreamReshardCollectionDocument
| ChangeStreamRefineCollectionShardKeyDocument;

/** @public */
export interface UpdateDescription<TSchema extends Document = Document> {
Expand Down
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,11 @@ export type {
ChangeStreamInvalidateDocument,
ChangeStreamNameSpace,
ChangeStreamOptions,
ChangeStreamRefineCollectionShardKeyDocument,
ChangeStreamRenameDocument,
ChangeStreamReplaceDocument,
ChangeStreamReshardCollectionDocument,
ChangeStreamShardCollectionDocument,
ChangeStreamUpdateDocument,
OperationTime,
PipeOptions,
Expand Down
31 changes: 26 additions & 5 deletions test/types/change_stream.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import { expectError, expectType } from 'tsd';

import type {
ChangeStreamCollModDocument,
ChangeStreamCreateDocument,
ChangeStreamCreateIndexDocument,
ChangeStreamDeleteDocument,
ChangeStreamDocument,
ChangeStreamDocumentCommon,
ChangeStreamDocumentKey,
ChangeStreamDropDatabaseDocument,
ChangeStreamDropDocument,
ChangeStreamDropIndexDocument,
ChangeStreamInsertDocument,
ChangeStreamInvalidateDocument,
ChangeStreamNameSpace,
Expand All @@ -22,10 +26,9 @@ import type {
UpdateDescription
} from '../../src';
import type {
ChangeStreamCollModDocument,
ChangeStreamCreateDocument,
ChangeStreamCreateIndexDocument,
ChangeStreamDropIndexDocument
ChangeStreamRefineCollectionShardKeyDocument,
ChangeStreamReshardCollectionDocument,
ChangeStreamShardCollectionDocument
} from '../../src/change_stream';

declare const changeStreamOptions: ChangeStreamOptions;
Expand All @@ -41,7 +44,10 @@ type ChangeStreamOperationType =
| 'create'
| 'modify'
| 'createIndexes'
| 'dropIndexes';
| 'dropIndexes'
| 'shardCollection'
| 'reshardCollection'
| 'refineCollectionShardKey';

// The change stream spec says that we cannot throw an error for invalid values to `fullDocument`
// for future compatibility. This means we must leave `fullDocument` as type string.
Expand Down Expand Up @@ -152,6 +158,21 @@ switch (change.operationType) {
expectType<'dropIndexes'>(change.operationType);
break;
}
case 'shardCollection': {
expectType<ChangeStreamShardCollectionDocument>(change);
expectType<'shardCollection'>(change.operationType);
break;
}
case 'reshardCollection': {
expectType<ChangeStreamReshardCollectionDocument>(change);
expectType<'reshardCollection'>(change.operationType);
break;
}
case 'refineCollectionShardKey': {
expectType<ChangeStreamRefineCollectionShardKeyDocument>(change);
expectType<'refineCollectionShardKey'>(change.operationType);
break;
}
default: {
expectType<never>(change);
}
Expand Down

0 comments on commit a945d02

Please sign in to comment.