From 3875c561e0837530b09f365cfc64f9bab39d8ae2 Mon Sep 17 00:00:00 2001 From: Raymond Feng Date: Wed, 20 Aug 2014 16:31:23 -0700 Subject: [PATCH] Add ping() to test connections --- lib/datasource.js | 18 ++++++++++++++++++ test/basic-querying.test.js | 10 +++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/lib/datasource.js b/lib/datasource.js index 36be539d..ffd2608c 100644 --- a/lib/datasource.js +++ b/lib/datasource.js @@ -1884,6 +1884,24 @@ DataSource.prototype.ready = function (obj, args) { return true; }; +/** + * Ping the underlying connector to test the connections + * @param {Function} [cb] Callback function + */ +DataSource.prototype.ping = function (cb) { + var self = this; + if (self.connector.ping) { + this.connector.ping(cb); + } else if (self.connector.discoverModelProperties) { + self.discoverModelProperties('dummy', {}, cb); + } else { + process.nextTick(function () { + var err = self.connected ? null : 'Not connected'; + cb(err); + }); + } +}; + /** * Define a hidden property * @param {Object} obj The property owner diff --git a/test/basic-querying.test.js b/test/basic-querying.test.js index dd26928a..4fc8feb9 100644 --- a/test/basic-querying.test.js +++ b/test/basic-querying.test.js @@ -7,7 +7,6 @@ describe('basic-querying', function () { before(function (done) { db = getSchema(); - User = db.define('User', { seq: {type: Number, index: true}, name: {type: String, index: true, sort: true}, @@ -22,6 +21,15 @@ describe('basic-querying', function () { }); + describe('ping', function () { + it('should be able to test connections', function (done) { + db.ping(function (err) { + should.not.exist(err); + done(); + }); + }); + }); + describe('findById', function () { before(function (done) {