Skip to content

Commit cfa21f5

Browse files
validate collection
1 parent e866c8b commit cfa21f5

File tree

2 files changed

+105
-29
lines changed

2 files changed

+105
-29
lines changed

src/operations/validate_collection.ts

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1+
import { type Connection } from '..';
12
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';
35
import { MongoUnexpectedServerResponseError } from '../error';
4-
import type { Server } from '../sdam/server';
56
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';
88

99
/** @public */
1010
export 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(/exception|corrupt/) != 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(/exception|corrupt/) != 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
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import { expect } from 'chai';
2+
3+
import { ValidateCollectionOperation } from '../../mongodb';
4+
5+
describe('ValidateCollectionOperation', function () {
6+
let client;
7+
8+
beforeEach(function () {
9+
client = this.configuration.newClient();
10+
});
11+
12+
afterEach(async function () {
13+
await client.close();
14+
});
15+
16+
describe('buildCommandDocument()', function () {
17+
it('builds the base command when no options are provided', function () {
18+
client = this.configuration.newClient();
19+
const op = new ValidateCollectionOperation(client.db('foo').admin(), 'bar', {});
20+
21+
const doc = op.buildCommandDocument({} as any, {} as any);
22+
expect(doc).to.deep.equal({
23+
validate: 'bar'
24+
});
25+
});
26+
27+
it('supports background=true', function () {
28+
client = this.configuration.newClient();
29+
const op = new ValidateCollectionOperation(client.db('foo').admin(), 'bar', {
30+
background: true
31+
});
32+
33+
const doc = op.buildCommandDocument({} as any, {} as any);
34+
expect(doc).to.deep.equal({
35+
validate: 'bar',
36+
background: true
37+
});
38+
});
39+
40+
it('supports background=false', function () {
41+
client = this.configuration.newClient();
42+
const op = new ValidateCollectionOperation(client.db('foo').admin(), 'bar', {
43+
background: false
44+
});
45+
46+
const doc = op.buildCommandDocument({} as any, {} as any);
47+
expect(doc).to.deep.equal({
48+
validate: 'bar',
49+
background: false
50+
});
51+
});
52+
53+
it('attaches all options to the command document', function () {
54+
client = this.configuration.newClient();
55+
const op = new ValidateCollectionOperation(client.db('foo').admin(), 'bar', {
56+
background: false,
57+
a: 1,
58+
b: 2,
59+
c: 3
60+
});
61+
62+
const doc = op.buildCommandDocument({} as any, {} as any);
63+
expect(doc).to.deep.equal({
64+
validate: 'bar',
65+
background: false,
66+
a: 1,
67+
b: 2,
68+
c: 3
69+
});
70+
});
71+
72+
it('does not attach session command document', function () {
73+
client = this.configuration.newClient();
74+
const op = new ValidateCollectionOperation(client.db('foo').admin(), 'bar', {
75+
session: client.startSession()
76+
});
77+
78+
const doc = op.buildCommandDocument({} as any, {} as any);
79+
expect(doc).to.deep.equal({
80+
validate: 'bar'
81+
});
82+
});
83+
});
84+
});

0 commit comments

Comments
 (0)