read_concern.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. 'use strict';
  2. /**
  3. * The **ReadConcern** class is a class that represents a MongoDB ReadConcern.
  4. * @class
  5. * @property {string} level The read concern level
  6. * @see https://docs.mongodb.com/manual/reference/read-concern/index.html
  7. */
  8. class ReadConcern {
  9. /**
  10. * Constructs a ReadConcern from the read concern properties.
  11. * @param {string} [level] The read concern level ({'local'|'available'|'majority'|'linearizable'|'snapshot'})
  12. */
  13. constructor(level) {
  14. if (level != null) {
  15. this.level = level;
  16. }
  17. }
  18. /**
  19. * Construct a ReadConcern given an options object.
  20. *
  21. * @param {object} options The options object from which to extract the write concern.
  22. * @return {ReadConcern}
  23. */
  24. static fromOptions(options) {
  25. if (options == null) {
  26. return;
  27. }
  28. if (options.readConcern) {
  29. if (options.readConcern instanceof ReadConcern) {
  30. return options.readConcern;
  31. }
  32. return new ReadConcern(options.readConcern.level);
  33. }
  34. if (options.level) {
  35. return new ReadConcern(options.level);
  36. }
  37. }
  38. static get MAJORITY() {
  39. return 'majority';
  40. }
  41. static get AVAILABLE() {
  42. return 'available';
  43. }
  44. static get LINEARIZABLE() {
  45. return 'linearizable';
  46. }
  47. static get SNAPSHOT() {
  48. return 'snapshot';
  49. }
  50. }
  51. module.exports = ReadConcern;