Skip to content

Commit 0c5c0b4

Browse files
authored
refactor(NODE-5471): refactor crud operations to use async/await syntax (#3777)
1 parent 11631a2 commit 0c5c0b4

File tree

12 files changed

+247
-281
lines changed

12 files changed

+247
-281
lines changed

src/bulk/common.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { promisify } from 'util';
2+
13
import { type BSONSerializeOptions, type Document, ObjectId, resolveBSONOptions } from '../bson';
24
import type { Collection } from '../collection';
35
import {
@@ -13,7 +15,7 @@ import type { CollationOptions, CommandOperationOptions } from '../operations/co
1315
import { DeleteOperation, type DeleteStatement, makeDeleteStatement } from '../operations/delete';
1416
import { executeOperation } from '../operations/execute_operation';
1517
import { InsertOperation } from '../operations/insert';
16-
import { AbstractCallbackOperation, type Hint } from '../operations/operation';
18+
import { AbstractOperation, type Hint } from '../operations/operation';
1719
import { makeUpdateStatement, UpdateOperation, type UpdateStatement } from '../operations/update';
1820
import type { Server } from '../sdam/server';
1921
import type { Topology } from '../sdam/topology';
@@ -828,33 +830,31 @@ export interface BulkWriteOptions extends CommandOperationOptions {
828830
let?: Document;
829831
}
830832

833+
const executeCommandsAsync = promisify(executeCommands);
834+
831835
/**
832836
* TODO(NODE-4063)
833837
* BulkWrites merge complexity is implemented in executeCommands
834838
* This provides a vehicle to treat bulkOperations like any other operation (hence "shim")
835839
* We would like this logic to simply live inside the BulkWriteOperation class
836840
* @internal
837841
*/
838-
class BulkWriteShimOperation extends AbstractCallbackOperation {
842+
class BulkWriteShimOperation extends AbstractOperation {
839843
bulkOperation: BulkOperationBase;
840844
constructor(bulkOperation: BulkOperationBase, options: BulkWriteOptions) {
841845
super(options);
842846
this.bulkOperation = bulkOperation;
843847
}
844848

845-
executeCallback(
846-
server: Server,
847-
session: ClientSession | undefined,
848-
callback: Callback<any>
849-
): void {
849+
execute(_server: Server, session: ClientSession | undefined): Promise<any> {
850850
if (this.options.session == null) {
851851
// An implicit session could have been created by 'executeOperation'
852852
// So if we stick it on finalOptions here, each bulk operation
853853
// will use this same session, it'll be passed in the same way
854854
// an explicit session would be
855855
this.options.session = session;
856856
}
857-
return executeCommands(this.bulkOperation, this.options, callback);
857+
return executeCommandsAsync(this.bulkOperation, this.options);
858858
}
859859
}
860860

src/bulk/unordered.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type { Collection } from '../collection';
44
import { MongoInvalidArgumentError } from '../error';
55
import type { DeleteStatement } from '../operations/delete';
66
import type { UpdateStatement } from '../operations/update';
7-
import type { Callback } from '../utils';
7+
import { type Callback } from '../utils';
88
import {
99
Batch,
1010
BatchType,

src/operations/aggregate.ts

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
import type { Document } from '../bson';
22
import { MongoInvalidArgumentError } from '../error';
3+
import { type TODO_NODE_3286 } from '../mongo_types';
34
import type { Server } from '../sdam/server';
45
import type { ClientSession } from '../sessions';
56
import { type Callback, maxWireVersion, type MongoDBNamespace } from '../utils';
67
import { WriteConcern } from '../write_concern';
7-
import {
8-
type CollationOptions,
9-
CommandCallbackOperation,
10-
type CommandOperationOptions
11-
} from './command';
8+
import { type CollationOptions, CommandOperation, type CommandOperationOptions } from './command';
129
import { Aspect, defineAspects, type Hint } from './operation';
1310

1411
/** @internal */
@@ -40,7 +37,7 @@ export interface AggregateOptions extends CommandOperationOptions {
4037
}
4138

4239
/** @internal */
43-
export class AggregateOperation<T = Document> extends CommandCallbackOperation<T> {
40+
export class AggregateOperation<T = Document> extends CommandOperation<T> {
4441
override options: AggregateOptions;
4542
target: string | typeof DB_AGGREGATE_COLLECTION;
4643
pipeline: Document[];
@@ -93,11 +90,7 @@ export class AggregateOperation<T = Document> extends CommandCallbackOperation<T
9390
this.pipeline.push(stage);
9491
}
9592

96-
override executeCallback(
97-
server: Server,
98-
session: ClientSession | undefined,
99-
callback: Callback<T>
100-
): void {
93+
override async execute(server: Server, session: ClientSession | undefined): Promise<T> {
10194
const options: AggregateOptions = this.options;
10295
const serverWireVersion = maxWireVersion(server);
10396
const command: Document = { aggregate: this.target, pipeline: this.pipeline };
@@ -137,7 +130,15 @@ export class AggregateOperation<T = Document> extends CommandCallbackOperation<T
137130
command.cursor.batchSize = options.batchSize;
138131
}
139132

140-
super.executeCommandCallback(server, session, command, callback);
133+
return super.executeCommand(server, session, command) as TODO_NODE_3286;
134+
}
135+
136+
protected override executeCallback(
137+
_server: Server,
138+
_session: ClientSession | undefined,
139+
_callback: Callback<T>
140+
): void {
141+
throw new Error('Method not implemented.');
141142
}
142143
}
143144

src/operations/bulk_write.ts

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,10 @@ import type {
77
import type { Collection } from '../collection';
88
import type { Server } from '../sdam/server';
99
import type { ClientSession } from '../sessions';
10-
import type { Callback } from '../utils';
11-
import { AbstractCallbackOperation, Aspect, defineAspects } from './operation';
10+
import { AbstractOperation, Aspect, defineAspects } from './operation';
1211

1312
/** @internal */
14-
export class BulkWriteOperation extends AbstractCallbackOperation<BulkWriteResult> {
13+
export class BulkWriteOperation extends AbstractOperation<BulkWriteResult> {
1514
override options: BulkWriteOptions;
1615
collection: Collection;
1716
operations: AnyBulkWriteOperation[];
@@ -27,11 +26,10 @@ export class BulkWriteOperation extends AbstractCallbackOperation<BulkWriteResul
2726
this.operations = operations;
2827
}
2928

30-
override executeCallback(
29+
override async execute(
3130
server: Server,
32-
session: ClientSession | undefined,
33-
callback: Callback<BulkWriteResult>
34-
): void {
31+
session: ClientSession | undefined
32+
): Promise<BulkWriteResult> {
3533
const coll = this.collection;
3634
const operations = this.operations;
3735
const options = { ...this.options, ...this.bsonOptions, readPreference: this.readPreference };
@@ -43,19 +41,13 @@ export class BulkWriteOperation extends AbstractCallbackOperation<BulkWriteResul
4341
: coll.initializeOrderedBulkOp(options);
4442

4543
// for each op go through and add to the bulk
46-
try {
47-
for (let i = 0; i < operations.length; i++) {
48-
bulk.raw(operations[i]);
49-
}
50-
} catch (err) {
51-
return callback(err);
44+
for (let i = 0; i < operations.length; i++) {
45+
bulk.raw(operations[i]);
5246
}
5347

5448
// Execute the bulk
55-
bulk.execute({ ...options, session }).then(
56-
result => callback(undefined, result),
57-
error => callback(error)
58-
);
49+
const result = await bulk.execute({ ...options, session });
50+
return result;
5951
}
6052
}
6153

src/operations/count.ts

Lines changed: 13 additions & 10 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 { CommandCallbackOperation, type CommandOperationOptions } from './command';
6+
import { CommandOperation, 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 CommandCallbackOperation<number> {
22+
export class CountOperation extends CommandOperation<number> {
2323
override options: CountOptions;
2424
collectionName?: string;
2525
query: Document;
@@ -32,11 +32,7 @@ export class CountOperation extends CommandCallbackOperation<number> {
3232
this.query = filter;
3333
}
3434

35-
override executeCallback(
36-
server: Server,
37-
session: ClientSession | undefined,
38-
callback: Callback<number>
39-
): void {
35+
override async execute(server: Server, session: ClientSession | undefined): Promise<number> {
4036
const options = this.options;
4137
const cmd: Document = {
4238
count: this.collectionName,
@@ -59,9 +55,16 @@ export class CountOperation extends CommandCallbackOperation<number> {
5955
cmd.maxTimeMS = options.maxTimeMS;
6056
}
6157

62-
super.executeCommandCallback(server, session, cmd, (err, result) => {
63-
callback(err, result ? result.n : 0);
64-
});
58+
const result = await super.executeCommand(server, session, cmd);
59+
return result ? result.n : 0;
60+
}
61+
62+
protected override executeCallback(
63+
_server: Server,
64+
_session: ClientSession | undefined,
65+
_callback: Callback<number>
66+
): void {
67+
throw new Error('Method not implemented.');
6568
}
6669
}
6770

src/operations/count_documents.ts

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import type { Document } from '../bson';
22
import type { Collection } from '../collection';
33
import type { Server } from '../sdam/server';
44
import type { ClientSession } from '../sessions';
5-
import type { Callback } from '../utils';
65
import { AggregateOperation, type AggregateOptions } from './aggregate';
76

87
/** @public */
@@ -32,26 +31,16 @@ export class CountDocumentsOperation extends AggregateOperation<number> {
3231
super(collection.s.namespace, pipeline, options);
3332
}
3433

35-
override executeCallback(
36-
server: Server,
37-
session: ClientSession | undefined,
38-
callback: Callback<number>
39-
): void {
40-
super.executeCallback(server, session, (err, result) => {
41-
if (err || !result) {
42-
callback(err);
43-
return;
44-
}
45-
46-
// NOTE: We're avoiding creating a cursor here to reduce the callstack.
47-
const response = result as unknown as Document;
48-
if (response.cursor == null || response.cursor.firstBatch == null) {
49-
callback(undefined, 0);
50-
return;
51-
}
52-
53-
const docs = response.cursor.firstBatch;
54-
callback(undefined, docs.length ? docs[0].n : 0);
55-
});
34+
override async execute(server: Server, session: ClientSession | undefined): Promise<number> {
35+
const result = await super.execute(server, session);
36+
37+
// NOTE: We're avoiding creating a cursor here to reduce the callstack.
38+
const response = result as unknown as Document;
39+
if (response.cursor == null || response.cursor.firstBatch == null) {
40+
return 0;
41+
}
42+
43+
const docs = response.cursor.firstBatch;
44+
return docs.length ? docs[0].n : 0;
5645
}
5746
}

0 commit comments

Comments
 (0)