Merge pull request #156 from strongloop/feature/handle-url-in-config
Use url to override other settings if url provided
This commit is contained in:
commit
7f8abbac7f
52
lib/mysql.js
52
lib/mysql.js
|
@ -72,31 +72,37 @@ function MySQL(settings) {
|
||||||
s.connectionLimit = 10;
|
s.connectionLimit = 10;
|
||||||
}
|
}
|
||||||
|
|
||||||
var options = {
|
var options;
|
||||||
host: s.host || s.hostname || 'localhost',
|
if (s.url) {
|
||||||
port: s.port || 3306,
|
// use url to override other settings if url provided
|
||||||
user: s.username || s.user,
|
options = s.url;
|
||||||
password: s.password,
|
} else {
|
||||||
timezone: s.timezone,
|
options = {
|
||||||
socketPath: s.socketPath,
|
host: s.host || s.hostname || 'localhost',
|
||||||
charset: s.collation.toUpperCase(), // Correct by docs despite seeming odd.
|
port: s.port || 3306,
|
||||||
supportBigNumbers: s.supportBigNumbers,
|
user: s.username || s.user,
|
||||||
connectionLimit: s.connectionLimit
|
password: s.password,
|
||||||
};
|
timezone: s.timezone,
|
||||||
|
socketPath: s.socketPath,
|
||||||
|
charset: s.collation.toUpperCase(), // Correct by docs despite seeming odd.
|
||||||
|
supportBigNumbers: s.supportBigNumbers,
|
||||||
|
connectionLimit: s.connectionLimit
|
||||||
|
};
|
||||||
|
|
||||||
// Don't configure the DB if the pool can be used for multiple DBs
|
// Don't configure the DB if the pool can be used for multiple DBs
|
||||||
if (!s.createDatabase) {
|
if (!s.createDatabase) {
|
||||||
options.database = s.database;
|
options.database = s.database;
|
||||||
}
|
|
||||||
|
|
||||||
// Take other options for mysql driver
|
|
||||||
// See https://github.com/strongloop/loopback-connector-mysql/issues/46
|
|
||||||
for (var p in s) {
|
|
||||||
if (p === 'database' && s.createDatabase) {
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
if (options[p] === undefined) {
|
|
||||||
options[p] = s[p];
|
// Take other options for mysql driver
|
||||||
|
// See https://github.com/strongloop/loopback-connector-mysql/issues/46
|
||||||
|
for (var p in s) {
|
||||||
|
if (p === 'database' && s.createDatabase) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (options[p] === undefined) {
|
||||||
|
options[p] = s[p];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,17 +1,42 @@
|
||||||
require('./init.js');
|
require('./init.js');
|
||||||
var assert = require('assert');
|
var assert = require('assert');
|
||||||
|
var DataSource = require('loopback-datasource-juggler').DataSource;
|
||||||
|
var mysqlConnector = require('../');
|
||||||
|
var url = require('url');
|
||||||
|
|
||||||
var db, DummyModel, odb;
|
var db, DummyModel, odb, config;
|
||||||
|
|
||||||
describe('connections', function () {
|
describe('connections', function () {
|
||||||
|
|
||||||
before(function () {
|
before(function () {
|
||||||
require('./init.js');
|
require('./init.js');
|
||||||
|
|
||||||
|
config = global.getConfig();
|
||||||
|
|
||||||
odb = getDataSource({collation: 'utf8_general_ci', createDatabase: true});
|
odb = getDataSource({collation: 'utf8_general_ci', createDatabase: true});
|
||||||
db = odb;
|
db = odb;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should pass with valid settings', function (done) {
|
||||||
|
var db = new DataSource(mysqlConnector, config);
|
||||||
|
db.ping(done);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('ignores all other settings when url is present', function (done) {
|
||||||
|
var formatedUrl = generateURL(config);
|
||||||
|
var dbConfig = {
|
||||||
|
url: formatedUrl,
|
||||||
|
host: 'invalid-hostname',
|
||||||
|
port: 80,
|
||||||
|
database: 'invalid-database',
|
||||||
|
username: 'invalid-username',
|
||||||
|
password: 'invalid-password',
|
||||||
|
};
|
||||||
|
|
||||||
|
var db = new DataSource(mysqlConnector, dbConfig);
|
||||||
|
db.ping(done);
|
||||||
|
});
|
||||||
|
|
||||||
it('should use utf8 charset', function (done) {
|
it('should use utf8 charset', function (done) {
|
||||||
|
|
||||||
var test_set = /utf8/;
|
var test_set = /utf8/;
|
||||||
|
@ -103,6 +128,17 @@ var query = function (sql, cb) {
|
||||||
odb.connector.execute(sql, cb);
|
odb.connector.execute(sql, cb);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
function generateURL(config) {
|
||||||
|
var urlObj = {
|
||||||
|
protocol: 'mysql',
|
||||||
|
auth: config.username + ':' + config.password,
|
||||||
|
hostname: config.host,
|
||||||
|
pathname: config.database,
|
||||||
|
slashes: true
|
||||||
|
};
|
||||||
|
var formatedUrl = url.format(urlObj);
|
||||||
|
return formatedUrl;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue