2019-06-04 00:37:17 +02:00
|
|
|
const { parse } = require('pg-connection-string');
|
|
|
|
const parsePG = parse;
|
2020-01-28 02:28:40 +01:00
|
|
|
const isWindows = process && process.platform && process.platform === 'win32';
|
2015-05-09 13:58:18 -04:00
|
|
|
|
2020-12-28 01:09:58 +03:00
|
|
|
/**
|
|
|
|
* @param str
|
|
|
|
* @returns {URL}
|
|
|
|
*/
|
|
|
|
function tryParse(str) {
|
|
|
|
try {
|
|
|
|
return new URL(str);
|
|
|
|
} catch (e) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-04 00:37:17 +02:00
|
|
|
module.exports = function parseConnectionString(str) {
|
2020-12-28 01:09:58 +03:00
|
|
|
const parsed = tryParse(str);
|
|
|
|
const isDriveLetter = isWindows && parsed && parsed.protocol.length === 2;
|
|
|
|
if (!parsed || isDriveLetter) {
|
2015-05-09 13:58:18 -04:00
|
|
|
return {
|
|
|
|
client: 'sqlite3',
|
|
|
|
connection: {
|
2018-07-09 08:10:34 -04:00
|
|
|
filename: str,
|
|
|
|
},
|
|
|
|
};
|
2015-05-09 13:58:18 -04:00
|
|
|
}
|
2020-12-28 01:09:58 +03:00
|
|
|
let { protocol } = parsed;
|
2015-05-09 13:58:18 -04:00
|
|
|
if (protocol.slice(-1) === ':') {
|
|
|
|
protocol = protocol.slice(0, -1);
|
|
|
|
}
|
2018-05-18 05:12:15 -04:00
|
|
|
|
|
|
|
const isPG = ['postgresql', 'postgres'].includes(protocol);
|
|
|
|
|
2015-05-09 13:58:18 -04:00
|
|
|
return {
|
|
|
|
client: protocol,
|
2018-07-09 08:10:34 -04:00
|
|
|
connection: isPG ? parsePG(str) : connectionObject(parsed),
|
2018-05-18 05:12:15 -04:00
|
|
|
};
|
2019-06-04 00:37:17 +02:00
|
|
|
};
|
2015-05-09 13:58:18 -04:00
|
|
|
|
2020-12-28 01:09:58 +03:00
|
|
|
/**
|
|
|
|
* @param {URL} parsed
|
|
|
|
* @returns {{}}
|
|
|
|
*/
|
2015-05-09 13:58:18 -04:00
|
|
|
function connectionObject(parsed) {
|
2016-05-17 01:01:34 +10:00
|
|
|
const connection = {};
|
|
|
|
let db = parsed.pathname;
|
2015-05-09 13:58:18 -04:00
|
|
|
if (db[0] === '/') {
|
2018-07-09 08:10:34 -04:00
|
|
|
db = db.slice(1);
|
2015-05-09 13:58:18 -04:00
|
|
|
}
|
2018-07-01 23:06:26 +03:00
|
|
|
|
2018-07-09 08:10:34 -04:00
|
|
|
connection.database = db;
|
2018-07-01 23:06:26 +03:00
|
|
|
|
2015-05-09 13:58:18 -04:00
|
|
|
if (parsed.hostname) {
|
2016-04-12 01:30:20 +04:00
|
|
|
if (parsed.protocol.indexOf('mssql') === 0) {
|
|
|
|
connection.server = parsed.hostname;
|
|
|
|
} else {
|
|
|
|
connection.host = parsed.hostname;
|
|
|
|
}
|
2015-05-09 13:58:18 -04:00
|
|
|
}
|
|
|
|
if (parsed.port) {
|
|
|
|
connection.port = parsed.port;
|
|
|
|
}
|
2020-12-28 01:09:58 +03:00
|
|
|
if (parsed.username || parsed.password) {
|
|
|
|
connection.user = decodeURIComponent(parsed.username);
|
|
|
|
}
|
|
|
|
if (parsed.password) {
|
|
|
|
connection.password = decodeURIComponent(parsed.password);
|
2015-05-09 13:58:18 -04:00
|
|
|
}
|
2020-12-28 01:09:58 +03:00
|
|
|
if (parsed.searchParams) {
|
|
|
|
for (const [key, value] of parsed.searchParams.entries()) {
|
2021-08-30 20:19:08 +03:00
|
|
|
const isMySQLLike = ['mysql:', 'mariadb:'].includes(parsed.protocol);
|
|
|
|
if (isMySQLLike) {
|
|
|
|
try {
|
|
|
|
connection[key] = JSON.parse(value);
|
|
|
|
} catch (err) {
|
|
|
|
connection[key] = value;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
connection[key] = value;
|
|
|
|
}
|
2019-11-21 15:21:51 +01:00
|
|
|
}
|
|
|
|
}
|
2018-07-09 08:10:34 -04:00
|
|
|
return connection;
|
2016-04-12 01:30:20 +04:00
|
|
|
}
|