|
| 1 | +import { type Connection } from '..'; |
1 | 2 | import type { Admin } from '../admin';
|
2 |
| -import type { Document } from '../bson'; |
| 3 | +import { BSONType, 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 {
|
11 | 11 | /** Validates a collection in the background, without interrupting read or write traffic (only in MongoDB 4.4+) */
|
12 | 12 | background?: boolean;
|
13 | 13 | }
|
14 | 14 |
|
| 15 | +class ValidateCollectionResponse extends MongoDBResponse { |
| 16 | + get result(): string | null { |
| 17 | + return this.get('result', BSONType.string); |
| 18 | + } |
| 19 | + |
| 20 | + get valid(): boolean | null { |
| 21 | + return this.get('valid', BSONType.bool); |
| 22 | + } |
| 23 | +} |
| 24 | + |
15 | 25 | /** @internal */
|
16 |
| -export class ValidateCollectionOperation extends CommandOperation<Document> { |
| 26 | +export class ValidateCollectionOperation extends ModernizedCommandOperation<Document> { |
| 27 | + override SERVER_COMMAND_RESPONSE_TYPE = ValidateCollectionResponse; |
17 | 28 | override options: ValidateCollectionOptions;
|
18 | 29 | collectionName: string;
|
19 |
| - command: Document; |
20 | 30 |
|
21 | 31 | 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 | 32 | super(admin.s.db, options);
|
32 | 33 | this.options = options;
|
33 |
| - this.command = command; |
34 | 34 | this.collectionName = collectionName;
|
35 | 35 | }
|
36 | 36 |
|
37 | 37 | override get commandName() {
|
38 | 38 | return 'validate' as const;
|
39 | 39 | }
|
40 | 40 |
|
41 |
| - override async execute( |
42 |
| - server: Server, |
43 |
| - session: ClientSession | undefined, |
44 |
| - timeoutContext: TimeoutContext |
45 |
| - ): Promise<Document> { |
46 |
| - const collectionName = this.collectionName; |
| 41 | + override buildCommandDocument(_connection: Connection, _session?: ClientSession): Document { |
| 42 | + // Decorate command with extra options |
| 43 | + return { |
| 44 | + validate: this.collectionName, |
| 45 | + ...Object.fromEntries(Object.entries(this.options).filter(entry => entry[0] !== 'session')) |
| 46 | + }; |
| 47 | + } |
47 | 48 |
|
48 |
| - const doc = await super.executeCommand(server, session, this.command, timeoutContext); |
49 |
| - if (doc.result != null && typeof doc.result !== 'string') |
| 49 | + override handleOk(response: InstanceType<typeof this.SERVER_COMMAND_RESPONSE_TYPE>): Document { |
| 50 | + if (response.result != null && typeof response.result !== 'string') |
50 | 51 | throw new MongoUnexpectedServerResponseError('Error with validation data');
|
51 |
| - if (doc.result != null && doc.result.match(/exception|corrupt/) != null) |
52 |
| - throw new MongoUnexpectedServerResponseError(`Invalid collection ${collectionName}`); |
53 |
| - if (doc.valid != null && !doc.valid) |
54 |
| - throw new MongoUnexpectedServerResponseError(`Invalid collection ${collectionName}`); |
| 52 | + if (response.result != null && response.result.match(/exception|corrupt/) != null) |
| 53 | + throw new MongoUnexpectedServerResponseError(`Invalid collection ${this.collectionName}`); |
| 54 | + if (response.valid != null && !response.valid) |
| 55 | + throw new MongoUnexpectedServerResponseError(`Invalid collection ${this.collectionName}`); |
55 | 56 |
|
56 |
| - return doc; |
| 57 | + return response.toObject(this.bsonOptions); |
57 | 58 | }
|
58 | 59 | }
|
0 commit comments