Skip to content

Commit 9e936fb

Browse files
update & replace
1 parent 69a3ec2 commit 9e936fb

File tree

1 file changed

+21
-34
lines changed

1 file changed

+21
-34
lines changed

src/operations/update.ts

Lines changed: 21 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
import type { Document } from '../bson';
2+
import { type Connection } from '../cmap/connection';
3+
import { MongoDBResponse } from '../cmap/wire_protocol/responses';
24
import type { Collection } from '../collection';
35
import { MongoCompatibilityError, MongoInvalidArgumentError, MongoServerError } from '../error';
4-
import type { InferIdType, TODO_NODE_3286 } from '../mongo_types';
5-
import type { Server } from '../sdam/server';
6+
import type { InferIdType } from '../mongo_types';
67
import type { ClientSession } from '../sessions';
78
import { formatSort, type Sort, type SortForCmd } from '../sort';
8-
import { type TimeoutContext } from '../timeout';
99
import { hasAtomicOperators, type MongoDBNamespace } from '../utils';
10-
import { type CollationOptions, CommandOperation, type CommandOperationOptions } from './command';
10+
import {
11+
type CollationOptions,
12+
type CommandOperationOptions,
13+
ModernizedCommandOperation
14+
} from './command';
1115
import { Aspect, defineAspects, type Hint } from './operation';
1216

1317
/** @public */
@@ -67,7 +71,8 @@ export interface UpdateStatement {
6771
* @internal
6872
* UpdateOperation is used in bulk write, while UpdateOneOperation and UpdateManyOperation are only used in the collections API
6973
*/
70-
export class UpdateOperation extends CommandOperation<Document> {
74+
export class UpdateOperation extends ModernizedCommandOperation<Document> {
75+
override SERVER_COMMAND_RESPONSE_TYPE = MongoDBResponse;
7176
override options: UpdateOptions & { ordered?: boolean };
7277
statements: UpdateStatement[];
7378

@@ -95,17 +100,12 @@ export class UpdateOperation extends CommandOperation<Document> {
95100
return this.statements.every(op => op.multi == null || op.multi === false);
96101
}
97102

98-
override async execute(
99-
server: Server,
100-
session: ClientSession | undefined,
101-
timeoutContext: TimeoutContext
102-
): Promise<Document> {
103-
const options = this.options ?? {};
104-
const ordered = typeof options.ordered === 'boolean' ? options.ordered : true;
103+
override buildCommandDocument(_connection: Connection, _session?: ClientSession): Document {
104+
const options = this.options;
105105
const command: Document = {
106106
update: this.ns.collection,
107107
updates: this.statements,
108-
ordered
108+
ordered: options.ordered ?? true
109109
};
110110

111111
if (typeof options.bypassDocumentValidation === 'boolean') {
@@ -122,16 +122,15 @@ export class UpdateOperation extends CommandOperation<Document> {
122122
command.comment = options.comment;
123123
}
124124

125-
const unacknowledgedWrite = this.writeConcern && this.writeConcern.w === 0;
125+
const unacknowledgedWrite = this.writeConcern?.w === 0;
126126
if (unacknowledgedWrite) {
127127
if (this.statements.find((o: Document) => o.hint)) {
128128
// TODO(NODE-3541): fix error for hint with unacknowledged writes
129129
throw new MongoCompatibilityError(`hint is not supported with unacknowledged writes`);
130130
}
131131
}
132132

133-
const res = await super.executeCommand(server, session, command, timeoutContext);
134-
return res;
133+
return command;
135134
}
136135
}
137136

@@ -149,12 +148,8 @@ export class UpdateOneOperation extends UpdateOperation {
149148
}
150149
}
151150

152-
override async execute(
153-
server: Server,
154-
session: ClientSession | undefined,
155-
timeoutContext: TimeoutContext
156-
): Promise<UpdateResult> {
157-
const res: TODO_NODE_3286 = await super.execute(server, session, timeoutContext);
151+
override handleOk(response: InstanceType<typeof this.SERVER_COMMAND_RESPONSE_TYPE>): Document {
152+
const res = super.handleOk(response);
158153
if (this.explain != null) return res;
159154
if (res.code) throw new MongoServerError(res);
160155
if (res.writeErrors) throw new MongoServerError(res.writeErrors[0]);
@@ -184,12 +179,8 @@ export class UpdateManyOperation extends UpdateOperation {
184179
}
185180
}
186181

187-
override async execute(
188-
server: Server,
189-
session: ClientSession | undefined,
190-
timeoutContext: TimeoutContext
191-
): Promise<UpdateResult> {
192-
const res: TODO_NODE_3286 = await super.execute(server, session, timeoutContext);
182+
override handleOk(response: InstanceType<typeof this.SERVER_COMMAND_RESPONSE_TYPE>): Document {
183+
const res = super.handleOk(response);
193184
if (this.explain != null) return res;
194185
if (res.code) throw new MongoServerError(res);
195186
if (res.writeErrors) throw new MongoServerError(res.writeErrors[0]);
@@ -240,12 +231,8 @@ export class ReplaceOneOperation extends UpdateOperation {
240231
}
241232
}
242233

243-
override async execute(
244-
server: Server,
245-
session: ClientSession | undefined,
246-
timeoutContext: TimeoutContext
247-
): Promise<UpdateResult> {
248-
const res: TODO_NODE_3286 = await super.execute(server, session, timeoutContext);
234+
override handleOk(response: InstanceType<typeof this.SERVER_COMMAND_RESPONSE_TYPE>): Document {
235+
const res = super.handleOk(response);
249236
if (this.explain != null) return res;
250237
if (res.code) throw new MongoServerError(res);
251238
if (res.writeErrors) throw new MongoServerError(res.writeErrors[0]);

0 commit comments

Comments
 (0)