read_preference.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.ReadPreference = exports.ReadPreferenceMode = void 0;
  4. const error_1 = require("./error");
  5. /** @public */
  6. exports.ReadPreferenceMode = Object.freeze({
  7. primary: 'primary',
  8. primaryPreferred: 'primaryPreferred',
  9. secondary: 'secondary',
  10. secondaryPreferred: 'secondaryPreferred',
  11. nearest: 'nearest'
  12. });
  13. /**
  14. * The **ReadPreference** class is a class that represents a MongoDB ReadPreference and is
  15. * used to construct connections.
  16. * @public
  17. *
  18. * @see https://docs.mongodb.com/manual/core/read-preference/
  19. */
  20. class ReadPreference {
  21. /**
  22. * @param mode - A string describing the read preference mode (primary|primaryPreferred|secondary|secondaryPreferred|nearest)
  23. * @param tags - A tag set used to target reads to members with the specified tag(s). tagSet is not available if using read preference mode primary.
  24. * @param options - Additional read preference options
  25. */
  26. constructor(mode, tags, options) {
  27. if (!ReadPreference.isValid(mode)) {
  28. throw new error_1.MongoInvalidArgumentError(`Invalid read preference mode ${JSON.stringify(mode)}`);
  29. }
  30. if (options == null && typeof tags === 'object' && !Array.isArray(tags)) {
  31. options = tags;
  32. tags = undefined;
  33. }
  34. else if (tags && !Array.isArray(tags)) {
  35. throw new error_1.MongoInvalidArgumentError('ReadPreference tags must be an array');
  36. }
  37. this.mode = mode;
  38. this.tags = tags;
  39. this.hedge = options === null || options === void 0 ? void 0 : options.hedge;
  40. this.maxStalenessSeconds = undefined;
  41. this.minWireVersion = undefined;
  42. options = options !== null && options !== void 0 ? options : {};
  43. if (options.maxStalenessSeconds != null) {
  44. if (options.maxStalenessSeconds <= 0) {
  45. throw new error_1.MongoInvalidArgumentError('maxStalenessSeconds must be a positive integer');
  46. }
  47. this.maxStalenessSeconds = options.maxStalenessSeconds;
  48. // NOTE: The minimum required wire version is 5 for this read preference. If the existing
  49. // topology has a lower value then a MongoError will be thrown during server selection.
  50. this.minWireVersion = 5;
  51. }
  52. if (this.mode === ReadPreference.PRIMARY) {
  53. if (this.tags && Array.isArray(this.tags) && this.tags.length > 0) {
  54. throw new error_1.MongoInvalidArgumentError('Primary read preference cannot be combined with tags');
  55. }
  56. if (this.maxStalenessSeconds) {
  57. throw new error_1.MongoInvalidArgumentError('Primary read preference cannot be combined with maxStalenessSeconds');
  58. }
  59. if (this.hedge) {
  60. throw new error_1.MongoInvalidArgumentError('Primary read preference cannot be combined with hedge');
  61. }
  62. }
  63. }
  64. // Support the deprecated `preference` property introduced in the porcelain layer
  65. get preference() {
  66. return this.mode;
  67. }
  68. static fromString(mode) {
  69. return new ReadPreference(mode);
  70. }
  71. /**
  72. * Construct a ReadPreference given an options object.
  73. *
  74. * @param options - The options object from which to extract the read preference.
  75. */
  76. static fromOptions(options) {
  77. var _a, _b, _c;
  78. if (!options)
  79. return;
  80. const readPreference = (_a = options.readPreference) !== null && _a !== void 0 ? _a : (_b = options.session) === null || _b === void 0 ? void 0 : _b.transaction.options.readPreference;
  81. const readPreferenceTags = options.readPreferenceTags;
  82. if (readPreference == null) {
  83. return;
  84. }
  85. if (typeof readPreference === 'string') {
  86. return new ReadPreference(readPreference, readPreferenceTags, {
  87. maxStalenessSeconds: options.maxStalenessSeconds,
  88. hedge: options.hedge
  89. });
  90. }
  91. else if (!(readPreference instanceof ReadPreference) && typeof readPreference === 'object') {
  92. const mode = readPreference.mode || readPreference.preference;
  93. if (mode && typeof mode === 'string') {
  94. return new ReadPreference(mode, (_c = readPreference.tags) !== null && _c !== void 0 ? _c : readPreferenceTags, {
  95. maxStalenessSeconds: readPreference.maxStalenessSeconds,
  96. hedge: options.hedge
  97. });
  98. }
  99. }
  100. if (readPreferenceTags) {
  101. readPreference.tags = readPreferenceTags;
  102. }
  103. return readPreference;
  104. }
  105. /**
  106. * Replaces options.readPreference with a ReadPreference instance
  107. */
  108. static translate(options) {
  109. if (options.readPreference == null)
  110. return options;
  111. const r = options.readPreference;
  112. if (typeof r === 'string') {
  113. options.readPreference = new ReadPreference(r);
  114. }
  115. else if (r && !(r instanceof ReadPreference) && typeof r === 'object') {
  116. const mode = r.mode || r.preference;
  117. if (mode && typeof mode === 'string') {
  118. options.readPreference = new ReadPreference(mode, r.tags, {
  119. maxStalenessSeconds: r.maxStalenessSeconds
  120. });
  121. }
  122. }
  123. else if (!(r instanceof ReadPreference)) {
  124. throw new error_1.MongoInvalidArgumentError(`Invalid read preference: ${r}`);
  125. }
  126. return options;
  127. }
  128. /**
  129. * Validate if a mode is legal
  130. *
  131. * @param mode - The string representing the read preference mode.
  132. */
  133. static isValid(mode) {
  134. const VALID_MODES = new Set([
  135. ReadPreference.PRIMARY,
  136. ReadPreference.PRIMARY_PREFERRED,
  137. ReadPreference.SECONDARY,
  138. ReadPreference.SECONDARY_PREFERRED,
  139. ReadPreference.NEAREST,
  140. null
  141. ]);
  142. return VALID_MODES.has(mode);
  143. }
  144. /**
  145. * Validate if a mode is legal
  146. *
  147. * @param mode - The string representing the read preference mode.
  148. */
  149. isValid(mode) {
  150. return ReadPreference.isValid(typeof mode === 'string' ? mode : this.mode);
  151. }
  152. /**
  153. * Indicates that this readPreference needs the "secondaryOk" bit when sent over the wire
  154. * @deprecated Use secondaryOk instead
  155. * @see https://docs.mongodb.com/manual/reference/mongodb-wire-protocol/#op-query
  156. */
  157. slaveOk() {
  158. return this.secondaryOk();
  159. }
  160. /**
  161. * Indicates that this readPreference needs the "SecondaryOk" bit when sent over the wire
  162. * @see https://docs.mongodb.com/manual/reference/mongodb-wire-protocol/#op-query
  163. */
  164. secondaryOk() {
  165. const NEEDS_SECONDARYOK = new Set([
  166. ReadPreference.PRIMARY_PREFERRED,
  167. ReadPreference.SECONDARY,
  168. ReadPreference.SECONDARY_PREFERRED,
  169. ReadPreference.NEAREST
  170. ]);
  171. return NEEDS_SECONDARYOK.has(this.mode);
  172. }
  173. /**
  174. * Check if the two ReadPreferences are equivalent
  175. *
  176. * @param readPreference - The read preference with which to check equality
  177. */
  178. equals(readPreference) {
  179. return readPreference.mode === this.mode;
  180. }
  181. /** Return JSON representation */
  182. toJSON() {
  183. const readPreference = { mode: this.mode };
  184. if (Array.isArray(this.tags))
  185. readPreference.tags = this.tags;
  186. if (this.maxStalenessSeconds)
  187. readPreference.maxStalenessSeconds = this.maxStalenessSeconds;
  188. if (this.hedge)
  189. readPreference.hedge = this.hedge;
  190. return readPreference;
  191. }
  192. }
  193. exports.ReadPreference = ReadPreference;
  194. ReadPreference.PRIMARY = exports.ReadPreferenceMode.primary;
  195. ReadPreference.PRIMARY_PREFERRED = exports.ReadPreferenceMode.primaryPreferred;
  196. ReadPreference.SECONDARY = exports.ReadPreferenceMode.secondary;
  197. ReadPreference.SECONDARY_PREFERRED = exports.ReadPreferenceMode.secondaryPreferred;
  198. ReadPreference.NEAREST = exports.ReadPreferenceMode.nearest;
  199. ReadPreference.primary = new ReadPreference(exports.ReadPreferenceMode.primary);
  200. ReadPreference.primaryPreferred = new ReadPreference(exports.ReadPreferenceMode.primaryPreferred);
  201. ReadPreference.secondary = new ReadPreference(exports.ReadPreferenceMode.secondary);
  202. ReadPreference.secondaryPreferred = new ReadPreference(exports.ReadPreferenceMode.secondaryPreferred);
  203. ReadPreference.nearest = new ReadPreference(exports.ReadPreferenceMode.nearest);
  204. //# sourceMappingURL=read_preference.js.map