温馨提示:本站仅提供公开网络链接索引服务,不存储、不篡改任何第三方内容,所有内容版权归原作者所有
AI智能索引来源:http://www.bun.com/reference/bun/SQL/transaction
点击访问原文链接

SQL.transaction method | Bun module | Bun

SQL.transaction method | Bun module | BunBuildDocsReferenceGuidesBlogDiscord/Bun/SQL/transactionMtransaction

Search the reference...

/

BuildDocsReferenceGuidesBlogDiscord/Bun/SQL/transactionMtransaction

method

SQL.transactiontransactionT>(fn: TransactionContextCallbackT>): PromiseContextCallbackResultT>>;

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>): PromiseContextCallbackResultT>>;

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 runner

Project

Bun 1.0Bun 1.1Bun 1.2Bun 1.3RoadmapContributingLicense

Baked with ❤️ in San Francisco

We're hiring →

SQL.transaction method | Bun module | Bun,AI智能索引,全网链接索引,智能导航,网页索引

    API documentation for method bun.SQL.transaction | Bun