@@ -163,33 +163,34 @@ export class Batch<T = Document> {
163163 }
164164}
165165
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- }
173166/**
174167 * @public
175168 * The result of a bulk write.
176169 */
177170export class BulkWriteResult {
178- private result : BulkResult ;
171+ private readonly result : BulkResult ;
179172 /** Number of documents inserted. */
180- insertedCount : number ;
173+ readonly insertedCount : number ;
181174 /** Number of documents matched for update. */
182- matchedCount : number ;
175+ readonly matchedCount : number ;
183176 /** Number of documents modified. */
184- modifiedCount : number ;
177+ readonly modifiedCount : number ;
185178 /** Number of documents deleted. */
186- deletedCount : number ;
179+ readonly deletedCount : number ;
187180 /** Number of documents upserted. */
188- upsertedCount : number ;
181+ readonly upsertedCount : number ;
189182 /** 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 } ;
191184 /** 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+ }
193194
194195 /**
195196 * Create a new BulkWriteResult instance
@@ -202,8 +203,8 @@ export class BulkWriteResult {
202203 this . modifiedCount = this . result . nModified ?? 0 ;
203204 this . deletedCount = this . result . nRemoved ?? 0 ;
204205 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 ) ;
207208 Object . defineProperty ( this , 'result' , { value : this . result , enumerable : false } ) ;
208209 }
209210
@@ -530,12 +531,8 @@ function executeCommands(
530531 }
531532
532533 // Merge the results together
533- const mergeResult = mergeBatchResults ( batch , bulkOperation . s . bulkResult , err , result ) ;
534+ mergeBatchResults ( batch , bulkOperation . s . bulkResult , err , result ) ;
534535 const writeResult = new BulkWriteResult ( bulkOperation . s . bulkResult ) ;
535- if ( mergeResult != null ) {
536- return callback ( undefined , writeResult ) ;
537- }
538-
539536 if ( bulkOperation . handleWriteError ( callback , writeResult ) ) return ;
540537
541538 // Execute the next command in line
0 commit comments