operation.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.AbstractOperation = exports.Aspect = void 0;
  4. exports.defineAspects = defineAspects;
  5. const bson_1 = require("../bson");
  6. const read_preference_1 = require("../read_preference");
  7. exports.Aspect = {
  8. READ_OPERATION: Symbol('READ_OPERATION'),
  9. WRITE_OPERATION: Symbol('WRITE_OPERATION'),
  10. RETRYABLE: Symbol('RETRYABLE'),
  11. EXPLAINABLE: Symbol('EXPLAINABLE'),
  12. SKIP_COLLATION: Symbol('SKIP_COLLATION'),
  13. CURSOR_CREATING: Symbol('CURSOR_CREATING'),
  14. MUST_SELECT_SAME_SERVER: Symbol('MUST_SELECT_SAME_SERVER'),
  15. COMMAND_BATCHING: Symbol('COMMAND_BATCHING'),
  16. SUPPORTS_RAW_DATA: Symbol('SUPPORTS_RAW_DATA')
  17. };
  18. /**
  19. * This class acts as a parent class for any operation and is responsible for setting this.options,
  20. * as well as setting and getting a session.
  21. * Additionally, this class implements `hasAspect`, which determines whether an operation has
  22. * a specific aspect.
  23. * @internal
  24. */
  25. class AbstractOperation {
  26. constructor(options = {}) {
  27. this.readPreference = this.hasAspect(exports.Aspect.WRITE_OPERATION)
  28. ? read_preference_1.ReadPreference.primary
  29. : (read_preference_1.ReadPreference.fromOptions(options) ?? 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._session = options.session != null ? options.session : undefined;
  33. this.options = options;
  34. this.bypassPinningCheck = !!options.bypassPinningCheck;
  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. // Make sure the session is not writable from outside this class.
  44. get session() {
  45. return this._session;
  46. }
  47. set session(session) {
  48. this._session = session;
  49. }
  50. clearSession() {
  51. this._session = undefined;
  52. }
  53. resetBatch() {
  54. return true;
  55. }
  56. get canRetryRead() {
  57. return this.hasAspect(exports.Aspect.RETRYABLE) && this.hasAspect(exports.Aspect.READ_OPERATION);
  58. }
  59. get canRetryWrite() {
  60. return this.hasAspect(exports.Aspect.RETRYABLE) && this.hasAspect(exports.Aspect.WRITE_OPERATION);
  61. }
  62. /**
  63. * Given an instance of a MongoDBResponse, map the response to the correct result type. For
  64. * example, a `CountOperation` might map the response as follows:
  65. *
  66. * ```typescript
  67. * override handleOk(response: InstanceType<typeof this.SERVER_COMMAND_RESPONSE_TYPE>): TResult {
  68. * return response.toObject(this.bsonOptions).n ?? 0;
  69. * }
  70. *
  71. * // or, with type safety:
  72. * override handleOk(response: InstanceType<typeof this.SERVER_COMMAND_RESPONSE_TYPE>): TResult {
  73. * return response.getNumber('n') ?? 0;
  74. * }
  75. * ```
  76. */
  77. handleOk(response) {
  78. return response.toObject(this.bsonOptions);
  79. }
  80. /**
  81. * Optional.
  82. *
  83. * If the operation performs error handling, such as wrapping, renaming the error, or squashing errors
  84. * this method can be overridden.
  85. */
  86. handleError(error) {
  87. throw error;
  88. }
  89. }
  90. exports.AbstractOperation = AbstractOperation;
  91. function defineAspects(operation, aspects) {
  92. if (!Array.isArray(aspects) && !(aspects instanceof Set)) {
  93. aspects = [aspects];
  94. }
  95. aspects = new Set(aspects);
  96. Object.defineProperty(operation, 'aspects', {
  97. value: aspects,
  98. writable: false
  99. });
  100. return aspects;
  101. }
  102. //# sourceMappingURL=operation.js.map