diff --git a/src/change_stream.ts b/src/change_stream.ts index 403c464edd..ed847519e8 100644 --- a/src/change_stream.ts +++ b/src/change_stream.ts @@ -205,6 +205,16 @@ export interface ChangeStreamDocumentCommon { splitEvent?: ChangeStreamSplitEvent; } +/** @public */ +export interface ChangeStreamDocumentWallTime { + /** + * The server date and time of the database operation. + * wallTime differs from clusterTime in that clusterTime is a timestamp taken from the oplog entry associated with the database operation event. + * @sinceServerVersion 6.0.0 + */ + wallTime?: Date; +} + /** @public */ export interface ChangeStreamDocumentCollectionUUID { /** @@ -239,7 +249,8 @@ export interface ChangeStreamDocumentOperationDescription { export interface ChangeStreamInsertDocument extends ChangeStreamDocumentCommon, ChangeStreamDocumentKey, - ChangeStreamDocumentCollectionUUID { + ChangeStreamDocumentCollectionUUID, + ChangeStreamDocumentWallTime { /** Describes the type of operation represented in this change notification */ operationType: 'insert'; /** This key will contain the document being inserted */ @@ -255,7 +266,8 @@ export interface ChangeStreamInsertDocument export interface ChangeStreamUpdateDocument extends ChangeStreamDocumentCommon, ChangeStreamDocumentKey, - ChangeStreamDocumentCollectionUUID { + ChangeStreamDocumentCollectionUUID, + ChangeStreamDocumentWallTime { /** Describes the type of operation represented in this change notification */ operationType: 'update'; /** @@ -285,7 +297,8 @@ export interface ChangeStreamUpdateDocument */ export interface ChangeStreamReplaceDocument extends ChangeStreamDocumentCommon, - ChangeStreamDocumentKey { + ChangeStreamDocumentKey, + ChangeStreamDocumentWallTime { /** Describes the type of operation represented in this change notification */ operationType: 'replace'; /** The fullDocument of a replace event represents the document after the insert of the replacement document */ @@ -309,7 +322,8 @@ export interface ChangeStreamReplaceDocument extends ChangeStreamDocumentCommon, ChangeStreamDocumentKey, - ChangeStreamDocumentCollectionUUID { + ChangeStreamDocumentCollectionUUID, + ChangeStreamDocumentWallTime { /** Describes the type of operation represented in this change notification */ operationType: 'delete'; /** Namespace the delete event occurred on */ @@ -330,7 +344,8 @@ export interface ChangeStreamDeleteDocument */ export interface ChangeStreamDropDocument extends ChangeStreamDocumentCommon, - ChangeStreamDocumentCollectionUUID { + ChangeStreamDocumentCollectionUUID, + ChangeStreamDocumentWallTime { /** Describes the type of operation represented in this change notification */ operationType: 'drop'; /** Namespace the drop event occurred on */ @@ -343,7 +358,8 @@ export interface ChangeStreamDropDocument */ export interface ChangeStreamRenameDocument extends ChangeStreamDocumentCommon, - ChangeStreamDocumentCollectionUUID { + ChangeStreamDocumentCollectionUUID, + ChangeStreamDocumentWallTime { /** Describes the type of operation represented in this change notification */ operationType: 'rename'; /** The new name for the `ns.coll` collection */ @@ -356,7 +372,9 @@ export interface ChangeStreamRenameDocument * @public * @see https://www.mongodb.com/docs/manual/reference/change-events/#dropdatabase-event */ -export interface ChangeStreamDropDatabaseDocument extends ChangeStreamDocumentCommon { +export interface ChangeStreamDropDatabaseDocument + extends ChangeStreamDocumentCommon, + ChangeStreamDocumentWallTime { /** Describes the type of operation represented in this change notification */ operationType: 'dropDatabase'; /** The database dropped */ @@ -367,7 +385,9 @@ export interface ChangeStreamDropDatabaseDocument extends ChangeStreamDocumentCo * @public * @see https://www.mongodb.com/docs/manual/reference/change-events/#invalidate-event */ -export interface ChangeStreamInvalidateDocument extends ChangeStreamDocumentCommon { +export interface ChangeStreamInvalidateDocument + extends ChangeStreamDocumentCommon, + ChangeStreamDocumentWallTime { /** Describes the type of operation represented in this change notification */ operationType: 'invalidate'; } @@ -380,7 +400,8 @@ export interface ChangeStreamInvalidateDocument extends ChangeStreamDocumentComm export interface ChangeStreamCreateIndexDocument extends ChangeStreamDocumentCommon, ChangeStreamDocumentCollectionUUID, - ChangeStreamDocumentOperationDescription { + ChangeStreamDocumentOperationDescription, + ChangeStreamDocumentWallTime { /** Describes the type of operation represented in this change notification */ operationType: 'createIndexes'; } @@ -393,7 +414,8 @@ export interface ChangeStreamCreateIndexDocument export interface ChangeStreamDropIndexDocument extends ChangeStreamDocumentCommon, ChangeStreamDocumentCollectionUUID, - ChangeStreamDocumentOperationDescription { + ChangeStreamDocumentOperationDescription, + ChangeStreamDocumentWallTime { /** Describes the type of operation represented in this change notification */ operationType: 'dropIndexes'; } @@ -405,7 +427,8 @@ export interface ChangeStreamDropIndexDocument */ export interface ChangeStreamCollModDocument extends ChangeStreamDocumentCommon, - ChangeStreamDocumentCollectionUUID { + ChangeStreamDocumentCollectionUUID, + ChangeStreamDocumentWallTime { /** Describes the type of operation represented in this change notification */ operationType: 'modify'; } @@ -416,7 +439,8 @@ export interface ChangeStreamCollModDocument */ export interface ChangeStreamCreateDocument extends ChangeStreamDocumentCommon, - ChangeStreamDocumentCollectionUUID { + ChangeStreamDocumentCollectionUUID, + ChangeStreamDocumentWallTime { /** Describes the type of operation represented in this change notification */ operationType: 'create'; @@ -435,7 +459,8 @@ export interface ChangeStreamCreateDocument export interface ChangeStreamShardCollectionDocument extends ChangeStreamDocumentCommon, ChangeStreamDocumentCollectionUUID, - ChangeStreamDocumentOperationDescription { + ChangeStreamDocumentOperationDescription, + ChangeStreamDocumentWallTime { /** Describes the type of operation represented in this change notification */ operationType: 'shardCollection'; } diff --git a/test/integration/change-streams/change_stream.test.ts b/test/integration/change-streams/change_stream.test.ts index 7a188b2e33..1e9ac09901 100644 --- a/test/integration/change-streams/change_stream.test.ts +++ b/test/integration/change-streams/change_stream.test.ts @@ -170,6 +170,26 @@ describe('Change Streams', function () { } }); + it('contains a wallTime date property on the change', { + metadata: { requires: { topology: 'replicaset', mongodb: '>=6.0.0' } }, + async test() { + const collection = db.collection('wallTimeTest'); + const changeStream = collection.watch(pipeline); + + const willBeChanges = on(changeStream, 'change'); + await once(changeStream.cursor, 'init'); + + await collection.insertOne({ d: 4 }); + + const change = (await willBeChanges.next()).value[0]; + + await changeStream.close(); + + expect(change).to.have.property('wallTime'); + expect(change.wallTime).to.be.instanceOf(Date); + } + }); + it('should create a ChangeStream on a collection and emit change events', { metadata: { requires: { topology: 'replicaset' } }, async test() { diff --git a/test/types/change_stream.test-d.ts b/test/types/change_stream.test-d.ts index 02815cefd6..f4a398f7c6 100644 --- a/test/types/change_stream.test-d.ts +++ b/test/types/change_stream.test-d.ts @@ -63,6 +63,7 @@ expectType(change._id); expectType(change.clusterTime); expectType(change.txnNumber); // Could be a Long if promoteLongs is off expectType(change.lsid); +expectType(change.wallTime); type CrudChangeDoc = | ChangeStreamInsertDocument // C