Inserting
You can insert rows into the table by specifying the individual columns:
import cats.data.NonEmptyVector
Users.insertInto(NonEmptyVector.of(
Users.id --> 42,
Users.firstName --> "John",
Users.lastName --> "Doe",
Users.nickname --> None,
))
// res0: Fragment = Fragment("INSERT INTO users (id, first_name, last_name, nickname) VALUES (?, ?, ?, ?)")
You can use the composite columns as well:
import cats.data.NonEmptyVector
Users.insertInto(NonEmptyVector.of(
Users.id --> 42,
) ++: (Users.Names ==> Users.Names("John", "Doe", Some("Johnny"))))
// res1: Fragment = Fragment("INSERT INTO users (id, first_name, last_name, nickname) VALUES (?, ?, ?, ?)")
Or you can use the Row
case class:
// Insert single row.
Users.insertInto(Users.Row ==> Users.Row(42, Users.Names("John", "Doe", Some("Johnny"))))
// res2: Fragment = Fragment("INSERT INTO users (id, first_name, last_name, nickname) VALUES (?, ?, ?, ?)")
Users.Row.insert.toUpdate0(Users.Row(42, Users.Names("John", "Doe", Some("Johnny"))))
// res3: Update0 = doobie.util.update$Update$$anon$3@118ccda1
// Batch insert.
Users.Row.insert.updateMany(NonEmptyVector.of(
Users.Row(42, Users.Names("John", "Doe", Some("Johnny"))),
Users.Row(43, Users.Names("Jane", "Doe", None))
))
// res4: Free[[A >: Nothing <: Any] => ConnectionOp[A], Int] = Suspend(
// a = Uncancelable(
// body = cats.effect.kernel.MonadCancel$$Lambda/0x00007fae7a281000@26f21366
// )
// )