You can use the `transactionSync()`

You can use the transactionSync() https://developers.cloudflare.com/durable-objects/api/storage-api/#transactionsync if you want to control when the writes happen. The error handling is not as clear cut though. You might need to handle the error in your worker calling the DO to make sure all exceptions are caught during that DO transaction. Using the above method guarantees that you won't have partial writes.
7 Replies
veeque
veeque5mo ago
i was already planning on error handling in the worker since that one will also know where the next shard lives. my only concern now is how I would go about using transactionSync() with drizzle (https://orm.drizzle.team/docs/connect-cloudflare-do) drizzle offers https://orm.drizzle.team/docs/transactions, but i don't know. have to look more into it
Drizzle ORM - Cloudflare Durable Objects
Drizzle ORM is a lightweight and performant TypeScript ORM with developer experience in mind.
veeque
veeque5mo ago
worst-case i'll use transactionSync() to wrap the drizzle calls, since i'd assume the DO way of doing it is "safer" than relying on drizzle to know "oops, something wrong"
lambrospetrou
lambrospetrouOP5mo ago
I haven't used Drizzle, so can't help there, unfortunately.
veeque
veeque5mo ago
don't worry, and thank you. do you know if the only way to access a DOs SQL tables is via calls to the SQL API? or is there a HTTP API I can use (kinda like a connection string)
lambrospetrou
lambrospetrouOP5mo ago
No HTTP approach for now. You can expose it with your own worker, or any other library doing that for you like https://github.com/outerbase/browsable-durable-object (I haven't tried it myself though!)
veeque
veeque5mo ago
wait, are transactionSync() and transaction() different in any way besides the asynchronicity? For drizzle I need to run
await this.db.insert(tableName).values(valuesToInsert);
await this.db.insert(tableName).values(valuesToInsert);
but the docs for the non-sync version say don't say "If callback() throws an exception, the transaction will be rolled back." like they do for transactionSync() Some testing seems to indicate that really just the asynchronicity is the difference, it's just that the docs word the two differently
lambrospetrou
lambrospetrouOP5mo ago
They should behave similarly.

Did you find this page helpful?