All files / src write_concern.ts

95.12% Statements 39/41
93.54% Branches 58/62
100% Functions 4/4
94.59% Lines 35/37

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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186  728x 728x                                                                             728x                                     728x                                                                       7669702x 7641715x 1998731x   5642984x     7669702x 2241874x   7669702x 5961x   7669702x 130x               1018329x   1018329x 1018329x 1018329x 1018329x 1018329x               37751596x 37254181x   37254181x   37254181x     37254181x     37254181x                 37254181x       37254181x               7667043x   29587138x         728x 13185569x   13185569x           13185569x 7640x        
import { type Document } from './bson';
import { MongoDBResponse } from './cmap/wire_protocol/responses';
import { MongoWriteConcernError } from './error';
 
/** @public */
export type W = number | 'majority';
 
/** @public */
export interface WriteConcernOptions {
  /** Write Concern as an object */
  writeConcern?: WriteConcern | WriteConcernSettings;
}
 
/** @public */
export interface WriteConcernSettings {
  /** The write concern */
  w?: W;
  /**
   * The write concern timeout.
   */
  wtimeoutMS?: number;
  /** The journal write concern */
  journal?: boolean;
 
  // legacy options
  /**
   * The journal write concern.
   * @deprecated Will be removed in the next major version. Please use the journal option.
   */
  j?: boolean;
  /**
   * The write concern timeout.
   */
  wtimeout?: number;
  /**
   * The file sync write concern.
   * @deprecated Will be removed in the next major version. Please use the journal option.
   */
  fsync?: boolean | 1;
}
 
export const WRITE_CONCERN_KEYS = ['w', 'wtimeout', 'j', 'journal', 'fsync'];
 
/** The write concern options that decorate the server command. */
interface CommandWriteConcernOptions {
  /** The write concern */
  w?: W;
  /** The journal write concern. */
  j?: boolean;
  /** The write concern timeout. */
  wtimeout?: number;
}
 
/**
 * A MongoDB WriteConcern, which describes the level of acknowledgement
 * requested from MongoDB for write operations.
 * @public
 *
 * @see https://www.mongodb.com/docs/manual/reference/write-concern/
 */
export class WriteConcern {
  /**
   * Request acknowledgment that the write operation has propagated to a specified number of mongod instances or to mongod instances with specified tags.
   * If w is 0 and is set on a write operation, the server will not send a response.
   */
  readonly w?: W;
  /** Request acknowledgment that the write operation has been written to the on-disk journal */
  readonly journal?: boolean;
  /**
   * Specify a time limit to prevent write operations from blocking indefinitely.
   */
  readonly wtimeoutMS?: number;
  /**
   * Specify a time limit to prevent write operations from blocking indefinitely.
   * @deprecated Will be removed in the next major version. Please use wtimeoutMS.
   */
  wtimeout?: number;
  /**
   * Request acknowledgment that the write operation has been written to the on-disk journal.
   * @deprecated Will be removed in the next major version. Please use journal.
   */
  j?: boolean;
  /**
   * Equivalent to the j option.
   * @deprecated Will be removed in the next major version. Please use journal.
   */
  fsync?: boolean | 1;
 
  /**
   * Constructs a WriteConcern from the write concern properties.
   * @param w - request acknowledgment that the write operation has propagated to a specified number of mongod instances or to mongod instances with specified tags.
   * @param wtimeoutMS - specify a time limit to prevent write operations from blocking indefinitely
   * @param journal - request acknowledgment that the write operation has been written to the on-disk journal
   * @param fsync - equivalent to the j option. Is deprecated and will be removed in the next major version.
   */
  constructor(w?: W, wtimeoutMS?: number, journal?: boolean, fsync?: boolean | 1) {
    if (w != null) {
      if (!Number.isNaN(Number(w))) {
        this.w = Number(w);
      } else {
        this.w = w;
      }
    }
    if (wtimeoutMS != null) {
      this.wtimeoutMS = this.wtimeout = wtimeoutMS;
    }
    if (journal != null) {
      this.journal = this.j = journal;
    }
    if (fsync != null) {
      this.journal = this.j = fsync ? true : false;
    }
  }
 
  /**
   * Apply a write concern to a command document. Will modify and return the command.
   */
  static apply(command: Document, writeConcern: WriteConcern): Document {
    const wc: CommandWriteConcernOptions = {};
    // The write concern document sent to the server has w/wtimeout/j fields.
    if (writeConcern.w != null) wc.w = writeConcern.w;
    if (writeConcern.wtimeoutMS != null) wc.wtimeout = writeConcern.wtimeoutMS;
    if (writeConcern.journal != null) wc.j = writeConcern.j;
    command.writeConcern = wc;
    return command;
  }
 
  /** Construct a WriteConcern given an options object. */
  static fromOptions(
    options?: WriteConcernOptions | WriteConcern | W,
    inherit?: WriteConcernOptions | WriteConcern
  ): WriteConcern | undefined {
    if (options == null) return undefined;
    inherit = inherit ?? {};
    let opts: WriteConcernSettings | WriteConcern | undefined;
    Iif (typeof options === 'string' || typeof options === 'number') {
      opts = { w: options };
    } else Iif (options instanceof WriteConcern) {
      opts = options;
    } else {
      opts = options.writeConcern;
    }
    const parentOpts: WriteConcern | WriteConcernSettings | undefined =
      inherit instanceof WriteConcern ? inherit : inherit.writeConcern;
 
    const {
      w = undefined,
      wtimeout = undefined,
      j = undefined,
      fsync = undefined,
      journal = undefined,
      wtimeoutMS = undefined
    } = {
      ...parentOpts,
      ...opts
    };
    if (
      w != null ||
      wtimeout != null ||
      wtimeoutMS != null ||
      j != null ||
      journal != null ||
      fsync != null
    ) {
      return new WriteConcern(w, wtimeout ?? wtimeoutMS, j ?? journal, fsync);
    }
    return undefined;
  }
}
 
/** Called with either a plain object or MongoDBResponse */
export function throwIfWriteConcernError(response: unknown): void {
  Eif (typeof response === 'object' && response != null) {
    const writeConcernError: object | null =
      MongoDBResponse.is(response) && response.has('writeConcernError')
        ? response.toObject()
        : !MongoDBResponse.is(response) && 'writeConcernError' in response
          ? response
          : null;
 
    if (writeConcernError != null) {
      throw new MongoWriteConcernError(writeConcernError as any);
    }
  }
}