write_concern.js 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.WriteConcern = exports.WRITE_CONCERN_KEYS = void 0;
  4. exports.WRITE_CONCERN_KEYS = ['w', 'wtimeout', 'j', 'journal', 'fsync'];
  5. /**
  6. * A MongoDB WriteConcern, which describes the level of acknowledgement
  7. * requested from MongoDB for write operations.
  8. * @public
  9. *
  10. * @see https://www.mongodb.com/docs/manual/reference/write-concern/
  11. */
  12. class WriteConcern {
  13. /**
  14. * Constructs a WriteConcern from the write concern properties.
  15. * @param w - request acknowledgment that the write operation has propagated to a specified number of mongod instances or to mongod instances with specified tags.
  16. * @param wtimeoutMS - specify a time limit to prevent write operations from blocking indefinitely
  17. * @param journal - request acknowledgment that the write operation has been written to the on-disk journal
  18. * @param fsync - equivalent to the j option. Is deprecated and will be removed in the next major version.
  19. */
  20. constructor(w, wtimeoutMS, journal, fsync) {
  21. if (w != null) {
  22. if (!Number.isNaN(Number(w))) {
  23. this.w = Number(w);
  24. }
  25. else {
  26. this.w = w;
  27. }
  28. }
  29. if (wtimeoutMS != null) {
  30. this.wtimeoutMS = this.wtimeout = wtimeoutMS;
  31. }
  32. if (journal != null) {
  33. this.journal = this.j = journal;
  34. }
  35. if (fsync != null) {
  36. this.journal = this.j = fsync ? true : false;
  37. }
  38. }
  39. /**
  40. * Apply a write concern to a command document. Will modify and return the command.
  41. */
  42. static apply(command, writeConcern) {
  43. const wc = {};
  44. // The write concern document sent to the server has w/wtimeout/j fields.
  45. if (writeConcern.w != null)
  46. wc.w = writeConcern.w;
  47. if (writeConcern.wtimeoutMS != null)
  48. wc.wtimeout = writeConcern.wtimeoutMS;
  49. if (writeConcern.journal != null)
  50. wc.j = writeConcern.j;
  51. command.writeConcern = wc;
  52. return command;
  53. }
  54. /** Construct a WriteConcern given an options object. */
  55. static fromOptions(options, inherit) {
  56. if (options == null)
  57. return undefined;
  58. inherit = inherit ?? {};
  59. let opts;
  60. if (typeof options === 'string' || typeof options === 'number') {
  61. opts = { w: options };
  62. }
  63. else if (options instanceof WriteConcern) {
  64. opts = options;
  65. }
  66. else {
  67. opts = options.writeConcern;
  68. }
  69. const parentOpts = inherit instanceof WriteConcern ? inherit : inherit.writeConcern;
  70. const { w = undefined, wtimeout = undefined, j = undefined, fsync = undefined, journal = undefined, wtimeoutMS = undefined } = {
  71. ...parentOpts,
  72. ...opts
  73. };
  74. if (w != null ||
  75. wtimeout != null ||
  76. wtimeoutMS != null ||
  77. j != null ||
  78. journal != null ||
  79. fsync != null) {
  80. return new WriteConcern(w, wtimeout ?? wtimeoutMS, j ?? journal, fsync);
  81. }
  82. return undefined;
  83. }
  84. }
  85. exports.WriteConcern = WriteConcern;
  86. //# sourceMappingURL=write_concern.js.map