Source: lib/read_preference.js

  1. "use strict";
  2. /**
  3. * @fileOverview The **ReadPreference** class is a class that represents a MongoDB ReadPreference and is
  4. * used to construct connections.
  5. *
  6. * @example
  7. * var Db = require('mongodb').Db,
  8. * ReplSet = require('mongodb').ReplSet,
  9. * Server = require('mongodb').Server,
  10. * ReadPreference = require('mongodb').ReadPreference,
  11. * test = require('assert');
  12. * // Connect using ReplSet
  13. * var server = new Server('localhost', 27017);
  14. * var db = new Db('test', new ReplSet([server]));
  15. * db.open(function(err, db) {
  16. * test.equal(null, err);
  17. * // Perform a read
  18. * var cursor = db.collection('t').find({});
  19. * cursor.setReadPreference(ReadPreference.PRIMARY);
  20. * cursor.toArray(function(err, docs) {
  21. * test.equal(null, err);
  22. * db.close();
  23. * });
  24. * });
  25. */
  26. /**
  27. * Creates a new ReadPreference instance
  28. *
  29. * Read Preferences
  30. * - **ReadPreference.PRIMARY**, Read from primary only. All operations produce an error (throw an exception where applicable) if primary is unavailable. Cannot be combined with tags (This is the default.).
  31. * - **ReadPreference.PRIMARY_PREFERRED**, Read from primary if available, otherwise a secondary.
  32. * - **ReadPreference.SECONDARY**, Read from secondary if available, otherwise error.
  33. * - **ReadPreference.SECONDARY_PREFERRED**, Read from a secondary if available, otherwise read from the primary.
  34. * - **ReadPreference.NEAREST**, All modes read from among the nearest candidates, but unlike other modes, NEAREST will include both the primary and all secondaries in the random selection.
  35. *
  36. * @class
  37. * @param {string} mode The ReadPreference mode as listed above.
  38. * @param {array|object} tags An object representing read preference tags.
  39. * @param {object} [options] Additional read preference options
  40. * @param {number} [options.maxStalenessSeconds] Max Secondary Read Staleness in Seconds
  41. * @return {ReadPreference} a ReadPreference instance.
  42. */
  43. var ReadPreference = function(mode, tags, options) {
  44. if(!(this instanceof ReadPreference)) {
  45. return new ReadPreference(mode, tags, options);
  46. }
  47. this._type = 'ReadPreference';
  48. this.mode = mode;
  49. this.tags = tags;
  50. this.options = options;
  51. // If no tags were passed in
  52. if(tags && typeof tags == 'object' && !Array.isArray(tags)) {
  53. if(tags.maxStalenessSeconds) {
  54. this.options = tags;
  55. this.tags = null;
  56. }
  57. }
  58. // Add the maxStalenessSeconds value to the read Preference
  59. if(this.options && this.options.maxStalenessSeconds) {
  60. this.maxStalenessSeconds = this.options.maxStalenessSeconds;
  61. }
  62. }
  63. /**
  64. * Validate if a mode is legal
  65. *
  66. * @method
  67. * @param {string} mode The string representing the read preference mode.
  68. * @return {boolean}
  69. */
  70. ReadPreference.isValid = function(_mode) {
  71. return (_mode == ReadPreference.PRIMARY || _mode == ReadPreference.PRIMARY_PREFERRED
  72. || _mode == ReadPreference.SECONDARY || _mode == ReadPreference.SECONDARY_PREFERRED
  73. || _mode == ReadPreference.NEAREST
  74. || _mode == true || _mode == false || _mode == null);
  75. }
  76. /**
  77. * Validate if a mode is legal
  78. *
  79. * @method
  80. * @param {string} mode The string representing the read preference mode.
  81. * @return {boolean}
  82. */
  83. ReadPreference.prototype.isValid = function(mode) {
  84. var _mode = typeof mode == 'string' ? mode : this.mode;
  85. return ReadPreference.isValid(_mode);
  86. }
  87. /**
  88. * @ignore
  89. */
  90. ReadPreference.prototype.toObject = function() {
  91. var object = {mode:this.mode};
  92. if(this.tags != null) {
  93. object['tags'] = this.tags;
  94. }
  95. if(this.maxStalenessSeconds) {
  96. object['maxStalenessSeconds'] = this.maxStalenessSeconds;
  97. }
  98. return object;
  99. }
  100. /**
  101. * @ignore
  102. */
  103. ReadPreference.prototype.toJSON = function() {
  104. return this.toObject();
  105. }
  106. /**
  107. * @ignore
  108. */
  109. ReadPreference.PRIMARY = 'primary';
  110. ReadPreference.PRIMARY_PREFERRED = 'primaryPreferred';
  111. ReadPreference.SECONDARY = 'secondary';
  112. ReadPreference.SECONDARY_PREFERRED = 'secondaryPreferred';
  113. ReadPreference.NEAREST = 'nearest'
  114. /**
  115. * @ignore
  116. */
  117. module.exports = ReadPreference;