Reputation: 1496
I am trying to use play-slick
plugin to interact with a mySQL
database. Everything works as expected other than a [warn]
I get everytime I compile the code.
On this line: val dbConfig = DatabaseConfigProvider.get[JdbcProfile](Play.current)
The warning is: method current in object Play is deprecated: This is a static reference to application, use DI instead
.
I have tried adding the inject() method with by defining Configuration using dependency injection but it's not working! How do I use the Dependency Injection
in the following code so that I don't have to use Play.current
which has bee deprecated since Play 2.5
import play.api.Play
import play.api.db.slick.DatabaseConfigProvider
import scala.concurrent.Future
import slick.driver.JdbcProfile
import slick.driver.MySQLDriver.api._
import scala.concurrent.ExecutionContext.Implicits.global
case class User(
id: Long,
firstName: String,
lastName: String,
mobile: Long,
email: String
)
class UserTableDef(tag: Tag) extends Table[User](tag, "users") {
def id = column[Long]("id", O.PrimaryKey,O.AutoInc)
def firstName = column[String]("first_name")
def lastName = column[String]("last_name")
def mobile = column[Long]("mobile")
def email = column[String]("email")
override def * =
(id, firstName, lastName, mobile, email) <>(User.tupled, User.unapply)
}
object Users {
val dbConfig = DatabaseConfigProvider.get[JdbcProfile](Play.current) //<-- PROBLEM
val users = TableQuery[UserTableDef]
def get(id: Long): Future[Option[User]] = {
dbConfig.db.run(users.filter(_.id === id).result.headOption)
}
}
Upvotes: 0
Views: 1364
Reputation: 14825
Play current
is deprecated. DBConfigProvider will be injected using guice.
DatabaseConfigProvider
will be injected into the class UsersRepo
using guice dependency injection.
Here is the way to do this using Guice and Play 2.5
case class User(profileName: ProfileName,
email: Email,
createdAt: DateTime,
id: UserId)
@Singleton
class UsersRepo @Inject()(protected val dbConfigProvider: DatabaseConfigProvider) {
val dbConfig = dbConfigProvider.get[JdbcProfile]
import dbConfig.driver.api._
private[services] val users = TableQuery[Users]
def exists(id: UserId): DBIO[Boolean] = {
users.filter(_.id === id).exists.result
}
private[services] class Users(tag: Tag) extends Table[User](tag, UsersTable.name) {
def profileName = column[ProfileName]("profile_name")
def id = column[UserId]("user_id", O.PrimaryKey)
def email = column[Email]("email")
def createdAt = column[DateTime]("created_at")
def * = (profileName, email, source, createdAt, id) <> (User.tupled, User.unapply)
def emailIndex = index("users_email_index", email, true)
}
}
DB configuration when using play-slick
in application.conf
slick.dbs.default.driver="slick.driver.PostgresDriver$"
slick.dbs.default.db.driver="org.postgresql.Driver"
slick.dbs.default.db.url="jdbc:postgresql://ec2-54-217-243-228.eu-west-1.compute.amazonaws.com:5432/d344onl0761ji5?user=user&password=pass"
slick.dbs.default.db.user=user
slick.dbs.default.db.password="pass"
Upvotes: 2