Skip to content

Commit 59083e1

Browse files
fix run command stuff
1 parent bbc61b7 commit 59083e1

File tree

7 files changed

+49
-77
lines changed

7 files changed

+49
-77
lines changed

src/admin.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@ import {
88
type ListDatabasesResult
99
} from './operations/list_databases';
1010
import { RemoveUserOperation, type RemoveUserOptions } from './operations/remove_user';
11-
import { RunAdminCommandOperation, type RunCommandOptions } from './operations/run_command';
11+
import { RunCommandOperation, type RunCommandOptions } from './operations/run_command';
1212
import {
1313
ValidateCollectionOperation,
1414
type ValidateCollectionOptions
1515
} from './operations/validate_collection';
16+
import { MongoDBNamespace } from './utils';
1617

1718
/** @internal */
1819
export interface AdminPrivate {
@@ -75,7 +76,7 @@ export class Admin {
7576
async command(command: Document, options?: RunCommandOptions): Promise<Document> {
7677
return await executeOperation(
7778
this.s.db.client,
78-
new RunAdminCommandOperation(command, {
79+
new RunCommandOperation(new MongoDBNamespace('admin'), command, {
7980
...resolveBSONOptions(options),
8081
session: options?.session,
8182
readPreference: options?.readPreference,

src/cursor/run_command_cursor.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { BSONSerializeOptions, Document } from '../bson';
2-
import { CursorResponse } from '../cmap/wire_protocol/responses';
2+
import { type CursorResponse } from '../cmap/wire_protocol/responses';
33
import type { Db } from '../db';
44
import { MongoAPIError, MongoRuntimeError } from '../error';
55
import { executeOperation } from '../operations/execute_operation';
@@ -143,11 +143,10 @@ export class RunCommandCursor extends AbstractCursor {
143143

144144
/** @internal */
145145
protected async _initialize(session: ClientSession): Promise<InitialCursorResponse> {
146-
const operation = new RunCursorCommandOperation(this.db, this.command, {
146+
const operation = new RunCursorCommandOperation(this.db.s.namespace, this.command, {
147147
...this.cursorOptions,
148148
session: session,
149-
readPreference: this.cursorOptions.readPreference,
150-
responseType: CursorResponse
149+
readPreference: this.cursorOptions.readPreference
151150
});
152151

153152
const response = await executeOperation(this.client, operation, this.timeoutContext);

src/db.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ export class Db {
272272
return await executeOperation(
273273
this.client,
274274
new RunCommandOperation(
275-
this,
275+
this.s.namespace,
276276
command,
277277
resolveOptions(undefined, {
278278
...resolveBSONOptions(options),

src/mongo_client.ts

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { promises as fs } from 'fs';
22
import type { TcpNetConnectOpts } from 'net';
33
import type { ConnectionOptions as TLSConnectionOptions, TLSSocketOptions } from 'tls';
44

5+
import { type ServerCommandOptions, type TimeoutContext } from '.';
56
import { type BSONSerializeOptions, type Document, resolveBSONOptions } from './bson';
67
import { ChangeStream, type ChangeStreamDocument, type ChangeStreamOptions } from './change_stream';
78
import type { AutoEncrypter, AutoEncryptionOptions } from './client-side-encryption/auto_encrypter';
@@ -20,6 +21,7 @@ import {
2021
makeClientMetadata
2122
} from './cmap/handshake/client_metadata';
2223
import type { CompressorName } from './cmap/wire_protocol/compression';
24+
import { MongoDBResponse } from './cmap/wire_protocol/responses';
2325
import { parseOptions, resolveSRVRecord } from './connection_string';
2426
import { MONGO_CLIENT_EVENTS } from './constants';
2527
import { type AbstractCursor } from './cursor/abstract_cursor';
@@ -42,7 +44,7 @@ import {
4244
} from './operations/client_bulk_write/common';
4345
import { ClientBulkWriteExecutor } from './operations/client_bulk_write/executor';
4446
import { executeOperation } from './operations/execute_operation';
45-
import { RunAdminCommandOperation } from './operations/run_command';
47+
import { ModernizedOperation } from './operations/operation';
4648
import type { ReadConcern, ReadConcernLevel, ReadConcernLike } from './read_concern';
4749
import { ReadPreference, type ReadPreferenceMode } from './read_preference';
4850
import { type AsyncDisposable, configureResourceManagement } from './resource_management';
@@ -790,13 +792,25 @@ export class MongoClient extends TypedEventEmitter<MongoClientEvents> implements
790792
const endSessions = Array.from(this.s.sessionPool.sessions, ({ id }) => id);
791793
if (endSessions.length !== 0) {
792794
try {
793-
await executeOperation(
794-
this,
795-
new RunAdminCommandOperation(
796-
{ endSessions },
797-
{ readPreference: ReadPreference.primaryPreferred, noResponse: true }
798-
)
799-
);
795+
class EndSessionsOperation extends ModernizedOperation<void> {
796+
override SERVER_COMMAND_RESPONSE_TYPE = MongoDBResponse;
797+
override buildCommand(_connection: Connection, _session?: ClientSession): Document {
798+
return {
799+
endSessions
800+
};
801+
}
802+
override buildOptions(timeoutContext: TimeoutContext): ServerCommandOptions {
803+
return {
804+
timeoutContext,
805+
readPreference: ReadPreference.primaryPreferred,
806+
noResponse: true
807+
};
808+
}
809+
override get commandName(): string {
810+
return 'endSessions';
811+
}
812+
}
813+
await executeOperation(this, new EndSessionsOperation());
800814
} catch (error) {
801815
squashError(error);
802816
}

src/operations/run_command.ts

Lines changed: 15 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
11
import { type Abortable } from '..';
22
import type { BSONSerializeOptions, Document } from '../bson';
33
import { type Connection } from '../cmap/connection';
4-
import {
5-
CursorResponse,
6-
MongoDBResponse,
7-
type MongoDBResponseConstructor
8-
} from '../cmap/wire_protocol/responses';
9-
import { type Db } from '../db';
4+
import { CursorResponse, MongoDBResponse } from '../cmap/wire_protocol/responses';
5+
import { ModernizedOperation } from '../operations/operation';
106
import type { ReadPreferenceLike } from '../read_preference';
117
import type { ServerCommandOptions } from '../sdam/server';
128
import type { ClientSession } from '../sessions';
139
import { type TimeoutContext } from '../timeout';
14-
import { MongoDBNamespace } from '../utils';
15-
import { ModernizedCommandOperation } from './command';
10+
import { type MongoDBNamespace } from '../utils';
1611

1712
/** @public */
1813
export type RunCommandOptions = {
@@ -27,31 +22,33 @@ export type RunCommandOptions = {
2722
timeoutMS?: number;
2823
/** @internal */
2924
omitMaxTimeMS?: boolean;
25+
26+
/**
27+
* @internal Hints to `executeOperation` that this operation should not unpin on an ended transaction
28+
* This is only used by the driver for transaction commands
29+
*/
30+
bypassPinningCheck?: boolean;
3031
} & BSONSerializeOptions &
3132
Abortable;
3233

3334
/** @internal */
34-
export class RunCommandOperation<T = Document> extends ModernizedCommandOperation<T> {
35+
export class RunCommandOperation<T = Document> extends ModernizedOperation<T> {
3536
override SERVER_COMMAND_RESPONSE_TYPE = MongoDBResponse;
3637
command: Document;
37-
override options: RunCommandOptions & { responseType?: MongoDBResponseConstructor };
38+
override options: RunCommandOptions;
3839

39-
constructor(
40-
parent: Db,
41-
command: Document,
42-
options: RunCommandOptions & { responseType?: MongoDBResponseConstructor }
43-
) {
44-
super(undefined, options);
40+
constructor(namespace: MongoDBNamespace, command: Document, options: RunCommandOptions) {
41+
super(options);
4542
this.command = command;
4643
this.options = options;
47-
this.ns = parent.s.namespace.withCollection('$cmd');
44+
this.ns = namespace.withCollection('$cmd');
4845
}
4946

5047
override get commandName() {
5148
return 'runCommand' as const;
5249
}
5350

54-
override buildCommandDocument(_connection: Connection, _session?: ClientSession): Document {
51+
override buildCommand(_connection: Connection, _session?: ClientSession): Document {
5552
return this.command;
5653
}
5754

@@ -79,37 +76,3 @@ export class RunCursorCommandOperation extends RunCommandOperation {
7976
return response;
8077
}
8178
}
82-
83-
export class RunAdminCommandOperation<T = Document> extends ModernizedCommandOperation<T> {
84-
override SERVER_COMMAND_RESPONSE_TYPE = MongoDBResponse;
85-
command: Document;
86-
override options: RunCommandOptions & {
87-
noResponse?: boolean;
88-
bypassPinningCheck?: boolean;
89-
};
90-
91-
constructor(
92-
command: Document,
93-
options: RunCommandOptions & {
94-
noResponse?: boolean;
95-
bypassPinningCheck?: boolean;
96-
}
97-
) {
98-
super(undefined, options);
99-
this.command = command;
100-
this.options = options;
101-
this.ns = new MongoDBNamespace('admin', '$cmd');
102-
}
103-
104-
override get commandName() {
105-
return 'runCommand' as const;
106-
}
107-
108-
override buildCommandDocument(_connection: Connection, _session?: ClientSession): Document {
109-
return this.command;
110-
}
111-
112-
override buildOptions(timeoutContext: TimeoutContext): ServerCommandOptions {
113-
return { session: this.session, timeoutContext, noResponse: this.options.noResponse };
114-
}
115-
}

src/sessions.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import {
2424
import type { MongoClient, MongoOptions } from './mongo_client';
2525
import { TypedEventEmitter } from './mongo_types';
2626
import { executeOperation } from './operations/execute_operation';
27-
import { RunAdminCommandOperation } from './operations/run_command';
27+
import { RunCommandOperation } from './operations/run_command';
2828
import { ReadConcernLevel } from './read_concern';
2929
import { ReadPreference } from './read_preference';
3030
import { type AsyncDisposable, configureResourceManagement } from './resource_management';
@@ -43,6 +43,7 @@ import {
4343
isPromiseLike,
4444
List,
4545
maxWireVersion,
46+
MongoDBNamespace,
4647
noop,
4748
now,
4849
squashError,
@@ -505,7 +506,7 @@ export class ClientSession
505506
command.recoveryToken = this.transaction.recoveryToken;
506507
}
507508

508-
const operation = new RunAdminCommandOperation(command, {
509+
const operation = new RunCommandOperation(new MongoDBNamespace('admin'), command, {
509510
session: this,
510511
readPreference: ReadPreference.primary,
511512
bypassPinningCheck: true
@@ -536,7 +537,7 @@ export class ClientSession
536537
try {
537538
await executeOperation(
538539
this.client,
539-
new RunAdminCommandOperation(command, {
540+
new RunCommandOperation(new MongoDBNamespace('admin'), command, {
540541
session: this,
541542
readPreference: ReadPreference.primary,
542543
bypassPinningCheck: true
@@ -637,7 +638,7 @@ export class ClientSession
637638
command.recoveryToken = this.transaction.recoveryToken;
638639
}
639640

640-
const operation = new RunAdminCommandOperation(command, {
641+
const operation = new RunCommandOperation(new MongoDBNamespace('admin'), command, {
641642
session: this,
642643
readPreference: ReadPreference.primary,
643644
bypassPinningCheck: true

test/integration/crud/abstract_operation.test.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -192,12 +192,6 @@ describe('abstract operation', function () {
192192
subclassType: mongodb.RunCommandOperation,
193193
correctCommandName: 'runCommand'
194194
},
195-
{
196-
subclassCreator: () =>
197-
new mongodb.RunAdminCommandOperation({ dummyCommand: 'dummyCommand' }, {}),
198-
subclassType: mongodb.RunAdminCommandOperation,
199-
correctCommandName: 'runCommand'
200-
},
201195
{
202196
subclassCreator: () =>
203197
new mongodb.CreateSearchIndexesOperation(collection, [{ definition: { a: 1 } }]),

0 commit comments

Comments
 (0)