@@ -31,6 +31,7 @@ import {
31
31
Callback ,
32
32
getTopology ,
33
33
hasAtomicOperators ,
34
+ maybeCallback ,
34
35
MongoDBNamespace ,
35
36
resolveOptions
36
37
} from '../utils' ;
@@ -1270,39 +1271,44 @@ export abstract class BulkOperationBase {
1270
1271
options ?: BulkWriteOptions | Callback < BulkWriteResult > ,
1271
1272
callback ?: Callback < BulkWriteResult >
1272
1273
) : Promise < BulkWriteResult > | void {
1273
- if ( typeof options === 'function' ) ( callback = options ) , ( options = { } ) ;
1274
- options = options ?? { } ;
1275
-
1276
- if ( this . s . executed ) {
1277
- return handleEarlyError ( new MongoBatchReExecutionError ( ) , callback ) ;
1278
- }
1274
+ callback =
1275
+ typeof callback === 'function'
1276
+ ? callback
1277
+ : typeof options === 'function'
1278
+ ? options
1279
+ : undefined ;
1280
+
1281
+ return maybeCallback ( async ( ) => {
1282
+ options = options != null && typeof options !== 'function' ? options : { } ;
1283
+
1284
+ if ( this . s . executed ) {
1285
+ throw new MongoBatchReExecutionError ( ) ;
1286
+ }
1279
1287
1280
- const writeConcern = WriteConcern . fromOptions ( options ) ;
1281
- if ( writeConcern ) {
1282
- this . s . writeConcern = writeConcern ;
1283
- }
1288
+ const writeConcern = WriteConcern . fromOptions ( options ) ;
1289
+ if ( writeConcern ) {
1290
+ this . s . writeConcern = writeConcern ;
1291
+ }
1284
1292
1285
- // If we have current batch
1286
- if ( this . isOrdered ) {
1287
- if ( this . s . currentBatch ) this . s . batches . push ( this . s . currentBatch ) ;
1288
- } else {
1289
- if ( this . s . currentInsertBatch ) this . s . batches . push ( this . s . currentInsertBatch ) ;
1290
- if ( this . s . currentUpdateBatch ) this . s . batches . push ( this . s . currentUpdateBatch ) ;
1291
- if ( this . s . currentRemoveBatch ) this . s . batches . push ( this . s . currentRemoveBatch ) ;
1292
- }
1293
- // If we have no operations in the bulk raise an error
1294
- if ( this . s . batches . length === 0 ) {
1295
- const emptyBatchError = new MongoInvalidArgumentError (
1296
- 'Invalid BulkOperation, Batch cannot be empty'
1297
- ) ;
1298
- return handleEarlyError ( emptyBatchError , callback ) ;
1299
- }
1293
+ // If we have current batch
1294
+ if ( this . isOrdered ) {
1295
+ if ( this . s . currentBatch ) this . s . batches . push ( this . s . currentBatch ) ;
1296
+ } else {
1297
+ if ( this . s . currentInsertBatch ) this . s . batches . push ( this . s . currentInsertBatch ) ;
1298
+ if ( this . s . currentUpdateBatch ) this . s . batches . push ( this . s . currentUpdateBatch ) ;
1299
+ if ( this . s . currentRemoveBatch ) this . s . batches . push ( this . s . currentRemoveBatch ) ;
1300
+ }
1301
+ // If we have no operations in the bulk raise an error
1302
+ if ( this . s . batches . length === 0 ) {
1303
+ throw new MongoInvalidArgumentError ( 'Invalid BulkOperation, Batch cannot be empty' ) ;
1304
+ }
1300
1305
1301
- this . s . executed = true ;
1302
- const finalOptions = { ...this . s . options , ...options } ;
1303
- const operation = new BulkWriteShimOperation ( this , finalOptions ) ;
1306
+ this . s . executed = true ;
1307
+ const finalOptions = { ...this . s . options , ...options } ;
1308
+ const operation = new BulkWriteShimOperation ( this , finalOptions ) ;
1304
1309
1305
- return executeOperation ( this . s . collection . s . db . s . client , operation , callback ) ;
1310
+ return await executeOperation ( this . s . collection . s . db . s . client , operation ) ;
1311
+ } , callback ) ;
1306
1312
}
1307
1313
1308
1314
/**
@@ -1351,20 +1357,6 @@ Object.defineProperty(BulkOperationBase.prototype, 'length', {
1351
1357
}
1352
1358
} ) ;
1353
1359
1354
- /** helper function to assist with promiseOrCallback behavior */
1355
- function handleEarlyError (
1356
- err ?: AnyError ,
1357
- callback ?: Callback < BulkWriteResult >
1358
- ) : Promise < BulkWriteResult > | void {
1359
- if ( typeof callback === 'function' ) {
1360
- callback ( err ) ;
1361
- return ;
1362
- }
1363
-
1364
- const PromiseConstructor = PromiseProvider . get ( ) ?? Promise ;
1365
- return PromiseConstructor . reject ( err ) ;
1366
- }
1367
-
1368
1360
function shouldForceServerObjectId ( bulkOperation : BulkOperationBase ) : boolean {
1369
1361
if ( typeof bulkOperation . s . options . forceServerObjectId === 'boolean' ) {
1370
1362
return bulkOperation . s . options . forceServerObjectId ;
0 commit comments