doobie icon indicating copy to clipboard operation
doobie copied to clipboard

use circe.Json as case class property type that ends up in a jsonb column in postgres

Open rbmdotdev opened this issue 3 years ago • 3 comments

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

rbmdotdev avatar Nov 04 '21 22:11 rbmdotdev

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.

jatcwang avatar Nov 05 '21 10:11 jatcwang

Thank you for the quick reply.

hmmm I get the same error when I change the type to the more general Json type

rbmdotdev avatar Nov 05 '21 15:11 rbmdotdev

If this is still an issue, can you provide a minimal example? (e.g. scastie.scala-lang.org)

jatcwang avatar Jul 25 '22 23:07 jatcwang