parse-connection.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. 'use strict';
  2. exports.__esModule = true;
  3. exports.default = parseConnectionString;
  4. var _url = require('url');
  5. var _url2 = _interopRequireDefault(_url);
  6. var _pgConnectionString = require('pg-connection-string');
  7. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  8. function parseConnectionString(str) {
  9. var parsed = _url2.default.parse(str);
  10. var protocol = parsed.protocol;
  11. if (protocol && protocol.indexOf('maria') === 0) {
  12. protocol = 'maria';
  13. }
  14. if (protocol === null) {
  15. return {
  16. client: 'sqlite3',
  17. connection: {
  18. filename: str
  19. }
  20. };
  21. }
  22. if (protocol.slice(-1) === ':') {
  23. protocol = protocol.slice(0, -1);
  24. }
  25. return {
  26. client: protocol,
  27. connection: protocol === 'postgres' ? (0, _pgConnectionString.parse)(str) : connectionObject(parsed)
  28. };
  29. }
  30. function connectionObject(parsed) {
  31. var connection = {};
  32. var db = parsed.pathname;
  33. if (db[0] === '/') {
  34. db = db.slice(1);
  35. }
  36. if (parsed.protocol.indexOf('maria') === 0) {
  37. connection.db = db;
  38. } else {
  39. connection.database = db;
  40. }
  41. if (parsed.hostname) {
  42. if (parsed.protocol.indexOf('mssql') === 0) {
  43. connection.server = parsed.hostname;
  44. } else {
  45. connection.host = parsed.hostname;
  46. }
  47. }
  48. if (parsed.port) {
  49. connection.port = parsed.port;
  50. }
  51. if (parsed.auth) {
  52. var idx = parsed.auth.indexOf(':');
  53. if (idx !== -1) {
  54. connection.user = parsed.auth.slice(0, idx);
  55. if (idx < parsed.auth.length - 1) {
  56. connection.password = parsed.auth.slice(idx + 1);
  57. }
  58. } else {
  59. connection.user = parsed.auth;
  60. }
  61. }
  62. return connection;
  63. }
  64. module.exports = exports['default'];