SQL.transaction method | Bun module | Bun
BuildDocsReferenceGuidesBlogDiscord/
Bun/
SQL/
transactionMtransaction
Search the reference...
/
BuildDocsReferenceGuidesBlogDiscord/
Bun/
SQL/
transactionMtransaction
method
SQL.transactiontransactionT>(fn:
TransactionContextCallbackT>): Promise
ContextCallbackResultT>>;
Alternative method to begin a transaction.
Will reserve a connection for the transaction and supply a scoped sql instance for all transaction uses in the callback function. sql.transaction will resolve with the returned value from the callback function. BEGIN is automatically sent with the optional options, and if anything fails ROLLBACK will be called so the connection can be released and execution can continue.
const [user, account] = await sql.transaction(async sql => {
const [user] = await sql`
insert into users (
name
) values (
'Murray'
)
returning *
`
const [account] = await sql`
insert into accounts (
user_id
) values (
${ user.user_id }
)
returning *
`
return [user, account]
})
transactionT>(options: string,fn:
TransactionContextCallbackT>): Promise
ContextCallbackResultT>>;
Alternative method to begin a transaction with options Will reserve a connection for the transaction and supply a scoped sql instance for all transaction uses in the callback function. sql.transaction will resolve with the returned value from the callback function. BEGIN is automatically sent with the optional options, and if anything fails ROLLBACK will be called so the connection can be released and execution can continue.
const [user, account] = await sql.transaction("read write", async sql => {
const [user] = await sql`
insert into users (
name
) values (
'Murray'
)
returning *
`
const [account] = await sql`
insert into accounts (
user_id
) values (
${ user.user_id }
)
returning *
`
return [user, account]
});
Referenced typestype
TransactionContextCallbackT> =
ContextCallbackT,
TransactionSQL>
Callback function type for transaction contexts
type
ContextCallbackResultT> = T extends PromiseLikeany>[] ?
AwaitPromisesArrayT> : AwaitedT>
Resources
ReferenceDocsGuidesDiscordMerch StoreGitHubBlog Toolkit
RuntimePackage managerTest runnerBundlerPackage runnerProject
Bun 1.0Bun 1.1Bun 1.2Bun 1.3RoadmapContributingLicenseBaked with ❤️ in San Francisco
We're hiring →