All files / src read_concern.ts

95.23% Statements 20/21
93.75% Branches 15/16
85.71% Functions 6/7
95.23% Lines 20/21

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      471x                                         471x                     38064x                       7328248x 98893x     7229355x 156141x 156141x 118624x 37517x 20509x 17008x 17008x       7073214x 539x   7072675x       31x       4x       4x               34584x      
import type { Document } from './bson';
 
/** @public */
export const ReadConcernLevel = Object.freeze({
  local: 'local',
  majority: 'majority',
  linearizable: 'linearizable',
  available: 'available',
  snapshot: 'snapshot'
} as const);
 
/** @public */
export type ReadConcernLevel = (typeof ReadConcernLevel)[keyof typeof ReadConcernLevel];
 
/** @public */
export type ReadConcernLike = ReadConcern | { level: ReadConcernLevel } | ReadConcernLevel;
 
/**
 * The MongoDB ReadConcern, which allows for control of the consistency and isolation properties
 * of the data read from replica sets and replica set shards.
 * @public
 *
 * @see https://www.mongodb.com/docs/manual/reference/read-concern/index.html
 */
export class ReadConcern {
  level: ReadConcernLevel | string;
 
  /** Constructs a ReadConcern from the read concern level.*/
  constructor(level: ReadConcernLevel) {
    /**
     * A spec test exists that allows level to be any string.
     * "invalid readConcern with out stage"
     * @see ./test/spec/crud/v2/aggregate-out-readConcern.json
     * @see https://github.com/mongodb/specifications/blob/master/source/read-write-concern/read-write-concern.md#unknown-levels-and-additional-options-for-string-based-readconcerns
     */
    this.level = ReadConcernLevel[level] ?? level;
  }
 
  /**
   * Construct a ReadConcern given an options object.
   *
   * @param options - The options object from which to extract the write concern.
   */
  static fromOptions(options?: {
    readConcern?: ReadConcernLike;
    level?: ReadConcernLevel;
  }): ReadConcern | undefined {
    if (options == null) {
      return;
    }
 
    if (options.readConcern) {
      const { readConcern } = options;
      if (readConcern instanceof ReadConcern) {
        return readConcern;
      } else if (typeof readConcern === 'string') {
        return new ReadConcern(readConcern);
      } else Eif ('level' in readConcern && readConcern.level) {
        return new ReadConcern(readConcern.level);
      }
    }
 
    if (options.level) {
      return new ReadConcern(options.level);
    }
    return;
  }
 
  static get MAJORITY(): 'majority' {
    return ReadConcernLevel.majority;
  }
 
  static get AVAILABLE(): 'available' {
    return ReadConcernLevel.available;
  }
 
  static get LINEARIZABLE(): 'linearizable' {
    return ReadConcernLevel.linearizable;
  }
 
  static get SNAPSHOT(): 'snapshot' {
    return ReadConcernLevel.snapshot;
  }
 
  toJSON(): Document {
    return { level: this.level };
  }
}