Issuing multiple sql queries in order, but without waiting for round trips?
I need to run several commands to set up transaction variables for my use case. They need to be run in order, but I don't need the result of any of them. Is there a way to issue e.g. 3 sequential commands in a single query to the database so I don't have to wait for the round trip for each one, like happens now with
await tx.execute(.....)
3 times in a row?12 Replies
Checkout this SO https://stackoverflow.com/a/55844728
Stack Overflow
Execute an Array of promises sequentially without using async/await
Lets say I have an array of promises. Each element of my array is a knex.js query builder and is ready to be executed and returns a promise.
How can I run each element of this array sequentially.
...
I've never tried it but it looks interesting
The problem I see with this is that they will wait for the previous one
The other think is sending the promise without awaiting, one after the other
But then your synchronous code will probably run before the the first 3 resolves. I don't know how bad will it be
Lastly, I believe that postgres-js has an API for this. But you'll need to use it directly without drizzle
yeah, this is exactly the same as
await 1; await 2; await 3
hm I'm not sure postgres-js does this either. looks like it's a new postgres feature in 14 https://www.postgresql.org/docs/devel/libpq-pipeline-mode.html
PostgreSQL Documentation
33.5. Pipeline Mode
33.5. Pipeline Mode # 33.5.1. Using Pipeline Mode 33.5.2. Functions Associated with Pipeline Mode 33.5.3. When to Use Pipeline Mode libpq …
Not Postgres, I meant postgres-js
GitHub
GitHub - porsager/postgres: Postgres.js - The Fastest full featured...
Postgres.js - The Fastest full featured PostgreSQL client for Node.js, Deno, Bun and CloudFlare - GitHub - porsager/postgres: Postgres.js - The Fastest full featured PostgreSQL client for Node.js, ...
There is an option to give the transaction an array of queries
ah yes, that is the pipelining feature I was talking about
hm
is there any way to get access to the underlying postgres-js transaction instance from a drizzle transaction instance?
No, if you want to go this route you'll need to export the same driver that you pass to drizzle so you can use it directly like this
ah, the problem is that I want to issue these pipelined commands in the same transaction that I then use to do drizzle things in
guess there is no way to do so
This should be easy to implement because the driver supports it. Can you add a feature request?
Now, I don't really know if postgres-js actually makes only one network round trip
ah, it doesn't actually work, since you can't provide the array and also do normal transaction functional stuff. I didn't realize that at first. but thank you for looking!