read_concern.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.ReadConcern = exports.ReadConcernLevel = void 0;
  4. /** @public */
  5. exports.ReadConcernLevel = Object.freeze({
  6. local: 'local',
  7. majority: 'majority',
  8. linearizable: 'linearizable',
  9. available: 'available',
  10. snapshot: 'snapshot'
  11. });
  12. /**
  13. * The MongoDB ReadConcern, which allows for control of the consistency and isolation properties
  14. * of the data read from replica sets and replica set shards.
  15. * @public
  16. *
  17. * @see https://docs.mongodb.com/manual/reference/read-concern/index.html
  18. */
  19. class ReadConcern {
  20. /** Constructs a ReadConcern from the read concern level.*/
  21. constructor(level) {
  22. var _a;
  23. /**
  24. * A spec test exists that allows level to be any string.
  25. * "invalid readConcern with out stage"
  26. * @see ./test/spec/crud/v2/aggregate-out-readConcern.json
  27. * @see https://github.com/mongodb/specifications/blob/master/source/read-write-concern/read-write-concern.rst#unknown-levels-and-additional-options-for-string-based-readconcerns
  28. */
  29. this.level = (_a = exports.ReadConcernLevel[level]) !== null && _a !== void 0 ? _a : level;
  30. }
  31. /**
  32. * Construct a ReadConcern given an options object.
  33. *
  34. * @param options - The options object from which to extract the write concern.
  35. */
  36. static fromOptions(options) {
  37. if (options == null) {
  38. return;
  39. }
  40. if (options.readConcern) {
  41. const { readConcern } = options;
  42. if (readConcern instanceof ReadConcern) {
  43. return readConcern;
  44. }
  45. else if (typeof readConcern === 'string') {
  46. return new ReadConcern(readConcern);
  47. }
  48. else if ('level' in readConcern && readConcern.level) {
  49. return new ReadConcern(readConcern.level);
  50. }
  51. }
  52. if (options.level) {
  53. return new ReadConcern(options.level);
  54. }
  55. return;
  56. }
  57. static get MAJORITY() {
  58. return exports.ReadConcernLevel.majority;
  59. }
  60. static get AVAILABLE() {
  61. return exports.ReadConcernLevel.available;
  62. }
  63. static get LINEARIZABLE() {
  64. return exports.ReadConcernLevel.linearizable;
  65. }
  66. static get SNAPSHOT() {
  67. return exports.ReadConcernLevel.snapshot;
  68. }
  69. toJSON() {
  70. return { level: this.level };
  71. }
  72. }
  73. exports.ReadConcern = ReadConcern;
  74. //# sourceMappingURL=read_concern.js.map