mirror of
https://github.com/knex/knex.git
synced 2026-01-04 02:57:58 +00:00
Add connection string qs to connection params (#3547)
This commit is contained in:
parent
3c2969ddd1
commit
07276bcbbb
@ -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;
|
||||
}
|
||||
|
||||
@ -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'), {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user