diff --git a/lib/mysql.js b/lib/mysql.js index b34d178..7527ff6 100644 --- a/lib/mysql.js +++ b/lib/mysql.js @@ -31,9 +31,15 @@ exports.initialize = function initializeDataSource(dataSource, callback) { dataSource.EnumFactory = EnumFactory; // factory for Enums. Note that currently Enums can not be registered. - process.nextTick(function() { - callback && callback(); - }); + if(callback) { + if(dataSource.settings.lazyConnect) { + process.nextTick(function() { + callback(); + }); + } else{ + dataSource.connector.connect(callback); + } + } }; exports.MySQL = MySQL; @@ -60,7 +66,41 @@ function defineMySQLTypes(dataSource) { */ function 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 || {}; if (s.collation) { // Charset should be first 'chunk' of collation. @@ -110,22 +150,8 @@ function MySQL(settings) { } } } - - 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); - } + return options; } - -require('util').inherits(MySQL, SqlConnector); - /** * Execute the sql statement * @@ -250,7 +276,7 @@ MySQL.prototype._modifyOrCreate = function(model, data, options, fields, cb) { cb(err, data, meta); }); }; - + /** * Replace if the model instance exists with the same id or create a new instance * @@ -274,7 +300,7 @@ MySQL.prototype.replaceOrCreate = function(model, data, options, cb) { */ MySQL.prototype.save = MySQL.prototype.updateOrCreate = function(model, data, options, cb) { - var fields = this.buildFields(model, data); + var fields = this.buildFields(model, data); this._modifyOrCreate(model, data, options, fields, cb); }; diff --git a/test/connection.test.js b/test/connection.test.js index cad94ff..9dcda97 100644 --- a/test/connection.test.js +++ b/test/connection.test.js @@ -5,6 +5,7 @@ require('./init.js'); var assert = require('assert'); +var should = require('should'); var DataSource = require('loopback-datasource-juggler').DataSource; var mysqlConnector = require('../'); 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) {