How to configure Slick 3.0.0 for Postgres DB (either with Hikari or without) Typesafe Play conf
One solution with basic useron application.conf
play.modules.enabled += "modules.DatabaseModule"MyNameIs = { database = { driver = org.postgresql.Driver url = "jdbc:postgresql://localhost:5432/databasename" user = "postgreuser" password = "" numThreads = 10 connectionTimeout = 5000 validationTimeout = 5000}dispatcher { fork-join-executor { parallelism-factor = 2 parallelism-max = 20 }}
on dir app you can create database module like this
import javax.inject.{Provider, Inject, Singleton}import com.google.inject.AbstractModuleimport com.typesafe.config.Configimport play.api.inject.ApplicationLifecycleimport play.api.{Configuration, Environment}import slick.jdbc.JdbcBackendimport scala.concurrent.Futureclass DatabaseModule(environment: Environment, configuration: Configuration) extends AbstractModule {override def configure(): Unit = { bind(classOf[Config]).toInstance(configuration.underlying) bind(classOf[slick.jdbc.JdbcBackend.Database]).toProvider(classOf[DatabaseProvider]) bind(classOf[models.UserDAO]).asEagerSingleton() }}@Singletonclass DatabaseProvider @Inject() (config: Config, lifecycle: ApplicationLifecycle) extends Provider[slick.jdbc.JdbcBackend.Database] { private val db = slick.jdbc.JdbcBackend.Database.forConfig("MyNameIs.database", config) lifecycle.addStopHook { () => Future.successful(db.close()) } override def get(): JdbcBackend.DatabaseDef = db}
On app/models: UserDAO
import java.sql.Dateimport javax.inject.{Inject, Singleton}import com.typesafe.config.Configimport slick.driver.PostgresDriver.api._import scala.concurrent.Futureimport scala.concurrent.ExecutionContext.Implicits.globalcase class User(id: Option[Long] = None, name: String, firstname: String, email: String, password: String, birthday: Option[Date] = None, signDate: Option[Date] = Some(new Date(System.currentTimeMillis)))@Singletonclass UserDAO @Inject()(config: Config, db: Database) { private val users = TableQuery[Users] override def delete(ids: Long*): Future[Boolean] = { Future.sequence(for (id <- ids) yield { db.run(users.filter(_.id === id).delete).map(_ == 1) }).map { _.find(i => i == false) == None } } def insert(user: User): Future[Unit] = db.run(users += user).map{()} override def update(obj: User): Future[Boolean] = ??? override def create(obj: User): Future[Boolean] = db.run(users += obj).map(_ == 1) override def all: Future[Seq[User]] = db.run(users.result) private class Users(tag: slick.lifted.Tag) extends Table[User](tag, "user") { type S = String type D = Date type L = Long // attribute name in table database def id = column[L](ID, O.AutoInc, O.PrimaryKey) def name = column[S](NAME) def password = column[S](PASSWORD) def firstname = column[S](FIRSTNAME) def email = column[S](EMAIL) def birthday = column[D](BIRTHDAY) def signDate = column[D](SIGN_DATE) override def * = (id.?, name, firstname, email, password, birthday.?, signDate.?) <> ((User.apply _) .tupled, User.unapply _) }}
On controllers Application.scala
@Singletonclass Application @Inject()(userdao: UserDAO) extends Controller { code }
and on route file:
# Home pageGET / @controllers.Application.index
Working example for me:
application.conf
database { dataSourceClass = org.postgresql.ds.PGSimpleDataSource properties = { databaseName = "some_db" user = "local" password = "local" } numThreads = 10}
build.sbt
libraryDependencies ++= Seq( "com.typesafe.slick" %% "slick" % "3.0.0", "com.zaxxer" % "HikariCP" % "2.4.1", "org.postgresql" % "postgresql" % "9.4-1201-jdbc41", // ...)
And then I just call
Database.forConfig("database")
Using Java 7, Scala 2.11.6 and sbt 0.13.6.
My config with PGSimpleDataSource
and specified connection pool size:
myDb = { connectionPool = "HikariCP" dataSourceClass = org.postgresql.ds.PGSimpleDataSource properties = { url = "jdbc:postgresql://"${dbHostName}":"${dbPortNumber}"/"${dbName} user = "postgres" password = "1" } numThreads = 10 minConnections = 10 maxConnections = 50 queueSize = 1000}