Improve the connector based on review feedbacks
Introduce a 'createDatabase' option to allow DB to created Wrap the callback with connection.release Fix typo
This commit is contained in:
parent
aac933c39d
commit
f171e74392
97
lib/mysql.js
97
lib/mysql.js
|
@ -31,22 +31,36 @@ exports.initialize = function initializeDataSource(dataSource, callback) {
|
||||||
s.supportBigNumbers = (s.supportBigNumbers || false);
|
s.supportBigNumbers = (s.supportBigNumbers || false);
|
||||||
s.timezone = (s.timezone || 'local');
|
s.timezone = (s.timezone || 'local');
|
||||||
|
|
||||||
dataSource.client = mysql.createPool({
|
if(isNaN(s.connectionLimit)) {
|
||||||
|
s.connectionLimit = s.connectionLimit;
|
||||||
|
} else {
|
||||||
|
s.connectionLimit = 10;
|
||||||
|
}
|
||||||
|
|
||||||
|
var options = {
|
||||||
host: s.host || s.hostname || 'localhost',
|
host: s.host || s.hostname || 'localhost',
|
||||||
port: s.port || 3306,
|
port: s.port || 3306,
|
||||||
user: s.username || s.user,
|
user: s.username || s.user,
|
||||||
password: s.password,
|
password: s.password,
|
||||||
timezone: s.timezone,
|
timezone: s.timezone,
|
||||||
// database: s.database, // Don't configure the DB here so that the pool can be used for multiple DBs
|
|
||||||
debug: s.debug,
|
debug: s.debug,
|
||||||
socketPath: s.socketPath,
|
socketPath: s.socketPath,
|
||||||
charset: s.collation.toUpperCase(), // Correct by docs despite seeming odd.
|
charset: s.collation.toUpperCase(), // Correct by docs despite seeming odd.
|
||||||
supportBigNumbers: s.supportBigNumbers,
|
supportBigNumbers: s.supportBigNumbers,
|
||||||
connectionLimit: s.connectionLimit || 10
|
connectionLimit: s.connectionLimit
|
||||||
});
|
};
|
||||||
|
|
||||||
|
// Don't configure the DB if the pool can be used for multiple DBs
|
||||||
|
if(!s.createDatabase) {
|
||||||
|
options.database = s.database;
|
||||||
|
}
|
||||||
|
|
||||||
|
dataSource.client = mysql.createPool(options);
|
||||||
|
|
||||||
dataSource.client.on('error', function (err) {
|
dataSource.client.on('error', function (err) {
|
||||||
dataSource.emit('error', err);
|
dataSource.emit('error', err);
|
||||||
|
dataSource.connected = false;
|
||||||
|
dataSource.connecting = false;
|
||||||
});
|
});
|
||||||
|
|
||||||
if (s.debug) {
|
if (s.debug) {
|
||||||
|
@ -105,49 +119,66 @@ MySQL.prototype.query = function (sql, callback) {
|
||||||
if (debug) {
|
if (debug) {
|
||||||
console.log('SQL:', sql);
|
console.log('SQL:', sql);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function releaseConnectionAndCallback(connection, err, result) {
|
||||||
|
connection.release();
|
||||||
|
callback && callback(err, result);
|
||||||
|
}
|
||||||
|
|
||||||
client.getConnection(function (err, connection) {
|
client.getConnection(function (err, connection) {
|
||||||
if (err) {
|
if (err) {
|
||||||
callback && callback(err);
|
callback && callback(err);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
connection.query('USE `' + db + '`', function (err) {
|
if (self.settings.createDatabase) {
|
||||||
if (err) {
|
// Call USE db ...
|
||||||
if (err && err.message.match(/(^|: )unknown database/i)) {
|
connection.query('USE `' + db + '`', function (err) {
|
||||||
var charset = self.settings.charset;
|
if (err) {
|
||||||
var collation = self.settings.collation;
|
if (err && err.message.match(/(^|: )unknown database/i)) {
|
||||||
var q = 'CREATE DATABASE ' + db + ' CHARACTER SET ' + charset + ' COLLATE ' + collation;
|
var charset = self.settings.charset;
|
||||||
connection.query(q, function (error) {
|
var collation = self.settings.collation;
|
||||||
if (!error) {
|
var q = 'CREATE DATABASE ' + db + ' CHARACTER SET ' + charset + ' COLLATE ' + collation;
|
||||||
connection.query('USE `' + db + '`', function (err) {
|
connection.query(q, function (err) {
|
||||||
connection.query(sql, function (err, result) {
|
if (!err) {
|
||||||
connection.release();
|
connection.query('USE `' + db + '`', function (err) {
|
||||||
callback && callback(err, result);
|
connection.query(sql, function (err, result) {
|
||||||
|
releaseConnectionAndCallback(connection, err, result);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
} else {
|
||||||
} else {
|
releaseConnectionAndCallback(connection, err);
|
||||||
connection.release();
|
}
|
||||||
callback && callback(err);
|
});
|
||||||
}
|
return;
|
||||||
});
|
} else {
|
||||||
return;
|
releaseConnectionAndCallback(connection, err);
|
||||||
} else {
|
return;
|
||||||
connection.release();
|
}
|
||||||
callback && callback(err);
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
}
|
connection.query(sql, function (err, data) {
|
||||||
|
if (debug) {
|
||||||
|
if (err) {
|
||||||
|
console.error('Error:', err);
|
||||||
|
}
|
||||||
|
console.log('Data:', data);
|
||||||
|
}
|
||||||
|
if (log) log(sql, time);
|
||||||
|
releaseConnectionAndCallback(connection, err, data);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
// Bypass USE db
|
||||||
connection.query(sql, function (err, data) {
|
connection.query(sql, function (err, data) {
|
||||||
if (debug) {
|
if (debug) {
|
||||||
if(err) {
|
if (err) {
|
||||||
console.error('Error:', err);
|
console.error('Error:', err);
|
||||||
}
|
}
|
||||||
console.log('Data:', data);
|
console.log('Data:', data);
|
||||||
}
|
}
|
||||||
if (log) log(sql, time);
|
if (log) log(sql, time);
|
||||||
connection.release();
|
releaseConnectionAndCallback(connection, err, data);
|
||||||
callback && callback(err, data);
|
|
||||||
});
|
});
|
||||||
});
|
}
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -1057,7 +1088,7 @@ function unsigned(p, dt) {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Disconnect from MongoDB
|
* Disconnect from MySQL
|
||||||
*/
|
*/
|
||||||
MySQL.prototype.disconnect = function () {
|
MySQL.prototype.disconnect = function () {
|
||||||
if(this.debug) {
|
if(this.debug) {
|
||||||
|
|
|
@ -8,7 +8,7 @@ describe('migrations', function() {
|
||||||
before(function() {
|
before(function() {
|
||||||
require('./init.js');
|
require('./init.js');
|
||||||
|
|
||||||
odb = getDataSource({collation: 'utf8_general_ci'});
|
odb = getDataSource({collation: 'utf8_general_ci', createDatabase: true});
|
||||||
db = odb;
|
db = odb;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -57,7 +57,7 @@ function charsetTest(test_set, test_collo, test_set_str, test_set_collo, done){
|
||||||
assert.ok(!err);
|
assert.ok(!err);
|
||||||
odb.client.end(function(){
|
odb.client.end(function(){
|
||||||
|
|
||||||
db = getSchema({collation: test_set_collo});
|
db = getSchema({collation: test_set_collo, createDatabase: true});
|
||||||
DummyModel = db.define('DummyModel', {string: String});
|
DummyModel = db.define('DummyModel', {string: String});
|
||||||
db.automigrate(function(){
|
db.automigrate(function(){
|
||||||
var q = 'SELECT DEFAULT_COLLATION_NAME FROM information_schema.SCHEMATA WHERE SCHEMA_NAME = ' + db.client.escape(db.settings.database) + ' LIMIT 1';
|
var q = 'SELECT DEFAULT_COLLATION_NAME FROM information_schema.SCHEMATA WHERE SCHEMA_NAME = ' + db.client.escape(db.settings.database) + ' LIMIT 1';
|
||||||
|
|
|
@ -11,7 +11,8 @@ global.getConfig = function(options) {
|
||||||
port: config.port || 3306,
|
port: config.port || 3306,
|
||||||
database: 'myapp_test',
|
database: 'myapp_test',
|
||||||
username: config.username,
|
username: config.username,
|
||||||
password: config.password
|
password: config.password,
|
||||||
|
createDatabase: true
|
||||||
};
|
};
|
||||||
|
|
||||||
if (options) {
|
if (options) {
|
||||||
|
|
Loading…
Reference in New Issue