(ops, options)
| 3419 | }; |
| 3420 | |
| 3421 | async function _bulkWrite(ops, options) { |
| 3422 | options = options || {}; |
| 3423 | const preFilter = buildMiddlewareFilter(options, 'pre'); |
| 3424 | const postFilter = buildMiddlewareFilter(options, 'post'); |
| 3425 | |
| 3426 | try { |
| 3427 | [ops, options] = await this.hooks.execPre('bulkWrite', this, [ops, options], { filter: preFilter }); |
| 3428 | } catch (err) { |
| 3429 | if (err instanceof Kareem.skipWrappedFunction) { |
| 3430 | ops = err; |
| 3431 | } else { |
| 3432 | await this.hooks.execPost('bulkWrite', this, [null], { error: err, filter: postFilter }); |
| 3433 | } |
| 3434 | } |
| 3435 | |
| 3436 | if (ops instanceof Kareem.skipWrappedFunction) { |
| 3437 | return ops.args[0]; |
| 3438 | } |
| 3439 | |
| 3440 | const ordered = options.ordered == null ? true : options.ordered; |
| 3441 | |
| 3442 | if (ops.length === 0) { |
| 3443 | const BulkWriteResult = this.base.driver.get().BulkWriteResult; |
| 3444 | const bulkWriteResult = new BulkWriteResult(getDefaultBulkwriteResult(), false); |
| 3445 | bulkWriteResult.n = 0; |
| 3446 | decorateBulkWriteResult(bulkWriteResult, [], []); |
| 3447 | return bulkWriteResult; |
| 3448 | } |
| 3449 | |
| 3450 | const validations = options?._skipCastBulkWrite ? [] : ops.map(op => castBulkWrite(this, op, options)); |
| 3451 | const asyncLocalStorage = this.db.base.transactionAsyncLocalStorage?.getStore(); |
| 3452 | if ((!options || !Object.hasOwn(options, 'session')) && asyncLocalStorage?.session != null) { |
| 3453 | options = { ...options, session: asyncLocalStorage.session }; |
| 3454 | } |
| 3455 | |
| 3456 | let res = null; |
| 3457 | if (ordered) { |
| 3458 | await new Promise((resolve, reject) => { |
| 3459 | each(validations, (fn, cb) => fn(cb), error => { |
| 3460 | if (error) { |
| 3461 | return reject(error); |
| 3462 | } |
| 3463 | |
| 3464 | resolve(); |
| 3465 | }); |
| 3466 | }); |
| 3467 | |
| 3468 | try { |
| 3469 | res = await this.$__collection.bulkWrite(ops, options); |
| 3470 | } catch (error) { |
| 3471 | await this.hooks.execPost('bulkWrite', this, [null], { error, filter: postFilter }); |
| 3472 | } |
| 3473 | } else { |
| 3474 | let validOpIndexes = []; |
| 3475 | let validationErrors = []; |
| 3476 | const results = []; |
| 3477 | if (validations.length > 0) { |
| 3478 | validOpIndexes = await Promise.all(ops.map((op, i) => { |
nothing calls this directly
no test coverage detected
searching dependent graphs…