Skip to content

Commit f153c6f

Browse files
qhellodurranbaileympearson
authored
fix(NODE-6955): add missing wallTime property TS change stream event interfaces (#4541)
Co-authored-by: Durran Jordan <[email protected]> Co-authored-by: Bailey Pearson <[email protected]>
1 parent 441186a commit f153c6f

File tree

4 files changed

+73
-13
lines changed

4 files changed

+73
-13
lines changed

src/change_stream.ts

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,16 @@ export interface ChangeStreamDocumentCommon {
205205
splitEvent?: ChangeStreamSplitEvent;
206206
}
207207

208+
/** @public */
209+
export interface ChangeStreamDocumentWallTime {
210+
/**
211+
* The server date and time of the database operation.
212+
* wallTime differs from clusterTime in that clusterTime is a timestamp taken from the oplog entry associated with the database operation event.
213+
* @sinceServerVersion 6.0.0
214+
*/
215+
wallTime?: Date;
216+
}
217+
208218
/** @public */
209219
export interface ChangeStreamDocumentCollectionUUID {
210220
/**
@@ -239,7 +249,8 @@ export interface ChangeStreamDocumentOperationDescription {
239249
export interface ChangeStreamInsertDocument<TSchema extends Document = Document>
240250
extends ChangeStreamDocumentCommon,
241251
ChangeStreamDocumentKey<TSchema>,
242-
ChangeStreamDocumentCollectionUUID {
252+
ChangeStreamDocumentCollectionUUID,
253+
ChangeStreamDocumentWallTime {
243254
/** Describes the type of operation represented in this change notification */
244255
operationType: 'insert';
245256
/** This key will contain the document being inserted */
@@ -255,7 +266,8 @@ export interface ChangeStreamInsertDocument<TSchema extends Document = Document>
255266
export interface ChangeStreamUpdateDocument<TSchema extends Document = Document>
256267
extends ChangeStreamDocumentCommon,
257268
ChangeStreamDocumentKey<TSchema>,
258-
ChangeStreamDocumentCollectionUUID {
269+
ChangeStreamDocumentCollectionUUID,
270+
ChangeStreamDocumentWallTime {
259271
/** Describes the type of operation represented in this change notification */
260272
operationType: 'update';
261273
/**
@@ -285,7 +297,8 @@ export interface ChangeStreamUpdateDocument<TSchema extends Document = Document>
285297
*/
286298
export interface ChangeStreamReplaceDocument<TSchema extends Document = Document>
287299
extends ChangeStreamDocumentCommon,
288-
ChangeStreamDocumentKey<TSchema> {
300+
ChangeStreamDocumentKey<TSchema>,
301+
ChangeStreamDocumentWallTime {
289302
/** Describes the type of operation represented in this change notification */
290303
operationType: 'replace';
291304
/** The fullDocument of a replace event represents the document after the insert of the replacement document */
@@ -309,7 +322,8 @@ export interface ChangeStreamReplaceDocument<TSchema extends Document = Document
309322
export interface ChangeStreamDeleteDocument<TSchema extends Document = Document>
310323
extends ChangeStreamDocumentCommon,
311324
ChangeStreamDocumentKey<TSchema>,
312-
ChangeStreamDocumentCollectionUUID {
325+
ChangeStreamDocumentCollectionUUID,
326+
ChangeStreamDocumentWallTime {
313327
/** Describes the type of operation represented in this change notification */
314328
operationType: 'delete';
315329
/** Namespace the delete event occurred on */
@@ -330,7 +344,8 @@ export interface ChangeStreamDeleteDocument<TSchema extends Document = Document>
330344
*/
331345
export interface ChangeStreamDropDocument
332346
extends ChangeStreamDocumentCommon,
333-
ChangeStreamDocumentCollectionUUID {
347+
ChangeStreamDocumentCollectionUUID,
348+
ChangeStreamDocumentWallTime {
334349
/** Describes the type of operation represented in this change notification */
335350
operationType: 'drop';
336351
/** Namespace the drop event occurred on */
@@ -343,7 +358,8 @@ export interface ChangeStreamDropDocument
343358
*/
344359
export interface ChangeStreamRenameDocument
345360
extends ChangeStreamDocumentCommon,
346-
ChangeStreamDocumentCollectionUUID {
361+
ChangeStreamDocumentCollectionUUID,
362+
ChangeStreamDocumentWallTime {
347363
/** Describes the type of operation represented in this change notification */
348364
operationType: 'rename';
349365
/** The new name for the `ns.coll` collection */
@@ -356,7 +372,9 @@ export interface ChangeStreamRenameDocument
356372
* @public
357373
* @see https://www.mongodb.com/docs/manual/reference/change-events/#dropdatabase-event
358374
*/
359-
export interface ChangeStreamDropDatabaseDocument extends ChangeStreamDocumentCommon {
375+
export interface ChangeStreamDropDatabaseDocument
376+
extends ChangeStreamDocumentCommon,
377+
ChangeStreamDocumentWallTime {
360378
/** Describes the type of operation represented in this change notification */
361379
operationType: 'dropDatabase';
362380
/** The database dropped */
@@ -367,7 +385,9 @@ export interface ChangeStreamDropDatabaseDocument extends ChangeStreamDocumentCo
367385
* @public
368386
* @see https://www.mongodb.com/docs/manual/reference/change-events/#invalidate-event
369387
*/
370-
export interface ChangeStreamInvalidateDocument extends ChangeStreamDocumentCommon {
388+
export interface ChangeStreamInvalidateDocument
389+
extends ChangeStreamDocumentCommon,
390+
ChangeStreamDocumentWallTime {
371391
/** Describes the type of operation represented in this change notification */
372392
operationType: 'invalidate';
373393
}
@@ -380,7 +400,8 @@ export interface ChangeStreamInvalidateDocument extends ChangeStreamDocumentComm
380400
export interface ChangeStreamCreateIndexDocument
381401
extends ChangeStreamDocumentCommon,
382402
ChangeStreamDocumentCollectionUUID,
383-
ChangeStreamDocumentOperationDescription {
403+
ChangeStreamDocumentOperationDescription,
404+
ChangeStreamDocumentWallTime {
384405
/** Describes the type of operation represented in this change notification */
385406
operationType: 'createIndexes';
386407
}
@@ -393,7 +414,8 @@ export interface ChangeStreamCreateIndexDocument
393414
export interface ChangeStreamDropIndexDocument
394415
extends ChangeStreamDocumentCommon,
395416
ChangeStreamDocumentCollectionUUID,
396-
ChangeStreamDocumentOperationDescription {
417+
ChangeStreamDocumentOperationDescription,
418+
ChangeStreamDocumentWallTime {
397419
/** Describes the type of operation represented in this change notification */
398420
operationType: 'dropIndexes';
399421
}
@@ -405,7 +427,8 @@ export interface ChangeStreamDropIndexDocument
405427
*/
406428
export interface ChangeStreamCollModDocument
407429
extends ChangeStreamDocumentCommon,
408-
ChangeStreamDocumentCollectionUUID {
430+
ChangeStreamDocumentCollectionUUID,
431+
ChangeStreamDocumentWallTime {
409432
/** Describes the type of operation represented in this change notification */
410433
operationType: 'modify';
411434
}
@@ -416,7 +439,8 @@ export interface ChangeStreamCollModDocument
416439
*/
417440
export interface ChangeStreamCreateDocument
418441
extends ChangeStreamDocumentCommon,
419-
ChangeStreamDocumentCollectionUUID {
442+
ChangeStreamDocumentCollectionUUID,
443+
ChangeStreamDocumentWallTime {
420444
/** Describes the type of operation represented in this change notification */
421445
operationType: 'create';
422446

@@ -435,7 +459,8 @@ export interface ChangeStreamCreateDocument
435459
export interface ChangeStreamShardCollectionDocument
436460
extends ChangeStreamDocumentCommon,
437461
ChangeStreamDocumentCollectionUUID,
438-
ChangeStreamDocumentOperationDescription {
462+
ChangeStreamDocumentOperationDescription,
463+
ChangeStreamDocumentWallTime {
439464
/** Describes the type of operation represented in this change notification */
440465
operationType: 'shardCollection';
441466
}

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ export type {
208208
ChangeStreamDocumentCommon,
209209
ChangeStreamDocumentKey,
210210
ChangeStreamDocumentOperationDescription,
211+
ChangeStreamDocumentWallTime,
211212
ChangeStreamDropDatabaseDocument,
212213
ChangeStreamDropDocument,
213214
ChangeStreamDropIndexDocument,

test/integration/change-streams/change_stream.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,26 @@ describe('Change Streams', function () {
170170
}
171171
});
172172

173+
it('contains a wallTime date property on the change', {
174+
metadata: { requires: { topology: 'replicaset', mongodb: '>=6.0.0' } },
175+
async test() {
176+
const collection = db.collection('wallTimeTest');
177+
const changeStream = collection.watch(pipeline);
178+
179+
const willBeChanges = on(changeStream, 'change');
180+
await once(changeStream.cursor, 'init');
181+
182+
await collection.insertOne({ d: 4 });
183+
184+
const change = (await willBeChanges.next()).value[0];
185+
186+
await changeStream.close();
187+
188+
expect(change).to.have.property('wallTime');
189+
expect(change.wallTime).to.be.instanceOf(Date);
190+
}
191+
});
192+
173193
it('should create a ChangeStream on a collection and emit change events', {
174194
metadata: { requires: { topology: 'replicaset' } },
175195
async test() {

test/types/change_stream.test-d.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ declare const crudChange: CrudChangeDoc;
7575
expectType<CrudChangeDoc extends ChangeStreamDocumentKey<Schema> ? true : false>(true);
7676
expectType<number>(crudChange.documentKey._id); // _id will get typed
7777
expectType<any>(crudChange.documentKey.blah); // shard keys could be anything
78+
expectType<Date | undefined>(crudChange.wallTime);
7879

7980
// ChangeStreamFullNameSpace
8081
expectType<ChangeStreamNameSpace>(crudChange.ns);
@@ -87,12 +88,14 @@ switch (change.operationType) {
8788
expectType<number>(change.documentKey._id);
8889
expectType<any>(change.documentKey.blah);
8990
expectType<Schema>(change.fullDocument);
91+
expectType<Date | undefined>(change.wallTime);
9092
break;
9193
}
9294
case 'update': {
9395
expectType<ChangeStreamUpdateDocument<Schema>>(change);
9496
expectType<'update'>(change.operationType);
9597
expectType<Schema | undefined>(change.fullDocument); // Update only attaches fullDocument if configured
98+
expectType<Date | undefined>(change.wallTime);
9699
expectType<UpdateDescription<Schema>>(change.updateDescription);
97100
expectType<Partial<Schema> | undefined>(change.updateDescription.updatedFields);
98101
expectType<string[] | undefined>(change.updateDescription.removedFields);
@@ -104,61 +107,72 @@ switch (change.operationType) {
104107
case 'replace': {
105108
expectType<ChangeStreamReplaceDocument<Schema>>(change);
106109
expectType<'replace'>(change.operationType);
110+
expectType<Date | undefined>(change.wallTime);
107111
expectType<Schema>(change.fullDocument);
108112
break;
109113
}
110114
case 'delete': {
111115
expectType<ChangeStreamDeleteDocument<Schema>>(change);
116+
expectType<Date | undefined>(change.wallTime);
112117
expectType<'delete'>(change.operationType);
113118
break;
114119
}
115120
case 'drop': {
116121
expectType<ChangeStreamDropDocument>(change);
117122
expectType<'drop'>(change.operationType);
123+
expectType<Date | undefined>(change.wallTime);
118124
expectType<{ db: string; coll: string }>(change.ns);
119125
break;
120126
}
121127
case 'rename': {
122128
expectType<ChangeStreamRenameDocument>(change);
123129
expectType<'rename'>(change.operationType);
130+
expectType<Date | undefined>(change.wallTime);
124131
expectType<{ db: string; coll: string }>(change.ns);
125132
expectType<{ db: string; coll: string }>(change.to);
126133
break;
127134
}
128135
case 'dropDatabase': {
129136
expectType<ChangeStreamDropDatabaseDocument>(change);
130137
expectType<'dropDatabase'>(change.operationType);
138+
expectType<Date | undefined>(change.wallTime);
131139
expectError(change.ns.coll);
132140
break;
133141
}
134142
case 'invalidate': {
135143
expectType<ChangeStreamInvalidateDocument>(change);
136144
expectType<'invalidate'>(change.operationType);
145+
expectType<Date | undefined>(change.wallTime);
137146
break;
138147
}
139148
case 'create': {
140149
expectType<ChangeStreamCreateDocument>(change);
141150
expectType<'create'>(change.operationType);
151+
expectType<Date | undefined>(change.wallTime);
142152
break;
143153
}
144154
case 'modify': {
145155
expectType<ChangeStreamCollModDocument>(change);
146156
expectType<'modify'>(change.operationType);
157+
expectType<Date | undefined>(change.wallTime);
147158
break;
148159
}
149160
case 'createIndexes': {
150161
expectType<ChangeStreamCreateIndexDocument>(change);
151162
expectType<'createIndexes'>(change.operationType);
163+
expectType<Date | undefined>(change.wallTime);
152164
break;
153165
}
154166
case 'dropIndexes': {
155167
expectType<ChangeStreamDropIndexDocument>(change);
156168
expectType<'dropIndexes'>(change.operationType);
169+
expectType<Date | undefined>(change.wallTime);
157170
break;
158171
}
159172
case 'shardCollection': {
160173
expectType<ChangeStreamShardCollectionDocument>(change);
161174
expectType<'shardCollection'>(change.operationType);
175+
expectType<Date | undefined>(change.wallTime);
162176
break;
163177
}
164178
case 'reshardCollection': {

0 commit comments

Comments
 (0)