Skip to content

Commit af2f640

Browse files
committed
Adds db.ping() (dresende#57)
1 parent 392535a commit af2f640

6 files changed

Lines changed: 40 additions & 0 deletions

File tree

lib/Drivers/DML/mysql.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ Driver.prototype.drop = function (opts, cb) {
3535
return require("../DDL/mysql").drop(this, opts, cb);
3636
};
3737

38+
Driver.prototype.ping = function (cb) {
39+
this.db.ping(cb);
40+
return this;
41+
};
42+
3843
Driver.prototype.on = function (ev, cb) {
3944
if (ev == "error") {
4045
this.db.on("error", cb);

lib/Drivers/DML/postgres.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ Driver.prototype.drop = function (opts, cb) {
2626
return require("../DDL/postgres").drop(this, opts, cb);
2727
};
2828

29+
Driver.prototype.ping = function (cb) {
30+
this.db.query("SELECT * FROM pg_stat_activity LIMIT 1", function () {
31+
return cb();
32+
});
33+
return this;
34+
};
35+
2936
Driver.prototype.on = function (ev, cb) {
3037
if (ev == "error") {
3138
this.db.on("error", cb);

lib/Drivers/DML/postgresaxomic.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@ Driver.prototype.sync = function (opts, cb) {
2525
Driver.prototype.drop = function (opts, cb) {
2626
return require("../DDL/postgres").drop(this, opts, cb);
2727
};
28+
29+
Driver.prototype.ping = function (cb) {
30+
this.db.query("SELECT * FROM pg_stat_activity LIMIT 1", function () {
31+
return cb();
32+
});
33+
return this;
34+
};
35+
2836
Driver.prototype.connect = function (cb) {
2937
//console.log("connect called");
3038
cb(null);

lib/Drivers/DML/sqlite.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ Driver.prototype.drop = function (opts, cb) {
3333
return require("../DDL/sqlite").drop(this, opts, cb);
3434
};
3535

36+
Driver.prototype.ping = function (cb) {
37+
process.nextTick(cb);
38+
return this;
39+
};
40+
3641
Driver.prototype.on = function (ev, cb) {
3742
if (ev == "error") {
3843
this.db.on("error", cb);

lib/ORM.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,11 @@ ORM.prototype.define = function (name, properties, opts) {
118118
});
119119
return this.models[name];
120120
};
121+
ORM.prototype.ping = function (cb) {
122+
this.driver.ping(cb);
123+
124+
return this;
125+
};
121126
ORM.prototype.close = function (cb) {
122127
this.driver.close(cb);
123128

test/integration/test-ping.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
var common = require('../common');
2+
var assert = require('assert');
3+
4+
common.createConnection(function (err, db) {
5+
db.ping(function (err) {
6+
assert.equal(err, null);
7+
8+
db.close();
9+
});
10+
});

0 commit comments

Comments
 (0)