@@ -163,33 +163,34 @@ export class Batch<T = Document> {
163
163
}
164
164
}
165
165
166
- function generateIdMap ( ids : Document [ ] ) : { [ key : number ] : any } {
167
- const idMap : { [ index : number ] : any } = { } ;
168
- for ( const doc of ids ?? [ ] ) {
169
- idMap [ doc . index ] = doc . _id ;
170
- }
171
- return idMap ;
172
- }
173
166
/**
174
167
* @public
175
168
* The result of a bulk write.
176
169
*/
177
170
export class BulkWriteResult {
178
- private result : BulkResult ;
171
+ private readonly result : BulkResult ;
179
172
/** Number of documents inserted. */
180
- insertedCount : number ;
173
+ readonly insertedCount : number ;
181
174
/** Number of documents matched for update. */
182
- matchedCount : number ;
175
+ readonly matchedCount : number ;
183
176
/** Number of documents modified. */
184
- modifiedCount : number ;
177
+ readonly modifiedCount : number ;
185
178
/** Number of documents deleted. */
186
- deletedCount : number ;
179
+ readonly deletedCount : number ;
187
180
/** Number of documents upserted. */
188
- upsertedCount : number ;
181
+ readonly upsertedCount : number ;
189
182
/** Upserted document generated Id's, hash key is the index of the originating operation */
190
- upsertedIds : { [ key : number ] : any } ;
183
+ readonly upsertedIds : { [ key : number ] : any } ;
191
184
/** Inserted document generated Id's, hash key is the index of the originating operation */
192
- insertedIds : { [ key : number ] : any } ;
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 ;
191
+ }
192
+ return idMap ;
193
+ }
193
194
194
195
/**
195
196
* Create a new BulkWriteResult instance
@@ -202,8 +203,8 @@ export class BulkWriteResult {
202
203
this . modifiedCount = this . result . nModified ?? 0 ;
203
204
this . deletedCount = this . result . nRemoved ?? 0 ;
204
205
this . upsertedCount = this . result . upserted . length ?? 0 ;
205
- this . upsertedIds = generateIdMap ( this . result . upserted ) ;
206
- this . insertedIds = generateIdMap ( this . result . insertedIds ) ;
206
+ this . upsertedIds = BulkWriteResult . generateIdMap ( this . result . upserted ) ;
207
+ this . insertedIds = BulkWriteResult . generateIdMap ( this . result . insertedIds ) ;
207
208
Object . defineProperty ( this , 'result' , { value : this . result , enumerable : false } ) ;
208
209
}
209
210
@@ -530,12 +531,8 @@ function executeCommands(
530
531
}
531
532
532
533
// Merge the results together
533
- const mergeResult = mergeBatchResults ( batch , bulkOperation . s . bulkResult , err , result ) ;
534
+ mergeBatchResults ( batch , bulkOperation . s . bulkResult , err , result ) ;
534
535
const writeResult = new BulkWriteResult ( bulkOperation . s . bulkResult ) ;
535
- if ( mergeResult != null ) {
536
- return callback ( undefined , writeResult ) ;
537
- }
538
-
539
536
if ( bulkOperation . handleWriteError ( callback , writeResult ) ) return ;
540
537
541
538
// Execute the next command in line
0 commit comments