knex/lib/util/parse-connection.js

57 lines
1.2 KiB
JavaScript
Raw Normal View History

'use strict';
2015-05-09 13:58:18 -04:00
var url = require('url');
2015-05-09 13:58:18 -04:00
module.exports = parseConnectionString;
function parseConnectionString(str) {
2015-05-09 13:58:18 -04:00
var parsed = url.parse(str);
var protocol = parsed.protocol;
if (protocol && protocol.indexOf('maria') === 0) {
2015-05-09 13:58:18 -04:00
protocol = 'maria';
}
if (protocol === null) {
return {
client: 'sqlite3',
connection: {
filename: str
}
2015-05-09 13:58:18 -04:00
};
}
if (protocol.slice(-1) === ':') {
protocol = protocol.slice(0, -1);
}
return {
client: protocol,
connection: connectionObject(parsed)
2015-05-09 13:58:18 -04:00
};
}
function connectionObject(parsed) {
var connection = {};
var db = parsed.pathname;
if (db[0] === '/') {
2015-05-09 13:58:18 -04:00
db = db.slice(1);
}
if (parsed.protocol.indexOf('maria') === 0) {
2015-05-09 13:58:18 -04:00
connection.db = db;
} else {
2015-05-09 13:58:18 -04:00
connection.database = db;
}
if (parsed.hostname) {
connection.host = parsed.hostname;
}
if (parsed.port) {
connection.port = parsed.port;
}
if (parsed.auth) {
var idx = parsed.auth.indexOf(':');
if (idx !== -1) {
connection.user = parsed.auth.slice(0, idx);
if (idx < parsed.auth.length - 1) {
connection.password = parsed.auth.slice(idx + 1);
}
}
}
2015-05-09 13:58:18 -04:00
return connection;
}