ReadPreference.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // Copyright 2015 The MemDB Authors.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
  12. // implied. See the License for the specific language governing
  13. // permissions and limitations under the License. See the AUTHORS file
  14. // for names of contributors.
  15. /* jshint ignore:start */
  16. /*!
  17. * Module dependencies.
  18. */
  19. var mongodb = require('mongodb');
  20. var ReadPref = mongodb.ReadPreference;
  21. /*!
  22. * Converts arguments to ReadPrefs the driver
  23. * can understand.
  24. *
  25. * @param {String|Array} pref
  26. * @param {Array} [tags]
  27. */
  28. module.exports = function readPref (pref, tags) {
  29. if (Array.isArray(pref)) {
  30. tags = pref[1];
  31. pref = pref[0];
  32. }
  33. if (pref instanceof ReadPref) {
  34. return pref;
  35. }
  36. switch (pref) {
  37. case 'p':
  38. pref = 'primary';
  39. break;
  40. case 'pp':
  41. pref = 'primaryPreferred';
  42. break;
  43. case 's':
  44. pref = 'secondary';
  45. break;
  46. case 'sp':
  47. pref = 'secondaryPreferred';
  48. break;
  49. case 'n':
  50. pref = 'nearest';
  51. break;
  52. }
  53. return new ReadPref(pref, tags);
  54. }