many-to-one transaction

Hello, I want to create transaction involving arbitrary number of inserts. For example, Artist (one table) and insert multiple Titles (another table). Not clear to me how to best accomplish based example provided:
const results = await conn.transaction(async (tx) => {
const whenBranch = await tx.execute('INSERT INTO branches (database_id, name) VALUES (?, ?)', [42, "planetscale"])
const whenCounter = await tx.execute('INSERT INTO slotted_counters(record_type, record_id, slot, count) VALUES (?, ?, RAND() * 100, 1) ON DUPLICATE KEY UPDATE count = count + 1', ['branch_count', 42])
return [whenBranch, whenCounter]
})
const results = await conn.transaction(async (tx) => {
const whenBranch = await tx.execute('INSERT INTO branches (database_id, name) VALUES (?, ?)', [42, "planetscale"])
const whenCounter = await tx.execute('INSERT INTO slotted_counters(record_type, record_id, slot, count) VALUES (?, ?, RAND() * 100, 1) ON DUPLICATE KEY UPDATE count = count + 1', ['branch_count', 42])
return [whenBranch, whenCounter]
})
Also, related, is there a way for me to include non db transactions as part of transaction, such that if exception thrown say, for example, copy object to S3 bucket fails, transaction also rolls back? Thanks!
4 Replies
hotshoe
hotshoe15mo ago
Never mind on first part -- found I can simply pass array into values for matching type. Too easy :0) Still curious about if a way I can include non-db calls into transaction.
Luxaritas
Luxaritas15mo ago
You could call tx.rollback() if a non-db call fails (Which would abort/rollback the DB transaction)
Luxaritas
Luxaritas15mo ago
However be wary about the semantics there - when working with multiple systems, you wind up with CAP theory tradeoffs. There's a pretty good talk discussing it if you're interested: https://www.youtube.com/watch?v=qhrd3vq5jkY Though I don't necessarily fully agree with the conclusions of that talk: https://twitter.com/luxaritas/status/1475297902067167236?s=20
NDC Conferences
YouTube
Eliminating Hidden Dangers to Your Applications Patterns for Relia...
There is a danger lurking in every system that hasn't taken explicit steps to eliminate it. This under-recognized data killer can cause inconsistencies in your system which can be as difficult to find as they are to correct. Fortunately, recognizing this monster is easy, and there are a number of great techniques you can use to keep it away for ...
Jonathan Romano (@luxaritas)
@bsstahl Just watched your NDC talk about avoiding dual writes - great content, but I'm left with a question: How does splitting tasks into different execution contexts as you described resolve/reduce issues encountered with dual writes?
Twitter
hotshoe
hotshoe15mo ago
Thanks @Luxaritas , much appreciate the suggestion and pointer. Will take a closer look.
Want results from more Discord servers?
Add your server
More Posts
[BUG] drizzle-kit doesn't like imports outside of a module`npx drizzle-kit generate:pg drizzle-kit: v0.17.4 drizzle-orm: v0.23.10 C:\Users\Abdul\Documents\Gidrizzle-kit throwing await is not available in the configured target environmentHi, when using the generate:pg command i am getting this error: **'Top-level await is not available [Bug] Postgres migrations are broken on 0.23.10Re-running the same postgres migration twice just throws an error that a particular table already exdrizzle-kit doesn't pull foreign keys from supabase/postgres?It's hard to really turn this into a proper bug report because, honestly, with Supabase there could Why does drizzle-zod create ZodString's instead of z.ZodStrings for mySQL?I was testing out drizzle-zod for mySQL, and I know it's still actively being worked on. It seems liQuery for Prisma like insert in multiple joined tablesIs there a way to achieve insert in multiple tables joined by foreign keys in one query like Prisma?Iterator for result setI've got some big ass database tables that I'd rather not read into memory. In other languages thereHow to filter length in where for postgres?I have a text field, which I want to select only if it's length is higher than 50. How should I do tIs there currently a way to utilize onUpdate?Trying to run `ON UPDATE NOW()` for a `last_updated` column for whenever the row is changed.Migrations not working on github actionsAny suggestions to debug this on actions? Is it possible that the database is not created?