Advertisement
taufikmas

connection-parameters.js

Jun 7th, 2017
310
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var url = require('url');
  2. var dns = require('dns');
  3.  
  4. var defaults = require('./defaults');
  5.  
  6. var val = function(key, config, envVar) {
  7.   if (envVar === undefined) {
  8.     envVar = process.env[ 'PG' + key.toUpperCase() ];
  9.   } else if (envVar === false) {
  10.     // do nothing ... use false
  11.   } else {
  12.     envVar = process.env[ envVar ];
  13.   }
  14.  
  15.   return config[key] ||
  16.     envVar ||
  17.     defaults[key];
  18. };
  19.  
  20. //parses a connection string
  21. var parse = require('pg-connection-string').parse;
  22.  
  23. var useSsl = function() {
  24.   switch(process.env.PGSSLMODE) {
  25.   case "disable":
  26.     return false;
  27.   case "prefer":
  28.   case "require":
  29.   case "verify-ca":
  30.   case "verify-full":
  31.     return true;
  32.   }
  33.   return defaults.ssl;
  34. };
  35.  
  36. var ConnectionParameters = function(config) {
  37.   config = typeof config == 'string' ? parse(config) : (config || {});
  38.   this.user = val('user', config);
  39.   this.database = val('database', config);
  40.   this.port = parseInt(val('port', config), 10);
  41.   this.host = val('host', config);
  42.   this.password = val('password', config);
  43.   this.binary = val('binary', config);
  44.   this.ssl = config.ssl || useSsl();
  45.   this.client_encoding = val("client_encoding", config);
  46.   //a domain socket begins with '/'
  47.   this.isDomainSocket = (!(this.host||'').indexOf('/'));
  48.  
  49.   this.application_name = val('application_name', config, 'PGAPPNAME');
  50.   this.fallback_application_name = val('fallback_application_name', config, false);
  51. };
  52.  
  53. var add = function(params, config, paramName) {
  54.   var value = config[paramName];
  55.   if(value) {
  56.     params.push(paramName+"='"+value+"'");
  57.   }
  58. };
  59.  
  60. ConnectionParameters.prototype.getLibpqConnectionString = function(cb) {
  61.   var params = [];
  62.   add(params, this, 'user');
  63.   add(params, this, 'password');
  64.   add(params, this, 'port');
  65.   add(params, this, 'application_name');
  66.   add(params, this, 'fallback_application_name');
  67.  
  68.   if(this.database) {
  69.     params.push("dbname='" + this.database + "'");
  70.   }
  71.   if(this.host) {
  72.     params.push("host=" + this.host);
  73.   }
  74.   if(this.isDomainSocket) {
  75.     return cb(null, params.join(' '));
  76.   }
  77.   if(this.client_encoding) {
  78.     params.push("client_encoding='" + this.client_encoding + "'");
  79.   }
  80.   dns.lookup(this.host, function(err, address) {
  81.     if(err) return cb(err, null);
  82.     params.push("hostaddr=" + address);
  83.     return cb(null, params.join(' '));
  84.   });
  85. };
  86.  
  87. module.exports = ConnectionParameters;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement