shared.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.isSharded = exports.applyCommonQueryOptions = exports.getReadPreference = void 0;
  4. const error_1 = require("../../error");
  5. const read_preference_1 = require("../../read_preference");
  6. const common_1 = require("../../sdam/common");
  7. const topology_description_1 = require("../../sdam/topology_description");
  8. function getReadPreference(cmd, options) {
  9. // Default to command version of the readPreference
  10. let readPreference = cmd.readPreference || read_preference_1.ReadPreference.primary;
  11. // If we have an option readPreference override the command one
  12. if (options === null || options === void 0 ? void 0 : options.readPreference) {
  13. readPreference = options.readPreference;
  14. }
  15. if (typeof readPreference === 'string') {
  16. readPreference = read_preference_1.ReadPreference.fromString(readPreference);
  17. }
  18. if (!(readPreference instanceof read_preference_1.ReadPreference)) {
  19. throw new error_1.MongoInvalidArgumentError('Option "readPreference" must be a ReadPreference instance');
  20. }
  21. return readPreference;
  22. }
  23. exports.getReadPreference = getReadPreference;
  24. function applyCommonQueryOptions(queryOptions, options) {
  25. Object.assign(queryOptions, {
  26. raw: typeof options.raw === 'boolean' ? options.raw : false,
  27. promoteLongs: typeof options.promoteLongs === 'boolean' ? options.promoteLongs : true,
  28. promoteValues: typeof options.promoteValues === 'boolean' ? options.promoteValues : true,
  29. promoteBuffers: typeof options.promoteBuffers === 'boolean' ? options.promoteBuffers : false,
  30. bsonRegExp: typeof options.bsonRegExp === 'boolean' ? options.bsonRegExp : false,
  31. enableUtf8Validation: typeof options.enableUtf8Validation === 'boolean' ? options.enableUtf8Validation : true
  32. });
  33. if (options.session) {
  34. queryOptions.session = options.session;
  35. }
  36. return queryOptions;
  37. }
  38. exports.applyCommonQueryOptions = applyCommonQueryOptions;
  39. function isSharded(topologyOrServer) {
  40. if (topologyOrServer == null) {
  41. return false;
  42. }
  43. if (topologyOrServer.description && topologyOrServer.description.type === common_1.ServerType.Mongos) {
  44. return true;
  45. }
  46. // NOTE: This is incredibly inefficient, and should be removed once command construction
  47. // happens based on `Server` not `Topology`.
  48. if (topologyOrServer.description && topologyOrServer.description instanceof topology_description_1.TopologyDescription) {
  49. const servers = Array.from(topologyOrServer.description.servers.values());
  50. return servers.some((server) => server.type === common_1.ServerType.Mongos);
  51. }
  52. return false;
  53. }
  54. exports.isSharded = isSharded;
  55. //# sourceMappingURL=shared.js.map