doobie
doobie copied to clipboard
use circe.Json as case class property type that ends up in a jsonb column in postgres
I wonder if I am missing something about how to get this to work or is it something that's explicitly not supported
I have the following case class
import io.circe._
case class Artefact(uuid: UUID, metaDataJSON: JsonObject)
Now Doobie doesn't like this and gives the following error
no implicit values were found that match type deriving.Mirror.ProductOf[io.circe.Json
from the following code
import doobie.implicits._
import doobie.postgres._
import doobie.postgres.implicits._
import doobie.postgres.circe.jsonb.implicits._
sql"SELECT * FROM Artefacts".query[Artefact].to[List]
The reason I want to do this is I want to give the client the ability to add custom properties to the metadata if they want to
That's because there is no Put/Get (i.e. Meta) for JsonObject. https://github.com/tpolecat/doobie/blob/78b02fb40cf30f0420d88a210100839ba3b6002c/modules/postgres-circe/src/main/scala/doobie/postgres/circe/Instances.scala I think the reasoning is that when you have a JSON column there's no guarantee that all values are JSON objects.
You can define one with something like implicit val metaJsonObjct: Meta[JsonObject] = Meta[Json].timap(jsonToJsonObj)(jsonObjToJson)
. (I have left out the implementation of jsonToJsonObj and jsonObjToJson).
If you only need to read or write the JSON column then you can define just Put/Get, respectively.
Thank you for the quick reply.
hmmm I get the same error when I change the type to the more general Json
type
If this is still an issue, can you provide a minimal example? (e.g. scastie.scala-lang.org)