olivierphi / Node-DBI

A SQL database abstraction layer strongly inspired by the PHP Zend Framework Zend_Db API, with support of multiple Node.js database engines
https://github.com/DrBenton/Node-DBI
93 stars 22 forks source link

connect() is asynchronous, API (esp. isConnected()) encourages synchronous interface #18

Open fatuhoku opened 11 years ago

fatuhoku commented 11 years ago

Node-DBI users are encouraged to write code like the following (in Coffeescript):

db = new DBWrapper 'sqlite3', { ... }
db.connect()
console.error 'Failed to connect to database' if not db.isConnected()

The two solutions to this issue are:

db = new DBWrapper 'sqlite3', { ... }
async.series [
   -> db.connect()
   -> console.error 'Failed to connect to database' if not db.isConnected()
]
olivierphi commented 10 years ago

@fatuhoku Hello !

Thank you for this report. Currently there is a "connection" Event emitted by the DBWrapper instance when the DB connection is resolved, with a "err" param. But you're right, it may be more "Nodish" to be able to provide a callback on the "connect()" method. I'm going to try to add this feature...