3 var needSlaveOk = ['primaryPreferred', 'secondary', 'secondaryPreferred', 'nearest'];
6 * @fileOverview The **ReadPreference** class is a class that represents a MongoDB ReadPreference and is
7 * used to construct connections.
10 * var ReplSet = require('mongodb-core').ReplSet
11 * , ReadPreference = require('mongodb-core').ReadPreference
12 * , assert = require('assert');
14 * var server = new ReplSet([{host: 'localhost', port: 30000}], {setName: 'rs'});
15 * // Wait for the connection event
16 * server.on('connect', function(server) {
17 * var cursor = server.cursor('db.test'
18 * , {find: 'db.test', query: {}}
19 * , {readPreference: new ReadPreference('secondary')});
20 * cursor.next(function(err, doc) {
30 * Creates a new Pool instance
32 * @param {string} preference A string describing the preference (primary|primaryPreferred|secondary|secondaryPreferred|nearest)
33 * @param {array} tags The tags object
34 * @param {object} [options] Additional read preference options
35 * @param {number} [options.maxStalenessSeconds] Max Secondary Read Stalleness in Seconds, Minimum value is 90 seconds.
36 * @property {string} preference The preference string (primary|primaryPreferred|secondary|secondaryPreferred|nearest)
37 * @property {array} tags The tags object
38 * @property {object} options Additional read preference options
39 * @property {number} maxStalenessSeconds MaxStalenessSeconds value for the read preference
40 * @return {ReadPreference}
42 var ReadPreference = function(preference, tags, options) {
43 this.preference = preference;
45 this.options = options;
47 // Add the maxStalenessSeconds value to the read Preference
48 if(this.options && this.options.maxStalenessSeconds != null) {
49 this.options = options;
50 this.maxStalenessSeconds = this.options.maxStalenessSeconds >= 0
51 ? this.options.maxStalenessSeconds : null;
52 } else if(tags && typeof tags == 'object') {
53 this.options = tags, tags = null;
58 * This needs slaveOk bit set
62 ReadPreference.prototype.slaveOk = function() {
63 return needSlaveOk.indexOf(this.preference) != -1;
67 * Are the two read preference equal
71 ReadPreference.prototype.equals = function(readPreference) {
72 return readPreference.preference == this.preference;
76 * Return JSON representation
80 ReadPreference.prototype.toJSON = function() {
81 var readPreference = {mode: this.preference};
82 if(Array.isArray(this.tags)) readPreference.tags = this.tags;
83 if(this.maxStalenessSeconds) readPreference.maxStalenessSeconds = this.maxStalenessSeconds;
84 return readPreference;
88 * Primary read preference
90 * @return {ReadPreference}
92 ReadPreference.primary = new ReadPreference('primary');
94 * Primary Preferred read preference
96 * @return {ReadPreference}
98 ReadPreference.primaryPreferred = new ReadPreference('primaryPreferred');
100 * Secondary read preference
102 * @return {ReadPreference}
104 ReadPreference.secondary = new ReadPreference('secondary');
106 * Secondary Preferred read preference
108 * @return {ReadPreference}
110 ReadPreference.secondaryPreferred = new ReadPreference('secondaryPreferred');
112 * Nearest read preference
114 * @return {ReadPreference}
116 ReadPreference.nearest = new ReadPreference('nearest');
118 module.exports = ReadPreference;