How can I apply a default filter on a Slick TableQuery object?

In slick, I have a setup that looks something like this:

class Users(tag: Tag) extends Table(tag) {
  def name = column[String]
  def disabled = column[Boolean]
  def * = ...
}

object Users extends TableQuery(new Users(_)) {}

      

I want someone to use an object Users

to query the database, excluding disabled users. For example:

Users.where(_.name === "Fred")

performs:

select * from users where name = 'Fred' and disabled = false

Is it possible? I can't seem to find anything in the object TableQuery

to override to allow me to do this.

Appreciate any light that can be shed on this.

+3


source to share


1 answer


One thing you should be able to do is define your queries as simple Scala expressions / functions:

val disabledUsers = Users.filterNot(_.disabled)

      

The above only defines a query that will filter out people with disabilities. Then you can combine it multiple times at different programming points with more specific queries:



disabledUsers.where(_.name === "Fred")

      

I believe Slick should be smart enough to compile and optimize the query into one SELECT

+3


source







All Articles