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
6
import type { MongoDBNamespace } from '../utils' ;
7
- import { CommandOperation , type CommandOperationOptions } from './command' ;
7
+ import { type CommandOperationOptions , ModernizedCommandOperation } from './command' ;
8
8
import { Aspect , defineAspects } from './operation' ;
9
9
10
10
/** @public */
@@ -21,8 +21,15 @@ export interface CountOptions extends CommandOperationOptions {
21
21
hint ?: string | Document ;
22
22
}
23
23
24
+ class CountResponse extends MongoDBResponse {
25
+ get n ( ) : number {
26
+ return this . getNumber ( 'n' ) ?? 0 ;
27
+ }
28
+ }
29
+
24
30
/** @internal */
25
- export class CountOperation extends CommandOperation < number > {
31
+ export class CountOperation extends ModernizedCommandOperation < number > {
32
+ override SERVER_COMMAND_RESPONSE_TYPE = CountResponse ;
26
33
override options : CountOptions ;
27
34
collectionName ?: string ;
28
35
query : Document ;
@@ -39,35 +46,33 @@ export class CountOperation extends CommandOperation<number> {
39
46
return 'count' as const ;
40
47
}
41
48
42
- override async execute (
43
- server : Server ,
44
- session : ClientSession | undefined ,
45
- timeoutContext : TimeoutContext
46
- ) : Promise < number > {
47
- const options = this . options ;
49
+ override buildCommandDocument ( _connection : Connection , _session ?: ClientSession ) : Document {
48
50
const cmd : Document = {
49
51
count : this . collectionName ,
50
52
query : this . query
51
53
} ;
52
54
53
- if ( typeof options . limit === 'number' ) {
54
- cmd . limit = options . limit ;
55
+ if ( typeof this . options . limit === 'number' ) {
56
+ cmd . limit = this . options . limit ;
55
57
}
56
58
57
- if ( typeof options . skip === 'number' ) {
58
- cmd . skip = options . skip ;
59
+ if ( typeof this . options . skip === 'number' ) {
60
+ cmd . skip = this . options . skip ;
59
61
}
60
62
61
- if ( options . hint != null ) {
62
- cmd . hint = options . hint ;
63
+ if ( this . options . hint != null ) {
64
+ cmd . hint = this . options . hint ;
63
65
}
64
66
65
- if ( typeof options . maxTimeMS === 'number' ) {
66
- cmd . maxTimeMS = options . maxTimeMS ;
67
+ if ( typeof this . options . maxTimeMS === 'number' ) {
68
+ cmd . maxTimeMS = this . options . maxTimeMS ;
67
69
}
68
70
69
- const result = await super . executeCommand ( server , session , cmd , timeoutContext ) ;
70
- return result ? result . n : 0 ;
71
+ return cmd ;
72
+ }
73
+
74
+ override handleOk ( response : InstanceType < typeof this . SERVER_COMMAND_RESPONSE_TYPE > ) : number {
75
+ return response . n ;
71
76
}
72
77
}
73
78
0 commit comments