Skip to content

Commit 130691d

Browse files
refactor(NODE-5764): add commandName to AbstractOperation subclasses (#3934)
1 parent ad2d15c commit 130691d

34 files changed

+539
-2
lines changed

src/bulk/common.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -880,13 +880,17 @@ const executeCommandsAsync = promisify(executeCommands);
880880
* We would like this logic to simply live inside the BulkWriteOperation class
881881
* @internal
882882
*/
883-
class BulkWriteShimOperation extends AbstractOperation {
883+
export class BulkWriteShimOperation extends AbstractOperation {
884884
bulkOperation: BulkOperationBase;
885885
constructor(bulkOperation: BulkOperationBase, options: BulkWriteOptions) {
886886
super(options);
887887
this.bulkOperation = bulkOperation;
888888
}
889889

890+
get commandName(): string {
891+
return 'bulkWrite' as const;
892+
}
893+
890894
execute(_server: Server, session: ClientSession | undefined): Promise<any> {
891895
if (this.options.session == null) {
892896
// An implicit session could have been created by 'executeOperation'

src/operations/aggregate.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ export class AggregateOperation<T = Document> extends CommandOperation<T> {
8282
}
8383
}
8484

85+
override get commandName() {
86+
return 'aggregate' as const;
87+
}
88+
8589
override get canRetryRead(): boolean {
8690
return !this.hasWriteStage;
8791
}

src/operations/bulk_write.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ export class BulkWriteOperation extends AbstractOperation<BulkWriteResult> {
2626
this.operations = operations;
2727
}
2828

29+
override get commandName() {
30+
return 'bulkWrite' as const;
31+
}
32+
2933
override async execute(
3034
server: Server,
3135
session: ClientSession | undefined

src/operations/collections.ts

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

22+
override get commandName() {
23+
return 'listCollections' as const;
24+
}
25+
2226
override async execute(
2327
server: Server,
2428
session: ClientSession | undefined

src/operations/count.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ export class CountOperation extends CommandOperation<number> {
3232
this.query = filter;
3333
}
3434

35+
override get commandName() {
36+
return 'count' as const;
37+
}
38+
3539
override async execute(server: Server, session: ClientSession | undefined): Promise<number> {
3640
const options = this.options;
3741
const cmd: Document = {

src/operations/create_collection.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,10 @@ export class CreateCollectionOperation extends CommandOperation<Collection> {
120120
this.name = name;
121121
}
122122

123+
override get commandName() {
124+
return 'create' as const;
125+
}
126+
123127
override async execute(server: Server, session: ClientSession | undefined): Promise<Collection> {
124128
const db = this.db;
125129
const name = this.name;

src/operations/delete.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ export class DeleteOperation extends CommandOperation<DeleteResult> {
5353
this.statements = statements;
5454
}
5555

56+
override get commandName() {
57+
return 'delete' as const;
58+
}
59+
5660
override get canRetryWrite(): boolean {
5761
if (super.canRetryWrite === false) {
5862
return false;

src/operations/distinct.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ export class DistinctOperation extends CommandOperation<any[]> {
3838
this.query = query;
3939
}
4040

41+
override get commandName() {
42+
return 'distinct' as const;
43+
}
44+
4145
override async execute(server: Server, session: ClientSession | undefined): Promise<any[]> {
4246
const coll = this.collection;
4347
const key = this.key;

src/operations/drop.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ export class DropCollectionOperation extends CommandOperation<boolean> {
2525
this.name = name;
2626
}
2727

28+
override get commandName() {
29+
return 'drop' as const;
30+
}
31+
2832
override async execute(server: Server, session: ClientSession | undefined): Promise<boolean> {
2933
const db = this.db;
3034
const options = this.options;
@@ -88,6 +92,10 @@ export class DropDatabaseOperation extends CommandOperation<boolean> {
8892
super(db, options);
8993
this.options = options;
9094
}
95+
override get commandName() {
96+
return 'dropDatabase' as const;
97+
}
98+
9199
override async execute(server: Server, session: ClientSession | undefined): Promise<boolean> {
92100
await super.executeCommand(server, session, { dropDatabase: 1 });
93101
return true;

src/operations/estimated_document_count.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ export class EstimatedDocumentCountOperation extends CommandOperation<number> {
2626
this.collectionName = collection.collectionName;
2727
}
2828

29+
override get commandName() {
30+
return 'count' as const;
31+
}
32+
2933
override async execute(server: Server, session: ClientSession | undefined): Promise<number> {
3034
const cmd: Document = { count: this.collectionName };
3135

src/operations/find.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ export class FindOperation extends CommandOperation<Document> {
9797
this.filter = filter != null && filter._bsontype === 'ObjectId' ? { _id: filter } : filter;
9898
}
9999

100+
override get commandName() {
101+
return 'find' as const;
102+
}
103+
100104
override async execute(server: Server, session: ClientSession | undefined): Promise<Document> {
101105
this.server = server;
102106

src/operations/find_and_modify.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ function configureFindAndModifyCmdBaseUpdateOpts(
121121
}
122122

123123
/** @internal */
124-
class FindAndModifyOperation extends CommandOperation<Document> {
124+
export class FindAndModifyOperation extends CommandOperation<Document> {
125125
override options: FindOneAndReplaceOptions | FindOneAndUpdateOptions | FindOneAndDeleteOptions;
126126
cmdBase: FindAndModifyCmdBase;
127127
collection: Collection;
@@ -178,6 +178,10 @@ class FindAndModifyOperation extends CommandOperation<Document> {
178178
this.query = query;
179179
}
180180

181+
override get commandName() {
182+
return 'findAndModify' as const;
183+
}
184+
181185
override async execute(server: Server, session: ClientSession | undefined): Promise<Document> {
182186
const coll = this.collection;
183187
const query = this.query;

src/operations/get_more.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ export class GetMoreOperation extends AbstractOperation {
4848
this.server = server;
4949
}
5050

51+
override get commandName() {
52+
return 'getMore' as const;
53+
}
5154
/**
5255
* Although there is a server already associated with the get more operation, the signature
5356
* for execute passes a server so we will just use that one.

src/operations/indexes.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,10 @@ export class IndexesOperation extends AbstractOperation<Document[]> {
186186
this.collection = collection;
187187
}
188188

189+
override get commandName() {
190+
return 'listIndexes' as const;
191+
}
192+
189193
override async execute(_server: Server, session: ClientSession | undefined): Promise<Document[]> {
190194
const coll = this.collection;
191195
const options = this.options;
@@ -235,6 +239,10 @@ export class CreateIndexesOperation<
235239
});
236240
}
237241

242+
override get commandName() {
243+
return 'createIndexes';
244+
}
245+
238246
override async execute(server: Server, session: ClientSession | undefined): Promise<T> {
239247
const options = this.options;
240248
const indexes = this.indexes;
@@ -272,6 +280,7 @@ export class CreateIndexOperation extends CreateIndexesOperation<string> {
272280
) {
273281
super(parent, collectionName, [makeIndexSpec(indexSpec, options)], options);
274282
}
283+
275284
override async execute(server: Server, session: ClientSession | undefined): Promise<string> {
276285
const indexNames = await super.execute(server, session);
277286
return indexNames[0];
@@ -295,6 +304,10 @@ export class EnsureIndexOperation extends CreateIndexOperation {
295304
this.collectionName = collectionName;
296305
}
297306

307+
override get commandName() {
308+
return 'listIndexes';
309+
}
310+
298311
override async execute(server: Server, session: ClientSession | undefined): Promise<string> {
299312
const indexName = this.indexes[0].name;
300313
const indexes = await this.db
@@ -328,6 +341,10 @@ export class DropIndexOperation extends CommandOperation<Document> {
328341
this.indexName = indexName;
329342
}
330343

344+
override get commandName() {
345+
return 'dropIndexes' as const;
346+
}
347+
331348
override async execute(server: Server, session: ClientSession | undefined): Promise<Document> {
332349
const cmd = { dropIndexes: this.collection.collectionName, index: this.indexName };
333350
return super.executeCommand(server, session, cmd);
@@ -360,6 +377,10 @@ export class ListIndexesOperation extends CommandOperation<Document> {
360377
this.collectionNamespace = collection.s.namespace;
361378
}
362379

380+
override get commandName() {
381+
return 'listIndexes' as const;
382+
}
383+
363384
override async execute(server: Server, session: ClientSession | undefined): Promise<Document> {
364385
const serverWireVersion = maxWireVersion(server);
365386

@@ -394,6 +415,10 @@ export class IndexExistsOperation extends AbstractOperation<boolean> {
394415
this.indexes = indexes;
395416
}
396417

418+
override get commandName() {
419+
return 'listIndexes' as const;
420+
}
421+
397422
override async execute(server: Server, session: ClientSession | undefined): Promise<boolean> {
398423
const coll = this.collection;
399424
const indexes = this.indexes;
@@ -423,6 +448,10 @@ export class IndexInformationOperation extends AbstractOperation<Document> {
423448
this.name = name;
424449
}
425450

451+
override get commandName() {
452+
return 'listIndexes' as const;
453+
}
454+
426455
override async execute(server: Server, session: ClientSession | undefined): Promise<Document> {
427456
const db = this.db;
428457
const name = this.name;

src/operations/insert.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ export class InsertOperation extends CommandOperation<Document> {
2424
this.documents = documents;
2525
}
2626

27+
override get commandName() {
28+
return 'insert' as const;
29+
}
30+
2731
override async execute(server: Server, session: ClientSession | undefined): Promise<Document> {
2832
const options = this.options ?? {};
2933
const ordered = typeof options.ordered === 'boolean' ? options.ordered : true;
@@ -114,6 +118,10 @@ export class InsertManyOperation extends AbstractOperation<InsertManyResult> {
114118
this.docs = docs;
115119
}
116120

121+
override get commandName() {
122+
return 'insert' as const;
123+
}
124+
117125
override async execute(
118126
server: Server,
119127
session: ClientSession | undefined

src/operations/is_capped.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ export class IsCappedOperation extends AbstractOperation<boolean> {
1515
this.collection = collection;
1616
}
1717

18+
override get commandName() {
19+
return 'listCollections' as const;
20+
}
21+
1822
override async execute(server: Server, session: ClientSession | undefined): Promise<boolean> {
1923
const coll = this.collection;
2024
const [collection] = await coll.s.db

src/operations/kill_cursors.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ export class KillCursorsOperation extends AbstractOperation {
2525
this.server = server;
2626
}
2727

28+
override get commandName() {
29+
return 'killCursors' as const;
30+
}
31+
2832
override async execute(server: Server, session: ClientSession | undefined): Promise<void> {
2933
if (server !== this.server) {
3034
throw new MongoRuntimeError('Killcursor must run on the same server operation began on');

src/operations/list_collections.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ export class ListCollectionsOperation extends CommandOperation<Document> {
4747
}
4848
}
4949

50+
override get commandName() {
51+
return 'listCollections' as const;
52+
}
53+
5054
override async execute(server: Server, session: ClientSession | undefined): Promise<Document> {
5155
return super.executeCommand(server, session, this.generateCommand(maxWireVersion(server)));
5256
}

src/operations/list_databases.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ export class ListDatabasesOperation extends CommandOperation<ListDatabasesResult
3535
this.ns = new MongoDBNamespace('admin', '$cmd');
3636
}
3737

38+
override get commandName() {
39+
return 'listDatabases' as const;
40+
}
41+
3842
override async execute(
3943
server: Server,
4044
session: ClientSession | undefined

src/operations/operation.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ export abstract class AbstractOperation<TResult = any> {
7575
this.trySecondaryWrite = false;
7676
}
7777

78+
/** Must match the first key of the command object sent to the server.
79+
Command name should be stateless (should not use 'this' keyword) */
80+
abstract get commandName(): string;
81+
7882
abstract execute(server: Server, session: ClientSession | undefined): Promise<TResult>;
7983

8084
hasAspect(aspect: symbol): boolean {

src/operations/options_operation.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ export class OptionsOperation extends AbstractOperation<Document> {
1515
this.options = options;
1616
this.collection = collection;
1717
}
18+
override get commandName() {
19+
return 'listCollections' as const;
20+
}
1821

1922
override async execute(server: Server, session: ClientSession | undefined): Promise<Document> {
2023
const coll = this.collection;

src/operations/profiling_level.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ export class ProfilingLevelOperation extends CommandOperation<string> {
1616
this.options = options;
1717
}
1818

19+
override get commandName() {
20+
return 'profile' as const;
21+
}
22+
1923
override async execute(server: Server, session: ClientSession | undefined): Promise<string> {
2024
const doc = await super.executeCommand(server, session, { profile: -1 });
2125
if (doc.ok === 1) {

src/operations/remove_user.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ export class RemoveUserOperation extends CommandOperation<boolean> {
1818
this.username = username;
1919
}
2020

21+
override get commandName() {
22+
return 'dropUser' as const;
23+
}
24+
2125
override async execute(server: Server, session: ClientSession | undefined): Promise<boolean> {
2226
await super.executeCommand(server, session, { dropUser: this.username });
2327
return true;

src/operations/rename.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ export class RenameOperation extends CommandOperation<Document> {
2525
this.ns = new MongoDBNamespace('admin', '$cmd');
2626
}
2727

28+
override get commandName(): string {
29+
return 'renameCollection' as const;
30+
}
31+
2832
override async execute(server: Server, session: ClientSession | undefined): Promise<Collection> {
2933
// Build the command
3034
const renameCollection = this.collection.namespace;

src/operations/run_command.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ export class RunCommandOperation<T = Document> extends AbstractOperation<T> {
2222
this.ns = parent.s.namespace.withCollection('$cmd');
2323
}
2424

25+
override get commandName() {
26+
return 'runCommand' as const;
27+
}
28+
2529
override async execute(server: Server, session: ClientSession | undefined): Promise<T> {
2630
this.server = server;
2731
return server.commandAsync(this.ns, this.command, {
@@ -44,6 +48,10 @@ export class RunAdminCommandOperation<T = Document> extends AbstractOperation<T>
4448
this.ns = new MongoDBNamespace('admin', '$cmd');
4549
}
4650

51+
override get commandName() {
52+
return 'runCommand' as const;
53+
}
54+
4755
override async execute(server: Server, session: ClientSession | undefined): Promise<T> {
4856
this.server = server;
4957
return server.commandAsync(this.ns, this.command, {

0 commit comments

Comments
 (0)