1
1
import type { Document } from '../bson' ;
2
+ import { type Connection } from '../cmap/connection' ;
3
+ import { MongoDBResponse } from '../cmap/wire_protocol/responses' ;
2
4
import type { Collection } from '../collection' ;
3
5
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' ;
6
7
import type { ClientSession } from '../sessions' ;
7
8
import { formatSort , type Sort , type SortForCmd } from '../sort' ;
8
- import { type TimeoutContext } from '../timeout' ;
9
9
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' ;
11
15
import { Aspect , defineAspects , type Hint } from './operation' ;
12
16
13
17
/** @public */
@@ -67,7 +71,8 @@ export interface UpdateStatement {
67
71
* @internal
68
72
* UpdateOperation is used in bulk write, while UpdateOneOperation and UpdateManyOperation are only used in the collections API
69
73
*/
70
- export class UpdateOperation extends CommandOperation < Document > {
74
+ export class UpdateOperation extends ModernizedCommandOperation < Document > {
75
+ override SERVER_COMMAND_RESPONSE_TYPE = MongoDBResponse ;
71
76
override options : UpdateOptions & { ordered ?: boolean } ;
72
77
statements : UpdateStatement [ ] ;
73
78
@@ -95,17 +100,12 @@ export class UpdateOperation extends CommandOperation<Document> {
95
100
return this . statements . every ( op => op . multi == null || op . multi === false ) ;
96
101
}
97
102
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 ;
105
105
const command : Document = {
106
106
update : this . ns . collection ,
107
107
updates : this . statements ,
108
- ordered
108
+ ordered : options . ordered ?? true
109
109
} ;
110
110
111
111
if ( typeof options . bypassDocumentValidation === 'boolean' ) {
@@ -122,16 +122,15 @@ export class UpdateOperation extends CommandOperation<Document> {
122
122
command . comment = options . comment ;
123
123
}
124
124
125
- const unacknowledgedWrite = this . writeConcern && this . writeConcern . w === 0 ;
125
+ const unacknowledgedWrite = this . writeConcern ? .w === 0 ;
126
126
if ( unacknowledgedWrite ) {
127
127
if ( this . statements . find ( ( o : Document ) => o . hint ) ) {
128
128
// TODO(NODE-3541): fix error for hint with unacknowledged writes
129
129
throw new MongoCompatibilityError ( `hint is not supported with unacknowledged writes` ) ;
130
130
}
131
131
}
132
132
133
- const res = await super . executeCommand ( server , session , command , timeoutContext ) ;
134
- return res ;
133
+ return command ;
135
134
}
136
135
}
137
136
@@ -149,12 +148,8 @@ export class UpdateOneOperation extends UpdateOperation {
149
148
}
150
149
}
151
150
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 ) ;
158
153
if ( this . explain != null ) return res ;
159
154
if ( res . code ) throw new MongoServerError ( res ) ;
160
155
if ( res . writeErrors ) throw new MongoServerError ( res . writeErrors [ 0 ] ) ;
@@ -184,12 +179,8 @@ export class UpdateManyOperation extends UpdateOperation {
184
179
}
185
180
}
186
181
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 ) ;
193
184
if ( this . explain != null ) return res ;
194
185
if ( res . code ) throw new MongoServerError ( res ) ;
195
186
if ( res . writeErrors ) throw new MongoServerError ( res . writeErrors [ 0 ] ) ;
@@ -240,12 +231,8 @@ export class ReplaceOneOperation extends UpdateOperation {
240
231
}
241
232
}
242
233
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 ) ;
249
236
if ( this . explain != null ) return res ;
250
237
if ( res . code ) throw new MongoServerError ( res ) ;
251
238
if ( res . writeErrors ) throw new MongoServerError ( res . writeErrors [ 0 ] ) ;
0 commit comments