Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
rbmdotdev opened this issue Nov 4, 2021 · 3 comments

Comments

@rbmdotdev
Copy link

rbmdotdev commented Nov 4, 2021

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

@jatcwang
Copy link
Collaborator

jatcwang commented Nov 5, 2021

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.

@rbmdotdev
Copy link
Author

Thank you for the quick reply.

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

@jatcwang
Copy link
Collaborator

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants