Skip to content

Commit bef8cd8

Browse files
passing
1 parent 6be96f4 commit bef8cd8

File tree

5 files changed

+39
-42
lines changed

5 files changed

+39
-42
lines changed

src/operations/execute_operation.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import type { Topology } from '../sdam/topology';
2626
import type { ClientSession } from '../sessions';
2727
import { TimeoutContext } from '../timeout';
2828
import { abortable, supportsRetryableWrites } from '../utils';
29-
import { AbstractOperation, Aspect, ModernOperation } from './operation';
29+
import { AbstractOperation, Aspect, ModernizedOperation } from './operation';
3030

3131
const MMAPv1_RETRY_WRITES_ERROR_CODE = MONGODB_ERROR_CODES.IllegalOperation;
3232
const MMAPv1_RETRY_WRITES_ERROR_MESSAGE =
@@ -233,7 +233,7 @@ async function tryOperation<
233233
let previousOperationError: MongoError | undefined;
234234
let previousServer: ServerDescription | undefined;
235235

236-
const isModernOperation = operation instanceof ModernOperation;
236+
const isModernOperation = operation instanceof ModernizedOperation;
237237

238238
for (let tries = 0; tries < maxTries; tries++) {
239239
if (previousOperationError) {

src/operations/operation.ts

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import { type Connection, type MongoDBResponse, type MongoError } from '..';
1+
import { type Connection, type MongoError } from '..';
22
import { type BSONSerializeOptions, type Document, resolveBSONOptions } from '../bson';
3+
import { type MongoDBResponse } from '../cmap/wire_protocol/responses';
34
import { type Abortable } from '../mongo_types';
45
import { ReadPreference, type ReadPreferenceLike } from '../read_preference';
56
import type { Server, ServerCommandOptions } from '../sdam/server';
@@ -130,11 +131,8 @@ export abstract class AbstractOperation<TResult = any> {
130131
}
131132
}
132133

133-
export abstract class ModernOperation<
134-
TResponse extends typeof MongoDBResponse | undefined,
135-
TResult
136-
> extends AbstractOperation<TResult> {
137-
abstract RESPONSE_TYPE: TResponse;
134+
export abstract class ModernizedOperation<TResult> extends AbstractOperation<TResult> {
135+
abstract SERVER_COMMAND_RESPONSE_TYPE: typeof MongoDBResponse;
138136

139137
/** this will never be used - but we must implement it to satisfy AbstractOperation's interface */
140138
override execute(
@@ -150,16 +148,27 @@ export abstract class ModernOperation<
150148
abstract buildOptions(timeoutContext: TimeoutContext): ServerCommandOptions;
151149

152150
/**
153-
* Optional - if the operation performs error handling, such as wrapping or renaming the error,
154-
* this method can be overridden.
151+
* Given an instance of a MongoDBResponse, map the response to the correct result type. For
152+
* example, a `CountOperation` might map the response as follows:
153+
*
154+
* ```typescript
155+
* override handleOk(response: InstanceType<typeof this.SERVER_COMMAND_RESPONSE_TYPE>): TResult {
156+
* return response.toObject(this.bsonOptions).n ?? 0;
157+
* }
158+
*
159+
* // or, with type safety:
160+
* override handleOk(response: InstanceType<typeof this.SERVER_COMMAND_RESPONSE_TYPE>): TResult {
161+
* return response.getNumber('n') ?? 0;
162+
* }
163+
* ```
155164
*/
156-
abstract handleOk(
157-
response: TResponse extends typeof MongoDBResponse ? InstanceType<TResponse> : Document
158-
): TResult;
165+
handleOk(response: InstanceType<typeof this.SERVER_COMMAND_RESPONSE_TYPE>): TResult {
166+
return response.toObject(this.bsonOptions) as TResult;
167+
}
159168

160169
/**
161-
* Optional - if the operation performs post-processing
162-
* on the result document, this method can be overridden.
170+
* Optional - if the operation performs error handling, such as wrapping or renaming the error,
171+
* this method can be overridden.
163172
*/
164173
handleError(error: MongoError): void {
165174
throw error;

src/operations/search_indexes/drop.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@ import { MONGODB_ERROR_CODES, MongoServerError } from '../../error';
55
import type { ServerCommandOptions } from '../../sdam/server';
66
import type { ClientSession } from '../../sessions';
77
import { type TimeoutContext } from '../../timeout';
8-
import { ModernOperation } from '../operation';
8+
import { ModernizedOperation } from '../operation';
9+
import { MongoDBResponse } from '../../cmap/wire_protocol/responses'
910

1011
/** @internal */
11-
export class DropSearchIndexOperation extends ModernOperation<undefined, void> {
12-
override RESPONSE_TYPE: undefined;
12+
export class DropSearchIndexOperation extends ModernizedOperation<void> {
13+
override SERVER_COMMAND_RESPONSE_TYPE = MongoDBResponse;
1314

1415
private readonly collection: Collection;
1516
private readonly name: string;
@@ -38,7 +39,7 @@ export class DropSearchIndexOperation extends ModernOperation<undefined, void> {
3839
return command;
3940
}
4041

41-
override handleOk(_response: Document): void {
42+
override handleOk(_response: MongoDBResponse): void {
4243
// do nothing
4344
}
4445

src/sdam/server.ts

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,7 @@ import {
77
type ConnectionPoolOptions
88
} from '../cmap/connection_pool';
99
import { PoolClearedError } from '../cmap/errors';
10-
import {
11-
type MongoDBResponse,
12-
type MongoDBResponseConstructor
13-
} from '../cmap/wire_protocol/responses';
10+
import { type MongoDBResponseConstructor } from '../cmap/wire_protocol/responses';
1411
import {
1512
APM_EVENTS,
1613
CLOSED,
@@ -41,7 +38,7 @@ import {
4138
import type { ServerApi } from '../mongo_client';
4239
import { type Abortable, TypedEventEmitter } from '../mongo_types';
4340
import type { GetMoreOptions } from '../operations/get_more';
44-
import { type ModernOperation } from '../operations/operation';
41+
import { type ModernizedOperation } from '../operations/operation';
4542
import type { ClientSession } from '../sessions';
4643
import { type TimeoutContext } from '../timeout';
4744
import { isTransactionCommand } from '../transactions';
@@ -281,14 +278,10 @@ export class Server extends TypedEventEmitter<ServerEvents> {
281278
}
282279
}
283280

284-
public async modernCommand<TResponse extends typeof MongoDBResponse | undefined, TResult>(
285-
operation: ModernOperation<TResponse, TResult>,
281+
public async modernCommand<TResult>(
282+
operation: ModernizedOperation<TResult>,
286283
timeoutContext: TimeoutContext
287-
): Promise<
288-
typeof operation.RESPONSE_TYPE extends typeof MongoDBResponse
289-
? InstanceType<TResponse>
290-
: Document
291-
> {
284+
): Promise<InstanceType<typeof operation.SERVER_COMMAND_RESPONSE_TYPE>> {
292285
if (this.s.state === STATE_CLOSING || this.s.state === STATE_CLOSED) {
293286
throw new MongoServerClosedError();
294287
}
@@ -333,12 +326,9 @@ export class Server extends TypedEventEmitter<ServerEvents> {
333326

334327
try {
335328
try {
336-
const res = await conn.command(ns, cmd, options, operation.RESPONSE_TYPE);
329+
const res = await conn.command(ns, cmd, options, operation.SERVER_COMMAND_RESPONSE_TYPE);
337330
throwIfWriteConcernError(res);
338-
// TODO: figure out why casting is necessary
339-
return res as typeof operation.RESPONSE_TYPE extends typeof MongoDBResponse
340-
? InstanceType<TResponse>
341-
: Document;
331+
return res;
342332
} catch (commandError) {
343333
throw this.decorateCommandError(conn, cmd, options, commandError);
344334
}
@@ -357,12 +347,9 @@ export class Server extends TypedEventEmitter<ServerEvents> {
357347
reauthPromise = null; // only reachable if reauth succeeds
358348

359349
try {
360-
const res = await conn.command(ns, cmd, options);
350+
const res = await conn.command(ns, cmd, options, operation.SERVER_COMMAND_RESPONSE_TYPE);
361351
throwIfWriteConcernError(res);
362-
// TODO: figure out why casting is necessary
363-
return res as typeof operation.RESPONSE_TYPE extends typeof MongoDBResponse
364-
? InstanceType<TResponse>
365-
: Document;
352+
return res;
366353
} catch (commandError) {
367354
throw this.decorateCommandError(conn, cmd, options, commandError);
368355
}

test/integration/crud/abstract_operation.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ describe('abstract operation', function () {
290290
if (!WrapperSubclasses.includes(subclassType.name.toString())) {
291291
it(`operation.commandName equals key in command document`, async function () {
292292
const subclassInstance = subclassCreator();
293-
if (subclassInstance instanceof mongodb.ModernOperation) {
293+
if (subclassInstance instanceof mongodb.ModernizedOperation) {
294294
return;
295295
}
296296
const yieldDoc =

0 commit comments

Comments
 (0)