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

Problem when promisify run #658

Closed
zweifisch opened this issue Jun 3, 2016 · 5 comments · May be fixed by #1233
Closed

Problem when promisify run #658

zweifisch opened this issue Jun 3, 2016 · 5 comments · May be fixed by #1233

Comments

@zweifisch
Copy link

zweifisch commented Jun 3, 2016

Currently the callback argument for run only accept one parameter, namely err, and lastID and changes can be get from this inside the callback. When promisify using tools like bluebird, things like lastID are lost. It would be nice to have those passed to the callback as the second argument, and this is also a nodejs convention I believe.

@rnewman
Copy link

rnewman commented Jun 28, 2016

In Tofino we manually promisify a bunch of the less-sane node-sqlite3 interface calls. Feel free to steal!

https://github.com/mozilla/tofino/blob/master/app/services/user-agent-service/sqlite.js#L109

@pabigot
Copy link

pabigot commented Dec 27, 2016

@rnewman Thanks. For others coming in late the link in the above comment is dead and the capability is now available as promise-sqlite.

@tmcw
Copy link
Contributor

tmcw commented Jan 9, 2017

We'd happily review a PR that implements lastID in the results object, but for now, you can use a library like pify that accepts a multiArgs option to successfully pass multiple arguments into a Promise object.

@tmcw tmcw closed this as completed Jan 9, 2017
@fasiha
Copy link

fasiha commented Jan 29, 2019

@tmcw can I get some additional information on using pify with sqlite3? When I adapt the README's example as such:

var sqlite3 = require('sqlite3').verbose();
var db = new sqlite3.Database(':memory:');
var pify = require('pify');

db.serialize(function() {
  db.run("CREATE TABLE lorem (info TEXT)");

  var stmt = db.prepare("INSERT INTO lorem VALUES (?)");
  for (var i = 0; i < 10; i++) { stmt.run("Ipsum " + i); }
  stmt.finalize();

  db.get("SELECT * FROM lorem", function(err, row) { console.log('err', err, 'row', row); });

  pify(db.get, {multiArgs: true})("SELECT * FROM lorem")
      .then(x => console.log('promise', x))
      .catch(e => console.log('promise error', e));
});

db.close();

the callback works but promisified get fails, with this error: promise error TypeError: Database object expected. This actually happens even if I omit the multiArgs option.

Has something changed in the last couple of years in sqlite3 that prevents pify from working, or am I doing something wrong?

@fasiha
Copy link

fasiha commented Jan 29, 2019

Sorry for bugging you all. I realized it was a this/bind issue. Both

  • (pify(db).get.bind(db))("select * from lorem").then // ... and
  • pify(db.get.bind(db))("select * from lorem").then // ...

work as expected. (And if this binding gets tiring, you can use bind-all and do this: pify(bindAll(db)).get('select * from lorem').then // ...!)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants