Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use util.promisify.custom to improve promisify support of db.run() #1233

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 18 additions & 0 deletions lib/sqlite3.js
Expand Up @@ -4,6 +4,7 @@ var binding_path = binary.find(path.resolve(path.join(__dirname,'../package.json
var binding = require(binding_path);
var sqlite3 = module.exports = exports = binding;
var EventEmitter = require('events').EventEmitter;
var util = require('util');

function normalizeMethod (fn) {
return function (sql) {
Expand Down Expand Up @@ -77,6 +78,23 @@ Database.prototype.run = normalizeMethod(function(statement, params) {
statement.run.apply(statement, params).finalize();
return this;
});
if (typeof util.promisify === 'function') {
function promisifyRun() {
var thisForRun = this;
var argumentsForRun = arguments;
return new Promise(function(resolve, reject) {
Array.prototype.push.call(
argumentsForRun,
function(err) {
err ? reject(err) : resolve(this);
}
);
thisForRun.run.apply(thisForRun, argumentsForRun);
});
}
Database.prototype.run[util.promisify.custom] = promisifyRun;
Statement.prototype.run[util.promisify.custom] = promisifyRun;
}

// Database#get(sql, [bind1, bind2, ...], [callback])
Database.prototype.get = normalizeMethod(function(statement, params) {
Expand Down
66 changes: 66 additions & 0 deletions test/promisify_run.test.js
@@ -0,0 +1,66 @@
var sqlite3 = require('..');
var assert = require('assert');
var util = require('util');

if (typeof util.promisify === 'function') {
describe('promisify run', function() {
var db;
var promisifyedDbRun;
before(function(done) {
db = new sqlite3.Database(':memory:', done);
promisifyedDbRun = util.promisify(db.run).bind(db);
});

it('should create the table', function() {
return promisifyedDbRun("CREATE TABLE foo (txt TEXT, num INT)")
.then(function(result) {
assert.equal(result.changes, 0);
assert.equal(result.lastID, 0);
});
});

it('should insert a value without placeholders', function() {
return promisifyedDbRun("INSERT INTO foo VALUES('Lorem Ipsum', 1)")
.then(function(result) {
assert.equal(result.changes, 1);
assert.equal(result.lastID, 1);
});
});

it('should update a value with placeholders', function() {
return promisifyedDbRun("UPDATE foo SET txt = $text WHERE num = $id", {
$id: 1,
$text: "Dolor Sit Amet"
})
.then(function(result) {
assert.equal(result.changes, 1);
assert.equal(result.lastID, 1);
});
});

it('should also work with statement', function() {
var stmt = db.prepare("INSERT INTO foo VALUES($text, $id)");
var promisifyedStatementRun = util.promisify(stmt.run).bind(stmt);
return promisifyedStatementRun({
$id: 2,
$text: "Consectetur Adipiscing Elit"
})
.then(function(result) {
assert.equal(result.changes, 1);
assert.equal(result.lastID, 2);
});
});

it('should retrieve values', function(done) {
db.all("SELECT txt, num FROM foo", function(err, rows) {
if (err) throw err;
assert.equal(rows[0].txt, "Dolor Sit Amet");
assert.equal(rows[0].num, 1);
assert.equal(rows[1].txt, "Consectetur Adipiscing Elit");
assert.equal(rows[1].num, 2);
assert.equal(rows.length, 2);
done();
});
});
});
}