如何實現透明交易系統(即:prisma.js)?
P粉226413256
P粉226413256 2023-09-09 15:23:51
0
1
282

在 Prisma ORM 文件中,我們可以找到以下將幾個資料庫呼叫分組到一個事務中的範例。我想知道如何實施以下內容。 $transactions() (prisma.post...) 中使用的方法與我們可以使用「獨立」的方法相同。

const [posts, totalPosts] = await prisma.$transaction([
  prisma.post.findMany({ where: { title: { contains: 'prisma' } } }),
  prisma.post.count(),
])

我想知道如何實作這樣的方法($transation())。 我唯一的想法是檢查“上下文”(this),但不確定這是否是最乾淨的想法。

P粉226413256
P粉226413256

全部回覆(1)
P粉087074897

我不確定我是否正確理解了這個問題。 您是否試圖返回標題包含“prisma”一詞的帖子數?如果是這樣,您應該使用互動式交易 API 來實現這一點。

const [posts, totalPosts] = await prisma.$transaction(async (prisma) => {
    const posts = await prisma.post.findMany({
        where: {
            title: {
                contains: 'prisma'
            }
        }
    })
    const count = prisma.post.count({
        where: {
            title: {
                contains: 'prisma'
            }
        }
    })
    return [posts, count]
})
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!