Skip to content

Commit 492456b

Browse files
authored
refactor(NODE-4633): executeOperation to use maybeCallback (#3421)
1 parent ef3b55d commit 492456b

File tree

16 files changed

+242
-222
lines changed

16 files changed

+242
-222
lines changed

.eslintrc.json

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,9 @@
201201
],
202202
"parser": "@typescript-eslint/parser",
203203
"parserOptions": {
204-
"project": ["./tsconfig.json"]
204+
"project": [
205+
"./tsconfig.json"
206+
]
205207
},
206208
"extends": [
207209
"plugin:@typescript-eslint/recommended-requiring-type-checking"
@@ -212,9 +214,13 @@
212214
"@typescript-eslint/no-unsafe-assignment": "off",
213215
"@typescript-eslint/no-unsafe-return": "off",
214216
"@typescript-eslint/no-unsafe-call": "off",
215-
216217
"@typescript-eslint/restrict-plus-operands": "off",
217-
"@typescript-eslint/restrict-template-expressions": "off"
218+
"@typescript-eslint/restrict-template-expressions": "off",
219+
"no-return-await": "off",
220+
"@typescript-eslint/return-await": [
221+
"error",
222+
"in-try-catch"
223+
]
218224
}
219225
},
220226
{

src/bulk/common.ts

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1276,41 +1276,37 @@ export abstract class BulkOperationBase {
12761276
: typeof options === 'function'
12771277
? options
12781278
: undefined;
1279-
options = options != null && typeof options !== 'function' ? options : {};
1279+
return maybeCallback(async () => {
1280+
options = options != null && typeof options !== 'function' ? options : {};
12801281

1281-
if (this.s.executed) {
1282-
// eslint-disable-next-line @typescript-eslint/require-await
1283-
return maybeCallback(async () => {
1282+
if (this.s.executed) {
12841283
throw new MongoBatchReExecutionError();
1285-
}, callback);
1286-
}
1284+
}
12871285

1288-
const writeConcern = WriteConcern.fromOptions(options);
1289-
if (writeConcern) {
1290-
this.s.writeConcern = writeConcern;
1291-
}
1286+
const writeConcern = WriteConcern.fromOptions(options);
1287+
if (writeConcern) {
1288+
this.s.writeConcern = writeConcern;
1289+
}
12921290

1293-
// If we have current batch
1294-
if (this.isOrdered) {
1295-
if (this.s.currentBatch) this.s.batches.push(this.s.currentBatch);
1296-
} else {
1297-
if (this.s.currentInsertBatch) this.s.batches.push(this.s.currentInsertBatch);
1298-
if (this.s.currentUpdateBatch) this.s.batches.push(this.s.currentUpdateBatch);
1299-
if (this.s.currentRemoveBatch) this.s.batches.push(this.s.currentRemoveBatch);
1300-
}
1301-
// If we have no operations in the bulk raise an error
1302-
if (this.s.batches.length === 0) {
1303-
// eslint-disable-next-line @typescript-eslint/require-await
1304-
return maybeCallback(async () => {
1291+
// If we have current batch
1292+
if (this.isOrdered) {
1293+
if (this.s.currentBatch) this.s.batches.push(this.s.currentBatch);
1294+
} else {
1295+
if (this.s.currentInsertBatch) this.s.batches.push(this.s.currentInsertBatch);
1296+
if (this.s.currentUpdateBatch) this.s.batches.push(this.s.currentUpdateBatch);
1297+
if (this.s.currentRemoveBatch) this.s.batches.push(this.s.currentRemoveBatch);
1298+
}
1299+
// If we have no operations in the bulk raise an error
1300+
if (this.s.batches.length === 0) {
13051301
throw new MongoInvalidArgumentError('Invalid BulkOperation, Batch cannot be empty');
1306-
}, callback);
1307-
}
1302+
}
13081303

1309-
this.s.executed = true;
1310-
const finalOptions = { ...this.s.options, ...options };
1311-
const operation = new BulkWriteShimOperation(this, finalOptions);
1304+
this.s.executed = true;
1305+
const finalOptions = { ...this.s.options, ...options };
1306+
const operation = new BulkWriteShimOperation(this, finalOptions);
13121307

1313-
return executeOperation(this.s.collection.s.db.s.client, operation, callback);
1308+
return executeOperation(this.s.collection.s.db.s.client, operation);
1309+
}, callback);
13141310
}
13151311

13161312
/**

src/collection.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -662,7 +662,7 @@ export class Collection<TSchema extends Document = Document> {
662662
new RenameOperation(this as TODO_NODE_3286, newName, {
663663
...options,
664664
readPreference: ReadPreference.PRIMARY
665-
}),
665+
}) as TODO_NODE_3286,
666666
callback
667667
);
668668
}
@@ -1299,7 +1299,7 @@ export class Collection<TSchema extends Document = Document> {
12991299

13001300
return executeOperation(
13011301
this.s.db.s.client,
1302-
new CollStatsOperation(this as TODO_NODE_3286, options),
1302+
new CollStatsOperation(this as TODO_NODE_3286, options) as TODO_NODE_3286,
13031303
callback
13041304
);
13051305
}

src/mongo_client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -590,7 +590,7 @@ export class MongoClient extends TypedEventEmitter<MongoClientEvents> {
590590
return maybeCallback(async () => {
591591
options = typeof options !== 'function' ? options : undefined;
592592
const client = new this(url, options);
593-
return await client.connect();
593+
return client.connect();
594594
}, callback);
595595
}
596596

src/operations/command.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,6 @@ export abstract class CommandOperation<T> extends AbstractOperation<T> {
117117
return true;
118118
}
119119

120-
abstract override execute(
121-
server: Server,
122-
session: ClientSession | undefined,
123-
callback: Callback<T>
124-
): void;
125-
126120
executeCommand(
127121
server: Server,
128122
session: ClientSession | undefined,

src/operations/delete.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export interface DeleteStatement {
4444
}
4545

4646
/** @internal */
47-
export class DeleteOperation extends CommandOperation<Document> {
47+
export class DeleteOperation extends CommandOperation<DeleteResult> {
4848
override options: DeleteOptions;
4949
statements: DeleteStatement[];
5050

src/operations/drop.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ export class DropCollectionOperation extends CommandOperation<boolean> {
7272
}
7373
}
7474

75-
return await this.executeWithoutEncryptedFieldsCheck(server, session);
75+
return this.executeWithoutEncryptedFieldsCheck(server, session);
7676
})().then(
7777
result => callback(undefined, result),
7878
err => callback(err)

0 commit comments

Comments
 (0)