Search code examples
scalaslickslick-3.0

Specifying sorting order for MappedColumnType based column in Slick


I have custom MappedColumnType for Java8 LocalDateTime, defined like this:

implicit val localDTtoDate = MappedColumnType.base[LocalDateTime, Timestamp] (
    l => Timestamp.valueOf(l),
    d => d.toLocalDateTime
)

Columns of that type are used in table mappings in that way:

def timestamp = column[LocalDateTime]("ts")

Everything looks good, but i'm not able to sort on that column with different directions, cause it lacks .asc and .desc (and, actually, is not a ColumnOrdered type). How can i add sorting functionality for that type?


Solution

  • You can use sort and do .desc and .asc. But, ensure the mapping implicit val is in scope of the query where you are using .desc and .asc, if not you will get compilation error.