read_concern.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. }
  56. static get MAJORITY() {
  57. return exports.ReadConcernLevel.majority;
  58. }
  59. static get AVAILABLE() {
  60. return exports.ReadConcernLevel.available;
  61. }
  62. static get LINEARIZABLE() {
  63. return exports.ReadConcernLevel.linearizable;
  64. }
  65. static get SNAPSHOT() {
  66. return exports.ReadConcernLevel.snapshot;
  67. }
  68. toJSON() {
  69. return { level: this.level };
  70. }
  71. }
  72. exports.ReadConcern = ReadConcern;
  73. //# sourceMappingURL=read_concern.js.map