Fix #780, allow connection strings without specifying a client

This commit is contained in:
Tim Griesser 2015-04-24 14:57:35 -04:00
parent ebcc22176f
commit 8758d5679c
8 changed files with 307 additions and 47 deletions

View File

@ -52,12 +52,6 @@ return /******/ (function(modules) { // webpackBootstrap
/************************************************************************/
/******/ ([
/* 0 */
/***/ function(module, exports, __webpack_require__) {
module.exports = __webpack_require__(1)
/***/ },
/* 1 */
/***/ function(module, exports, __webpack_require__) {
// Knex.js 0.8.0
@ -67,19 +61,26 @@ return /******/ (function(modules) { // webpackBootstrap
// For details and documentation:
// http://knexjs.org
module.exports = __webpack_require__(1)
/***/ },
/* 1 */
/***/ function(module, exports, __webpack_require__) {
'use strict';
var Raw = __webpack_require__(2)
var warn = __webpack_require__(3).warn
var Client = __webpack_require__(4)
var makeClient = __webpack_require__(5)
var makeKnex = __webpack_require__(6)
var assign = __webpack_require__(23)
var makeClient = __webpack_require__(5)
var makeKnex = __webpack_require__(6)
var parseConnection = __webpack_require__(187)
var assign = __webpack_require__(23)
function Knex(config) {
if (typeof config === 'string') {
return new Knex(assign(parseUrl(config), arguments[2]))
return new Knex(assign(parseConnection(config), arguments[2]))
}
var Dialect;
if (arguments.length === 0 || (!config.client && !config.dialect)) {
@ -103,10 +104,6 @@ return /******/ (function(modules) { // webpackBootstrap
return new Knex(config)
}
function parseUrl() {
}
// The client names we'll allow in the `{name: lib}` pairing.
var aliases = {
'mariadb' : 'maria',
@ -880,7 +877,9 @@ return /******/ (function(modules) { // webpackBootstrap
"./util/make-client": 5,
"./util/make-client.js": 5,
"./util/make-knex": 6,
"./util/make-knex.js": 6
"./util/make-knex.js": 6,
"./util/parse-connection": 187,
"./util/parse-connection.js": 187
};
function webpackContext(req) {
return __webpack_require__(webpackContextResolve(req));
@ -19023,6 +19022,58 @@ return /******/ (function(modules) { // webpackBootstrap
}
/***/ },
/* 187 */
/***/ function(module, exports, __webpack_require__) {
module.exports = parseConnectionString
function parseConnectionString(str) {
var parsed = url.parse(string)
var protocol = parsed.protocol
if (protocol === null) {
return {
client: 'sqlite3',
connection: {
filename: string
}
}
}
if (protocol.slice(-1) === ':') {
protocol = protocol.slice(0, -1);
}
return {
client: protocol,
connection: connectionObject(parsed)
}
}
function connectionObject(parsed) {
var connection = {};
var db = parsed.pathname;
if (db[0] === '/') {
db = db.slice(1)
}
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);
}
}
}
return connection
}
/***/ }
/******/ ])
});

View File

@ -43,12 +43,6 @@ module.exports =
/************************************************************************/
/******/ ([
/* 0 */
/***/ function(module, exports, __webpack_require__) {
module.exports = __webpack_require__(1)
/***/ },
/* 1 */
/***/ function(module, exports, __webpack_require__) {
// Knex.js 0.8.0
@ -58,19 +52,26 @@ module.exports =
// For details and documentation:
// http://knexjs.org
module.exports = __webpack_require__(1)
/***/ },
/* 1 */
/***/ function(module, exports, __webpack_require__) {
'use strict';
var Raw = __webpack_require__(3)
var warn = __webpack_require__(4).warn
var Client = __webpack_require__(5)
var makeClient = __webpack_require__(6)
var makeKnex = __webpack_require__(7)
var assign = __webpack_require__(2)
var makeClient = __webpack_require__(6)
var makeKnex = __webpack_require__(7)
var parseConnection = __webpack_require__(54)
var assign = __webpack_require__(2)
function Knex(config) {
if (typeof config === 'string') {
return new Knex(assign(parseUrl(config), arguments[2]))
return new Knex(assign(parseConnection(config), arguments[2]))
}
var Dialect;
if (arguments.length === 0 || (!config.client && !config.dialect)) {
@ -94,10 +95,6 @@ module.exports =
return new Knex(config)
}
function parseUrl() {
}
// The client names we'll allow in the `{name: lib}` pairing.
var aliases = {
'mariadb' : 'maria',
@ -871,7 +868,9 @@ module.exports =
"./util/make-client": 6,
"./util/make-client.js": 6,
"./util/make-knex": 7,
"./util/make-knex.js": 7
"./util/make-knex.js": 7,
"./util/parse-connection": 54,
"./util/parse-connection.js": 54
};
function webpackContext(req) {
return __webpack_require__(webpackContextResolve(req));
@ -1349,7 +1348,6 @@ module.exports =
var inherits = __webpack_require__(12)
var makeKnex = __webpack_require__(7)
var assign = __webpack_require__(2)
var uniqueId = __webpack_require__(15)
var debug = __webpack_require__(17)('knex:tx')
@ -4943,5 +4941,73 @@ module.exports =
module.exports = require("bluebird");
/***/ },
/* 54 */
/***/ function(module, exports, __webpack_require__) {
'use strict';
var url = __webpack_require__(55)
module.exports = parseConnectionString
function parseConnectionString(str) {
var parsed = url.parse(str)
var protocol = parsed.protocol
if (protocol && protocol.indexOf('maria') === 0) {
protocol = 'maria'
}
if (protocol === null) {
return {
client: 'sqlite3',
connection: {
filename: str
}
}
}
if (protocol.slice(-1) === ':') {
protocol = protocol.slice(0, -1);
}
return {
client: protocol,
connection: connectionObject(parsed)
}
}
function connectionObject(parsed) {
var connection = {};
var db = parsed.pathname;
if (db[0] === '/') {
db = db.slice(1)
}
if (parsed.protocol.indexOf('maria') === 0) {
connection.db = db
} else {
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);
}
}
}
return connection
}
/***/ },
/* 55 */
/***/ function(module, exports, __webpack_require__) {
module.exports = require("url");
/***/ }
/******/ ]);

View File

@ -1 +1,8 @@
// Knex.js 0.8.0
// --------------
// (c) 2014 Tim Griesser
// Knex may be freely distributed under the MIT license.
// For details and documentation:
// http://knexjs.org
module.exports = require('./lib/index')

View File

@ -1,23 +1,17 @@
// Knex.js 0.8.0
// --------------
// (c) 2014 Tim Griesser
// Knex may be freely distributed under the MIT license.
// For details and documentation:
// http://knexjs.org
'use strict';
var Raw = require('./raw')
var warn = require('./helpers').warn
var Client = require('./client')
var makeClient = require('./util/make-client')
var makeKnex = require('./util/make-knex')
var assign = require('lodash/object/assign')
var makeClient = require('./util/make-client')
var makeKnex = require('./util/make-knex')
var parseConnection = require('./util/parse-connection')
var assign = require('lodash/object/assign')
function Knex(config) {
if (typeof config === 'string') {
return new Knex(assign(parseUrl(config), arguments[2]))
return new Knex(assign(parseConnection(config), arguments[2]))
}
var Dialect;
if (arguments.length === 0 || (!config.client && !config.dialect)) {
@ -41,10 +35,6 @@ Knex.initialize = function(config) {
return new Knex(config)
}
function parseUrl() {
}
// The client names we'll allow in the `{name: lib}` pairing.
var aliases = {
'mariadb' : 'maria',

View File

@ -7,7 +7,6 @@ var EventEmitter = require('events').EventEmitter
var inherits = require('inherits')
var makeKnex = require('./util/make-knex')
var assign = require('lodash/object/assign')
var uniqueId = require('lodash/utility/uniqueId')
var debug = require('debug')('knex:tx')

View File

@ -0,0 +1,57 @@
'use strict';
var url = require('url')
module.exports = parseConnectionString
function parseConnectionString(str) {
var parsed = url.parse(str)
var protocol = parsed.protocol
if (protocol && protocol.indexOf('maria') === 0) {
protocol = 'maria'
}
if (protocol === null) {
return {
client: 'sqlite3',
connection: {
filename: str
}
}
}
if (protocol.slice(-1) === ':') {
protocol = protocol.slice(0, -1);
}
return {
client: protocol,
connection: connectionObject(parsed)
}
}
function connectionObject(parsed) {
var connection = {};
var db = parsed.pathname;
if (db[0] === '/') {
db = db.slice(1)
}
if (parsed.protocol.indexOf('maria') === 0) {
connection.db = db
} else {
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);
}
}
}
return connection
}

View File

@ -6,6 +6,8 @@ var knexfile = require('../knexfile')
Object.keys(knexfile).forEach(function(key) {
require('./parse-connection')
var knex = makeKnex(knexfile[key])
require('./transactions')(knex)

View File

@ -0,0 +1,88 @@
'use strict';
var parseConnection = require('../../lib/util/parse-connection')
var test = require('tape')
test('parses standard connections', function(t) {
t.plan(1)
t.deepEqual(parseConnection('postgres://username:pass@path.to.some-url:6000/testdb'), {
client: 'postgres',
connection: {
user: 'username',
password: 'pass',
host: 'path.to.some-url',
port: '6000',
database: 'testdb'
}
})
})
test('parses maria connections, aliasing database to db', function(t) {
t.plan(3)
t.deepEqual(parseConnection('maria://username:pass@path.to.some-url:6000/testdb'), {
client: 'maria',
connection: {
user: 'username',
password: 'pass',
host: 'path.to.some-url',
port: '6000',
db: 'testdb'
}
})
t.deepEqual(parseConnection('mariasql://username:pass@path.to.some-url:6000/testdb'), {
client: 'maria',
connection: {
user: 'username',
password: 'pass',
host: 'path.to.some-url',
port: '6000',
db: 'testdb'
}
})
t.deepEqual(parseConnection('mariadb://username:pass@path.to.some-url:6000/testdb'), {
client: 'maria',
connection: {
user: 'username',
password: 'pass',
host: 'path.to.some-url',
port: '6000',
db: 'testdb'
}
})
})
test('assume a path is mysql', function(t) {
t.plan(1)
t.deepEqual(parseConnection('/path/to/file.db'), {
client: 'sqlite3',
connection: {
filename: '/path/to/file.db'
}
})
})