Skip to content

refactor(NODE-5473): Remove Unused Callback Inheritance in Operations Layer #3793

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 3 commits into from
Aug 3, 2023
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
8 changes: 1 addition & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,6 @@ 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 Expand Up @@ -421,12 +420,7 @@ export type {
export type { InsertManyResult, InsertOneOptions, InsertOneResult } from './operations/insert';
export type { CollectionInfo, ListCollectionsOptions } from './operations/list_collections';
export type { ListDatabasesOptions, ListDatabasesResult } from './operations/list_databases';
export type {
AbstractCallbackOperation,
AbstractOperation,
Hint,
OperationOptions
} from './operations/operation';
export type { AbstractOperation, Hint, OperationOptions } from './operations/operation';
export type { ProfilingLevelOptions } from './operations/profiling_level';
export type { RemoveUserOptions } from './operations/remove_user';
export type { RenameOptions } from './operations/rename';
Expand Down
10 changes: 1 addition & 9 deletions src/operations/aggregate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { MongoInvalidArgumentError } from '../error';
import { type TODO_NODE_3286 } from '../mongo_types';
import type { Server } from '../sdam/server';
import type { ClientSession } from '../sessions';
import { type Callback, maxWireVersion, type MongoDBNamespace } from '../utils';
import { maxWireVersion, type MongoDBNamespace } from '../utils';
import { WriteConcern } from '../write_concern';
import { type CollationOptions, CommandOperation, type CommandOperationOptions } from './command';
import { Aspect, defineAspects, type Hint } from './operation';
Expand Down Expand Up @@ -132,14 +132,6 @@ export class AggregateOperation<T = Document> extends CommandOperation<T> {

return super.executeCommand(server, session, command) as TODO_NODE_3286;
}

protected override executeCallback(
_server: Server,
_session: ClientSession | undefined,
_callback: Callback<T>
): void {
throw new Error('Method not implemented.');
}
}

defineAspects(AggregateOperation, [
Expand Down
5 changes: 4 additions & 1 deletion src/operations/collections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ export class CollectionsOperation extends AbstractOperation<Collection[]> {
this.db = db;
}

async execute(server: Server, session: ClientSession | undefined): Promise<Collection[]> {
override async execute(
server: Server,
session: ClientSession | undefined
): Promise<Collection[]> {
// Let's get the collection names
const documents = await this.db
.listCollections(
Expand Down
24 changes: 2 additions & 22 deletions src/operations/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@ import type { Server } from '../sdam/server';
import { MIN_SECONDARY_WRITE_WIRE_VERSION } from '../sdam/server_selection';
import type { ClientSession } from '../sessions';
import {
type Callback,
commandSupportsReadConcern,
decorateWithExplain,
maxWireVersion,
MongoDBNamespace
} from '../utils';
import { WriteConcern, type WriteConcernOptions } from '../write_concern';
import type { ReadConcernLike } from './../read_concern';
import { AbstractCallbackOperation, Aspect, type OperationOptions } from './operation';
import { AbstractOperation, Aspect, type OperationOptions } from './operation';

/** @public */
export interface CollationOptions {
Expand Down Expand Up @@ -68,7 +67,7 @@ export interface OperationParent {
}

/** @internal */
export abstract class CommandOperation<T> extends AbstractCallbackOperation<T> {
export abstract class CommandOperation<T> extends AbstractOperation<T> {
override options: CommandOperationOptions;
readConcern?: ReadConcern;
writeConcern?: WriteConcern;
Expand Down Expand Up @@ -156,22 +155,3 @@ export abstract class CommandOperation<T> extends AbstractCallbackOperation<T> {
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)
);
}
}
10 changes: 1 addition & 9 deletions src/operations/count.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { Document } from '../bson';
import type { Collection } from '../collection';
import type { Server } from '../sdam/server';
import type { ClientSession } from '../sessions';
import type { Callback, MongoDBNamespace } from '../utils';
import type { MongoDBNamespace } from '../utils';
import { CommandOperation, type CommandOperationOptions } from './command';
import { Aspect, defineAspects } from './operation';

Expand Down Expand Up @@ -58,14 +58,6 @@ export class CountOperation extends CommandOperation<number> {
const result = await super.executeCommand(server, session, cmd);
return result ? result.n : 0;
}

protected override executeCallback(
_server: Server,
_session: ClientSession | undefined,
_callback: Callback<number>
): void {
throw new Error('Method not implemented.');
}
}

defineAspects(CountOperation, [Aspect.READ_OPERATION, Aspect.RETRYABLE]);
9 changes: 0 additions & 9 deletions src/operations/create_collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { MongoCompatibilityError } from '../error';
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 { CreateIndexOperation } from './indexes';
import { Aspect, defineAspects } from './operation';
Expand Down Expand Up @@ -171,14 +170,6 @@ export class CreateCollectionOperation extends CommandOperation<Collection> {
return coll;
}

protected executeCallback(
_server: Server,
_session: ClientSession | undefined,
_callback: Callback<Collection>
): void {
throw new Error('Method not implemented.');
}

private async executeWithoutEncryptedFieldsCheck(
server: Server,
session: ClientSession | undefined
Expand Down
10 changes: 1 addition & 9 deletions src/operations/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { MongoCompatibilityError, MongoServerError } from '../error';
import { type TODO_NODE_3286 } from '../mongo_types';
import type { Server } from '../sdam/server';
import type { ClientSession } from '../sessions';
import type { Callback, MongoDBNamespace } from '../utils';
import type { MongoDBNamespace } from '../utils';
import type { WriteConcernOptions } from '../write_concern';
import { type CollationOptions, CommandOperation, type CommandOperationOptions } from './command';
import { Aspect, defineAspects, type Hint } from './operation';
Expand Down Expand Up @@ -93,14 +93,6 @@ export class DeleteOperation extends CommandOperation<DeleteResult> {

return super.executeCommand(server, session, command) as TODO_NODE_3286;
}

protected override executeCallback(
_server: Server,
_session: ClientSession | undefined,
_callback: Callback<DeleteResult>
): void {
throw new Error('Method not implemented.');
}
}

export class DeleteOneOperation extends DeleteOperation {
Expand Down
10 changes: 1 addition & 9 deletions src/operations/distinct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { Document } from '../bson';
import type { Collection } from '../collection';
import type { Server } from '../sdam/server';
import type { ClientSession } from '../sessions';
import { type Callback, decorateWithCollation, decorateWithReadConcern } from '../utils';
import { decorateWithCollation, decorateWithReadConcern } from '../utils';
import { CommandOperation, type CommandOperationOptions } from './command';
import { Aspect, defineAspects } from './operation';

Expand Down Expand Up @@ -72,14 +72,6 @@ export class DistinctOperation extends CommandOperation<any[]> {

return this.explain ? result : result.values;
}

protected override executeCallback(
_server: Server,
_session: ClientSession | undefined,
_callback: Callback<any[]>
): void {
throw new Error('Method not implemented.');
}
}

defineAspects(DistinctOperation, [Aspect.READ_OPERATION, Aspect.RETRYABLE, Aspect.EXPLAINABLE]);
17 changes: 0 additions & 17 deletions src/operations/drop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import type { Db } from '../db';
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 { Aspect, defineAspects } from './operation';

Expand Down Expand Up @@ -69,14 +68,6 @@ export class DropCollectionOperation extends CommandOperation<boolean> {
return this.executeWithoutEncryptedFieldsCheck(server, session);
}

protected executeCallback(
_server: Server,
_session: ClientSession | undefined,
_callback: Callback<boolean>
): void {
throw new Error('Method not implemented.');
}

private async executeWithoutEncryptedFieldsCheck(
server: Server,
session: ClientSession | undefined
Expand All @@ -101,14 +92,6 @@ export class DropDatabaseOperation extends CommandOperation<boolean> {
await super.executeCommand(server, session, { dropDatabase: 1 });
return true;
}

protected executeCallback(
_server: Server,
_session: ClientSession | undefined,
_callback: Callback<boolean>
): void {
throw new Error('Method not implemented.');
}
}

defineAspects(DropCollectionOperation, [Aspect.WRITE_OPERATION]);
Expand Down
9 changes: 0 additions & 9 deletions src/operations/estimated_document_count.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import type { Document } from '../bson';
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 { Aspect, defineAspects } from './operation';

Expand Down Expand Up @@ -44,14 +43,6 @@ export class EstimatedDocumentCountOperation extends CommandOperation<number> {

return response?.n || 0;
}

protected override executeCallback(
_server: Server,
_session: ClientSession | undefined,
_callback: Callback<number>
): void {
throw new Error('Method not implemented.');
}
}

defineAspects(EstimatedDocumentCountOperation, [
Expand Down
30 changes: 11 additions & 19 deletions src/operations/eval.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@ import { MongoServerError } from '../error';
import { ReadPreference } from '../read_preference';
import type { Server } from '../sdam/server';
import type { ClientSession } from '../sessions';
import type { Callback } from '../utils';
import { CommandCallbackOperation, type CommandOperationOptions } from './command';
import { CommandOperation, type CommandOperationOptions } from './command';

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

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

override executeCallback(
server: Server,
session: ClientSession | undefined,
callback: Callback<Document>
): void {
override async execute(server: Server, session: ClientSession | undefined): Promise<Document> {
let finalCode = this.code;
let finalParameters: Document[] = [];

Expand All @@ -65,18 +60,15 @@ export class EvalOperation extends CommandCallbackOperation<Document> {
}

// Execute the command
super.executeCommandCallback(server, session, cmd, (err, result) => {
if (err) return callback(err);
if (result && result.ok === 1) {
return callback(undefined, result.retval);
}
const result = await super.executeCommand(server, session, cmd);
if (result && result.ok === 1) {
return result.retval;
}

if (result) {
callback(new MongoServerError({ message: `eval failed: ${result.errmsg}` }));
return;
}
if (result) {
throw new MongoServerError({ message: `eval failed: ${result.errmsg}` });
}

callback(err, result);
});
return result;
}
}
15 changes: 1 addition & 14 deletions src/operations/find.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,7 @@ import { ReadConcern } from '../read_concern';
import type { Server } from '../sdam/server';
import type { ClientSession } from '../sessions';
import { formatSort, type Sort } from '../sort';
import {
type Callback,
decorateWithExplain,
type MongoDBNamespace,
normalizeHintField
} from '../utils';
import { decorateWithExplain, type MongoDBNamespace, normalizeHintField } from '../utils';
import { type CollationOptions, CommandOperation, type CommandOperationOptions } from './command';
import { Aspect, defineAspects, type Hint } from './operation';

Expand Down Expand Up @@ -119,14 +114,6 @@ export class FindOperation extends CommandOperation<Document> {
session
});
}

protected executeCallback(
_server: Server,
_session: ClientSession | undefined,
_callback: Callback<Document>
): void {
throw new Error('Method not implemented.');
}
}

function makeFindCommand(ns: MongoDBNamespace, filter: Document, options: FindOptions): Document {
Expand Down
10 changes: 1 addition & 9 deletions src/operations/find_and_modify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { ReadPreference } from '../read_preference';
import type { Server } from '../sdam/server';
import type { ClientSession } from '../sessions';
import { formatSort, type Sort, type SortForCmd } from '../sort';
import { type Callback, decorateWithCollation, hasAtomicOperators, maxWireVersion } from '../utils';
import { decorateWithCollation, hasAtomicOperators, maxWireVersion } from '../utils';
import type { WriteConcern, WriteConcernSettings } from '../write_concern';
import { CommandOperation, type CommandOperationOptions } from './command';
import { Aspect, defineAspects } from './operation';
Expand Down Expand Up @@ -215,14 +215,6 @@ class FindAndModifyOperation extends CommandOperation<Document> {
const result = await super.executeCommand(server, session, cmd);
return options.includeResultMetadata ? result : result.value ?? null;
}

protected override executeCallback(
_server: Server,
_session: ClientSession | undefined,
_callback: Callback<Document>
): void {
throw new Error('Method not implemented.');
}
}

/** @internal */
Expand Down
2 changes: 1 addition & 1 deletion src/operations/get_more.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export class GetMoreOperation extends AbstractOperation {
* Although there is a server already associated with the get more operation, the signature
* for execute passes a server so we will just use that one.
*/
async execute(server: Server, _session: ClientSession | undefined): Promise<Document> {
override async execute(server: Server, _session: ClientSession | undefined): Promise<Document> {
if (server !== this.server) {
throw new MongoRuntimeError('Getmore must run on the same server operation began on');
}
Expand Down
Loading