Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support all settings #96

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 71 additions & 15 deletions lib/mysql.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,25 +89,81 @@ 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 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 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];
if (typeof settings[name] !== 'undefined') {
return settings[name]
} else if (typeof currentAliases !== 'undefined') {
for (var key in currentAliases) {
var setting = getSetting(settings, currentAliases[key], []);
if (setting !== null) {
return setting;
}
}
}

return null;
}

function filterSettings(settings, options) {
filteredSettings = {}
for (var key in options) {
var option = options[key];
var setting = getSetting(settings, option, aliases);

if (setting !== null) {
filteredSettings[option] = setting;
}
}

return filteredSettings;
}

if (settings.pool) {
connectionSettings.connectionLimit = settings.connectionLimit || 10;
connectionSettings.queueLimit = settings.queueLimit || 0;
connectionSettings.waitForConnections = settings.waitForConnections || true;
return mysql.createPool(connectionSettings);
var poolSettings = filterSettings(settings, poolOptions);

return mysql.createPool(poolSettings);
}

var connectionSettings = filterSettings(settings, options);

return mysql.createConnection(connectionSettings);
}

Expand Down