使用泛型的 Doobie 批量更新

Doobie batch update using generics

你能用泛型在 doobie 中制作 batch update 吗?

此代码:

def insertMany[T](ps: List[T]): Task[List[T]] = {
  val sql = "insert into person (name, age) values (?, ?)"
  Update[T](sql).updateMany(ps)
}

给我:could not find implicit value for parameter W: doobie.util.Write[T]

是的,这是它的样子:

import doobie.implicits._
import doobie._
import monix.eval.Task
import cats.data.NonEmptyList

def insertMany[T: Write](ps: NonEmptyList[T]): ConnectionIO[Int] = {
  val sql = "insert into person (name, age) values (?, ?)"
  Update[T](sql).updateMany(ps)
}


println(insertMany(NonEmptyList.of[String]("aa", "bb")))

The answer is based on gitter conversion with @J0kerPanda