write_concern.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. 'use strict';
  2. const kWriteConcernKeys = new Set(['w', 'wtimeout', 'j', 'journal', 'fsync']);
  3. let utils;
  4. /**
  5. * The **WriteConcern** class is a class that represents a MongoDB WriteConcern.
  6. * @class
  7. * @property {(number|string)} w The write concern
  8. * @property {number} wtimeout The write concern timeout
  9. * @property {boolean} j The journal write concern
  10. * @property {boolean} fsync The file sync write concern
  11. * @see https://docs.mongodb.com/manual/reference/write-concern/index.html
  12. */
  13. class WriteConcern {
  14. /**
  15. * Constructs a WriteConcern from the write concern properties.
  16. * @param {(number|string)} [w] The write concern
  17. * @param {number} [wtimeout] The write concern timeout
  18. * @param {boolean} [j] The journal write concern
  19. * @param {boolean} [fsync] The file sync write concern
  20. */
  21. constructor(w, wtimeout, j, fsync) {
  22. if (w != null) {
  23. this.w = w;
  24. }
  25. if (wtimeout != null) {
  26. this.wtimeout = wtimeout;
  27. }
  28. if (j != null) {
  29. this.j = j;
  30. }
  31. if (fsync != null) {
  32. this.fsync = fsync;
  33. }
  34. }
  35. /**
  36. * Construct a WriteConcern given an options object.
  37. *
  38. * @param {object} [options] The options object from which to extract the write concern.
  39. * @param {(number|string)} [options.w] **Deprecated** Use `options.writeConcern` instead
  40. * @param {number} [options.wtimeout] **Deprecated** Use `options.writeConcern` instead
  41. * @param {boolean} [options.j] **Deprecated** Use `options.writeConcern` instead
  42. * @param {boolean} [options.fsync] **Deprecated** Use `options.writeConcern` instead
  43. * @param {object|WriteConcern} [options.writeConcern] Specify write concern settings.
  44. * @return {WriteConcern}
  45. */
  46. static fromOptions(options) {
  47. if (
  48. options == null ||
  49. (options.writeConcern == null &&
  50. options.w == null &&
  51. options.wtimeout == null &&
  52. options.j == null &&
  53. options.journal == null &&
  54. options.fsync == null)
  55. ) {
  56. return;
  57. }
  58. if (options.writeConcern) {
  59. if (typeof options.writeConcern === 'string') {
  60. return new WriteConcern(options.writeConcern);
  61. }
  62. if (!Object.keys(options.writeConcern).some(key => kWriteConcernKeys.has(key))) {
  63. return;
  64. }
  65. return new WriteConcern(
  66. options.writeConcern.w,
  67. options.writeConcern.wtimeout,
  68. options.writeConcern.j || options.writeConcern.journal,
  69. options.writeConcern.fsync
  70. );
  71. }
  72. // this is down here to prevent circular dependency
  73. if (!utils) utils = require('./utils');
  74. utils.emitWarningOnce(
  75. `Top-level use of w, wtimeout, j, and fsync is deprecated. Use writeConcern instead.`
  76. );
  77. return new WriteConcern(
  78. options.w,
  79. options.wtimeout,
  80. options.j || options.journal,
  81. options.fsync
  82. );
  83. }
  84. }
  85. module.exports = WriteConcern;