@@ -163,61 +163,50 @@ 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
+ }
166
173
/**
167
174
* @public
168
175
* The result of a bulk write.
169
176
*/
170
177
export class BulkWriteResult {
171
178
private result : BulkResult ;
179
+ /** Number of documents inserted. */
180
+ insertedCount : number ;
181
+ /** Number of documents matched for update. */
182
+ matchedCount : number ;
183
+ /** Number of documents modified. */
184
+ modifiedCount : number ;
185
+ /** Number of documents deleted. */
186
+ deletedCount : number ;
187
+ /** Number of documents upserted. */
188
+ upsertedCount : number ;
189
+ /** Upserted document generated Id's, hash key is the index of the originating operation */
190
+ upsertedIds : { [ key : number ] : any } ;
191
+ /** Inserted document generated Id's, hash key is the index of the originating operation */
192
+ insertedIds : { [ key : number ] : any } ;
172
193
173
194
/**
174
195
* Create a new BulkWriteResult instance
175
196
* @internal
176
197
*/
177
198
constructor ( bulkResult : BulkResult ) {
178
199
this . result = bulkResult ;
200
+ this . insertedCount = this . result . nInserted ?? 0 ;
201
+ this . matchedCount = this . result . nMatched ?? 0 ;
202
+ this . modifiedCount = this . result . nModified ?? 0 ;
203
+ this . deletedCount = this . result . nRemoved ?? 0 ;
204
+ this . upsertedCount = this . result . upserted . length ?? 0 ;
205
+ this . upsertedIds = generateIdMap ( this . result . upserted ) ;
206
+ this . insertedIds = generateIdMap ( this . result . insertedIds ) ;
179
207
Object . defineProperty ( this , 'result' , { value : this . result , enumerable : false } ) ;
180
208
}
181
209
182
- /** Number of documents inserted. */
183
- get insertedCount ( ) : number {
184
- return this . result . nInserted ?? 0 ;
185
- }
186
- /** Number of documents matched for update. */
187
- get matchedCount ( ) : number {
188
- return this . result . nMatched ?? 0 ;
189
- }
190
- /** Number of documents modified. */
191
- get modifiedCount ( ) : number {
192
- return this . result . nModified ?? 0 ;
193
- }
194
- /** Number of documents deleted. */
195
- get deletedCount ( ) : number {
196
- return this . result . nRemoved ?? 0 ;
197
- }
198
- /** Number of documents upserted. */
199
- get upsertedCount ( ) : number {
200
- return this . result . upserted . length ?? 0 ;
201
- }
202
-
203
- /** Upserted document generated Id's, hash key is the index of the originating operation */
204
- get upsertedIds ( ) : { [ key : number ] : any } {
205
- const upserted : { [ index : number ] : any } = { } ;
206
- for ( const doc of this . result . upserted ?? [ ] ) {
207
- upserted [ doc . index ] = doc . _id ;
208
- }
209
- return upserted ;
210
- }
211
-
212
- /** Inserted document generated Id's, hash key is the index of the originating operation */
213
- get insertedIds ( ) : { [ key : number ] : any } {
214
- const inserted : { [ index : number ] : any } = { } ;
215
- for ( const doc of this . result . insertedIds ?? [ ] ) {
216
- inserted [ doc . index ] = doc . _id ;
217
- }
218
- return inserted ;
219
- }
220
-
221
210
/** Evaluates to true if the bulk operation correctly executes */
222
211
get ok ( ) : number {
223
212
return this . result . ok ;
0 commit comments