brianc / node-pg-pool

A connection pool for node-postgres
MIT License
180 stars 64 forks source link

Promise pg pool connect (v1.8.0) #79

Closed hadasiii closed 6 years ago

hadasiii commented 6 years ago

Hello :) This pr is branched from version v1.8.0, it introduces promised connect. According to documentation (https://node-postgres.com/api/pool) the pool 'onConnect' event is the place to run setup commands on a client. Unfortunately the _create function who's emitting the connect event does not wait on the connect to finish its work, which makes it impossible to run set up functionality like setting query group on the client or any async functionality for that matter. The new code implements 'promise-events' as the EventEmitter. It inherits Node's built-in EventEmitter interface, except that selected methods are overridden to return a promise. (https://www.npmjs.com/package/promise-events) I've also added tests to check that we wait on the connect, plus catching an error if there is one. If you approve the change is it possible to create v1.8.1 for the fix? Tnx! Hadas.