11import type { ObjectId } from '../bson' ;
2- import type { AnyError } from '../error' ;
2+ import {
3+ CONNECTION_CHECK_OUT_FAILED ,
4+ CONNECTION_CHECK_OUT_STARTED ,
5+ CONNECTION_CHECKED_IN ,
6+ CONNECTION_CHECKED_OUT ,
7+ CONNECTION_CLOSED ,
8+ CONNECTION_CREATED ,
9+ CONNECTION_POOL_CLEARED ,
10+ CONNECTION_POOL_CLOSED ,
11+ CONNECTION_POOL_CREATED ,
12+ CONNECTION_POOL_READY ,
13+ CONNECTION_READY
14+ } from '../constants' ;
15+ import type { MongoError } from '../error' ;
316import type { Connection } from './connection' ;
417import type { ConnectionPool , ConnectionPoolOptions } from './connection_pool' ;
518
@@ -8,11 +21,24 @@ import type { ConnectionPool, ConnectionPoolOptions } from './connection_pool';
821 * @public
922 * @category Event
1023 */
11- export class ConnectionPoolMonitoringEvent {
24+ export abstract class ConnectionPoolMonitoringEvent {
1225 /** A timestamp when the event was created */
1326 time : Date ;
1427 /** The address (host/port pair) of the pool */
1528 address : string ;
29+ /** @internal */
30+ abstract name :
31+ | typeof CONNECTION_CHECK_OUT_FAILED
32+ | typeof CONNECTION_CHECK_OUT_STARTED
33+ | typeof CONNECTION_CHECKED_IN
34+ | typeof CONNECTION_CHECKED_OUT
35+ | typeof CONNECTION_CLOSED
36+ | typeof CONNECTION_CREATED
37+ | typeof CONNECTION_POOL_CLEARED
38+ | typeof CONNECTION_POOL_CLOSED
39+ | typeof CONNECTION_POOL_CREATED
40+ | typeof CONNECTION_POOL_READY
41+ | typeof CONNECTION_READY ;
1642
1743 /** @internal */
1844 constructor ( pool : ConnectionPool ) {
@@ -29,6 +55,8 @@ export class ConnectionPoolMonitoringEvent {
2955export class ConnectionPoolCreatedEvent extends ConnectionPoolMonitoringEvent {
3056 /** The options used to create this connection pool */
3157 options ?: ConnectionPoolOptions ;
58+ /** @internal */
59+ name = CONNECTION_POOL_CREATED ;
3260
3361 /** @internal */
3462 constructor ( pool : ConnectionPool ) {
@@ -43,6 +71,9 @@ export class ConnectionPoolCreatedEvent extends ConnectionPoolMonitoringEvent {
4371 * @category Event
4472 */
4573export class ConnectionPoolReadyEvent extends ConnectionPoolMonitoringEvent {
74+ /** @internal */
75+ name = CONNECTION_POOL_READY ;
76+
4677 /** @internal */
4778 constructor ( pool : ConnectionPool ) {
4879 super ( pool ) ;
@@ -55,6 +86,9 @@ export class ConnectionPoolReadyEvent extends ConnectionPoolMonitoringEvent {
5586 * @category Event
5687 */
5788export class ConnectionPoolClosedEvent extends ConnectionPoolMonitoringEvent {
89+ /** @internal */
90+ name = CONNECTION_POOL_CLOSED ;
91+
5892 /** @internal */
5993 constructor ( pool : ConnectionPool ) {
6094 super ( pool ) ;
@@ -69,6 +103,8 @@ export class ConnectionPoolClosedEvent extends ConnectionPoolMonitoringEvent {
69103export class ConnectionCreatedEvent extends ConnectionPoolMonitoringEvent {
70104 /** A monotonically increasing, per-pool id for the newly created connection */
71105 connectionId : number | '<monitor>' ;
106+ /** @internal */
107+ name = CONNECTION_CREATED ;
72108
73109 /** @internal */
74110 constructor ( pool : ConnectionPool , connection : { id : number | '<monitor>' } ) {
@@ -85,6 +121,8 @@ export class ConnectionCreatedEvent extends ConnectionPoolMonitoringEvent {
85121export class ConnectionReadyEvent extends ConnectionPoolMonitoringEvent {
86122 /** The id of the connection */
87123 connectionId : number | '<monitor>' ;
124+ /** @internal */
125+ name = CONNECTION_READY ;
88126
89127 /** @internal */
90128 constructor ( pool : ConnectionPool , connection : Connection ) {
@@ -104,17 +142,23 @@ export class ConnectionClosedEvent extends ConnectionPoolMonitoringEvent {
104142 /** The reason the connection was closed */
105143 reason : string ;
106144 serviceId ?: ObjectId ;
145+ /** @internal */
146+ name = CONNECTION_CLOSED ;
147+ /** @internal */
148+ error : MongoError | null ;
107149
108150 /** @internal */
109151 constructor (
110152 pool : ConnectionPool ,
111153 connection : Pick < Connection , 'id' | 'serviceId' > ,
112- reason : string
154+ reason : 'idle' | 'stale' | 'poolClosed' | 'error' ,
155+ error ?: MongoError
113156 ) {
114157 super ( pool ) ;
115158 this . connectionId = connection . id ;
116- this . reason = reason || 'unknown' ;
159+ this . reason = reason ;
117160 this . serviceId = connection . serviceId ;
161+ this . error = error ?? null ;
118162 }
119163}
120164
@@ -124,6 +168,9 @@ export class ConnectionClosedEvent extends ConnectionPoolMonitoringEvent {
124168 * @category Event
125169 */
126170export class ConnectionCheckOutStartedEvent extends ConnectionPoolMonitoringEvent {
171+ /** @internal */
172+ name = CONNECTION_CHECK_OUT_STARTED ;
173+
127174 /** @internal */
128175 constructor ( pool : ConnectionPool ) {
129176 super ( pool ) ;
@@ -137,12 +184,21 @@ export class ConnectionCheckOutStartedEvent extends ConnectionPoolMonitoringEven
137184 */
138185export class ConnectionCheckOutFailedEvent extends ConnectionPoolMonitoringEvent {
139186 /** The reason the attempt to check out failed */
140- reason : AnyError | string ;
187+ reason : string ;
188+ /** @internal */
189+ error ?: MongoError ;
190+ /** @internal */
191+ name = CONNECTION_CHECK_OUT_FAILED ;
141192
142193 /** @internal */
143- constructor ( pool : ConnectionPool , reason : AnyError | string ) {
194+ constructor (
195+ pool : ConnectionPool ,
196+ reason : 'poolClosed' | 'timeout' | 'connectionError' ,
197+ error ?: MongoError
198+ ) {
144199 super ( pool ) ;
145200 this . reason = reason ;
201+ this . error = error ;
146202 }
147203}
148204
@@ -154,6 +210,8 @@ export class ConnectionCheckOutFailedEvent extends ConnectionPoolMonitoringEvent
154210export class ConnectionCheckedOutEvent extends ConnectionPoolMonitoringEvent {
155211 /** The id of the connection */
156212 connectionId : number | '<monitor>' ;
213+ /** @internal */
214+ name = CONNECTION_CHECKED_OUT ;
157215
158216 /** @internal */
159217 constructor ( pool : ConnectionPool , connection : Connection ) {
@@ -170,6 +228,8 @@ export class ConnectionCheckedOutEvent extends ConnectionPoolMonitoringEvent {
170228export class ConnectionCheckedInEvent extends ConnectionPoolMonitoringEvent {
171229 /** The id of the connection */
172230 connectionId : number | '<monitor>' ;
231+ /** @internal */
232+ name = CONNECTION_CHECKED_IN ;
173233
174234 /** @internal */
175235 constructor ( pool : ConnectionPool , connection : Connection ) {
@@ -188,6 +248,8 @@ export class ConnectionPoolClearedEvent extends ConnectionPoolMonitoringEvent {
188248 serviceId ?: ObjectId ;
189249
190250 interruptInUseConnections ?: boolean ;
251+ /** @internal */
252+ name = CONNECTION_POOL_CLEARED ;
191253
192254 /** @internal */
193255 constructor (
0 commit comments