Add connection string qs to connection params (#3547)

This commit is contained in:
Andrei 2019-11-21 15:21:51 +01:00 committed by Igor Savin
parent 3c2969ddd1
commit 07276bcbbb
2 changed files with 45 additions and 1 deletions

View File

@ -3,7 +3,7 @@ const { parse } = require('pg-connection-string');
const parsePG = parse;
module.exports = function parseConnectionString(str) {
const parsed = url.parse(str);
const parsed = url.parse(str, true);
let { protocol } = parsed;
if (protocol === null) {
return {
@ -55,5 +55,10 @@ function connectionObject(parsed) {
connection.user = parsed.auth;
}
}
if (parsed.query) {
for (const key in parsed.query) {
connection[key] = parsed.query[key];
}
}
return connection;
}

View File

@ -36,6 +36,24 @@ test('parses standard connections without password', function(t) {
);
});
test('mysql connection protocol with query string params', function(t) {
t.plan(1);
t.deepEqual(
parseConnection('mysql://user:pass@path.to.some-url:3306/testdb?foo=bar'),
{
client: 'mysql',
connection: {
user: 'user',
password: 'pass',
host: 'path.to.some-url',
port: '3306',
database: 'testdb',
foo: 'bar',
},
}
);
});
test('parses mssql connections, aliasing host to server', function(t) {
t.plan(1);
const mssql = {
@ -54,6 +72,27 @@ test('parses mssql connections, aliasing host to server', function(t) {
);
});
test('parses mssql connections, aliasing host to server and adding extra params', function(t) {
t.plan(1);
const mssql = {
client: 'mssql',
connection: {
user: 'user',
password: 'pass',
server: 'path.to.some-url',
port: '6000',
database: 'testdb',
param: 'value',
},
};
t.deepEqual(
parseConnection(
'mssql://user:pass@path.to.some-url:6000/testdb?param=value'
),
mssql
);
});
test('assume a path is mysql', function(t) {
t.plan(1);
t.deepEqual(parseConnection('/path/to/file.db'), {