I use scala 2.13 and doobie 0.12.1
For example, I have case class
case class UserInfo(name: String, age: Int, hobbies: Vector[String])
I want insert user info in column info as jsonb
sql"""
INSERT INTO users(
id,
info
created_at,
) values (
${id},
${userInfo},
${createdAt},
)
""".update.run.transact(t)
In my DAO I have implicit val
implicit val JsonbMeta: Meta[Json] = Meta
.Advanced.other[PGobject]("jsonb")
.timap[Json](jsonStr => parser.parse(jsonStr.getValue).leftMap[Json](err => throw err).merge)(json => {
val o = new PGobject
o.setType("jsonb")
o.setValue(json.noSpaces)
o
})
But I have compile exception
found : ***.****.UserInfo
[error] required: doobie.syntax.SqlInterpolator.SingleFragment[_]; incompatible interpolation method sql
[error] sql"""
[error] ^
The doobie-postgres-circe module provides
pgEncoderPut
andpgDecoderGet
. With these, and an implicit circeEncoder
andDecoder
in scope, you can create aMeta[UserInfo]
. Then your example insert should work.Example usage: