From 34722ca3afd486bcdf7d1123528f72f0daba72f5 Mon Sep 17 00:00:00 2001 From: chok Date: Fri, 22 Aug 2014 16:52:38 +0200 Subject: [PATCH 1/4] support all settings --- lib/mysql.js | 68 ++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 56 insertions(+), 12 deletions(-) diff --git a/lib/mysql.js b/lib/mysql.js index c2dcac4..20f3b5a 100644 --- a/lib/mysql.js +++ b/lib/mysql.js @@ -89,18 +89,61 @@ function initializeConnection(connection, schema, callback) { */ function getConnection(settings) { var connection; - var connectionSettings = { - host: settings.host || 'localhost', - port: settings.port || 3306, - user: settings.username, - password: settings.password, - timezone: settings.timezone, - debug: settings.debug, - socketPath: settings.socketPath, - charset: settings.collation.toUpperCase(), - supportBigNumbers: settings.supportBigNumbers, - insecureAuth: settings.insecureAuth || false - }; + var connectionSettings = {}; + + var options = [ + 'host', // The hostname of the database you are connecting to. (Default: localhost) + 'port', // The port number to connect to. (Default: 3306) + 'localAddress', // The source IP address to use for TCP connection. (Optional) + 'socketPath', // The path to a unix domain socket to connect to. When used host and port are ignored. + 'user', // The MySQL user to authenticate as. + 'password', // The password of that MySQL user. + 'database', // Name of the database to use for this connection (Optional). + 'charset', // The charset for the connection. This is called "collation" in the SQL-level of MySQL (like utf8_general_ci). If a SQL-level charset is specified (like utf8mb4) then the default collation for that charset is used. (Default: 'UTF8_GENERAL_CI') + 'timezone', // The timezone used to store local dates. (Default: 'local') + 'connectTimeout', // The milliseconds before a timeout occurs during the initial connection to the MySQL server. (Default: 10 seconds) + 'stringifyObjects', // Stringify objects instead of converting to values. See issue #501. (Default: 'false') + 'insecureAuth', // Allow connecting to MySQL instances that ask for the old (insecure) authentication method. (Default: false) + 'typeCast', // Determines if column values should be converted to native JavaScript types. (Default: true) + 'queryFormat', // A custom query format function. See Custom format. + 'supportBigNumbers', // When dealing with big numbers (BIGINT and DECIMAL columns) in the database, you should enable this option (Default: false). + 'bigNumberStrings', // Enabling both supportBigNumbers and bigNumberStrings forces big numbers (BIGINT and DECIMAL columns) to be always returned as JavaScript String objects (Default: false). Enabling supportBigNumbers but leaving bigNumberStrings disabled will return big numbers as String objects only when they cannot be accurately represented with JavaScript Number objects (which happens when they exceed the [-2^53, +2^53] range), otherwise they will be returned as Number objects. This option is ignored if supportBigNumbers is disabled. + 'dateStrings', // Force date types (TIMESTAMP, DATETIME, DATE) to be returned as strings rather then inflated into JavaScript Date objects. (Default: false) + 'debug', // Prints protocol details to stdout. (Default: false) + 'trace', // Generates stack traces on Error to include call site of library entrance ("long stack traces"). Slight performance penalty for most calls. (Default: true) + 'multipleStatements', // Allow multiple mysql statements per query. Be careful with this, it exposes you to SQL injection attacks. (Default: false) + 'flags', // List of connection flags to use other than the default ones. It is also possible to blacklist default ones. For more information, check Connection Flags. + 'ssl', // object with ssl parameters or a string containing name of ssl profile. See SSL options. + ] + + var aliases = { + 'user': ['username'] + } + + function getSetting(settings, name, aliases) { + var currentAliases = aliases[name]; + if (typeof settings[name] !== 'undefined') { + return settings[name] + } else if (typeof currentAliases === 'Array') { + for (var key in currentAliases) { + var setting = getSetting(settings, alias, []); + if (setting !== null) { + return setting; + } + } + } + + return null; + } + + for (var key in options) { + var option = options[key]; + var setting = getSetting(settings, option, aliases); + + if (setting !== null) { + connectionSettings[option] = setting; + } + } if (settings.pool) { connectionSettings.connectionLimit = settings.connectionLimit || 10; @@ -108,6 +151,7 @@ function getConnection(settings) { connectionSettings.waitForConnections = settings.waitForConnections || true; return mysql.createPool(connectionSettings); } + return mysql.createConnection(connectionSettings); } From 9e9d453efc970f2889e5eeb2e2722bde5b59c8a0 Mon Sep 17 00:00:00 2001 From: chok Date: Mon, 25 Aug 2014 10:37:41 +0200 Subject: [PATCH 2/4] all settings --- lib/mysql.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/mysql.js b/lib/mysql.js index 20f3b5a..46dd320 100644 --- a/lib/mysql.js +++ b/lib/mysql.js @@ -124,9 +124,9 @@ function getConnection(settings) { var currentAliases = aliases[name]; if (typeof settings[name] !== 'undefined') { return settings[name] - } else if (typeof currentAliases === 'Array') { + } else if (typeof currentAliases !== 'undefined') { for (var key in currentAliases) { - var setting = getSetting(settings, alias, []); + var setting = getSetting(settings, currentAliases[key], []); if (setting !== null) { return setting; } From a2f2e879ac0568e436c2a525433740389172ac34 Mon Sep 17 00:00:00 2001 From: chok Date: Thu, 28 Aug 2014 10:54:11 +0200 Subject: [PATCH 3/4] support pool --- lib/mysql.js | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/lib/mysql.js b/lib/mysql.js index 46dd320..41a90a5 100644 --- a/lib/mysql.js +++ b/lib/mysql.js @@ -89,7 +89,6 @@ function initializeConnection(connection, schema, callback) { */ function getConnection(settings) { var connection; - var connectionSettings = {}; var options = [ 'host', // The hostname of the database you are connecting to. (Default: localhost) @@ -116,6 +115,13 @@ function getConnection(settings) { 'ssl', // object with ssl parameters or a string containing name of ssl profile. See SSL options. ] + var poolOptions = [ + 'acquireTimeout', // The milliseconds before a timeout occurs during the connection acquisition. This is slightly different from connectTimeout, because acquiring a pool connection does not always involve making a connection. (Default: 10 seconds) + 'waitForConnections', // Determines the pool's action when no connections are available and the limit has been reached. If true, the pool will queue the connection request and call it when one becomes available. If false, the pool will immediately call back with an error. (Default: true) + 'connectionLimit', // The maximum number of connections to create at once. (Default: 10) + 'queueLimit' // The maximum number of connection requests the pool will queue before returning an error from getConnection. If set to 0, there is no limit to the number of queued connection requests. (Default: 0) + ] + var aliases = { 'user': ['username'] } @@ -136,22 +142,26 @@ function getConnection(settings) { return null; } - for (var key in options) { - var option = options[key]; - var setting = getSetting(settings, option, aliases); + function filterSettings(settings, options) { + filteredSettings = {} + for (var key in options) { + var option = options[key]; + var setting = getSetting(settings, option, aliases); - if (setting !== null) { - connectionSettings[option] = setting; - } + if (setting !== null) { + filteredSettings[option] = setting; + } + } } if (settings.pool) { - connectionSettings.connectionLimit = settings.connectionLimit || 10; - connectionSettings.queueLimit = settings.queueLimit || 0; - connectionSettings.waitForConnections = settings.waitForConnections || true; + var poolSettings = filterSettings(settings, poolOptions); + return mysql.createPool(connectionSettings); } + var connectionSettings = filterSettings(settings, options); + return mysql.createConnection(connectionSettings); } From 7a3585dac3f8df23f8545f885adf6679b91110a8 Mon Sep 17 00:00:00 2001 From: chok Date: Thu, 28 Aug 2014 12:25:24 +0200 Subject: [PATCH 4/4] all pool options --- lib/mysql.js | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/lib/mysql.js b/lib/mysql.js index 41a90a5..8fc5a93 100644 --- a/lib/mysql.js +++ b/lib/mysql.js @@ -113,18 +113,18 @@ function getConnection(settings) { 'multipleStatements', // Allow multiple mysql statements per query. Be careful with this, it exposes you to SQL injection attacks. (Default: false) 'flags', // List of connection flags to use other than the default ones. It is also possible to blacklist default ones. For more information, check Connection Flags. 'ssl', // object with ssl parameters or a string containing name of ssl profile. See SSL options. - ] + ]; var poolOptions = [ 'acquireTimeout', // The milliseconds before a timeout occurs during the connection acquisition. This is slightly different from connectTimeout, because acquiring a pool connection does not always involve making a connection. (Default: 10 seconds) 'waitForConnections', // Determines the pool's action when no connections are available and the limit has been reached. If true, the pool will queue the connection request and call it when one becomes available. If false, the pool will immediately call back with an error. (Default: true) 'connectionLimit', // The maximum number of connections to create at once. (Default: 10) 'queueLimit' // The maximum number of connection requests the pool will queue before returning an error from getConnection. If set to 0, there is no limit to the number of queued connection requests. (Default: 0) - ] + ].concat(options); var aliases = { 'user': ['username'] - } + }; function getSetting(settings, name, aliases) { var currentAliases = aliases[name]; @@ -151,13 +151,15 @@ function getConnection(settings) { if (setting !== null) { filteredSettings[option] = setting; } - } + } + + return filteredSettings; } if (settings.pool) { var poolSettings = filterSettings(settings, poolOptions); - return mysql.createPool(connectionSettings); + return mysql.createPool(poolSettings); } var connectionSettings = filterSettings(settings, options);