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. CURSOR_ITERATING: Symbol('CURSOR_ITERATING')
  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. var _a;
  27. this.readPreference = this.hasAspect(exports.Aspect.WRITE_OPERATION)
  28. ? read_preference_1.ReadPreference.primary
  29. : (_a = read_preference_1.ReadPreference.fromOptions(options)) !== null && _a !== void 0 ? _a : read_preference_1.ReadPreference.primary;
  30. // Pull the BSON serialize options from the already-resolved options
  31. this.bsonOptions = (0, bson_1.resolveBSONOptions)(options);
  32. if (options.session) {
  33. this[kSession] = options.session;
  34. }
  35. this.options = options;
  36. this.bypassPinningCheck = !!options.bypassPinningCheck;
  37. this.trySecondaryWrite = false;
  38. }
  39. hasAspect(aspect) {
  40. const ctor = this.constructor;
  41. if (ctor.aspects == null) {
  42. return false;
  43. }
  44. return ctor.aspects.has(aspect);
  45. }
  46. get session() {
  47. return this[kSession];
  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