Search code examples
javascalaslick

Slick comparing Rep[Option[ZonedDateTime]] to ZonedDateTime


I'm trying to compare ZonedDateTime with following code:

val now = ZonedDateTime.now()

val query = for {
  x <- xTable.query if x === id
  if x.starts.isAfter(now) // Doesn't work
} yield x

...slick.run(query.result)

But it seems that I can't access .isAfter because x.starts is Rep[Option[...]], is there a better way to do what I'm trying to achieve?


Solution

  • From what you've described, it sounds like a suitable column type mapping might be missing. For date/time schemas, Slick only supports JDBC-based java.sql.{Date, Time, Timestamp}. You would need an implicit mapper in scope wherever ZonedDateTime is used. The mapper should look something like below:

    import java.sql.Timestamp
    import java.time.ZonedDateTime
    import scala.slick.driver.JdbcProfile.MappedColumnType
    
    implicit val zonedDateTimeMapper = MappedColumnType.base[ZonedDateTime, Timestamp](
      zdt => Timestamp.from(zdt.toInstant),
      ts => ZonedDateTime.ofInstant(ts.toInstant, ZoneOffset.UTC)
    )