write_concern.js 3.9 KB

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