From 12cf967b7b7aaf53998e6b5296a1287f64690d91 Mon Sep 17 00:00:00 2001 From: juehou Date: Thu, 24 Mar 2016 13:20:56 -0400 Subject: [PATCH] Override other settings if url provided Handle url in config Override other settings if url provided --- lib/mysql.js | 52 +++++++++++++++++++++++------------------ test/connection.test.js | 38 +++++++++++++++++++++++++++++- 2 files changed, 66 insertions(+), 24 deletions(-) diff --git a/lib/mysql.js b/lib/mysql.js index 0f0b36e..6c94e1a 100644 --- a/lib/mysql.js +++ b/lib/mysql.js @@ -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]; + } } } diff --git a/test/connection.test.js b/test/connection.test.js index d31360c..0dbf4de 100644 --- a/test/connection.test.js +++ b/test/connection.test.js @@ -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; +}