1
+ import { type Connection } from '..' ;
1
2
import type { Document } from '../bson' ;
3
+ import { MongoDBResponse } from '../cmap/wire_protocol/responses' ;
2
4
import type { Collection } from '../collection' ;
3
- import type { Server } from '../sdam/server' ;
4
5
import type { ClientSession } from '../sessions' ;
5
- import { type TimeoutContext } from '../timeout' ;
6
- import { CommandOperation , type CommandOperationOptions } from './command' ;
6
+ import { type CommandOperationOptions , ModernizedCommandOperation } from './command' ;
7
7
import { Aspect , defineAspects } from './operation' ;
8
8
9
9
/** @public */
@@ -16,8 +16,15 @@ export interface EstimatedDocumentCountOptions extends CommandOperationOptions {
16
16
maxTimeMS ?: number ;
17
17
}
18
18
19
+ class EstimatedDocumentCountResponse extends MongoDBResponse {
20
+ get n ( ) : number {
21
+ return this . getNumber ( 'n' ) ?? 0 ;
22
+ }
23
+ }
24
+
19
25
/** @internal */
20
- export class EstimatedDocumentCountOperation extends CommandOperation < number > {
26
+ export class EstimatedDocumentCountOperation extends ModernizedCommandOperation < number > {
27
+ override SERVER_COMMAND_RESPONSE_TYPE = EstimatedDocumentCountResponse ;
21
28
override options : EstimatedDocumentCountOptions ;
22
29
collectionName : string ;
23
30
@@ -31,11 +38,7 @@ export class EstimatedDocumentCountOperation extends CommandOperation<number> {
31
38
return 'count' as const ;
32
39
}
33
40
34
- override async execute (
35
- server : Server ,
36
- session : ClientSession | undefined ,
37
- timeoutContext : TimeoutContext
38
- ) : Promise < number > {
41
+ override buildCommandDocument ( _connection : Connection , _session ?: ClientSession ) : Document {
39
42
const cmd : Document = { count : this . collectionName } ;
40
43
41
44
if ( typeof this . options . maxTimeMS === 'number' ) {
@@ -48,9 +51,11 @@ export class EstimatedDocumentCountOperation extends CommandOperation<number> {
48
51
cmd . comment = this . options . comment ;
49
52
}
50
53
51
- const response = await super . executeCommand ( server , session , cmd , timeoutContext ) ;
54
+ return cmd ;
55
+ }
52
56
53
- return response ?. n || 0 ;
57
+ override handleOk ( response : InstanceType < typeof this . SERVER_COMMAND_RESPONSE_TYPE > ) : number {
58
+ return response . n ;
54
59
}
55
60
}
56
61
0 commit comments