-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Introduce new query event to elegantly mix evented and callback-style code #109
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
Comments
There are few other options I thought about, but all of them look less attractive than described above.
query
.on('row', function (row) {
// row handling code
})
.on('end', function (result, error) { // function introduced just to reverse arguments
callback(error, result);
});
query
.on('row', function (row) {
// row handling code
})
.on('end', function (result) { // function introduced just to unwrap error
callback(query.error, result);
});
query
.on('row', function (row) {
// row handling code
})
.on('error', function (error) {
callback(error);
})
.on('result', function (result) {
callback(null, result); // won't be emitted if error occurred
}); |
Does the I agree with you that it is currently awkward when both |
Yes, it does
Don't think so. query.on('end', function () {
client.end();
}); |
I think long-term it would be nice for the pg.Query object to implement the interface of the node ReadStream. |
Following standard interface would be nice. However, But pg |
Also, take a look at error handling implementation of |
Found couple of tricks that can help deal with the issue until some resolution will be available.
query
.on('error', cb)
.on('end', function (result) {
if (result) // result is undefined if we got an error
{
cb(null, result);
}
});
// helper function
function execute(client, func, cb) {
// substitue callback with one that will close client and then call original one
func(client, function (err, result) {
client.end();
cb(err, result);
});
}
// usage
execute(client, function (client, cb) {
// any client usage, then call cb as usual
// client.end() will be called in cb
}, cb); |
Sorry - it got pretty late for me last night & I wasn't really giving the best answers. First of all, you're right in saying implementing the stream interface isn't the way to go. Secondly, the 'end' callback should have a result object sometime soon. Postgres returns an "end of query" type of message which contains number of rows affected and so on. This should be made available to the end callback. I'll take a look at this. |
|
I first thought that the I propose we just add a query
.on('row', function (row) {
// row handling code
})
.on('error', function (err) {
callback(err);
})
.on('success', function (result) {
callback(null, result);
});
.on('end', function () {
client.end();
}); This avoids having to scope a This has the advantage that it is backwards compatible, since |
makes sense to me and double ❤️ for backwards compat |
This has to be the easiest feature to add in the universe. |
Is this still an issue? It seems to me that for evented style, use: client.query(queryString)
.on('row', function (row) {
// do some stuff
})
.on('error', callback)
.on('end', function (result) {
if (result)
callback(null, result);
}); and for callback style, just use (of course):
|
Currently query always emits
end
even iferror
was emitted before, andend
handler doesn't know what error occurred if there was one.It leads to bad looking code if you want to continue with callback-style:
It would be great to have some special event to use when we want to continue with a callback:
Just using a callback-style is not a solution, because currently it implies rows collecting that is not what we could want. An option to prevent rows collecting could be useful as separate feature, but attempt to solve the problem this way looks less elegant, because we still need row handling code and it will follow callback code in confusing order:
The text was updated successfully, but these errors were encountered: