All files / src/cursor client_bulk_write_cursor.ts

84.21% Statements 16/19
66.66% Branches 2/3
80% Functions 4/5
83.33% Lines 15/18

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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84      392x     392x   392x 392x                               392x                         6613x   6613x 6613x               11088x 2138x       12462x                         6613x           6613x         4475x   4475x      
import { type Document } from '../bson';
import { type ClientBulkWriteCursorResponse } from '../cmap/wire_protocol/responses';
import type { MongoClient } from '../mongo_client';
import { ClientBulkWriteOperation } from '../operations/client_bulk_write/client_bulk_write';
import { type ClientBulkWriteCommandBuilder } from '../operations/client_bulk_write/command_builder';
import { type ClientBulkWriteOptions } from '../operations/client_bulk_write/common';
import { executeOperation } from '../operations/execute_operation';
import type { ClientSession } from '../sessions';
import { mergeOptions, MongoDBNamespace } from '../utils';
import {
  AbstractCursor,
  type AbstractCursorOptions,
  type InitialCursorResponse
} from './abstract_cursor';
 
/** @public */
export interface ClientBulkWriteCursorOptions
  extends Omit<AbstractCursorOptions, 'maxAwaitTimeMS' | 'tailable' | 'awaitData'>,
    ClientBulkWriteOptions {}
 
/**
 * This is the cursor that handles client bulk write operations. Note this is never
 * exposed directly to the user and is always immediately exhausted.
 * @internal
 */
export class ClientBulkWriteCursor extends AbstractCursor {
  commandBuilder: ClientBulkWriteCommandBuilder;
  /** @internal */
  private cursorResponse?: ClientBulkWriteCursorResponse;
  /** @internal */
  private clientBulkWriteOptions: ClientBulkWriteOptions;
 
  /** @internal */
  constructor(
    client: MongoClient,
    commandBuilder: ClientBulkWriteCommandBuilder,
    options: ClientBulkWriteCursorOptions = {}
  ) {
    super(client, new MongoDBNamespace('admin', '$cmd'), options);
 
    this.commandBuilder = commandBuilder;
    this.clientBulkWriteOptions = options;
  }
 
  /**
   * We need a way to get the top level cursor response fields for
   * generating the bulk write result, so we expose this here.
   */
  get response(): ClientBulkWriteCursorResponse | null {
    if (this.cursorResponse) return this.cursorResponse;
    return null;
  }
 
  get operations(): Document[] {
    return this.commandBuilder.lastOperations;
  }
 
  clone(): ClientBulkWriteCursor {
    const clonedOptions = mergeOptions({}, this.clientBulkWriteOptions);
    delete clonedOptions.session;
    return new ClientBulkWriteCursor(this.client, this.commandBuilder, {
      ...clonedOptions
    });
  }
 
  /** @internal */
  async _initialize(session: ClientSession): Promise<InitialCursorResponse> {
    const clientBulkWriteOperation = new ClientBulkWriteOperation(this.commandBuilder, {
      ...this.clientBulkWriteOptions,
      ...this.cursorOptions,
      session
    });
 
    const response = await executeOperation(
      this.client,
      clientBulkWriteOperation,
      this.timeoutContext
    );
    this.cursorResponse = response;
 
    return { server: clientBulkWriteOperation.server, session, response };
  }
}