All files / src/operations bulk_write.ts

100% Statements 16/16
100% Branches 2/2
100% Functions 3/3
100% Lines 15/15

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65                    412x     412x                   410455x 410455x 410455x 410455x       29194x               410226x 410226x 410226x                 410226x         410226x 28166622x       409081x       412x  
import type {
  AnyBulkWriteOperation,
  BulkOperationBase,
  BulkWriteOptions,
  BulkWriteResult
} from '../bulk/common';
import type { Collection } from '../collection';
import type { Server } from '../sdam/server';
import type { ClientSession } from '../sessions';
import { type TimeoutContext } from '../timeout';
import { AbstractOperation, Aspect, defineAspects } from './operation';
 
/** @internal */
export class BulkWriteOperation extends AbstractOperation<BulkWriteResult> {
  override options: BulkWriteOptions;
  collection: Collection;
  operations: ReadonlyArray<AnyBulkWriteOperation>;
 
  constructor(
    collection: Collection,
    operations: ReadonlyArray<AnyBulkWriteOperation>,
    options: BulkWriteOptions
  ) {
    super(options);
    this.options = options;
    this.collection = collection;
    this.operations = operations;
  }
 
  override get commandName() {
    return 'bulkWrite' as const;
  }
 
  override async execute(
    server: Server,
    session: ClientSession | undefined,
    timeoutContext: TimeoutContext
  ): Promise<BulkWriteResult> {
    const coll = this.collection;
    const operations = this.operations;
    const options = {
      ...this.options,
      ...this.bsonOptions,
      readPreference: this.readPreference,
      timeoutContext
    };
 
    // Create the bulk operation
    const bulk: BulkOperationBase =
      options.ordered === false
        ? coll.initializeUnorderedBulkOp(options)
        : coll.initializeOrderedBulkOp(options);
 
    // for each op go through and add to the bulk
    for (let i = 0; i < operations.length; i++) {
      bulk.raw(operations[i]);
    }
 
    // Execute the bulk
    return await bulk.execute({ ...options, session });
  }
}
 
defineAspects(BulkWriteOperation, [Aspect.WRITE_OPERATION]);