write_concern.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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://docs.mongodb.com/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 wtimeout - specify a time limit to prevent write operations from blocking indefinitely
  17. * @param j - request acknowledgment that the write operation has been written to the on-disk journal
  18. * @param fsync - equivalent to the j option
  19. */
  20. constructor(w, wtimeout, j, 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 (wtimeout != null) {
  30. this.wtimeout = wtimeout;
  31. }
  32. if (j != null) {
  33. this.j = j;
  34. }
  35. if (fsync != null) {
  36. this.fsync = fsync;
  37. }
  38. }
  39. /** Construct a WriteConcern given an options object. */
  40. static fromOptions(options, inherit) {
  41. if (options == null)
  42. return undefined;
  43. inherit = inherit !== null && inherit !== void 0 ? inherit : {};
  44. let opts;
  45. if (typeof options === 'string' || typeof options === 'number') {
  46. opts = { w: options };
  47. }
  48. else if (options instanceof WriteConcern) {
  49. opts = options;
  50. }
  51. else {
  52. opts = options.writeConcern;
  53. }
  54. const parentOpts = inherit instanceof WriteConcern ? inherit : inherit.writeConcern;
  55. const { w = undefined, wtimeout = undefined, j = undefined, fsync = undefined, journal = undefined, wtimeoutMS = undefined } = {
  56. ...parentOpts,
  57. ...opts
  58. };
  59. if (w != null ||
  60. wtimeout != null ||
  61. wtimeoutMS != null ||
  62. j != null ||
  63. journal != null ||
  64. fsync != null) {
  65. return new WriteConcern(w, wtimeout !== null && wtimeout !== void 0 ? wtimeout : wtimeoutMS, j !== null && j !== void 0 ? j : journal, fsync);
  66. }
  67. return undefined;
  68. }
  69. }
  70. exports.WriteConcern = WriteConcern;
  71. //# sourceMappingURL=write_concern.js.map