Has anyone implemented transactions with Cloudflare D1 and Drizzle?
Hi guys!
As stated in the title, I’m trying to use Drizzle and D1 to build my app, and it requires transactions. I’m also using tRPC.
I was surprised when I saw that my queries didn’t work in prod because of transactions. After reading Cloudflare’s blog, I learned that you need to create a procedure and use it within your API.
Has anyone successfuly done that in a scenario close to mine? Or found any other solution? I want to avoid manually handling data deletion in case of errors.
Maybe Drizzle ORM could read whether we’re using D1 and handle transactions in Cloudfare’s way under the hood?
I’ll appreaciate any help!
5 Replies
Cloudflare D1 doesn't yet support transactions.
Cloudflare d1 supports batch api
Which implemented in drizzle in last release
So all statements under batch will hit one http call and one statements fail will rollback all other statements
Its the way d1 supports only
I'm using the T4 stack and this is what I am wanting to try next. I've got data to be inserted into two related tables and want to send it with one tRPC function and have it rollback if the first insert fails.
I'm using T4 too 😄 Finally got my app working locally and after publishing to Pages and Workers I learned that transactions don't work. I got worried that I may have to rewrite everything
Thank you very much, looks good 😄 But I guess there is no way to get data from one of the queries and use it in next queries?
Like in case I wanted to first insert an address to addresses table, and then assign it to one of the users in the users table?
Unless I generate the UUIDs in JS before
https://discord.com/channels/1043890932593987624/1043890932593987627/1169554258128031805
Check it here hahaha
No way yet
In some scenario you can create uuid manually