Generic interface for a provider of a DatabaseConfig
instance.
Generic interface for a provider of a DatabaseConfig
instance. A DatabaseConfig
is Slick type
that bundles a database and profile.
Usually, you shouldn't need to create instances of DatabaseConfigProvider
explicitly. Rather, you
should rely on dependency injection. If you don't want to use dependency injection, then use the
companion object and call DatabaseConfigProvider.get
.
Here is an example of how you can use dependency injection to obtain an instance of DatabaseConfigProvider
,
for the database named default
in your **application.conf**.
class Application @Inject()(protected val dbConfigProvider: DatabaseConfigProvider) { // ... }
While here is an example for injecting a DatabaseConfigProvider
for a database named orders
in your
**application.conf**.
import play.db.NamedDatabase class Application @Inject()(@NamedDatabase("orders") protected val dbConfigProvider: DatabaseConfigProvider) { // ... }
The name used in the application's config file to reference a slick database configuration.
Mix-in this trait if you need a Slick database and profile.
Mix-in this trait if you need a Slick database and profile. This is useful if you need to define a Slick table or need to execute some operation in the database.
There is only one abstract field, dbConfig
, which you can implement by calling DatabaseConfigProvider.get
.
If you are injecting DatabaseConfigProvider
instances using dependency injection, prefer using the trait
HasDatabaseConfigProvider
instead of this one.
// model definition class Cat(name: String, color: String) // DAO definition class CatDAO extends HasDatabaseConfig[RelationalProfile] { protected val dbConfig = DatabaseConfigProvider.get[RelationalProfile](Play.current) import profile.api._ private val Cats = TableQuery[CatsTable] def all() = db.run(Cats.result) def insert(cat: Cat) = db.run(Cats += cat) // Slick table definition private class CatsTable(tag: Tag) extends Table[Cat](tag, "CAT") { def name = column[String]("NAME", O.PrimaryKey) def color = column[String]("COLOR") def * = (name, color) <> (Cat.tupled, Cat.unapply _) } }
Of course, you do not need to define a DAO to use this trait (the above it is really just an example of usage).
Mix-in this trait if you need a Slick database and profile, and you are using dependency injection for obtaining
an instance of DatabaseConfigProvider
.
Mix-in this trait if you need a Slick database and profile, and you are using dependency injection for obtaining
an instance of DatabaseConfigProvider
. If you are not using dependency injection, then prefer mixing
HasDatabaseConfig
instead.
This trait is useful if you need to define a Slick table or need to execute some operation in the database.
// model definition class Cat(name: String, color: String) // DAO definition class CatDAO @Inject()(protected val dbConfigProvider: DatabaseConfigProvider) extends HasDatabaseConfigProvider[RelationalProfile] { import profile.api._ private val Cats = TableQuery[CatsTable] def all() = db.run(Cats.result) def insert(cat: Cat) = db.run(Cats += cat) // Slick table definition private class CatsTable(tag: Tag) extends Table[Cat](tag, "CAT") { def name = column[String]("NAME", O.PrimaryKey) def color = column[String]("COLOR") def * = (name, color) <> (Cat.tupled, Cat.unapply _) } }
Of course, you do not need to define a DAO to use this trait (the above it is really just an example of usage).
Inject provider for named databases.
Look up a DatabaseConfig
(which is Slick type that bundles a database and profile) for the passed
database name.
Look up a DatabaseConfig
(which is Slick type that bundles a database and profile) for the passed
database name. The DatabaseConfig
instance is created using the database's configuration you have
provided in your **application.conf**, for the passed database name.
Note that if no database name is passed, default
is used, and hence the configuration
slick.dbs.default
is used to create the DatabaseConfig
instance.
Here is an example for obtaining a DatabaseConfig
instance for the database named default
in
your **application.conf**.
import play.api.Play import play.api.db.slick.DatabaseConfigProvider import slick.profile.RelationalProfile val dbConfig = DatabaseConfigProvider.get[RelationalProfile](Play.current)
While here is an example for obtaining a DatabaseConfig
instance for the database named orders
in your **application.conf**.
import play.api.Play import play.api.db.slick.DatabaseConfigProvider import slick.profile.RelationalProfile val dbConfig = DatabaseConfigProvider.get[RelationalProfile]("orders")(Play.current)