I need to use plain sql to make a query in Slick, and then cast the resulting rows into the db models. I'm unable to figure out how to convert an array postgres column (in this case an array of strings) to a scala List.
Example query
sql"""SELECT n.id, n.created_at, n.values
FROM names n
WHERE n.id = $id""".as[NamesDB])
Where values is an array of strings.
DB Model:
case class NamesDB(
id: Long
createdAt: Timestamp,
values: List[String]
)
and table definition:
class Names(tag: Tag) extends Table[NamesDB](tag, "names"){
def id = column[Long]("id", O.PrimaryKey, O.AutoInc)
def createdAt = column[Timestamp]("created_at")
def values = column[List[String]]("values")
override def * = (
id,
createdAt,
values
) <> ((NamesDB.apply _).tupled, NamesDB.unapply)
}
This works, but it feels like there must be a better way:
implicit val NamesDBRecord = GetResult(r => {
val id = r.nextLong()
val createdAt = r.nextTimestamp()
val values = r.nextString() // "{name1, name2}"
.drop(1).dropRight(1)
.split(",").toList //
NamesDB(id, createdAt, values)
})
I have been attempting to read slick's source code to see how they convert pg column types to scala/java types, but it's not super clear where to look for this.