1
1
import type { Document } from '../bson' ;
2
2
import { type Connection } from '../cmap/connection' ;
3
3
import { MongoDBResponse } from '../cmap/wire_protocol/responses' ;
4
- import type { Collection } from '../collection' ;
5
4
import { MongoCompatibilityError , MongoInvalidArgumentError , MongoServerError } from '../error' ;
6
5
import type { InferIdType } from '../mongo_types' ;
7
6
import type { ClientSession } from '../sessions' ;
8
7
import { formatSort , type Sort , type SortForCmd } from '../sort' ;
9
- import { hasAtomicOperators , type MongoDBNamespace } from '../utils' ;
8
+ import {
9
+ hasAtomicOperators ,
10
+ type MongoDBCollectionNamespace ,
11
+ type MongoDBNamespace
12
+ } from '../utils' ;
10
13
import {
11
14
type CollationOptions ,
12
15
type CommandOperationOptions ,
@@ -136,21 +139,27 @@ export class UpdateOperation extends ModernizedCommandOperation<Document> {
136
139
137
140
/** @internal */
138
141
export class UpdateOneOperation extends UpdateOperation {
139
- constructor ( collection : Collection , filter : Document , update : Document , options : UpdateOptions ) {
140
- super (
141
- collection . s . namespace ,
142
- [ makeUpdateStatement ( filter , update , { ...options , multi : false } ) ] ,
143
- options
144
- ) ;
142
+ constructor (
143
+ ns : MongoDBCollectionNamespace ,
144
+ filter : Document ,
145
+ update : Document ,
146
+ options : UpdateOptions
147
+ ) {
148
+ super ( ns , [ makeUpdateStatement ( filter , update , { ...options , multi : false } ) ] , options ) ;
145
149
146
150
if ( ! hasAtomicOperators ( update , options ) ) {
147
151
throw new MongoInvalidArgumentError ( 'Update document requires atomic operators' ) ;
148
152
}
149
153
}
150
154
151
- override handleOk ( response : InstanceType < typeof this . SERVER_COMMAND_RESPONSE_TYPE > ) : Document {
155
+ override handleOk (
156
+ response : InstanceType < typeof this . SERVER_COMMAND_RESPONSE_TYPE >
157
+ ) : UpdateResult {
152
158
const res = super . handleOk ( response ) ;
159
+
160
+ // @ts -expect-error Explain typing is broken
153
161
if ( this . explain != null ) return res ;
162
+
154
163
if ( res . code ) throw new MongoServerError ( res ) ;
155
164
if ( res . writeErrors ) throw new MongoServerError ( res . writeErrors [ 0 ] ) ;
156
165
@@ -167,20 +176,25 @@ export class UpdateOneOperation extends UpdateOperation {
167
176
168
177
/** @internal */
169
178
export class UpdateManyOperation extends UpdateOperation {
170
- constructor ( collection : Collection , filter : Document , update : Document , options : UpdateOptions ) {
171
- super (
172
- collection . s . namespace ,
173
- [ makeUpdateStatement ( filter , update , { ...options , multi : true } ) ] ,
174
- options
175
- ) ;
179
+ constructor (
180
+ ns : MongoDBCollectionNamespace ,
181
+ filter : Document ,
182
+ update : Document ,
183
+ options : UpdateOptions
184
+ ) {
185
+ super ( ns , [ makeUpdateStatement ( filter , update , { ...options , multi : true } ) ] , options ) ;
176
186
177
187
if ( ! hasAtomicOperators ( update , options ) ) {
178
188
throw new MongoInvalidArgumentError ( 'Update document requires atomic operators' ) ;
179
189
}
180
190
}
181
191
182
- override handleOk ( response : InstanceType < typeof this . SERVER_COMMAND_RESPONSE_TYPE > ) : Document {
192
+ override handleOk (
193
+ response : InstanceType < typeof this . SERVER_COMMAND_RESPONSE_TYPE >
194
+ ) : UpdateResult {
183
195
const res = super . handleOk ( response ) ;
196
+
197
+ // @ts -expect-error Explain typing is broken
184
198
if ( this . explain != null ) return res ;
185
199
if ( res . code ) throw new MongoServerError ( res ) ;
186
200
if ( res . writeErrors ) throw new MongoServerError ( res . writeErrors [ 0 ] ) ;
@@ -215,24 +229,24 @@ export interface ReplaceOptions extends CommandOperationOptions {
215
229
/** @internal */
216
230
export class ReplaceOneOperation extends UpdateOperation {
217
231
constructor (
218
- collection : Collection ,
232
+ ns : MongoDBCollectionNamespace ,
219
233
filter : Document ,
220
234
replacement : Document ,
221
235
options : ReplaceOptions
222
236
) {
223
- super (
224
- collection . s . namespace ,
225
- [ makeUpdateStatement ( filter , replacement , { ...options , multi : false } ) ] ,
226
- options
227
- ) ;
237
+ super ( ns , [ makeUpdateStatement ( filter , replacement , { ...options , multi : false } ) ] , options ) ;
228
238
229
239
if ( hasAtomicOperators ( replacement ) ) {
230
240
throw new MongoInvalidArgumentError ( 'Replacement document must not contain atomic operators' ) ;
231
241
}
232
242
}
233
243
234
- override handleOk ( response : InstanceType < typeof this . SERVER_COMMAND_RESPONSE_TYPE > ) : Document {
244
+ override handleOk (
245
+ response : InstanceType < typeof this . SERVER_COMMAND_RESPONSE_TYPE >
246
+ ) : UpdateResult {
235
247
const res = super . handleOk ( response ) ;
248
+
249
+ // @ts -expect-error Explain typing is broken
236
250
if ( this . explain != null ) return res ;
237
251
if ( res . code ) throw new MongoServerError ( res ) ;
238
252
if ( res . writeErrors ) throw new MongoServerError ( res . writeErrors [ 0 ] ) ;
0 commit comments