{"success":true,"name":"wrap-endpoint-writes-in-one-transaction-boundary-all-or-noth","description":"An endpoint performs multiple database writes for one operation","category":"ai-brimind","path":"ai-brimind/wrap-endpoint-writes-in-one-transaction-boundary-all-or-noth/SKILL.md","file":"SKILL.md","content":"---\nname: wrap-endpoint-writes-in-one-transaction-boundary-all-or-noth\ndescription: \"An endpoint performs multiple database writes for one operation\"\n---\n\n# Wrap Endpoint Writes in One Transaction Boundary — All or Nothing\n\n## When to use\nA single API request writes to more than one table or row, and the writes currently run independently with no transaction around them.\n\n## Example request\nOrder row saved, stock decrement fails → order exists for stock that was never reserved; one transaction makes both succeed or neither. — Transaction opened, then an external API called inside it → the connection sits locked for seconds; slow external work doesn't belong inside the transaction. — Half the writes in a helper with its own connection → they escape the transaction silently; all writes must share the same transactional context. — Error caught and swallowed mid-sequence → the request returns 200 with half the writes done; failures inside the boundary must roll back and surface.\n\n## Instructions\nAct as a transactional-thinking developer who groups an operation's writes into one atomic unit or explains why not. For multi-write endpoints: 1) List every write the request performs and ask: is a partial subset ever a valid final state? If no — the normal case — they belong in one transaction using the ORM/database's existing mechanism, not manual cleanup code. 2) Keep the boundary tight: open late, close early — validation before it, response formatting after it, and never hold it open across external API calls or slow work; a long transaction is a lock queue. 3) Ensure every write actually joins the transaction: helpers and repositories must receive and use the same transactional context, or they silently escape the boundary. 4) On any failure inside, let it roll back and return an honest error — catching and continuing converts atomicity into corruption. 5) If a write genuinely can't join (an external system), that's not a transaction problem anymore — name it as a distributed-consistency case and handle it with its own pattern. Only ship once the failure test is done (force a mid-sequence failure, confirm nothing persisted), and explain how the boundary guarantees all-or-nothing rather than usually-both.\n\n## Metadata\n- role: developer\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #918\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}