operation.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.defineAspects = exports.AbstractOperation = exports.Aspect = void 0;
  4. const bson_1 = require("../bson");
  5. const read_preference_1 = require("../read_preference");
  6. exports.Aspect = {
  7. READ_OPERATION: Symbol('READ_OPERATION'),
  8. WRITE_OPERATION: Symbol('WRITE_OPERATION'),
  9. RETRYABLE: Symbol('RETRYABLE'),
  10. EXPLAINABLE: Symbol('EXPLAINABLE'),
  11. SKIP_COLLATION: Symbol('SKIP_COLLATION'),
  12. CURSOR_CREATING: Symbol('CURSOR_CREATING'),
  13. MUST_SELECT_SAME_SERVER: Symbol('MUST_SELECT_SAME_SERVER')
  14. };
  15. /** @internal */
  16. const kSession = Symbol('session');
  17. /**
  18. * This class acts as a parent class for any operation and is responsible for setting this.options,
  19. * as well as setting and getting a session.
  20. * Additionally, this class implements `hasAspect`, which determines whether an operation has
  21. * a specific aspect.
  22. * @internal
  23. */
  24. class AbstractOperation {
  25. constructor(options = {}) {
  26. this.readPreference = this.hasAspect(exports.Aspect.WRITE_OPERATION)
  27. ? read_preference_1.ReadPreference.primary
  28. : read_preference_1.ReadPreference.fromOptions(options) ?? read_preference_1.ReadPreference.primary;
  29. // Pull the BSON serialize options from the already-resolved options
  30. this.bsonOptions = (0, bson_1.resolveBSONOptions)(options);
  31. this[kSession] = options.session != null ? options.session : undefined;
  32. this.options = options;
  33. this.bypassPinningCheck = !!options.bypassPinningCheck;
  34. this.trySecondaryWrite = false;
  35. }
  36. hasAspect(aspect) {
  37. const ctor = this.constructor;
  38. if (ctor.aspects == null) {
  39. return false;
  40. }
  41. return ctor.aspects.has(aspect);
  42. }
  43. get session() {
  44. return this[kSession];
  45. }
  46. clearSession() {
  47. this[kSession] = undefined;
  48. }
  49. get canRetryRead() {
  50. return true;
  51. }
  52. get canRetryWrite() {
  53. return true;
  54. }
  55. }
  56. exports.AbstractOperation = AbstractOperation;
  57. function defineAspects(operation, aspects) {
  58. if (!Array.isArray(aspects) && !(aspects instanceof Set)) {
  59. aspects = [aspects];
  60. }
  61. aspects = new Set(aspects);
  62. Object.defineProperty(operation, 'aspects', {
  63. value: aspects,
  64. writable: false
  65. });
  66. return aspects;
  67. }
  68. exports.defineAspects = defineAspects;
  69. //# sourceMappingURL=operation.js.map