Skip to content

Commit 25dd18e

Browse files
refactor(NODE-7084): refactor CUD ops to use ModernizedOperation (#4610)
1 parent 4c2ff26 commit 25dd18e

File tree

11 files changed

+256
-305
lines changed

11 files changed

+256
-305
lines changed

src/collection.ts

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -402,12 +402,7 @@ export class Collection<TSchema extends Document = Document> {
402402
): Promise<UpdateResult<TSchema>> {
403403
return await executeOperation(
404404
this.client,
405-
new UpdateOneOperation(
406-
this as TODO_NODE_3286,
407-
filter,
408-
update,
409-
resolveOptions(this, options)
410-
) as TODO_NODE_3286
405+
new UpdateOneOperation(this.s.namespace, filter, update, resolveOptions(this, options))
411406
);
412407
}
413408

@@ -425,12 +420,7 @@ export class Collection<TSchema extends Document = Document> {
425420
): Promise<UpdateResult<TSchema>> {
426421
return await executeOperation(
427422
this.client,
428-
new ReplaceOneOperation(
429-
this as TODO_NODE_3286,
430-
filter,
431-
replacement,
432-
resolveOptions(this, options)
433-
)
423+
new ReplaceOneOperation(this.s.namespace, filter, replacement, resolveOptions(this, options))
434424
);
435425
}
436426

@@ -452,12 +442,7 @@ export class Collection<TSchema extends Document = Document> {
452442
): Promise<UpdateResult<TSchema>> {
453443
return await executeOperation(
454444
this.client,
455-
new UpdateManyOperation(
456-
this as TODO_NODE_3286,
457-
filter,
458-
update,
459-
resolveOptions(this, options)
460-
) as TODO_NODE_3286
445+
new UpdateManyOperation(this.s.namespace, filter, update, resolveOptions(this, options))
461446
);
462447
}
463448

@@ -473,7 +458,7 @@ export class Collection<TSchema extends Document = Document> {
473458
): Promise<DeleteResult> {
474459
return await executeOperation(
475460
this.client,
476-
new DeleteOneOperation(this as TODO_NODE_3286, filter, resolveOptions(this, options))
461+
new DeleteOneOperation(this.s.namespace, filter, resolveOptions(this, options))
477462
);
478463
}
479464

@@ -489,7 +474,7 @@ export class Collection<TSchema extends Document = Document> {
489474
): Promise<DeleteResult> {
490475
return await executeOperation(
491476
this.client,
492-
new DeleteManyOperation(this as TODO_NODE_3286, filter, resolveOptions(this, options))
477+
new DeleteManyOperation(this.s.namespace, filter, resolveOptions(this, options))
493478
);
494479
}
495480

@@ -513,7 +498,7 @@ export class Collection<TSchema extends Document = Document> {
513498
...options,
514499
readPreference: ReadPreference.PRIMARY
515500
})
516-
) as TODO_NODE_3286
501+
)
517502
);
518503
}
519504

@@ -581,7 +566,7 @@ export class Collection<TSchema extends Document = Document> {
581566
this.client,
582567
this.s.namespace,
583568
filter,
584-
resolveOptions(this as TODO_NODE_3286, options)
569+
resolveOptions(this, options)
585570
);
586571
}
587572

src/operations/client_bulk_write/client_bulk_write.ts

Lines changed: 23 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
import { MongoClientBulkWriteExecutionError, ServerType } from '../../beta';
1+
import { type Connection } from '../../cmap/connection';
22
import { ClientBulkWriteCursorResponse } from '../../cmap/wire_protocol/responses';
3-
import type { Server } from '../../sdam/server';
43
import type { ClientSession } from '../../sessions';
5-
import { type TimeoutContext } from '../../timeout';
64
import { MongoDBNamespace } from '../../utils';
7-
import { CommandOperation } from '../command';
5+
import { ModernizedCommandOperation } from '../command';
86
import { Aspect, defineAspects } from '../operation';
9-
import { type ClientBulkWriteCommandBuilder } from './command_builder';
7+
import { type ClientBulkWriteCommand, type ClientBulkWriteCommandBuilder } from './command_builder';
108
import { type ClientBulkWriteOptions } from './common';
119

1210
/**
1311
* Executes a single client bulk write operation within a potential batch.
1412
* @internal
1513
*/
16-
export class ClientBulkWriteOperation extends CommandOperation<ClientBulkWriteCursorResponse> {
14+
export class ClientBulkWriteOperation extends ModernizedCommandOperation<ClientBulkWriteCursorResponse> {
15+
override SERVER_COMMAND_RESPONSE_TYPE = ClientBulkWriteCursorResponse;
16+
1717
commandBuilder: ClientBulkWriteCommandBuilder;
1818
override options: ClientBulkWriteOptions;
1919

@@ -36,72 +36,28 @@ export class ClientBulkWriteOperation extends CommandOperation<ClientBulkWriteCu
3636
return this.commandBuilder.isBatchRetryable;
3737
}
3838

39-
/**
40-
* Execute the command. Superclass will handle write concern, etc.
41-
* @param server - The server.
42-
* @param session - The session.
43-
* @returns The response.
44-
*/
45-
override async execute(
46-
server: Server,
47-
session: ClientSession | undefined,
48-
timeoutContext: TimeoutContext
49-
): Promise<ClientBulkWriteCursorResponse> {
50-
let command;
39+
override handleOk(
40+
response: InstanceType<typeof this.SERVER_COMMAND_RESPONSE_TYPE>
41+
): ClientBulkWriteCursorResponse {
42+
return response;
43+
}
5144

52-
if (server.description.type === ServerType.LoadBalancer) {
53-
if (session) {
54-
let connection;
55-
if (!session.pinnedConnection) {
56-
// Checkout a connection to build the command.
57-
connection = await server.pool.checkOut({ timeoutContext });
58-
// Pin the connection to the session so it get used to execute the command and we do not
59-
// perform a double check-in/check-out.
60-
session.pin(connection);
61-
} else {
62-
connection = session.pinnedConnection;
63-
}
64-
command = this.commandBuilder.buildBatch(
65-
connection.hello?.maxMessageSizeBytes,
66-
connection.hello?.maxWriteBatchSize,
67-
connection.hello?.maxBsonObjectSize
68-
);
69-
} else {
70-
throw new MongoClientBulkWriteExecutionError(
71-
'Session provided to the client bulk write operation must be present.'
72-
);
73-
}
74-
} else {
75-
// At this point we have a server and the auto connect code has already
76-
// run in executeOperation, so the server description will be populated.
77-
// We can use that to build the command.
78-
if (
79-
!server.description.maxWriteBatchSize ||
80-
!server.description.maxMessageSizeBytes ||
81-
!server.description.maxBsonObjectSize
82-
) {
83-
throw new MongoClientBulkWriteExecutionError(
84-
'In order to execute a client bulk write, both maxWriteBatchSize, maxMessageSizeBytes and maxBsonObjectSize must be provided by the servers hello response.'
85-
);
86-
}
87-
command = this.commandBuilder.buildBatch(
88-
server.description.maxMessageSizeBytes,
89-
server.description.maxWriteBatchSize,
90-
server.description.maxBsonObjectSize
91-
);
92-
}
45+
override buildCommandDocument(
46+
connection: Connection,
47+
_session?: ClientSession
48+
): ClientBulkWriteCommand {
49+
const command = this.commandBuilder.buildBatch(
50+
connection.description.maxMessageSizeBytes,
51+
connection.description.maxWriteBatchSize,
52+
connection.description.maxBsonObjectSize
53+
);
9354

94-
// Check after the batch is built if we cannot retry it and override the option.
55+
// Check _after_ the batch is built if we cannot retry it and override the option.
9556
if (!this.canRetryWrite) {
9657
this.options.willRetryWrite = false;
9758
}
98-
return await super.executeCommand(
99-
server,
100-
session,
101-
command,
102-
timeoutContext,
103-
ClientBulkWriteCursorResponse
104-
);
59+
60+
return command;
10561
}
10662
}
10763

src/operations/count.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1+
import { type Connection } from '..';
12
import type { Document } from '../bson';
3+
import { MongoDBResponse } from '../cmap/wire_protocol/responses';
24
import type { Collection } from '../collection';
3-
import type { Server } from '../sdam/server';
45
import type { ClientSession } from '../sessions';
5-
import { type TimeoutContext } from '../timeout';
66
import type { MongoDBNamespace } from '../utils';
7-
import { CommandOperation, type CommandOperationOptions } from './command';
7+
import { type CommandOperationOptions, ModernizedCommandOperation } from './command';
88
import { Aspect, defineAspects } from './operation';
99

1010
/** @public */
@@ -22,7 +22,8 @@ export interface CountOptions extends CommandOperationOptions {
2222
}
2323

2424
/** @internal */
25-
export class CountOperation extends CommandOperation<number> {
25+
export class CountOperation extends ModernizedCommandOperation<number> {
26+
override SERVER_COMMAND_RESPONSE_TYPE = MongoDBResponse;
2627
override options: CountOptions;
2728
collectionName?: string;
2829
query: Document;
@@ -39,11 +40,7 @@ export class CountOperation extends CommandOperation<number> {
3940
return 'count' as const;
4041
}
4142

42-
override async execute(
43-
server: Server,
44-
session: ClientSession | undefined,
45-
timeoutContext: TimeoutContext
46-
): Promise<number> {
43+
override buildCommandDocument(_connection: Connection, _session?: ClientSession): Document {
4744
const options = this.options;
4845
const cmd: Document = {
4946
count: this.collectionName,
@@ -66,8 +63,11 @@ export class CountOperation extends CommandOperation<number> {
6663
cmd.maxTimeMS = options.maxTimeMS;
6764
}
6865

69-
const result = await super.executeCommand(server, session, cmd, timeoutContext);
70-
return result ? result.n : 0;
66+
return cmd;
67+
}
68+
69+
override handleOk(response: InstanceType<typeof this.SERVER_COMMAND_RESPONSE_TYPE>): number {
70+
return response.getNumber('n') ?? 0;
7171
}
7272
}
7373

src/operations/delete.ts

Lines changed: 32 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
import type { Document } from '../bson';
2-
import type { Collection } from '../collection';
2+
import { type Connection } from '../cmap/connection';
3+
import { MongoDBResponse } from '../cmap/wire_protocol/responses';
34
import { MongoCompatibilityError, MongoServerError } from '../error';
4-
import { type TODO_NODE_3286 } from '../mongo_types';
5-
import type { Server } from '../sdam/server';
65
import type { ClientSession } from '../sessions';
7-
import { type TimeoutContext } from '../timeout';
8-
import { type MongoDBNamespace } from '../utils';
6+
import { type MongoDBCollectionNamespace, type MongoDBNamespace } from '../utils';
97
import { type WriteConcernOptions } from '../write_concern';
10-
import { type CollationOptions, CommandOperation, type CommandOperationOptions } from './command';
8+
import {
9+
type CollationOptions,
10+
type CommandOperationOptions,
11+
ModernizedCommandOperation
12+
} from './command';
1113
import { Aspect, defineAspects, type Hint } from './operation';
1214

1315
/** @public */
@@ -43,7 +45,8 @@ export interface DeleteStatement {
4345
}
4446

4547
/** @internal */
46-
export class DeleteOperation extends CommandOperation<DeleteResult> {
48+
export class DeleteOperation extends ModernizedCommandOperation<Document> {
49+
override SERVER_COMMAND_RESPONSE_TYPE = MongoDBResponse;
4750
override options: DeleteOptions;
4851
statements: DeleteStatement[];
4952

@@ -66,12 +69,9 @@ export class DeleteOperation extends CommandOperation<DeleteResult> {
6669
return this.statements.every(op => (op.limit != null ? op.limit > 0 : true));
6770
}
6871

69-
override async execute(
70-
server: Server,
71-
session: ClientSession | undefined,
72-
timeoutContext: TimeoutContext
73-
): Promise<DeleteResult> {
74-
const options = this.options ?? {};
72+
override buildCommandDocument(_connection: Connection, _session?: ClientSession): Document {
73+
const options = this.options;
74+
7575
const ordered = typeof options.ordered === 'boolean' ? options.ordered : true;
7676
const command: Document = {
7777
delete: this.ns.collection,
@@ -97,28 +97,23 @@ export class DeleteOperation extends CommandOperation<DeleteResult> {
9797
}
9898
}
9999

100-
const res: TODO_NODE_3286 = await super.executeCommand(
101-
server,
102-
session,
103-
command,
104-
timeoutContext
105-
);
106-
return res;
100+
return command;
107101
}
108102
}
109103

110104
export class DeleteOneOperation extends DeleteOperation {
111-
constructor(collection: Collection, filter: Document, options: DeleteOptions) {
112-
super(collection.s.namespace, [makeDeleteStatement(filter, { ...options, limit: 1 })], options);
105+
constructor(ns: MongoDBCollectionNamespace, filter: Document, options: DeleteOptions) {
106+
super(ns, [makeDeleteStatement(filter, { ...options, limit: 1 })], options);
113107
}
114108

115-
override async execute(
116-
server: Server,
117-
session: ClientSession | undefined,
118-
timeoutContext: TimeoutContext
119-
): Promise<DeleteResult> {
120-
const res: TODO_NODE_3286 = await super.execute(server, session, timeoutContext);
109+
override handleOk(
110+
response: InstanceType<typeof this.SERVER_COMMAND_RESPONSE_TYPE>
111+
): DeleteResult {
112+
const res = super.handleOk(response);
113+
114+
// @ts-expect-error Explain commands have broken TS
121115
if (this.explain) return res;
116+
122117
if (res.code) throw new MongoServerError(res);
123118
if (res.writeErrors) throw new MongoServerError(res.writeErrors[0]);
124119

@@ -129,17 +124,18 @@ export class DeleteOneOperation extends DeleteOperation {
129124
}
130125
}
131126
export class DeleteManyOperation extends DeleteOperation {
132-
constructor(collection: Collection, filter: Document, options: DeleteOptions) {
133-
super(collection.s.namespace, [makeDeleteStatement(filter, options)], options);
127+
constructor(ns: MongoDBCollectionNamespace, filter: Document, options: DeleteOptions) {
128+
super(ns, [makeDeleteStatement(filter, options)], options);
134129
}
135130

136-
override async execute(
137-
server: Server,
138-
session: ClientSession | undefined,
139-
timeoutContext: TimeoutContext
140-
): Promise<DeleteResult> {
141-
const res: TODO_NODE_3286 = await super.execute(server, session, timeoutContext);
131+
override handleOk(
132+
response: InstanceType<typeof this.SERVER_COMMAND_RESPONSE_TYPE>
133+
): DeleteResult {
134+
const res = super.handleOk(response);
135+
136+
// @ts-expect-error Explain commands have broken TS
142137
if (this.explain) return res;
138+
143139
if (res.code) throw new MongoServerError(res);
144140
if (res.writeErrors) throw new MongoServerError(res.writeErrors[0]);
145141

0 commit comments

Comments
 (0)