Skip to main content
added 681 characters in body
Source Link
David Corral
  • 4.1k
  • 3
  • 29
  • 36

Doobie documentation is quite great, but sometimes you might find yourself in some scenarios that are not directly explained in their docs.

In order to insert directly a case class object (not their attributes), you must define a Write[A] which will tells Doobie how the data must be inserted. This is used when the mapping of the attributes, in the case class, are slightly different from the ones in the database table.

Imagine the following case class:

case class Course (id: UUID, name: String, year: Int)

In this case, we need to define a Write[Course] for doobie, which would be:

// Scala 3:
given Write[Course] = Write[(UUID, String, Int)].contramap(c => (c.id, c.name, c.year))

// Scala 2:
implicit val writer : Write[Course] = Write[(UUID, String, Int)].contramap(c => (c.id, c.name, c.year))

So now, you can run your Update and Doobie will know how to map your columns:

def insertCourse(course: Course): Update0 =
    sql"""INSERT INTO courses (id, name, year) VALUES ($course)""".update

Also, you might need these imports:

import doobie.implicits.*
import doobie.implicits.javasql.*
import doobie.postgres.implicits.*
import doobie.*

If your case class attributes and their types exactly matches with those specified in the table of the database, you don't need to manually specify the Writer[Course] because Doobie will automatically derivate it for you [1] and this should work for you:

case class Course (id: UUID, name: String, year: Int)

def insertCourse(course: Course): Update0 =
  sql"""INSERT INTO courses (id, name, year) VALUES ($course)""".update

Credits to my partner Y.C. that helped me to resolve this issue!

Doobie documentation is quite great, but sometimes you might find yourself in some scenarios that are not directly explained in their docs.

In order to insert directly a case class object (not their attributes), you must define a Write[A] which will tells Doobie how the data must be inserted.

Imagine the following case class:

case class Course (id: UUID, name: String, year: Int)

In this case, we need to define a Write[Course] for doobie, which would be:

// Scala 3:
given Write[Course] = Write[(UUID, String, Int)].contramap(c => (c.id, c.name, c.year))

// Scala 2:
implicit val writer : Write[Course] = Write[(UUID, String, Int)].contramap(c => (c.id, c.name, c.year))

So now, you can run your Update and Doobie will know how to map your columns:

def insertCourse(course: Course): Update0 =
    sql"""INSERT INTO courses (id, name, year) VALUES ($course)""".update

Also, you might need these imports:

import doobie.implicits.*
import doobie.implicits.javasql.*
import doobie.postgres.implicits.*
import doobie.*

Credits to my partner Y.C. that helped me to resolve this issue!

Doobie documentation is quite great, but sometimes you might find yourself in some scenarios that are not directly explained in their docs.

In order to insert directly a case class object (not their attributes), you must define a Write[A] which will tells Doobie how the data must be inserted. This is used when the mapping of the attributes, in the case class, are slightly different from the ones in the database table.

Imagine the following case class:

case class Course (id: UUID, name: String, year: Int)

In this case, we need to define a Write[Course] for doobie, which would be:

// Scala 3:
given Write[Course] = Write[(UUID, String, Int)].contramap(c => (c.id, c.name, c.year))

// Scala 2:
implicit val writer : Write[Course] = Write[(UUID, String, Int)].contramap(c => (c.id, c.name, c.year))

So now, you can run your Update and Doobie will know how to map your columns:

def insertCourse(course: Course): Update0 =
    sql"""INSERT INTO courses (id, name, year) VALUES ($course)""".update

Also, you might need these imports:

import doobie.implicits.*
import doobie.implicits.javasql.*
import doobie.postgres.implicits.*
import doobie.*

If your case class attributes and their types exactly matches with those specified in the table of the database, you don't need to manually specify the Writer[Course] because Doobie will automatically derivate it for you [1] and this should work for you:

case class Course (id: UUID, name: String, year: Int)

def insertCourse(course: Course): Update0 =
  sql"""INSERT INTO courses (id, name, year) VALUES ($course)""".update

Credits to my partner Y.C. that helped me to resolve this issue!

added 188 characters in body
Source Link
David Corral
  • 4.1k
  • 3
  • 29
  • 36

Doobie documentation is quite great, but sometimes you might find yourself in some scenarios that are not directly explained in their docs.

In order to insert directly a case class object (not their attributes), you must define a Write[A] which will tells Doobie how the data must be inserted.

Imagine the following case class:

case class Course (id: UUID, name: String, year: Int)

In this case, we need to define a Write[Course] for doobie, which would be:

// Scala 3:
given Write[Course] = Write[(UUID, String, Int)].contramap(c => (c.id, c.name, c.year))

// Scala 2:
implicit val writer : Write[Course] = Write[(UUID, String, Int)].contramap(c => (c.id, c.name, c.year))

So now, you can run your Update and Doobie will know how to map your columns:

def insertCourse(course: Course): Update0 =
    sql"""INSERT INTO courses (id, name, year) VALUES ($course)""".update

Also, you might need these imports:

import doobie.implicits.*
import doobie.implicits.javasql.*
import doobie.postgres.implicits.*
import doobie.*

Credits to my partner Y.C. that helped me to resolve this issue!

Doobie documentation is quite great, but sometimes you might find yourself in some scenarios that are not directly explained in their docs.

In order to insert directly a case class object (not their attributes), you must define a Write[A] which will tells Doobie how the data must be inserted.

Imagine the following case class:

case class Course (id: UUID, name: String, year: Int)

In this case, we need to define a Write[Course] for doobie, which would be:

// Scala 3:
given Write[Course] = Write[(UUID, String, Int)].contramap(c => (c.id, c.name, c.year))

// Scala 2:
implicit val writer : Write[Course] = Write[(UUID, String, Int)].contramap(c => (c.id, c.name, c.year))

So now, you can run your Update and Doobie will know how to map your columns:

def insertCourse(course: Course): Update0 =
    sql"""INSERT INTO courses (id, name, year) VALUES ($course)""".update

Credits to my partner Y.C. that helped me to resolve this issue!

Doobie documentation is quite great, but sometimes you might find yourself in some scenarios that are not directly explained in their docs.

In order to insert directly a case class object (not their attributes), you must define a Write[A] which will tells Doobie how the data must be inserted.

Imagine the following case class:

case class Course (id: UUID, name: String, year: Int)

In this case, we need to define a Write[Course] for doobie, which would be:

// Scala 3:
given Write[Course] = Write[(UUID, String, Int)].contramap(c => (c.id, c.name, c.year))

// Scala 2:
implicit val writer : Write[Course] = Write[(UUID, String, Int)].contramap(c => (c.id, c.name, c.year))

So now, you can run your Update and Doobie will know how to map your columns:

def insertCourse(course: Course): Update0 =
    sql"""INSERT INTO courses (id, name, year) VALUES ($course)""".update

Also, you might need these imports:

import doobie.implicits.*
import doobie.implicits.javasql.*
import doobie.postgres.implicits.*
import doobie.*

Credits to my partner Y.C. that helped me to resolve this issue!

added 133 characters in body
Source Link
David Corral
  • 4.1k
  • 3
  • 29
  • 36

Doobie documentation is quite great, but sometimes you might find yourself in some scenarios that are not directly explained in their docs.

In order to insert directly a case class object (not their attributes), you must define a Write[A] which will tells Doobie how the data must be inserted.

Imagine the following case class:

case class Course (id: UUID, name: String, year: Int)

In this case, we need to define a Write[Course] for doobie, which would be:

// Scala 3:
given Write[Course] = Write[(UUID, String, Int)].contramap(c => (c.id, c.name, c.year))

// Scala 2:
implicit val writer : Write[Course] = Write[(UUID, String, Int)].contramap(c => (c.id, c.name, c.year))

So now, you can run your Update and Doobie will know how to map your columns:

def insertCourse(course: Course): Update0 =
    sql"""INSERT INTO courses (id, name, year) VALUES ($course)""".update

Credits to my partner Y.C. that helped me to resolve this issue!

Doobie documentation is quite great, but sometimes you might find yourself in some scenarios that are not directly explained in their docs.

In order to insert directly a case class object (not their attributes), you must define a Write[A] which will tells Doobie how the data must be inserted.

Imagine the following case class:

case class Course (id: UUID, name: String, year: Int)

In this case, we need to define a Write[Course] for doobie, which would be:

given Write[Course] = Write[(UUID, String, Int)].contramap(c => (c.id, c.name, c.year))

So now, you can run your Update and Doobie will know how to map your columns:

def insertCourse(course: Course): Update0 =
    sql"""INSERT INTO courses (id, name, year) VALUES ($course)""".update

Credits to my partner Y.C. that helped me to resolve this issue!

Doobie documentation is quite great, but sometimes you might find yourself in some scenarios that are not directly explained in their docs.

In order to insert directly a case class object (not their attributes), you must define a Write[A] which will tells Doobie how the data must be inserted.

Imagine the following case class:

case class Course (id: UUID, name: String, year: Int)

In this case, we need to define a Write[Course] for doobie, which would be:

// Scala 3:
given Write[Course] = Write[(UUID, String, Int)].contramap(c => (c.id, c.name, c.year))

// Scala 2:
implicit val writer : Write[Course] = Write[(UUID, String, Int)].contramap(c => (c.id, c.name, c.year))

So now, you can run your Update and Doobie will know how to map your columns:

def insertCourse(course: Course): Update0 =
    sql"""INSERT INTO courses (id, name, year) VALUES ($course)""".update

Credits to my partner Y.C. that helped me to resolve this issue!

Source Link
David Corral
  • 4.1k
  • 3
  • 29
  • 36
Loading