1+ import { type Connection } from '..' ;
12import type { Admin } from '../admin' ;
2- import type { Document } from '../bson' ;
3+ import { type Document } from '../bson' ;
4+ import { MongoDBResponse } from '../cmap/wire_protocol/responses' ;
35import { MongoUnexpectedServerResponseError } from '../error' ;
4- import type { Server } from '../sdam/server' ;
56import type { ClientSession } from '../sessions' ;
6- import { type TimeoutContext } from '../timeout' ;
7- import { CommandOperation , type CommandOperationOptions } from './command' ;
7+ import { type CommandOperationOptions , ModernizedCommandOperation } from './command' ;
88
99/** @public */
1010export interface ValidateCollectionOptions extends CommandOperationOptions {
@@ -13,46 +13,38 @@ export interface ValidateCollectionOptions extends CommandOperationOptions {
1313}
1414
1515/** @internal */
16- export class ValidateCollectionOperation extends CommandOperation < Document > {
16+ export class ValidateCollectionOperation extends ModernizedCommandOperation < Document > {
17+ override SERVER_COMMAND_RESPONSE_TYPE = MongoDBResponse ;
1718 override options : ValidateCollectionOptions ;
1819 collectionName : string ;
19- command : Document ;
2020
2121 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-
3122 super ( admin . s . db , options ) ;
3223 this . options = options ;
33- this . command = command ;
3424 this . collectionName = collectionName ;
3525 }
3626
3727 override get commandName ( ) {
3828 return 'validate' as const ;
3929 }
4030
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+ }
4738
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' )
5042 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 } ` ) ;
5547
56- return doc ;
48+ return response ;
5749 }
5850}
0 commit comments