@@ -123,11 +123,7 @@ export type AnyBulkWriteOperation<TSchema extends Document = Document> =
123
123
| { deleteOne : DeleteOneModel < TSchema > }
124
124
| { deleteMany : DeleteManyModel < TSchema > } ;
125
125
126
- /**
127
- * @public
128
- *
129
- * @deprecated Will be made internal in 5.0
130
- */
126
+ /** @internal */
131
127
export interface BulkResult {
132
128
ok : number ;
133
129
writeErrors : WriteError [ ] ;
@@ -172,54 +168,44 @@ export class Batch<T = Document> {
172
168
* The result of a bulk write.
173
169
*/
174
170
export class BulkWriteResult {
175
- /** @deprecated Will be removed in 5.0 */
176
- result : BulkResult ;
177
-
178
- /**
179
- * Create a new BulkWriteResult instance
180
- * @internal
181
- */
182
- constructor ( bulkResult : BulkResult ) {
183
- this . result = bulkResult ;
184
- }
185
-
171
+ private readonly result : BulkResult ;
186
172
/** Number of documents inserted. */
187
- get insertedCount ( ) : number {
188
- return this . result . nInserted ?? 0 ;
189
- }
173
+ readonly insertedCount : number ;
190
174
/** Number of documents matched for update. */
191
- get matchedCount ( ) : number {
192
- return this . result . nMatched ?? 0 ;
193
- }
175
+ readonly matchedCount : number ;
194
176
/** Number of documents modified. */
195
- get modifiedCount ( ) : number {
196
- return this . result . nModified ?? 0 ;
197
- }
177
+ readonly modifiedCount : number ;
198
178
/** Number of documents deleted. */
199
- get deletedCount ( ) : number {
200
- return this . result . nRemoved ?? 0 ;
201
- }
179
+ readonly deletedCount : number ;
202
180
/** Number of documents upserted. */
203
- get upsertedCount ( ) : number {
204
- return this . result . upserted . length ?? 0 ;
205
- }
206
-
181
+ readonly upsertedCount : number ;
207
182
/** Upserted document generated Id's, hash key is the index of the originating operation */
208
- get upsertedIds ( ) : { [ key : number ] : any } {
209
- const upserted : { [ index : number ] : any } = { } ;
210
- for ( const doc of this . result . upserted ?? [ ] ) {
211
- upserted [ doc . index ] = doc . _id ;
183
+ readonly upsertedIds : { [ key : number ] : any } ;
184
+ /** Inserted document generated Id's, hash key is the index of the originating operation */
185
+ readonly insertedIds : { [ key : number ] : any } ;
186
+
187
+ private static generateIdMap ( ids : Document [ ] ) : { [ key : number ] : any } {
188
+ const idMap : { [ index : number ] : any } = { } ;
189
+ for ( const doc of ids ) {
190
+ idMap [ doc . index ] = doc . _id ;
212
191
}
213
- return upserted ;
192
+ return idMap ;
214
193
}
215
194
216
- /** Inserted document generated Id's, hash key is the index of the originating operation */
217
- get insertedIds ( ) : { [ key : number ] : any } {
218
- const inserted : { [ index : number ] : any } = { } ;
219
- for ( const doc of this . result . insertedIds ?? [ ] ) {
220
- inserted [ doc . index ] = doc . _id ;
221
- }
222
- return inserted ;
195
+ /**
196
+ * Create a new BulkWriteResult instance
197
+ * @internal
198
+ */
199
+ constructor ( bulkResult : BulkResult ) {
200
+ this . result = bulkResult ;
201
+ this . insertedCount = this . result . nInserted ?? 0 ;
202
+ this . matchedCount = this . result . nMatched ?? 0 ;
203
+ this . modifiedCount = this . result . nModified ?? 0 ;
204
+ this . deletedCount = this . result . nRemoved ?? 0 ;
205
+ this . upsertedCount = this . result . upserted . length ?? 0 ;
206
+ this . upsertedIds = BulkWriteResult . generateIdMap ( this . result . upserted ) ;
207
+ this . insertedIds = BulkWriteResult . generateIdMap ( this . result . insertedIds ) ;
208
+ Object . defineProperty ( this , 'result' , { value : this . result , enumerable : false } ) ;
223
209
}
224
210
225
211
/** Evaluates to true if the bulk operation correctly executes */
@@ -314,13 +300,8 @@ export class BulkWriteResult {
314
300
}
315
301
}
316
302
317
- /* @deprecated Will be removed in 5.0 release */
318
- toJSON ( ) : BulkResult {
319
- return this . result ;
320
- }
321
-
322
303
toString ( ) : string {
323
- return `BulkWriteResult(${ this . toJSON ( ) } )` ;
304
+ return `BulkWriteResult(${ this . result } )` ;
324
305
}
325
306
326
307
isOk ( ) : boolean {
@@ -550,12 +531,8 @@ function executeCommands(
550
531
}
551
532
552
533
// Merge the results together
534
+ mergeBatchResults ( batch , bulkOperation . s . bulkResult , err , result ) ;
553
535
const writeResult = new BulkWriteResult ( bulkOperation . s . bulkResult ) ;
554
- const mergeResult = mergeBatchResults ( batch , bulkOperation . s . bulkResult , err , result ) ;
555
- if ( mergeResult != null ) {
556
- return callback ( undefined , writeResult ) ;
557
- }
558
-
559
536
if ( bulkOperation . handleWriteError ( callback , writeResult ) ) return ;
560
537
561
538
// Execute the next command in line
@@ -1246,7 +1223,6 @@ export abstract class BulkOperationBase {
1246
1223
this . s . executed = true ;
1247
1224
const finalOptions = { ...this . s . options , ...options } ;
1248
1225
const operation = new BulkWriteShimOperation ( this , finalOptions ) ;
1249
-
1250
1226
return executeOperation ( this . s . collection . s . db . s . client , operation ) ;
1251
1227
} , callback ) ;
1252
1228
}
0 commit comments