Reputation: 723
I have my Play
app, and I want to implement kind of uniqueness validation by myself, I use Slick
, so it's natural that I do a simple sum(1) where
call and if that count
is more than zero I set the error to my case class object
. So I can't imagine the workaround to avoid Await.result
on my DB query, because I need to set the error right in the current time.
But I now that Await.result
is really bad practice, any suggestions ?
Upvotes: 0
Views: 1064
Reputation: 6462
You should use map on the future:
db.run(query.result) map { response =>
//do whatever you want with the response
}
In this case you get the response but it is not blocking as Await.result
is.
Edit: so for the code you showed in your comment, it would look like:
def exists(tableName: String, column: String, value: Any): Future[Boolean] = {
val query = dbConfig.db.run(sql"SELECT COUNT(1) from #$tableName WHERE #$column = ${value.toString};".as[Int])
query.map(_.sum > 0)
}
Then where you need to call exists
you will do
exists(...) map { result =>
if (result == true)
//doSomething
}
Upvotes: 2