operation.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. this[kSession] = options.session != null ? options.session : undefined;
  33. this.options = options;
  34. this.bypassPinningCheck = !!options.bypassPinningCheck;
  35. this.trySecondaryWrite = false;
  36. }
  37. hasAspect(aspect) {
  38. const ctor = this.constructor;
  39. if (ctor.aspects == null) {
  40. return false;
  41. }
  42. return ctor.aspects.has(aspect);
  43. }
  44. get session() {
  45. return this[kSession];
  46. }
  47. get canRetryRead() {
  48. return true;
  49. }
  50. get canRetryWrite() {
  51. return true;
  52. }
  53. }
  54. exports.AbstractOperation = AbstractOperation;
  55. function defineAspects(operation, aspects) {
  56. if (!Array.isArray(aspects) && !(aspects instanceof Set)) {
  57. aspects = [aspects];
  58. }
  59. aspects = new Set(aspects);
  60. Object.defineProperty(operation, 'aspects', {
  61. value: aspects,
  62. writable: false
  63. });
  64. return aspects;
  65. }
  66. exports.defineAspects = defineAspects;
  67. //# sourceMappingURL=operation.js.map