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,13 @@ export interface EstimatedDocumentCountOptions extends CommandOperationOptions {
16
16
maxTimeMS ?: number ;
17
17
}
18
18
19
+ class EstimatedDocumentCountResponse extends MongoDBResponse {
20
+ n = this . getNumber ( 'n' ) ?? 0 ;
21
+ }
22
+
19
23
/** @internal */
20
- export class EstimatedDocumentCountOperation extends CommandOperation < number > {
24
+ export class EstimatedDocumentCountOperation extends ModernizedCommandOperation < number > {
25
+ override SERVER_COMMAND_RESPONSE_TYPE = EstimatedDocumentCountResponse ;
21
26
override options : EstimatedDocumentCountOptions ;
22
27
collectionName : string ;
23
28
@@ -31,11 +36,7 @@ export class EstimatedDocumentCountOperation extends CommandOperation<number> {
31
36
return 'count' as const ;
32
37
}
33
38
34
- override async execute (
35
- server : Server ,
36
- session : ClientSession | undefined ,
37
- timeoutContext : TimeoutContext
38
- ) : Promise < number > {
39
+ override buildCommandDocument ( _connection : Connection , _session ?: ClientSession ) : Document {
39
40
const cmd : Document = { count : this . collectionName } ;
40
41
41
42
if ( typeof this . options . maxTimeMS === 'number' ) {
@@ -48,9 +49,11 @@ export class EstimatedDocumentCountOperation extends CommandOperation<number> {
48
49
cmd . comment = this . options . comment ;
49
50
}
50
51
51
- const response = await super . executeCommand ( server , session , cmd , timeoutContext ) ;
52
+ return cmd ;
53
+ }
52
54
53
- return response ?. n || 0 ;
55
+ override handleOk ( response : InstanceType < typeof this . SERVER_COMMAND_RESPONSE_TYPE > ) : number {
56
+ return response . n ;
54
57
}
55
58
}
56
59
0 commit comments