node-jdbc icon indicating copy to clipboard operation
node-jdbc copied to clipboard

Implement promise based API in addition to callback API

Open lachlanhunt opened this issue 8 years ago • 9 comments

I'm planning to use this library, but I'm wondering are there any plans to convert this library to be based on Promises instead of old style nodejs callbacks? This would be particularly useful now that Node supports async/await. My current attempts at doing this will Bluebird's Promisify feature is tedious since I not only need to promisify JDBC, but also the Connection, Statement and ResultSet objects when they get returned.

Given that doing this would likely be a breaking API change, are you willing to accept PRs for such a change?

lachlanhunt avatar Mar 21 '17 05:03 lachlanhunt

no, please don't

... but if you do, retain the callback API instead of deprecating/ replacing it.

bguiz avatar Mar 21 '17 07:03 bguiz

OK, no worries. Then the options are:

a) Introduce new Async() methods (like executeQueryAsync) into each class that are a promisified version of the original methods. This could possibly be done by using Bluebird's .promisifyAll() on every object before it gets returned.

b) Release a new npm module that is basically a wrapper around this module, which promisifies every object as its returned.

lachlanhunt avatar Mar 21 '17 07:03 lachlanhunt

b. sounds like the best option, no need to pollute this module to appease promise users.

KoryNunn avatar Mar 21 '17 23:03 KoryNunn

b) Release a new npm module that is basically a wrapper around this module, which promisifies every object as its returned.

Isn't blue-color/nodejdbc kind of what you're describing? Note it has jdbc as a dependency.

aaronbartell avatar Apr 13 '17 21:04 aaronbartell

Isn't blue-color/nodejdbc kind of what you're describing?

It would be, if it worked and was maintained. But it hasn't been maintained in a while and has installation failures due to its dependency on an old broken version of the node java package.

lachlanhunt avatar Apr 17 '17 04:04 lachlanhunt

+1 for the promisified version. Don't care if it is a fork or not. Just hard to have code that is 90% async/await and 10% callbacks.

jmargaglione avatar Oct 25 '17 01:10 jmargaglione

I will have some more time after the new year. I'm am going to implement a promise oriented API (in addition to maintaining the current API).

CraZySacX avatar Dec 02 '17 00:12 CraZySacX

I just did this in my service class:

function promisifyConnectionPool(pool: ConnectionPool): void {
    pool.initializeP = util.promisify(pool.initialize.bind(pool));
    pool.reserveP = util.promisify(pool.reserve.bind(pool));
    pool.releaseP = util.promisify(pool.release.bind(pool));
}

function promisifyConnection(conn: Connection): void {
    conn.setAutoCommitP = util.promisify(conn.setAutoCommit.bind(conn));
    conn.createStatementP = util.promisify(conn.createStatement.bind(conn));
}

function promisifyStatement(stmt: Statement): void {
    stmt.executeUpdateP = util.promisify(stmt.executeUpdate.bind(stmt));
    stmt.executeQueryP = util.promisify(stmt.executeQuery.bind(stmt));
    stmt.setFetchSizeP = util.promisify(stmt.setFetchSize.bind(stmt));
    stmt.setMaxRowsP = util.promisify(stmt.setMaxRows.bind(stmt));
    stmt.setQueryTimeoutP = util.promisify(stmt.setQueryTimeout.bind(stmt));
}

function promisifyResultSet(rs: ResultSet): void {
    rs.closeP = util.promisify(rs.close.bind(rs));
    rs.toObjectIterP = util.promisify(rs.toObjectIter.bind(rs));
}

andrejs-sisojevs avatar Jun 08 '18 04:06 andrejs-sisojevs

Actually, there is no need to call promisifyAll on every object. This can be done just in the same way as for mysql (http://bluebirdjs.com/docs/working-with-callbacks.html#more-common-examples)

const JDBC = require('jdbc');
const Promise = require('bluebird');
Promise.promisifyAll([
  require('jdbc/lib/pool'),
  require('jdbc/lib/connection'),
  require('jdbc/lib/statement'),
  require('jdbc/lib/callablestatement'),
  require('jdbc/lib/preparedstatement'),
  require('jdbc/lib/resultset')
]);

underoll avatar Mar 19 '20 10:03 underoll