Override other settings if url provided

Handle url in config
Override other settings if url provided
This commit is contained in:
juehou 2016-03-24 13:20:56 -04:00
parent 18e05b1365
commit 12cf967b7b
2 changed files with 66 additions and 24 deletions

View File

@ -72,31 +72,37 @@ function MySQL(settings) {
s.connectionLimit = 10;
}
var options = {
host: s.host || s.hostname || 'localhost',
port: s.port || 3306,
user: s.username || s.user,
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
};
var options;
if (s.url) {
// use url to override other settings if url provided
options = s.url;
} else {
options = {
host: s.host || s.hostname || 'localhost',
port: s.port || 3306,
user: s.username || s.user,
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
if (!s.createDatabase) {
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;
// Don't configure the DB if the pool can be used for multiple DBs
if (!s.createDatabase) {
options.database = s.database;
}
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];
}
}
}

View File

@ -1,17 +1,42 @@
require('./init.js');
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 () {
before(function () {
require('./init.js');
config = global.getConfig();
odb = getDataSource({collation: 'utf8_general_ci', createDatabase: true});
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) {
var test_set = /utf8/;
@ -103,6 +128,17 @@ var query = function (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;
}