簡體   English   中英

Prisma batch upsert with raw SQL

[英]Prisma batch upsert with raw SQL

我想對 postgres 中的表進行批量更新插入。 由於 prisma 在其 api 中不支持此功能,因此我必須使用$executeRaw 我對如何正確使用Prisma.joinPrisma.sql將數據插入模板標簽有點困惑。

這篇文章似乎表明了一個可能的解決方案: https://github.com/prisma/prisma/discussions/15452#discussioncomment-3737632 ,但是 Prisma.sql 的Prisma.sql簽名是export declare function sqltag(strings: ReadonlyArray<string>, ...values: RawValue[]): Sql; 這表明它需要兩個arrays而不是一個字符串。

這是我到目前為止所擁有的:

async function batchUpsertPosts(posts){
  await prisma.$executeRaw`INSERT INTO Posts (
    feedDomain,
    postId,
    title,
    score,
  )
  VALUES (), (), ()--... and so on with posts
  ON CONFLICT ("feedDomain","postId") DO UPDATE SET score = excluded.score`
}

好的,我想我設法弄清楚了。

async function batchUpsertPosts(posts) {
  await prisma.$executeRaw`INSERT INTO "public"."Posts" (
    "feedDomain",
    "postId",
    "title",
    "score",
  )
  VALUES ${Prisma.join(
    posts.map(post =>
      Prisma.sql`(${post.feedDomain}, ${post.postId}, ${post.title} ,${post.score})`
    )
  )}
  ON CONFLICT ("feedDomain","postId") DO UPDATE SET score = excluded.score`
}

所以我只需要使用:

Prisma.sql``

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM