Merge pull request #166 from strongloop/feature/lazy-connect

Add function connect
This commit is contained in:
Janny 2016-05-13 08:41:01 -04:00
commit a63765fe7f
2 changed files with 80 additions and 20 deletions

View File

@ -31,9 +31,15 @@ exports.initialize = function initializeDataSource(dataSource, callback) {
dataSource.EnumFactory = EnumFactory; // factory for Enums. Note that currently Enums can not be registered. dataSource.EnumFactory = EnumFactory; // factory for Enums. Note that currently Enums can not be registered.
if(callback) {
if(dataSource.settings.lazyConnect) {
process.nextTick(function() { process.nextTick(function() {
callback && callback(); callback();
}); });
} else{
dataSource.connector.connect(callback);
}
}
}; };
exports.MySQL = MySQL; exports.MySQL = MySQL;
@ -60,7 +66,41 @@ function defineMySQLTypes(dataSource) {
*/ */
function MySQL(settings) { function MySQL(settings) {
SqlConnector.call(this, 'mysql', settings); SqlConnector.call(this, 'mysql', settings);
}
require('util').inherits(MySQL, SqlConnector);
MySQL.prototype.connect = function(callback) {
var self = this;
var options = generateOptions(this.settings);
var s = self.settings || {};
if (this.client) {
if (callback) {
process.nextTick(function() {
callback(null, self.client);
});
}
} else {
this.client = mysql.createPool(options);
this.client.getConnection(function(err, connection) {
var conn = connection;
if (!err) {
if (self.debug) {
debug('MySQL connection is established: ' + self.settings || {});
}
connection.release();
} else {
if (self.debug || !callback) {
console.error('MySQL connection is failed: ' + self.settings || {}, err);
}
}
callback && callback(err, conn);
});
}
}
function generateOptions (settings) {
var s = settings || {}; var s = settings || {};
if (s.collation) { if (s.collation) {
// Charset should be first 'chunk' of collation. // Charset should be first 'chunk' of collation.
@ -110,22 +150,8 @@ function MySQL(settings) {
} }
} }
} }
return options;
this.client = mysql.createPool(options);
this.client.on('error', function(err) {
dataSource.emit('error', err);
dataSource.connected = false;
dataSource.connecting = false;
});
if (debug.enabled) {
debug('Settings: %j', s);
}
} }
require('util').inherits(MySQL, SqlConnector);
/** /**
* Execute the sql statement * Execute the sql statement
* *

View File

@ -5,6 +5,7 @@
require('./init.js'); require('./init.js');
var assert = require('assert'); var assert = require('assert');
var should = require('should');
var DataSource = require('loopback-datasource-juggler').DataSource; var DataSource = require('loopback-datasource-juggler').DataSource;
var mysqlConnector = require('../'); var mysqlConnector = require('../');
var url = require('url'); var url = require('url');
@ -76,6 +77,39 @@ describe('connections', function () {
}); });
}); });
}); });
describe('lazyConnect', function() {
it('should skip connect phase (lazyConnect = true)', function(done) {
var dbConfig = {
host: '127.0.0.1',
port: 4,
lazyConnect: true,
};
var ds = new DataSource(mysqlConnector, dbConfig);
var errTimeout = setTimeout(function() {
done();
}, 2000);
ds.on('error', function(err) {
clearTimeout(errTimeout);
done(err);
});
});
it('should report connection error (lazyConnect = false)', function(done) {
var dbConfig = {
host: '127.0.0.1',
port: 4,
lazyConnect: false,
};
var ds = new DataSource(mysqlConnector, dbConfig);
ds.on('error', function(err) {
err.message.should.containEql('ECONNREFUSED');
done();
});
});
});
}); });
function charsetTest(test_set, test_collo, test_set_str, test_set_collo, done) { function charsetTest(test_set, test_collo, test_set_str, test_set_collo, done) {