6

Can you make batch update in doobie with generic types?

This code:

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

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

techkuz
  • 3,608
  • 5
  • 34
  • 62

1 Answers1

8

Yes, here how it would look like:

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

techkuz
  • 3,608
  • 5
  • 34
  • 62