Skip to content
This repository was archived by the owner on Feb 4, 2022. It is now read-only.

Commit 0587cde

Browse files
authored
feat(readPreference): add ReadPreference.fromOptions
1 parent 3a2989f commit 0587cde

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

lib/topologies/read_preference.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,34 @@ const VALID_MODES = [
103103
null
104104
];
105105

106+
/**
107+
* Construct a ReadPreference given an options object.
108+
*
109+
* @param {object} options The options object from which to extract the read preference.
110+
* @return {ReadPreference}
111+
*/
112+
ReadPreference.fromOptions = function(options) {
113+
const readPreference = options.readPreference;
114+
const readPreferenceTags = options.readPreferenceTags;
115+
116+
if (readPreference == null) {
117+
return null;
118+
}
119+
120+
if (typeof readPreference === 'string') {
121+
return new ReadPreference(readPreference, readPreferenceTags);
122+
} else if (!(readPreference instanceof ReadPreference) && typeof readPreference === 'object') {
123+
const mode = readPreference.mode || readPreference.preference;
124+
if (mode && typeof mode === 'string') {
125+
return new ReadPreference(mode, readPreference.tags, {
126+
maxStalenessSeconds: readPreference.maxStalenessSeconds
127+
});
128+
}
129+
}
130+
131+
return readPreference;
132+
};
133+
106134
/**
107135
* Validate if a mode is legal
108136
*

0 commit comments

Comments
 (0)