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://www.mongodb.com/docs/manual/reference/read-concern/index.html
  18. */
  19. class ReadConcern {
  20. /** Constructs a ReadConcern from the read concern level.*/
  21. constructor(level) {
  22. /**
  23. * A spec test exists that allows level to be any string.
  24. * "invalid readConcern with out stage"
  25. * @see ./test/spec/crud/v2/aggregate-out-readConcern.json
  26. * @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
  27. */
  28. this.level = exports.ReadConcernLevel[level] ?? level;
  29. }
  30. /**
  31. * Construct a ReadConcern given an options object.
  32. *
  33. * @param options - The options object from which to extract the write concern.
  34. */
  35. static fromOptions(options) {
  36. if (options == null) {
  37. return;
  38. }
  39. if (options.readConcern) {
  40. const { readConcern } = options;
  41. if (readConcern instanceof ReadConcern) {
  42. return readConcern;
  43. }
  44. else if (typeof readConcern === 'string') {
  45. return new ReadConcern(readConcern);
  46. }
  47. else if ('level' in readConcern && readConcern.level) {
  48. return new ReadConcern(readConcern.level);
  49. }
  50. }
  51. if (options.level) {
  52. return new ReadConcern(options.level);
  53. }
  54. return;
  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