write_concern.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. 'use strict';
  2. /**
  3. * The **WriteConcern** class is a class that represents a MongoDB WriteConcern.
  4. * @class
  5. * @property {(number|string)} w The write concern
  6. * @property {number} wtimeout The write concern timeout
  7. * @property {boolean} j The journal write concern
  8. * @property {boolean} fsync The file sync write concern
  9. * @see https://docs.mongodb.com/manual/reference/write-concern/index.html
  10. */
  11. class WriteConcern {
  12. /**
  13. * Constructs a WriteConcern from the write concern properties.
  14. * @param {(number|string)} [w] The write concern
  15. * @param {number} [wtimeout] The write concern timeout
  16. * @param {boolean} [j] The journal write concern
  17. * @param {boolean} [fsync] The file sync write concern
  18. */
  19. constructor(w, wtimeout, j, fsync) {
  20. if (w != null) {
  21. this.w = w;
  22. }
  23. if (wtimeout != null) {
  24. this.wtimeout = wtimeout;
  25. }
  26. if (j != null) {
  27. this.j = j;
  28. }
  29. if (fsync != null) {
  30. this.fsync = fsync;
  31. }
  32. }
  33. /**
  34. * Construct a WriteConcern given an options object.
  35. *
  36. * @param {object} options The options object from which to extract the write concern.
  37. * @return {WriteConcern}
  38. */
  39. static fromOptions(options) {
  40. if (
  41. options == null ||
  42. (options.writeConcern == null &&
  43. options.w == null &&
  44. options.wtimeout == null &&
  45. options.j == null &&
  46. options.fsync == null)
  47. ) {
  48. return;
  49. }
  50. if (options.writeConcern) {
  51. return new WriteConcern(
  52. options.writeConcern.w,
  53. options.writeConcern.wtimeout,
  54. options.writeConcern.j,
  55. options.writeConcern.fsync
  56. );
  57. }
  58. return new WriteConcern(options.w, options.wtimeout, options.j, options.fsync);
  59. }
  60. }
  61. module.exports = WriteConcern;