Skip to content

refactor(NODE-4633): executeOperation to use maybeCallback #3421

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Sep 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,9 @@
],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"project": ["./tsconfig.json"]
"project": [
"./tsconfig.json"
]
},
"extends": [
"plugin:@typescript-eslint/recommended-requiring-type-checking"
Expand All @@ -212,9 +214,13 @@
"@typescript-eslint/no-unsafe-assignment": "off",
"@typescript-eslint/no-unsafe-return": "off",
"@typescript-eslint/no-unsafe-call": "off",

"@typescript-eslint/restrict-plus-operands": "off",
"@typescript-eslint/restrict-template-expressions": "off"
"@typescript-eslint/restrict-template-expressions": "off",
"no-return-await": "off",
"@typescript-eslint/return-await": [
"error",
"in-try-catch"
]
}
},
{
Expand Down
52 changes: 24 additions & 28 deletions src/bulk/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1276,41 +1276,37 @@ export abstract class BulkOperationBase {
: typeof options === 'function'
? options
: undefined;
options = options != null && typeof options !== 'function' ? options : {};
return maybeCallback(async () => {
options = options != null && typeof options !== 'function' ? options : {};

if (this.s.executed) {
// eslint-disable-next-line @typescript-eslint/require-await
return maybeCallback(async () => {
if (this.s.executed) {
throw new MongoBatchReExecutionError();
}, callback);
}
}

const writeConcern = WriteConcern.fromOptions(options);
if (writeConcern) {
this.s.writeConcern = writeConcern;
}
const writeConcern = WriteConcern.fromOptions(options);
if (writeConcern) {
this.s.writeConcern = writeConcern;
}

// If we have current batch
if (this.isOrdered) {
if (this.s.currentBatch) this.s.batches.push(this.s.currentBatch);
} else {
if (this.s.currentInsertBatch) this.s.batches.push(this.s.currentInsertBatch);
if (this.s.currentUpdateBatch) this.s.batches.push(this.s.currentUpdateBatch);
if (this.s.currentRemoveBatch) this.s.batches.push(this.s.currentRemoveBatch);
}
// If we have no operations in the bulk raise an error
if (this.s.batches.length === 0) {
// eslint-disable-next-line @typescript-eslint/require-await
return maybeCallback(async () => {
// If we have current batch
if (this.isOrdered) {
if (this.s.currentBatch) this.s.batches.push(this.s.currentBatch);
} else {
if (this.s.currentInsertBatch) this.s.batches.push(this.s.currentInsertBatch);
if (this.s.currentUpdateBatch) this.s.batches.push(this.s.currentUpdateBatch);
if (this.s.currentRemoveBatch) this.s.batches.push(this.s.currentRemoveBatch);
}
// If we have no operations in the bulk raise an error
if (this.s.batches.length === 0) {
throw new MongoInvalidArgumentError('Invalid BulkOperation, Batch cannot be empty');
}, callback);
}
}

this.s.executed = true;
const finalOptions = { ...this.s.options, ...options };
const operation = new BulkWriteShimOperation(this, finalOptions);
this.s.executed = true;
const finalOptions = { ...this.s.options, ...options };
const operation = new BulkWriteShimOperation(this, finalOptions);

return executeOperation(this.s.collection.s.db.s.client, operation, callback);
return executeOperation(this.s.collection.s.db.s.client, operation);
}, callback);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,7 @@ export class Collection<TSchema extends Document = Document> {
new RenameOperation(this as TODO_NODE_3286, newName, {
...options,
readPreference: ReadPreference.PRIMARY
}),
}) as TODO_NODE_3286,
callback
);
}
Expand Down Expand Up @@ -1299,7 +1299,7 @@ export class Collection<TSchema extends Document = Document> {

return executeOperation(
this.s.db.s.client,
new CollStatsOperation(this as TODO_NODE_3286, options),
new CollStatsOperation(this as TODO_NODE_3286, options) as TODO_NODE_3286,
callback
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/mongo_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,7 @@ export class MongoClient extends TypedEventEmitter<MongoClientEvents> {
return maybeCallback(async () => {
options = typeof options !== 'function' ? options : undefined;
const client = new this(url, options);
return await client.connect();
return client.connect();
}, callback);
}

Expand Down
6 changes: 0 additions & 6 deletions src/operations/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,6 @@ export abstract class CommandOperation<T> extends AbstractOperation<T> {
return true;
}

abstract override execute(
server: Server,
session: ClientSession | undefined,
callback: Callback<T>
): void;

executeCommand(
server: Server,
session: ClientSession | undefined,
Expand Down
2 changes: 1 addition & 1 deletion src/operations/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export interface DeleteStatement {
}

/** @internal */
export class DeleteOperation extends CommandOperation<Document> {
export class DeleteOperation extends CommandOperation<DeleteResult> {
override options: DeleteOptions;
statements: DeleteStatement[];

Expand Down
2 changes: 1 addition & 1 deletion src/operations/drop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export class DropCollectionOperation extends CommandOperation<boolean> {
}
}

return await this.executeWithoutEncryptedFieldsCheck(server, session);
return this.executeWithoutEncryptedFieldsCheck(server, session);
})().then(
result => callback(undefined, result),
err => callback(err)
Expand Down
Loading