Skip to content

Commit eafde1d

Browse files
committed
refactor(NODE-7086): refactor misc operations
1 parent 25dd18e commit eafde1d

File tree

6 files changed

+71
-95
lines changed

6 files changed

+71
-95
lines changed

src/cmap/wire_protocol/responses.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,3 +391,15 @@ export class ClientBulkWriteCursorResponse extends CursorResponse {
391391
return this.get('writeConcernError', BSONType.object, false);
392392
}
393393
}
394+
395+
export class ProfilingLevelResponse extends MongoDBResponse {
396+
get was() {
397+
return this.get('was', BSONType.int, true);
398+
}
399+
}
400+
401+
export class DistinctResponse extends MongoDBResponse {
402+
get values() {
403+
return this.get('values', BSONType.array, true);
404+
}
405+
}

src/operations/distinct.ts

Lines changed: 13 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import type { Document } from '../bson';
2+
import { type Connection } from '../cmap/connection';
3+
import { DistinctResponse } from '../cmap/wire_protocol/responses';
24
import type { Collection } from '../collection';
3-
import type { Server } from '../sdam/server';
4-
import type { ClientSession } from '../sessions';
5-
import { type TimeoutContext } from '../timeout';
6-
import { decorateWithCollation, decorateWithReadConcern } from '../utils';
7-
import { CommandOperation, type CommandOperationOptions } from './command';
5+
import { type CommandOperationOptions, ModernizedCommandOperation } from './command';
86
import { Aspect, defineAspects } from './operation';
97

108
/** @public */
@@ -27,7 +25,8 @@ export type DistinctOptions = CommandOperationOptions & {
2725
* Return a list of distinct values for the given key across a collection.
2826
* @internal
2927
*/
30-
export class DistinctOperation extends CommandOperation<any[]> {
28+
export class DistinctOperation extends ModernizedCommandOperation<any[]> {
29+
override SERVER_COMMAND_RESPONSE_TYPE = DistinctResponse;
3130
override options: DistinctOptions;
3231
collection: Collection;
3332
/** Field of the document to find distinct values for. */
@@ -56,48 +55,16 @@ export class DistinctOperation extends CommandOperation<any[]> {
5655
return 'distinct' as const;
5756
}
5857

59-
override async execute(
60-
server: Server,
61-
session: ClientSession | undefined,
62-
timeoutContext: TimeoutContext
63-
): Promise<any[]> {
64-
const coll = this.collection;
65-
const key = this.key;
66-
const query = this.query;
67-
const options = this.options;
68-
69-
// Distinct command
70-
const cmd: Document = {
71-
distinct: coll.collectionName,
72-
key: key,
73-
query: query
58+
override buildCommandDocument(_connection: Connection): Document {
59+
return {
60+
distinct: this.collection.collectionName,
61+
key: this.key,
62+
query: this.query
7463
};
64+
}
7565

76-
// Add maxTimeMS if defined
77-
if (typeof options.maxTimeMS === 'number') {
78-
cmd.maxTimeMS = options.maxTimeMS;
79-
}
80-
81-
// we check for undefined specifically here to allow falsy values
82-
// eslint-disable-next-line no-restricted-syntax
83-
if (typeof options.comment !== 'undefined') {
84-
cmd.comment = options.comment;
85-
}
86-
87-
if (options.hint != null) {
88-
cmd.hint = options.hint;
89-
}
90-
91-
// Do we have a readConcern specified
92-
decorateWithReadConcern(cmd, coll, options);
93-
94-
// Have we specified collation
95-
decorateWithCollation(cmd, coll, options);
96-
97-
const result = await super.executeCommand(server, session, cmd, timeoutContext);
98-
99-
// @ts-expect-error: Explain always returns a document
100-
return this.explain ? result : result.values;
66+
override handleOk(response: InstanceType<typeof this.SERVER_COMMAND_RESPONSE_TYPE>): any[] {
67+
this.explain ? response.toObject() : response.values;
10168
}
10269
}
10370

src/operations/profiling_level.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1+
import { type Document } from '../bson';
2+
import { type Connection } from '../cmap/connection';
3+
import { ProfilingLevelResponse } from '../cmap/wire_protocol/responses';
14
import type { Db } from '../db';
25
import { MongoUnexpectedServerResponseError } from '../error';
3-
import type { Server } from '../sdam/server';
4-
import type { ClientSession } from '../sessions';
5-
import { type TimeoutContext } from '../timeout';
6-
import { CommandOperation, type CommandOperationOptions } from './command';
6+
import { type CommandOperationOptions, ModernizedCommandOperation } from './command';
77

88
/** @public */
99
export type ProfilingLevelOptions = CommandOperationOptions;
1010

1111
/** @internal */
12-
export class ProfilingLevelOperation extends CommandOperation<string> {
12+
export class ProfilingLevelOperation extends ModernizedCommandOperation<string> {
13+
override SERVER_COMMAND_RESPONSE_TYPE = ProfilingLevelResponse;
1314
override options: ProfilingLevelOptions;
1415

1516
constructor(db: Db, options: ProfilingLevelOptions) {
@@ -21,14 +22,13 @@ export class ProfilingLevelOperation extends CommandOperation<string> {
2122
return 'profile' as const;
2223
}
2324

24-
override async execute(
25-
server: Server,
26-
session: ClientSession | undefined,
27-
timeoutContext: TimeoutContext
28-
): Promise<string> {
29-
const doc = await super.executeCommand(server, session, { profile: -1 }, timeoutContext);
30-
if (doc.ok === 1) {
31-
const was = doc.was;
25+
override buildCommandDocument(_connection: Connection): Document {
26+
return { profile: -1 };
27+
}
28+
29+
override handleOk(response: InstanceType<typeof this.SERVER_COMMAND_RESPONSE_TYPE>): string {
30+
if (response.ok === 1) {
31+
const was = response.was;
3232
if (was === 0) return 'off';
3333
if (was === 1) return 'slow_only';
3434
if (was === 2) return 'all';

src/operations/remove_user.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1+
import { type Document } from '../bson';
2+
import { type Connection } from '../cmap/connection';
3+
import { MongoDBResponse } from '../cmap/wire_protocol/responses';
14
import type { Db } from '../db';
2-
import type { Server } from '../sdam/server';
3-
import type { ClientSession } from '../sessions';
4-
import { type TimeoutContext } from '../timeout';
5-
import { CommandOperation, type CommandOperationOptions } from './command';
5+
import { type CommandOperationOptions, ModernizedCommandOperation } from './command';
66
import { Aspect, defineAspects } from './operation';
77

88
/** @public */
99
export type RemoveUserOptions = CommandOperationOptions;
1010

1111
/** @internal */
12-
export class RemoveUserOperation extends CommandOperation<boolean> {
12+
export class RemoveUserOperation extends ModernizedCommandOperation<boolean> {
13+
override SERVER_COMMAND_RESPONSE_TYPE = MongoDBResponse;
1314
override options: RemoveUserOptions;
1415
username: string;
1516

@@ -23,12 +24,11 @@ export class RemoveUserOperation extends CommandOperation<boolean> {
2324
return 'dropUser' as const;
2425
}
2526

26-
override async execute(
27-
server: Server,
28-
session: ClientSession | undefined,
29-
timeoutContext: TimeoutContext
30-
): Promise<boolean> {
31-
await super.executeCommand(server, session, { dropUser: this.username }, timeoutContext);
27+
override buildCommandDocument(_connection: Connection): Document {
28+
return { dropUser: this.username };
29+
}
30+
31+
override handleOk(_response: InstanceType<typeof this.SERVER_COMMAND_RESPONSE_TYPE>): boolean {
3232
return true;
3333
}
3434
}

src/operations/set_profiling_level.ts

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1+
import { type Document } from '../bson';
2+
import { type Connection } from '../cmap/connection';
3+
import { MongoDBResponse } from '../cmap/wire_protocol/responses';
14
import type { Db } from '../db';
25
import { MongoInvalidArgumentError } from '../error';
3-
import type { Server } from '../sdam/server';
4-
import type { ClientSession } from '../sessions';
5-
import { type TimeoutContext } from '../timeout';
66
import { enumToString } from '../utils';
7-
import { CommandOperation, type CommandOperationOptions } from './command';
7+
import { type CommandOperationOptions, ModernizedCommandOperation } from './command';
88

99
const levelValues = new Set(['off', 'slow_only', 'all']);
1010

@@ -22,7 +22,8 @@ export type ProfilingLevel = (typeof ProfilingLevel)[keyof typeof ProfilingLevel
2222
export type SetProfilingLevelOptions = CommandOperationOptions;
2323

2424
/** @internal */
25-
export class SetProfilingLevelOperation extends CommandOperation<ProfilingLevel> {
25+
export class SetProfilingLevelOperation extends ModernizedCommandOperation<ProfilingLevel> {
26+
override SERVER_COMMAND_RESPONSE_TYPE = MongoDBResponse;
2627
override options: SetProfilingLevelOptions;
2728
level: ProfilingLevel;
2829
profile: 0 | 1 | 2;
@@ -52,21 +53,22 @@ export class SetProfilingLevelOperation extends CommandOperation<ProfilingLevel>
5253
return 'profile' as const;
5354
}
5455

55-
override async execute(
56-
server: Server,
57-
session: ClientSession | undefined,
58-
timeoutContext: TimeoutContext
59-
): Promise<ProfilingLevel> {
56+
override buildCommandDocument(_connection: Connection): Document {
6057
const level = this.level;
6158

6259
if (!levelValues.has(level)) {
60+
// TODO(NODE-3483): Determine error to put here
6361
throw new MongoInvalidArgumentError(
6462
`Profiling level must be one of "${enumToString(ProfilingLevel)}"`
6563
);
6664
}
6765

68-
// TODO(NODE-3483): Determine error to put here
69-
await super.executeCommand(server, session, { profile: this.profile }, timeoutContext);
70-
return level;
66+
return { profile: this.profile };
67+
}
68+
69+
override handleOk(
70+
_response: InstanceType<typeof this.SERVER_COMMAND_RESPONSE_TYPE>
71+
): ProfilingLevel {
72+
return this.level;
7173
}
7274
}

src/operations/stats.ts

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import type { Document } from '../bson';
2+
import { type Connection } from '../cmap/connection';
3+
import { MongoDBResponse } from '../cmap/wire_protocol/responses';
24
import type { Db } from '../db';
3-
import type { Server } from '../sdam/server';
4-
import type { ClientSession } from '../sessions';
5-
import { type TimeoutContext } from '../timeout';
6-
import { CommandOperation, type CommandOperationOptions } from './command';
5+
import { type CommandOperationOptions, ModernizedCommandOperation } from './command';
76
import { Aspect, defineAspects } from './operation';
87

98
/** @public */
@@ -13,7 +12,8 @@ export interface DbStatsOptions extends CommandOperationOptions {
1312
}
1413

1514
/** @internal */
16-
export class DbStatsOperation extends CommandOperation<Document> {
15+
export class DbStatsOperation extends ModernizedCommandOperation<Document> {
16+
override SERVER_COMMAND_RESPONSE_TYPE = MongoDBResponse;
1717
override options: DbStatsOptions;
1818

1919
constructor(db: Db, options: DbStatsOptions) {
@@ -25,17 +25,12 @@ export class DbStatsOperation extends CommandOperation<Document> {
2525
return 'dbStats' as const;
2626
}
2727

28-
override async execute(
29-
server: Server,
30-
session: ClientSession | undefined,
31-
timeoutContext: TimeoutContext
32-
): Promise<Document> {
28+
override buildCommandDocument(_connection: Connection): Document {
3329
const command: Document = { dbStats: true };
3430
if (this.options.scale != null) {
3531
command.scale = this.options.scale;
3632
}
37-
38-
return await super.executeCommand(server, session, command, timeoutContext);
33+
return command;
3934
}
4035
}
4136

0 commit comments

Comments
 (0)