Skip to content

Commit 77a2709

Browse files
authored
refactor(NODE-5360): refactor CommandOperation to use async (#3749)
1 parent eb99291 commit 77a2709

26 files changed

+173
-84
lines changed

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,7 @@ export type {
388388
CommandOperationOptions,
389389
OperationParent
390390
} from './operations/command';
391+
export type { CommandCallbackOperation } from './operations/command';
391392
export type { IndexInformationOptions } from './operations/common_functions';
392393
export type { CountOptions } from './operations/count';
393394
export type { CountDocumentsOptions } from './operations/count_documents';

src/operations/add_user.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { MongoInvalidArgumentError } from '../error';
66
import type { Server } from '../sdam/server';
77
import type { ClientSession } from '../sessions';
88
import { type Callback, emitWarningOnce, getTopology } from '../utils';
9-
import { CommandOperation, type CommandOperationOptions } from './command';
9+
import { CommandCallbackOperation, type CommandOperationOptions } from './command';
1010
import { Aspect, defineAspects } from './operation';
1111

1212
/**
@@ -35,7 +35,7 @@ export interface AddUserOptions extends CommandOperationOptions {
3535
}
3636

3737
/** @internal */
38-
export class AddUserOperation extends CommandOperation<Document> {
38+
export class AddUserOperation extends CommandCallbackOperation<Document> {
3939
override options: AddUserOptions;
4040
db: Db;
4141
username: string;
@@ -117,7 +117,7 @@ export class AddUserOperation extends CommandOperation<Document> {
117117
command.pwd = userPassword;
118118
}
119119

120-
super.executeCommand(server, session, command, callback);
120+
super.executeCommandCallback(server, session, command, callback);
121121
}
122122
}
123123

src/operations/aggregate.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ import type { Server } from '../sdam/server';
44
import type { ClientSession } from '../sessions';
55
import { type Callback, maxWireVersion, type MongoDBNamespace } from '../utils';
66
import { WriteConcern } from '../write_concern';
7-
import { type CollationOptions, CommandOperation, type CommandOperationOptions } from './command';
7+
import {
8+
type CollationOptions,
9+
CommandCallbackOperation,
10+
type CommandOperationOptions
11+
} from './command';
812
import { Aspect, defineAspects, type Hint } from './operation';
913

1014
/** @internal */
@@ -36,7 +40,7 @@ export interface AggregateOptions extends CommandOperationOptions {
3640
}
3741

3842
/** @internal */
39-
export class AggregateOperation<T = Document> extends CommandOperation<T> {
43+
export class AggregateOperation<T = Document> extends CommandCallbackOperation<T> {
4044
override options: AggregateOptions;
4145
target: string | typeof DB_AGGREGATE_COLLECTION;
4246
pipeline: Document[];
@@ -133,7 +137,7 @@ export class AggregateOperation<T = Document> extends CommandOperation<T> {
133137
command.cursor.batchSize = options.batchSize;
134138
}
135139

136-
super.executeCommand(server, session, command, callback);
140+
super.executeCommandCallback(server, session, command, callback);
137141
}
138142
}
139143

src/operations/command.ts

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,12 +107,11 @@ export abstract class CommandOperation<T> extends AbstractCallbackOperation<T> {
107107
return true;
108108
}
109109

110-
executeCommand(
110+
async executeCommand(
111111
server: Server,
112112
session: ClientSession | undefined,
113-
cmd: Document,
114-
callback: Callback
115-
): void {
113+
cmd: Document
114+
): Promise<Document> {
116115
// TODO: consider making this a non-enumerable property
117116
this.server = server;
118117

@@ -154,6 +153,25 @@ export abstract class CommandOperation<T> extends AbstractCallbackOperation<T> {
154153
cmd = decorateWithExplain(cmd, this.explain);
155154
}
156155

157-
server.command(this.ns, cmd, options, callback);
156+
return server.commandAsync(this.ns, cmd, options);
157+
}
158+
}
159+
160+
/** @internal */
161+
export abstract class CommandCallbackOperation<T = any> extends CommandOperation<T> {
162+
constructor(parent?: OperationParent, options?: CommandOperationOptions) {
163+
super(parent, options);
164+
}
165+
166+
executeCommandCallback(
167+
server: Server,
168+
session: ClientSession | undefined,
169+
cmd: Document,
170+
callback: Callback
171+
): void {
172+
super.executeCommand(server, session, cmd).then(
173+
res => callback(undefined, res),
174+
err => callback(err, undefined)
175+
);
158176
}
159177
}

src/operations/count.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { Collection } from '../collection';
33
import type { Server } from '../sdam/server';
44
import type { ClientSession } from '../sessions';
55
import type { Callback, MongoDBNamespace } from '../utils';
6-
import { CommandOperation, type CommandOperationOptions } from './command';
6+
import { CommandCallbackOperation, type CommandOperationOptions } from './command';
77
import { Aspect, defineAspects } from './operation';
88

99
/** @public */
@@ -19,7 +19,7 @@ export interface CountOptions extends CommandOperationOptions {
1919
}
2020

2121
/** @internal */
22-
export class CountOperation extends CommandOperation<number> {
22+
export class CountOperation extends CommandCallbackOperation<number> {
2323
override options: CountOptions;
2424
collectionName?: string;
2525
query: Document;
@@ -59,7 +59,7 @@ export class CountOperation extends CommandOperation<number> {
5959
cmd.maxTimeMS = options.maxTimeMS;
6060
}
6161

62-
super.executeCommand(server, session, cmd, (err, result) => {
62+
super.executeCommandCallback(server, session, cmd, (err, result) => {
6363
callback(err, result ? result.n : 0);
6464
});
6565
}

src/operations/create_collection.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import type { PkFactory } from '../mongo_client';
1010
import type { Server } from '../sdam/server';
1111
import type { ClientSession } from '../sessions';
1212
import type { Callback } from '../utils';
13-
import { CommandOperation, type CommandOperationOptions } from './command';
13+
import { CommandCallbackOperation, type CommandOperationOptions } from './command';
1414
import { CreateIndexOperation } from './indexes';
1515
import { Aspect, defineAspects } from './operation';
1616

@@ -108,7 +108,7 @@ const INVALID_QE_VERSION =
108108
'Driver support of Queryable Encryption is incompatible with server. Upgrade server to use Queryable Encryption.';
109109

110110
/** @internal */
111-
export class CreateCollectionOperation extends CommandOperation<Collection> {
111+
export class CreateCollectionOperation extends CommandCallbackOperation<Collection> {
112112
override options: CreateCollectionOptions;
113113
db: Db;
114114
name: string;
@@ -209,7 +209,7 @@ export class CreateCollectionOperation extends CommandOperation<Collection> {
209209
}
210210

211211
// otherwise just execute the command
212-
super.executeCommand(server, session, cmd, done);
212+
super.executeCommandCallback(server, session, cmd, done);
213213
});
214214
}
215215
}

src/operations/delete.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ import type { Server } from '../sdam/server';
55
import type { ClientSession } from '../sessions';
66
import type { Callback, MongoDBNamespace } from '../utils';
77
import type { WriteConcernOptions } from '../write_concern';
8-
import { type CollationOptions, CommandOperation, type CommandOperationOptions } from './command';
8+
import {
9+
type CollationOptions,
10+
CommandCallbackOperation,
11+
type CommandOperationOptions
12+
} from './command';
913
import { Aspect, defineAspects, type Hint } from './operation';
1014

1115
/** @public */
@@ -41,7 +45,7 @@ export interface DeleteStatement {
4145
}
4246

4347
/** @internal */
44-
export class DeleteOperation extends CommandOperation<DeleteResult> {
48+
export class DeleteOperation extends CommandCallbackOperation<DeleteResult> {
4549
override options: DeleteOptions;
4650
statements: DeleteStatement[];
4751

@@ -92,7 +96,7 @@ export class DeleteOperation extends CommandOperation<DeleteResult> {
9296
}
9397
}
9498

95-
super.executeCommand(server, session, command, callback);
99+
super.executeCommandCallback(server, session, command, callback);
96100
}
97101
}
98102

src/operations/distinct.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { Collection } from '../collection';
33
import type { Server } from '../sdam/server';
44
import type { ClientSession } from '../sessions';
55
import { type Callback, decorateWithCollation, decorateWithReadConcern } from '../utils';
6-
import { CommandOperation, type CommandOperationOptions } from './command';
6+
import { CommandCallbackOperation, type CommandOperationOptions } from './command';
77
import { Aspect, defineAspects } from './operation';
88

99
/** @public */
@@ -13,7 +13,7 @@ export type DistinctOptions = CommandOperationOptions;
1313
* Return a list of distinct values for the given key across a collection.
1414
* @internal
1515
*/
16-
export class DistinctOperation extends CommandOperation<any[]> {
16+
export class DistinctOperation extends CommandCallbackOperation<any[]> {
1717
override options: DistinctOptions;
1818
collection: Collection;
1919
/** Field of the document to find distinct values for. */
@@ -76,7 +76,7 @@ export class DistinctOperation extends CommandOperation<any[]> {
7676
return callback(err);
7777
}
7878

79-
super.executeCommand(server, session, cmd, (err, result) => {
79+
super.executeCommandCallback(server, session, cmd, (err, result) => {
8080
if (err) {
8181
callback(err);
8282
return;

src/operations/drop.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { MONGODB_ERROR_CODES, MongoServerError } from '../error';
44
import type { Server } from '../sdam/server';
55
import type { ClientSession } from '../sessions';
66
import type { Callback } from '../utils';
7-
import { CommandOperation, type CommandOperationOptions } from './command';
7+
import { CommandCallbackOperation, type CommandOperationOptions } from './command';
88
import { Aspect, defineAspects } from './operation';
99

1010
/** @public */
@@ -14,7 +14,7 @@ export interface DropCollectionOptions extends CommandOperationOptions {
1414
}
1515

1616
/** @internal */
17-
export class DropCollectionOperation extends CommandOperation<boolean> {
17+
export class DropCollectionOperation extends CommandCallbackOperation<boolean> {
1818
override options: DropCollectionOptions;
1919
db: Db;
2020
name: string;
@@ -83,7 +83,7 @@ export class DropCollectionOperation extends CommandOperation<boolean> {
8383
session: ClientSession | undefined
8484
): Promise<boolean> {
8585
return new Promise<boolean>((resolve, reject) => {
86-
super.executeCommand(server, session, { drop: this.name }, (err, result) => {
86+
super.executeCommandCallback(server, session, { drop: this.name }, (err, result) => {
8787
if (err) return reject(err);
8888
resolve(!!result.ok);
8989
});
@@ -95,7 +95,7 @@ export class DropCollectionOperation extends CommandOperation<boolean> {
9595
export type DropDatabaseOptions = CommandOperationOptions;
9696

9797
/** @internal */
98-
export class DropDatabaseOperation extends CommandOperation<boolean> {
98+
export class DropDatabaseOperation extends CommandCallbackOperation<boolean> {
9999
override options: DropDatabaseOptions;
100100

101101
constructor(db: Db, options: DropDatabaseOptions) {
@@ -107,7 +107,7 @@ export class DropDatabaseOperation extends CommandOperation<boolean> {
107107
session: ClientSession | undefined,
108108
callback: Callback<boolean>
109109
): void {
110-
super.executeCommand(server, session, { dropDatabase: 1 }, (err, result) => {
110+
super.executeCommandCallback(server, session, { dropDatabase: 1 }, (err, result) => {
111111
if (err) return callback(err);
112112
if (result.ok) return callback(undefined, true);
113113
callback(undefined, false);

src/operations/estimated_document_count.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { Collection } from '../collection';
33
import type { Server } from '../sdam/server';
44
import type { ClientSession } from '../sessions';
55
import type { Callback } from '../utils';
6-
import { CommandOperation, type CommandOperationOptions } from './command';
6+
import { CommandCallbackOperation, type CommandOperationOptions } from './command';
77
import { Aspect, defineAspects } from './operation';
88

99
/** @public */
@@ -17,7 +17,7 @@ export interface EstimatedDocumentCountOptions extends CommandOperationOptions {
1717
}
1818

1919
/** @internal */
20-
export class EstimatedDocumentCountOperation extends CommandOperation<number> {
20+
export class EstimatedDocumentCountOperation extends CommandCallbackOperation<number> {
2121
override options: EstimatedDocumentCountOptions;
2222
collectionName: string;
2323

@@ -44,7 +44,7 @@ export class EstimatedDocumentCountOperation extends CommandOperation<number> {
4444
cmd.comment = this.options.comment;
4545
}
4646

47-
super.executeCommand(server, session, cmd, (err, response) => {
47+
super.executeCommandCallback(server, session, cmd, (err, response) => {
4848
if (err) {
4949
callback(err);
5050
return;

src/operations/eval.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ import { ReadPreference } from '../read_preference';
66
import type { Server } from '../sdam/server';
77
import type { ClientSession } from '../sessions';
88
import type { Callback } from '../utils';
9-
import { CommandOperation, type CommandOperationOptions } from './command';
9+
import { CommandCallbackOperation, type CommandOperationOptions } from './command';
1010

1111
/** @public */
1212
export interface EvalOptions extends CommandOperationOptions {
1313
nolock?: boolean;
1414
}
1515

1616
/** @internal */
17-
export class EvalOperation extends CommandOperation<Document> {
17+
export class EvalOperation extends CommandCallbackOperation<Document> {
1818
override options: EvalOptions;
1919
code: Code;
2020
parameters?: Document | Document[];
@@ -65,7 +65,7 @@ export class EvalOperation extends CommandOperation<Document> {
6565
}
6666

6767
// Execute the command
68-
super.executeCommand(server, session, cmd, (err, result) => {
68+
super.executeCommandCallback(server, session, cmd, (err, result) => {
6969
if (err) return callback(err);
7070
if (result && result.ok === 1) {
7171
return callback(undefined, result.retval);

src/operations/find.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ import {
1111
type MongoDBNamespace,
1212
normalizeHintField
1313
} from '../utils';
14-
import { type CollationOptions, CommandOperation, type CommandOperationOptions } from './command';
14+
import {
15+
type CollationOptions,
16+
CommandCallbackOperation,
17+
type CommandOperationOptions
18+
} from './command';
1519
import { Aspect, defineAspects, type Hint } from './operation';
1620

1721
/**
@@ -71,7 +75,7 @@ export interface FindOptions<TSchema extends Document = Document>
7175
}
7276

7377
/** @internal */
74-
export class FindOperation extends CommandOperation<Document> {
78+
export class FindOperation extends CommandCallbackOperation<Document> {
7579
/**
7680
* @remarks WriteConcern can still be present on the options because
7781
* we inherit options from the client/db/collection. The

src/operations/find_and_modify.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import type { ClientSession } from '../sessions';
77
import { formatSort, type Sort, type SortForCmd } from '../sort';
88
import { type Callback, decorateWithCollation, hasAtomicOperators, maxWireVersion } from '../utils';
99
import type { WriteConcern, WriteConcernSettings } from '../write_concern';
10-
import { CommandOperation, type CommandOperationOptions } from './command';
10+
import { CommandCallbackOperation, type CommandOperationOptions } from './command';
1111
import { Aspect, defineAspects } from './operation';
1212

1313
/** @public */
@@ -122,7 +122,7 @@ function configureFindAndModifyCmdBaseUpdateOpts(
122122
}
123123

124124
/** @internal */
125-
class FindAndModifyOperation extends CommandOperation<Document> {
125+
class FindAndModifyOperation extends CommandCallbackOperation<Document> {
126126
override options: FindOneAndReplaceOptions | FindOneAndUpdateOptions | FindOneAndDeleteOptions;
127127
cmdBase: FindAndModifyCmdBase;
128128
collection: Collection;
@@ -220,7 +220,7 @@ class FindAndModifyOperation extends CommandOperation<Document> {
220220
}
221221

222222
// Execute the command
223-
super.executeCommand(server, session, cmd, (err, result) => {
223+
super.executeCommandCallback(server, session, cmd, (err, result) => {
224224
if (err) return callback(err);
225225
return callback(undefined, options.includeResultMetadata ? result : result.value ?? null);
226226
});

0 commit comments

Comments
 (0)