Calculate a derived column in the select output - Scala Slick 3.2.3

17 views
Skip to first unread message

Ujjal Dey

unread,
Mar 8, 2020, 12:04:04 PM3/8/20
to Slick / ScalaQuery
I am trying to write some REST API to fetch the data using Scala Slick 3.2.3. Is there a way to calculate a derived column and include it in the returned output?

My model:

case class Task(id: Option[TaskId], title: String, dueOn: String, status: String, createdAt: String, updatedAt: String)

Table class:

class TasksTable(tag: Tag) extends Table[Task](tag, _tableName = "TASKS") {
  def id: Rep[TaskId] = column[TaskId]("ID", O.PrimaryKey, O.AutoInc)
  def title: Rep[String] = column[String]("TITLE")
  def dueOn: Rep[String] = column[String]("DUE_ON")
  def status: Rep[String] = column[String]("STATUS")
  def createdAt: Rep[String] = column[String]("CREATED_AT")
  def updatedAt: Rep[String] = column[String]("UPDATED_AT")
  def * = (id.?, title, dueOn, status, createdAt, updatedAt) <> ((Task.apply _).tupled, Task.unapply)
}

DAO:

object TasksDao extends BaseDao {
  def findAll: Future[Seq[Task]] = tasksTable.result
}

I want to add a column in the response json called timeline with values "overdue", "today", "tomorrow", "upcoming", etc. calculated based on the dueOn value. I have also created a new case class TaskResonse. But I am stuck in creating the TaskResponse data in def findAll: Future[Seq[Task]] = tasksTable.result.

I tried searching but could not find any help. Any help with an example or any pointers would be highly appreciated. Thanks!

Craig Tataryn

unread,
Mar 11, 2020, 1:36:11 PM3/11/20
to Slick / ScalaQuery
Unless I'm missing something, in this case you should be able to just extend your Task model like so:

case class Task(id: Option[TaskId], title: String, dueOn: String, status: String, createdAt: String, updatedAt: String) {

def timeline = {
// code that derives its value from dueOn
}
}
Reply all
Reply to author
Forward
0 new messages