1
+ import { type Connection } from '..' ;
1
2
import type { Admin } from '../admin' ;
2
- import type { Document } from '../bson' ;
3
+ import { type Document } from '../bson' ;
4
+ import { MongoDBResponse } from '../cmap/wire_protocol/responses' ;
3
5
import { MongoUnexpectedServerResponseError } from '../error' ;
4
- import type { Server } from '../sdam/server' ;
5
6
import type { ClientSession } from '../sessions' ;
6
- import { type TimeoutContext } from '../timeout' ;
7
- import { CommandOperation , type CommandOperationOptions } from './command' ;
7
+ import { type CommandOperationOptions , ModernizedCommandOperation } from './command' ;
8
8
9
9
/** @public */
10
10
export interface ValidateCollectionOptions extends CommandOperationOptions {
@@ -13,46 +13,38 @@ export interface ValidateCollectionOptions extends CommandOperationOptions {
13
13
}
14
14
15
15
/** @internal */
16
- export class ValidateCollectionOperation extends CommandOperation < Document > {
16
+ export class ValidateCollectionOperation extends ModernizedCommandOperation < Document > {
17
+ override SERVER_COMMAND_RESPONSE_TYPE = MongoDBResponse ;
17
18
override options : ValidateCollectionOptions ;
18
19
collectionName : string ;
19
- command : Document ;
20
20
21
21
constructor ( admin : Admin , collectionName : string , options : ValidateCollectionOptions ) {
22
- // Decorate command with extra options
23
- const command : Document = { validate : collectionName } ;
24
- const keys = Object . keys ( options ) ;
25
- for ( let i = 0 ; i < keys . length ; i ++ ) {
26
- if ( Object . prototype . hasOwnProperty . call ( options , keys [ i ] ) && keys [ i ] !== 'session' ) {
27
- command [ keys [ i ] ] = ( options as Document ) [ keys [ i ] ] ;
28
- }
29
- }
30
-
31
22
super ( admin . s . db , options ) ;
32
23
this . options = options ;
33
- this . command = command ;
34
24
this . collectionName = collectionName ;
35
25
}
36
26
37
27
override get commandName ( ) {
38
28
return 'validate' as const ;
39
29
}
40
30
41
- override async execute (
42
- server : Server ,
43
- session : ClientSession | undefined ,
44
- timeoutContext : TimeoutContext
45
- ) : Promise < Document > {
46
- const collectionName = this . collectionName ;
31
+ override buildCommandDocument ( _connection : Connection , _session ?: ClientSession ) : Document {
32
+ // Decorate command with extra options
33
+ return {
34
+ validate : this . collectionName ,
35
+ ...Object . fromEntries ( Object . entries ( this . options ) . filter ( entry => entry [ 0 ] !== 'session' ) )
36
+ } ;
37
+ }
47
38
48
- const doc = await super . executeCommand ( server , session , this . command , timeoutContext ) ;
49
- if ( doc . result != null && typeof doc . result !== 'string' )
39
+ override handleOk ( response : InstanceType < typeof this . SERVER_COMMAND_RESPONSE_TYPE > ) : Document {
40
+ const result = super . handleOk ( response ) ;
41
+ if ( result . result != null && typeof result . result !== 'string' )
50
42
throw new MongoUnexpectedServerResponseError ( 'Error with validation data' ) ;
51
- if ( doc . result != null && doc . result . match ( / e x c e p t i o n | c o r r u p t / ) != null )
52
- throw new MongoUnexpectedServerResponseError ( `Invalid collection ${ collectionName } ` ) ;
53
- if ( doc . valid != null && ! doc . valid )
54
- throw new MongoUnexpectedServerResponseError ( `Invalid collection ${ collectionName } ` ) ;
43
+ if ( result . result != null && result . result . match ( / e x c e p t i o n | c o r r u p t / ) != null )
44
+ throw new MongoUnexpectedServerResponseError ( `Invalid collection ${ this . collectionName } ` ) ;
45
+ if ( result . valid != null && ! result . valid )
46
+ throw new MongoUnexpectedServerResponseError ( `Invalid collection ${ this . collectionName } ` ) ;
55
47
56
- return doc ;
48
+ return response ;
57
49
}
58
50
}
0 commit comments