Skip to content

Commit

Permalink
refactor(NODE-5360): refactor CommandOperation to use async (#3749)
Browse files Browse the repository at this point in the history
  • Loading branch information
malikj2000 committed Jul 7, 2023
1 parent eb99291 commit 77a2709
Show file tree
Hide file tree
Showing 26 changed files with 173 additions and 84 deletions.
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,7 @@ export type {
CommandOperationOptions,
OperationParent
} from './operations/command';
export type { CommandCallbackOperation } from './operations/command';
export type { IndexInformationOptions } from './operations/common_functions';
export type { CountOptions } from './operations/count';
export type { CountDocumentsOptions } from './operations/count_documents';
Expand Down
6 changes: 3 additions & 3 deletions src/operations/add_user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { MongoInvalidArgumentError } from '../error';
import type { Server } from '../sdam/server';
import type { ClientSession } from '../sessions';
import { type Callback, emitWarningOnce, getTopology } from '../utils';
import { CommandOperation, type CommandOperationOptions } from './command';
import { CommandCallbackOperation, type CommandOperationOptions } from './command';
import { Aspect, defineAspects } from './operation';

/**
Expand Down Expand Up @@ -35,7 +35,7 @@ export interface AddUserOptions extends CommandOperationOptions {
}

/** @internal */
export class AddUserOperation extends CommandOperation<Document> {
export class AddUserOperation extends CommandCallbackOperation<Document> {
override options: AddUserOptions;
db: Db;
username: string;
Expand Down Expand Up @@ -117,7 +117,7 @@ export class AddUserOperation extends CommandOperation<Document> {
command.pwd = userPassword;
}

super.executeCommand(server, session, command, callback);
super.executeCommandCallback(server, session, command, callback);
}
}

Expand Down
10 changes: 7 additions & 3 deletions src/operations/aggregate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import type { Server } from '../sdam/server';
import type { ClientSession } from '../sessions';
import { type Callback, maxWireVersion, type MongoDBNamespace } from '../utils';
import { WriteConcern } from '../write_concern';
import { type CollationOptions, CommandOperation, type CommandOperationOptions } from './command';
import {
type CollationOptions,
CommandCallbackOperation,
type CommandOperationOptions
} from './command';
import { Aspect, defineAspects, type Hint } from './operation';

/** @internal */
Expand Down Expand Up @@ -36,7 +40,7 @@ export interface AggregateOptions extends CommandOperationOptions {
}

/** @internal */
export class AggregateOperation<T = Document> extends CommandOperation<T> {
export class AggregateOperation<T = Document> extends CommandCallbackOperation<T> {
override options: AggregateOptions;
target: string | typeof DB_AGGREGATE_COLLECTION;
pipeline: Document[];
Expand Down Expand Up @@ -133,7 +137,7 @@ export class AggregateOperation<T = Document> extends CommandOperation<T> {
command.cursor.batchSize = options.batchSize;
}

super.executeCommand(server, session, command, callback);
super.executeCommandCallback(server, session, command, callback);
}
}

Expand Down
28 changes: 23 additions & 5 deletions src/operations/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,11 @@ export abstract class CommandOperation<T> extends AbstractCallbackOperation<T> {
return true;
}

executeCommand(
async executeCommand(
server: Server,
session: ClientSession | undefined,
cmd: Document,
callback: Callback
): void {
cmd: Document
): Promise<Document> {
// TODO: consider making this a non-enumerable property
this.server = server;

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

server.command(this.ns, cmd, options, callback);
return server.commandAsync(this.ns, cmd, options);
}
}

/** @internal */
export abstract class CommandCallbackOperation<T = any> extends CommandOperation<T> {
constructor(parent?: OperationParent, options?: CommandOperationOptions) {
super(parent, options);
}

executeCommandCallback(
server: Server,
session: ClientSession | undefined,
cmd: Document,
callback: Callback
): void {
super.executeCommand(server, session, cmd).then(
res => callback(undefined, res),
err => callback(err, undefined)
);
}
}
6 changes: 3 additions & 3 deletions src/operations/count.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { Collection } from '../collection';
import type { Server } from '../sdam/server';
import type { ClientSession } from '../sessions';
import type { Callback, MongoDBNamespace } from '../utils';
import { CommandOperation, type CommandOperationOptions } from './command';
import { CommandCallbackOperation, type CommandOperationOptions } from './command';
import { Aspect, defineAspects } from './operation';

/** @public */
Expand All @@ -19,7 +19,7 @@ export interface CountOptions extends CommandOperationOptions {
}

/** @internal */
export class CountOperation extends CommandOperation<number> {
export class CountOperation extends CommandCallbackOperation<number> {
override options: CountOptions;
collectionName?: string;
query: Document;
Expand Down Expand Up @@ -59,7 +59,7 @@ export class CountOperation extends CommandOperation<number> {
cmd.maxTimeMS = options.maxTimeMS;
}

super.executeCommand(server, session, cmd, (err, result) => {
super.executeCommandCallback(server, session, cmd, (err, result) => {
callback(err, result ? result.n : 0);
});
}
Expand Down
6 changes: 3 additions & 3 deletions src/operations/create_collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import type { PkFactory } from '../mongo_client';
import type { Server } from '../sdam/server';
import type { ClientSession } from '../sessions';
import type { Callback } from '../utils';
import { CommandOperation, type CommandOperationOptions } from './command';
import { CommandCallbackOperation, type CommandOperationOptions } from './command';
import { CreateIndexOperation } from './indexes';
import { Aspect, defineAspects } from './operation';

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

/** @internal */
export class CreateCollectionOperation extends CommandOperation<Collection> {
export class CreateCollectionOperation extends CommandCallbackOperation<Collection> {
override options: CreateCollectionOptions;
db: Db;
name: string;
Expand Down Expand Up @@ -209,7 +209,7 @@ export class CreateCollectionOperation extends CommandOperation<Collection> {
}

// otherwise just execute the command
super.executeCommand(server, session, cmd, done);
super.executeCommandCallback(server, session, cmd, done);
});
}
}
Expand Down
10 changes: 7 additions & 3 deletions src/operations/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import type { Server } from '../sdam/server';
import type { ClientSession } from '../sessions';
import type { Callback, MongoDBNamespace } from '../utils';
import type { WriteConcernOptions } from '../write_concern';
import { type CollationOptions, CommandOperation, type CommandOperationOptions } from './command';
import {
type CollationOptions,
CommandCallbackOperation,
type CommandOperationOptions
} from './command';
import { Aspect, defineAspects, type Hint } from './operation';

/** @public */
Expand Down Expand Up @@ -41,7 +45,7 @@ export interface DeleteStatement {
}

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

Expand Down Expand Up @@ -92,7 +96,7 @@ export class DeleteOperation extends CommandOperation<DeleteResult> {
}
}

super.executeCommand(server, session, command, callback);
super.executeCommandCallback(server, session, command, callback);
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/operations/distinct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { Collection } from '../collection';
import type { Server } from '../sdam/server';
import type { ClientSession } from '../sessions';
import { type Callback, decorateWithCollation, decorateWithReadConcern } from '../utils';
import { CommandOperation, type CommandOperationOptions } from './command';
import { CommandCallbackOperation, type CommandOperationOptions } from './command';
import { Aspect, defineAspects } from './operation';

/** @public */
Expand All @@ -13,7 +13,7 @@ export type DistinctOptions = CommandOperationOptions;
* Return a list of distinct values for the given key across a collection.
* @internal
*/
export class DistinctOperation extends CommandOperation<any[]> {
export class DistinctOperation extends CommandCallbackOperation<any[]> {
override options: DistinctOptions;
collection: Collection;
/** Field of the document to find distinct values for. */
Expand Down Expand Up @@ -76,7 +76,7 @@ export class DistinctOperation extends CommandOperation<any[]> {
return callback(err);
}

super.executeCommand(server, session, cmd, (err, result) => {
super.executeCommandCallback(server, session, cmd, (err, result) => {
if (err) {
callback(err);
return;
Expand Down
10 changes: 5 additions & 5 deletions src/operations/drop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { MONGODB_ERROR_CODES, MongoServerError } from '../error';
import type { Server } from '../sdam/server';
import type { ClientSession } from '../sessions';
import type { Callback } from '../utils';
import { CommandOperation, type CommandOperationOptions } from './command';
import { CommandCallbackOperation, type CommandOperationOptions } from './command';
import { Aspect, defineAspects } from './operation';

/** @public */
Expand All @@ -14,7 +14,7 @@ export interface DropCollectionOptions extends CommandOperationOptions {
}

/** @internal */
export class DropCollectionOperation extends CommandOperation<boolean> {
export class DropCollectionOperation extends CommandCallbackOperation<boolean> {
override options: DropCollectionOptions;
db: Db;
name: string;
Expand Down Expand Up @@ -83,7 +83,7 @@ export class DropCollectionOperation extends CommandOperation<boolean> {
session: ClientSession | undefined
): Promise<boolean> {
return new Promise<boolean>((resolve, reject) => {
super.executeCommand(server, session, { drop: this.name }, (err, result) => {
super.executeCommandCallback(server, session, { drop: this.name }, (err, result) => {
if (err) return reject(err);
resolve(!!result.ok);
});
Expand All @@ -95,7 +95,7 @@ export class DropCollectionOperation extends CommandOperation<boolean> {
export type DropDatabaseOptions = CommandOperationOptions;

/** @internal */
export class DropDatabaseOperation extends CommandOperation<boolean> {
export class DropDatabaseOperation extends CommandCallbackOperation<boolean> {
override options: DropDatabaseOptions;

constructor(db: Db, options: DropDatabaseOptions) {
Expand All @@ -107,7 +107,7 @@ export class DropDatabaseOperation extends CommandOperation<boolean> {
session: ClientSession | undefined,
callback: Callback<boolean>
): void {
super.executeCommand(server, session, { dropDatabase: 1 }, (err, result) => {
super.executeCommandCallback(server, session, { dropDatabase: 1 }, (err, result) => {
if (err) return callback(err);
if (result.ok) return callback(undefined, true);
callback(undefined, false);
Expand Down
6 changes: 3 additions & 3 deletions src/operations/estimated_document_count.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { Collection } from '../collection';
import type { Server } from '../sdam/server';
import type { ClientSession } from '../sessions';
import type { Callback } from '../utils';
import { CommandOperation, type CommandOperationOptions } from './command';
import { CommandCallbackOperation, type CommandOperationOptions } from './command';
import { Aspect, defineAspects } from './operation';

/** @public */
Expand All @@ -17,7 +17,7 @@ export interface EstimatedDocumentCountOptions extends CommandOperationOptions {
}

/** @internal */
export class EstimatedDocumentCountOperation extends CommandOperation<number> {
export class EstimatedDocumentCountOperation extends CommandCallbackOperation<number> {
override options: EstimatedDocumentCountOptions;
collectionName: string;

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

super.executeCommand(server, session, cmd, (err, response) => {
super.executeCommandCallback(server, session, cmd, (err, response) => {
if (err) {
callback(err);
return;
Expand Down
6 changes: 3 additions & 3 deletions src/operations/eval.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ import { ReadPreference } from '../read_preference';
import type { Server } from '../sdam/server';
import type { ClientSession } from '../sessions';
import type { Callback } from '../utils';
import { CommandOperation, type CommandOperationOptions } from './command';
import { CommandCallbackOperation, type CommandOperationOptions } from './command';

/** @public */
export interface EvalOptions extends CommandOperationOptions {
nolock?: boolean;
}

/** @internal */
export class EvalOperation extends CommandOperation<Document> {
export class EvalOperation extends CommandCallbackOperation<Document> {
override options: EvalOptions;
code: Code;
parameters?: Document | Document[];
Expand Down Expand Up @@ -65,7 +65,7 @@ export class EvalOperation extends CommandOperation<Document> {
}

// Execute the command
super.executeCommand(server, session, cmd, (err, result) => {
super.executeCommandCallback(server, session, cmd, (err, result) => {
if (err) return callback(err);
if (result && result.ok === 1) {
return callback(undefined, result.retval);
Expand Down
8 changes: 6 additions & 2 deletions src/operations/find.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ import {
type MongoDBNamespace,
normalizeHintField
} from '../utils';
import { type CollationOptions, CommandOperation, type CommandOperationOptions } from './command';
import {
type CollationOptions,
CommandCallbackOperation,
type CommandOperationOptions
} from './command';
import { Aspect, defineAspects, type Hint } from './operation';

/**
Expand Down Expand Up @@ -71,7 +75,7 @@ export interface FindOptions<TSchema extends Document = Document>
}

/** @internal */
export class FindOperation extends CommandOperation<Document> {
export class FindOperation extends CommandCallbackOperation<Document> {
/**
* @remarks WriteConcern can still be present on the options because
* we inherit options from the client/db/collection. The
Expand Down
6 changes: 3 additions & 3 deletions src/operations/find_and_modify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type { ClientSession } from '../sessions';
import { formatSort, type Sort, type SortForCmd } from '../sort';
import { type Callback, decorateWithCollation, hasAtomicOperators, maxWireVersion } from '../utils';
import type { WriteConcern, WriteConcernSettings } from '../write_concern';
import { CommandOperation, type CommandOperationOptions } from './command';
import { CommandCallbackOperation, type CommandOperationOptions } from './command';
import { Aspect, defineAspects } from './operation';

/** @public */
Expand Down Expand Up @@ -122,7 +122,7 @@ function configureFindAndModifyCmdBaseUpdateOpts(
}

/** @internal */
class FindAndModifyOperation extends CommandOperation<Document> {
class FindAndModifyOperation extends CommandCallbackOperation<Document> {
override options: FindOneAndReplaceOptions | FindOneAndUpdateOptions | FindOneAndDeleteOptions;
cmdBase: FindAndModifyCmdBase;
collection: Collection;
Expand Down Expand Up @@ -220,7 +220,7 @@ class FindAndModifyOperation extends CommandOperation<Document> {
}

// Execute the command
super.executeCommand(server, session, cmd, (err, result) => {
super.executeCommandCallback(server, session, cmd, (err, result) => {
if (err) return callback(err);
return callback(undefined, options.includeResultMetadata ? result : result.value ?? null);
});
Expand Down
Loading

0 comments on commit 77a2709

Please sign in to comment.