Refactor transaction to batched expression
Im trying to refactor a transaction to a batched expression. I need help with basic sql concepts involving updating tables based on sub queries, and also drizzle syntax such as .
This is my current transaction to expire orders and release related stock:
6 Replies
This is what I've tried:
The worst part about the current solution (transaction), other than the round trips, is the looped updates. If someone could provide an improvement I might stick with that implementation.
I think the subquery solution is better, just 2 round trips to the db
This one should work
This will be a lot better when we have:
That will make it great because the subqueries will be reused internally in the db.
There's an open PR for it https://github.com/drizzle-team/drizzle-orm/pull/1578
GitHub
Improve
with
clause by L-Mario564 · Pull Request #1578 · drizzle-...Addresses #344 and #1541.
This PR makes it so you can use a with clause alongside insert, update and delete.
Things worth mentioning:
MySQL doesn't support with ... insert.
selectDistinct and ...
Thanks a lot Angelelz, will give this a go in the morning!
@Angelelz please take a look at my comments. It's nearly working, but I think this is as far as I can get without the forementioned PR.
If I change the line in question to
.set({ stock: sql’${expiredProductsSq.amount}’ })
And the drizzle studio, the stock field for that product is
“amount”
When it should be a number.
I think the query is out of scope, especially because i am selecting only the id from it in the where clause.
Is there a way i can run expiredProductsSq in the batch expression so that its “loaded” by the time i run releaseProducts?
I don't think queries in a batch have access to each other
Yeah, this won't work, until we have update ... from
That update query doesn't have access to expiredProductsSq
It has to be included in the query somehow. The only way is with an update .. from query
But you should be able to write it with db.execute(sql...)
Yea I think that's gonna be the best solution -- to write raw sql.
Ill keep an eye on the PR and implement a drizzle solution as one becomes available.
Thanks 💪