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
Transactions with async/await? #1252
Comments
Ok. I've posted my question to Stack Overflow here. |
Because doing transactions are usually pretty application specific and node-postgres aims to be a low layer driver doing the nitty gritty communication between your app & postgres over the wire, I've intentionally left any higher-level transaction handling code out of the library. It's 100% possible to do transactions with just node-postgres (this is what I do in my apps) but in my experience it always ends up looking custom to your application, particularly when inside the transaction the output of a query forms some of the input to a subsequent query. That being said...your code looks similar to how I've done transactions within my own apps using async/await. You can even made a simple abstraction like this: const tx = callback => {
const client = await pool.connect()
try {
await client.query('BEGIN')
try {
await callback(client)
client.query('COMMIT')
} catch(e) {
client.query('ROLLBACK')
}
} finally {
client.release()
}
} This allows you, in your own app, to do things like:
fwiw I avoid using callbacks directly in node whenever I can - I think hope this helps 😄 |
thx for the input @brianc I think it would be nice to have this in the readme or somewhere in the wiki |
for the record, I had an issues because I was using |
Here a different version, based on @brianc's one! https://gist.github.com/zerbfra/70b155fa00b4e0d6fd1d4e090a039ad4 |
Thanks @zerbfra for the gist, I added a comment to the gist for the case where there are parameterized queries. |
Hi @brianc, const tx = callback => {
const client = await pool.connect()
try {
await client.query('BEGIN')
try {
await callback(client) //What does this line do?
client.query('COMMIT')
} catch(e) {
client.query('ROLLBACK')
}
} finally {
client.release()
}
} |
taking a stab at this:
says that tx is a function that takes
So this line in
The effect is that the queries inside the anonymous function are wrapped by |
You can use the package https://www.npmjs.com/package/pg-essential, it applies a patch on top of pg, which gives you some helper function against the client object. In this case you can use the executeTransaction function and pass your function as callback.
|
I'm running Node 7.5.0 with the
--harmony
flag, which enables support for async/await. I'm using the client pool with async/await, which works great and has a nice example here.The example for transactions (here) uses callbacks instead of async/await, so I thought I'd try something like this as a quick test:
This seems to work just fine, but is this a bad idea? Is there some technical or other reason I should be using the callback approach with transactions?
The text was updated successfully, but these errors were encountered: