AugustNagro / magnum

A 'new look' for database access in Scala
Apache License 2.0
153 stars 10 forks source link

Support for JSONB #18

Closed williamhaw closed 11 months ago

williamhaw commented 11 months ago

hi @AugustNagro, thanks for this amazing library!

Is it possible to decode JSONB columns from Postgres directly? Or should I query it as string and then decode it separately?

AugustNagro commented 11 months ago

Thanks William, IIRC the postgres JDBC driver doesn't have a JsonB type, so you'll have to use String and decode separately.

If you find it's a common pattern in your code, one option would be to

  1. Define a 'marker type'
  2. Add the marker on your Json case class fields as a intersection type
  3. Create a DbCodec that knows how to deserialize

For example:

opaque type Json = Any

// in a different file
case class Friend(id: Int, name: String) derives MyJsonCodec

case class User(id: Int, friends: Seq[Friend] & Json) derives DbCodec

given JsonBCodec[A](using jsonCodec: MyJsonCodec[A]): DbCodec[A & Json] = ???
williamhaw commented 11 months ago

Thanks!

AugustNagro commented 11 months ago

My pleasure. Feel free to re-open or file another issue if anything comes up.