Skip to content

Commit dc0aef4

Browse files
authored
refactor(NODE-5473): remove unused callback inheritance in operations layer (#3793)
1 parent 0c5c0b4 commit dc0aef4

28 files changed

+35
-336
lines changed

src/index.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,6 @@ export type {
389389
CommandOperationOptions,
390390
OperationParent
391391
} from './operations/command';
392-
export type { CommandCallbackOperation } from './operations/command';
393392
export type { IndexInformationOptions } from './operations/common_functions';
394393
export type { CountOptions } from './operations/count';
395394
export type { CountDocumentsOptions } from './operations/count_documents';
@@ -421,12 +420,7 @@ export type {
421420
export type { InsertManyResult, InsertOneOptions, InsertOneResult } from './operations/insert';
422421
export type { CollectionInfo, ListCollectionsOptions } from './operations/list_collections';
423422
export type { ListDatabasesOptions, ListDatabasesResult } from './operations/list_databases';
424-
export type {
425-
AbstractCallbackOperation,
426-
AbstractOperation,
427-
Hint,
428-
OperationOptions
429-
} from './operations/operation';
423+
export type { AbstractOperation, Hint, OperationOptions } from './operations/operation';
430424
export type { ProfilingLevelOptions } from './operations/profiling_level';
431425
export type { RemoveUserOptions } from './operations/remove_user';
432426
export type { RenameOptions } from './operations/rename';

src/operations/aggregate.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { MongoInvalidArgumentError } from '../error';
33
import { type TODO_NODE_3286 } from '../mongo_types';
44
import type { Server } from '../sdam/server';
55
import type { ClientSession } from '../sessions';
6-
import { type Callback, maxWireVersion, type MongoDBNamespace } from '../utils';
6+
import { maxWireVersion, type MongoDBNamespace } from '../utils';
77
import { WriteConcern } from '../write_concern';
88
import { type CollationOptions, CommandOperation, type CommandOperationOptions } from './command';
99
import { Aspect, defineAspects, type Hint } from './operation';
@@ -132,14 +132,6 @@ export class AggregateOperation<T = Document> extends CommandOperation<T> {
132132

133133
return super.executeCommand(server, session, command) as TODO_NODE_3286;
134134
}
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.');
142-
}
143135
}
144136

145137
defineAspects(AggregateOperation, [

src/operations/collections.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ export class CollectionsOperation extends AbstractOperation<Collection[]> {
1919
this.db = db;
2020
}
2121

22-
async execute(server: Server, session: ClientSession | undefined): Promise<Collection[]> {
22+
override async execute(
23+
server: Server,
24+
session: ClientSession | undefined
25+
): Promise<Collection[]> {
2326
// Let's get the collection names
2427
const documents = await this.db
2528
.listCollections(

src/operations/command.ts

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,14 @@ import type { Server } from '../sdam/server';
77
import { MIN_SECONDARY_WRITE_WIRE_VERSION } from '../sdam/server_selection';
88
import type { ClientSession } from '../sessions';
99
import {
10-
type Callback,
1110
commandSupportsReadConcern,
1211
decorateWithExplain,
1312
maxWireVersion,
1413
MongoDBNamespace
1514
} from '../utils';
1615
import { WriteConcern, type WriteConcernOptions } from '../write_concern';
1716
import type { ReadConcernLike } from './../read_concern';
18-
import { AbstractCallbackOperation, Aspect, type OperationOptions } from './operation';
17+
import { AbstractOperation, Aspect, type OperationOptions } from './operation';
1918

2019
/** @public */
2120
export interface CollationOptions {
@@ -68,7 +67,7 @@ export interface OperationParent {
6867
}
6968

7069
/** @internal */
71-
export abstract class CommandOperation<T> extends AbstractCallbackOperation<T> {
70+
export abstract class CommandOperation<T> extends AbstractOperation<T> {
7271
override options: CommandOperationOptions;
7372
readConcern?: ReadConcern;
7473
writeConcern?: WriteConcern;
@@ -156,22 +155,3 @@ export abstract class CommandOperation<T> extends AbstractCallbackOperation<T> {
156155
return server.commandAsync(this.ns, cmd, options);
157156
}
158157
}
159-
160-
/** @internal */
161-
export abstract class CommandCallbackOperation<T = any> extends CommandOperation<T> {
162-
constructor(parent?: OperationParent, options?: CommandOperationOptions) {
163-
super(parent, options);
164-
}
165-
166-
executeCommandCallback(
167-
server: Server,
168-
session: ClientSession | undefined,
169-
cmd: Document,
170-
callback: Callback
171-
): void {
172-
super.executeCommand(server, session, cmd).then(
173-
res => callback(undefined, res),
174-
err => callback(err, undefined)
175-
);
176-
}
177-
}

src/operations/count.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ 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, MongoDBNamespace } from '../utils';
5+
import type { MongoDBNamespace } from '../utils';
66
import { CommandOperation, type CommandOperationOptions } from './command';
77
import { Aspect, defineAspects } from './operation';
88

@@ -58,14 +58,6 @@ export class CountOperation extends CommandOperation<number> {
5858
const result = await super.executeCommand(server, session, cmd);
5959
return result ? result.n : 0;
6060
}
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.');
68-
}
6961
}
7062

7163
defineAspects(CountOperation, [Aspect.READ_OPERATION, Aspect.RETRYABLE]);

src/operations/create_collection.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import { MongoCompatibilityError } from '../error';
99
import type { PkFactory } from '../mongo_client';
1010
import type { Server } from '../sdam/server';
1111
import type { ClientSession } from '../sessions';
12-
import { type Callback } from '../utils';
1312
import { CommandOperation, type CommandOperationOptions } from './command';
1413
import { CreateIndexOperation } from './indexes';
1514
import { Aspect, defineAspects } from './operation';
@@ -171,14 +170,6 @@ export class CreateCollectionOperation extends CommandOperation<Collection> {
171170
return coll;
172171
}
173172

174-
protected executeCallback(
175-
_server: Server,
176-
_session: ClientSession | undefined,
177-
_callback: Callback<Collection>
178-
): void {
179-
throw new Error('Method not implemented.');
180-
}
181-
182173
private async executeWithoutEncryptedFieldsCheck(
183174
server: Server,
184175
session: ClientSession | undefined

src/operations/delete.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { MongoCompatibilityError, MongoServerError } from '../error';
44
import { type TODO_NODE_3286 } from '../mongo_types';
55
import type { Server } from '../sdam/server';
66
import type { ClientSession } from '../sessions';
7-
import type { Callback, MongoDBNamespace } from '../utils';
7+
import type { MongoDBNamespace } from '../utils';
88
import type { WriteConcernOptions } from '../write_concern';
99
import { type CollationOptions, CommandOperation, type CommandOperationOptions } from './command';
1010
import { Aspect, defineAspects, type Hint } from './operation';
@@ -93,14 +93,6 @@ export class DeleteOperation extends CommandOperation<DeleteResult> {
9393

9494
return super.executeCommand(server, session, command) as TODO_NODE_3286;
9595
}
96-
97-
protected override executeCallback(
98-
_server: Server,
99-
_session: ClientSession | undefined,
100-
_callback: Callback<DeleteResult>
101-
): void {
102-
throw new Error('Method not implemented.');
103-
}
10496
}
10597

10698
export class DeleteOneOperation extends DeleteOperation {

src/operations/distinct.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ 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, decorateWithCollation, decorateWithReadConcern } from '../utils';
5+
import { decorateWithCollation, decorateWithReadConcern } from '../utils';
66
import { CommandOperation, type CommandOperationOptions } from './command';
77
import { Aspect, defineAspects } from './operation';
88

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

7373
return this.explain ? result : result.values;
7474
}
75-
76-
protected override executeCallback(
77-
_server: Server,
78-
_session: ClientSession | undefined,
79-
_callback: Callback<any[]>
80-
): void {
81-
throw new Error('Method not implemented.');
82-
}
8375
}
8476

8577
defineAspects(DistinctOperation, [Aspect.READ_OPERATION, Aspect.RETRYABLE, Aspect.EXPLAINABLE]);

src/operations/drop.ts

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import type { Db } from '../db';
33
import { MONGODB_ERROR_CODES, MongoServerError } from '../error';
44
import type { Server } from '../sdam/server';
55
import type { ClientSession } from '../sessions';
6-
import { type Callback } from '../utils';
76
import { CommandOperation, type CommandOperationOptions } from './command';
87
import { Aspect, defineAspects } from './operation';
98

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

72-
protected executeCallback(
73-
_server: Server,
74-
_session: ClientSession | undefined,
75-
_callback: Callback<boolean>
76-
): void {
77-
throw new Error('Method not implemented.');
78-
}
79-
8071
private async executeWithoutEncryptedFieldsCheck(
8172
server: Server,
8273
session: ClientSession | undefined
@@ -101,14 +92,6 @@ export class DropDatabaseOperation extends CommandOperation<boolean> {
10192
await super.executeCommand(server, session, { dropDatabase: 1 });
10293
return true;
10394
}
104-
105-
protected executeCallback(
106-
_server: Server,
107-
_session: ClientSession | undefined,
108-
_callback: Callback<boolean>
109-
): void {
110-
throw new Error('Method not implemented.');
111-
}
11295
}
11396

11497
defineAspects(DropCollectionOperation, [Aspect.WRITE_OPERATION]);

src/operations/estimated_document_count.ts

Lines changed: 0 additions & 9 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 { CommandOperation, type CommandOperationOptions } from './command';
76
import { Aspect, defineAspects } from './operation';
87

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

4544
return response?.n || 0;
4645
}
47-
48-
protected override executeCallback(
49-
_server: Server,
50-
_session: ClientSession | undefined,
51-
_callback: Callback<number>
52-
): void {
53-
throw new Error('Method not implemented.');
54-
}
5546
}
5647

5748
defineAspects(EstimatedDocumentCountOperation, [

src/operations/eval.ts

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,15 @@ import { MongoServerError } from '../error';
55
import { ReadPreference } from '../read_preference';
66
import type { Server } from '../sdam/server';
77
import type { ClientSession } from '../sessions';
8-
import type { Callback } from '../utils';
9-
import { CommandCallbackOperation, type CommandOperationOptions } from './command';
8+
import { CommandOperation, type CommandOperationOptions } from './command';
109

1110
/** @public */
1211
export interface EvalOptions extends CommandOperationOptions {
1312
nolock?: boolean;
1413
}
1514

1615
/** @internal */
17-
export class EvalOperation extends CommandCallbackOperation<Document> {
16+
export class EvalOperation extends CommandOperation<Document> {
1817
override options: EvalOptions;
1918
code: Code;
2019
parameters?: Document | Document[];
@@ -38,11 +37,7 @@ export class EvalOperation extends CommandCallbackOperation<Document> {
3837
});
3938
}
4039

41-
override executeCallback(
42-
server: Server,
43-
session: ClientSession | undefined,
44-
callback: Callback<Document>
45-
): void {
40+
override async execute(server: Server, session: ClientSession | undefined): Promise<Document> {
4641
let finalCode = this.code;
4742
let finalParameters: Document[] = [];
4843

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

6762
// Execute the command
68-
super.executeCommandCallback(server, session, cmd, (err, result) => {
69-
if (err) return callback(err);
70-
if (result && result.ok === 1) {
71-
return callback(undefined, result.retval);
72-
}
63+
const result = await super.executeCommand(server, session, cmd);
64+
if (result && result.ok === 1) {
65+
return result.retval;
66+
}
7367

74-
if (result) {
75-
callback(new MongoServerError({ message: `eval failed: ${result.errmsg}` }));
76-
return;
77-
}
68+
if (result) {
69+
throw new MongoServerError({ message: `eval failed: ${result.errmsg}` });
70+
}
7871

79-
callback(err, result);
80-
});
72+
return result;
8173
}
8274
}

src/operations/find.ts

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,7 @@ import { ReadConcern } from '../read_concern';
55
import type { Server } from '../sdam/server';
66
import type { ClientSession } from '../sessions';
77
import { formatSort, type Sort } from '../sort';
8-
import {
9-
type Callback,
10-
decorateWithExplain,
11-
type MongoDBNamespace,
12-
normalizeHintField
13-
} from '../utils';
8+
import { decorateWithExplain, type MongoDBNamespace, normalizeHintField } from '../utils';
149
import { type CollationOptions, CommandOperation, type CommandOperationOptions } from './command';
1510
import { Aspect, defineAspects, type Hint } from './operation';
1611

@@ -119,14 +114,6 @@ export class FindOperation extends CommandOperation<Document> {
119114
session
120115
});
121116
}
122-
123-
protected executeCallback(
124-
_server: Server,
125-
_session: ClientSession | undefined,
126-
_callback: Callback<Document>
127-
): void {
128-
throw new Error('Method not implemented.');
129-
}
130117
}
131118

132119
function makeFindCommand(ns: MongoDBNamespace, filter: Document, options: FindOptions): Document {

src/operations/find_and_modify.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { ReadPreference } from '../read_preference';
55
import type { Server } from '../sdam/server';
66
import type { ClientSession } from '../sessions';
77
import { formatSort, type Sort, type SortForCmd } from '../sort';
8-
import { type Callback, decorateWithCollation, hasAtomicOperators, maxWireVersion } from '../utils';
8+
import { decorateWithCollation, hasAtomicOperators, maxWireVersion } from '../utils';
99
import type { WriteConcern, WriteConcernSettings } from '../write_concern';
1010
import { CommandOperation, type CommandOperationOptions } from './command';
1111
import { Aspect, defineAspects } from './operation';
@@ -215,14 +215,6 @@ class FindAndModifyOperation extends CommandOperation<Document> {
215215
const result = await super.executeCommand(server, session, cmd);
216216
return options.includeResultMetadata ? result : result.value ?? null;
217217
}
218-
219-
protected override executeCallback(
220-
_server: Server,
221-
_session: ClientSession | undefined,
222-
_callback: Callback<Document>
223-
): void {
224-
throw new Error('Method not implemented.');
225-
}
226218
}
227219

228220
/** @internal */

src/operations/get_more.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export class GetMoreOperation extends AbstractOperation {
5252
* Although there is a server already associated with the get more operation, the signature
5353
* for execute passes a server so we will just use that one.
5454
*/
55-
async execute(server: Server, _session: ClientSession | undefined): Promise<Document> {
55+
override async execute(server: Server, _session: ClientSession | undefined): Promise<Document> {
5656
if (server !== this.server) {
5757
throw new MongoRuntimeError('Getmore must run on the same server operation began on');
5858
}

0 commit comments

Comments
 (0)