I have an idea how my data access level should look like using Scala Slick, but I'm not sure if this is really possible.
Suppose I have a User table that has regular fields like id, email, password, etc.
object Users extends Table[(String, String, Option[String], Boolean)]("User") { def id = column[String]("id", O.PrimaryKey) def email = column[String]("email") def password = column[String]("password") def active = column[Boolean]("active") def * = id ~ email ~ password.? ~ active }
And I want to query them in different ways, the currently ugly way is to have a new database session, do it for understanding, and then execute different if statements to achieve what I want.
eg.
def getUser(email: String, password: String): Option[User] = { database withSession { implicit session: Session => val queryUser = (for { user <- Users if user.email === email && user.password === password && user.active === true }
I would prefer to have a private method for the query, and then public methods that define the queries row by row
type UserQuery = User => Boolean private def getUserByQuery(whereQuery: UserQuery): Option[User] = { database withSession { implicit session: Session => val queryUser = (for { user <- Users somehow run whereQuery here to filter }
Thus, the request logic is encapsulated in the corresponding public functions, and the actual request and mapping to the user object is in a reusable function that other people should not bother.
I have a problem refactoring the "where" bit in a function that I can execute. Trying to do things like select them in intellij and use the refactoring results in some pretty crazy print.
Does anyone have any examples that they could show regarding what I'm trying to achieve?