How to Inject DBApi into Class

25 views
Skip to first unread message

Jonathan Willis

unread,
Sep 19, 2016, 4:29:54 PM9/19/16
to Play Framework
I'm looking into using this template from the template list on lightbend.comhttps://www.lightbend.com/activator/template/playing-reactjs. I noticed that DB.withConnection is deprecated and am looking into how I can inject DBApi instead. What I am wanting is to be able to switch out DB with a dbApi injected object. Is there any way i can do this?

Here is the class I am looking into modifying:

case class Employee(id: Long, name: String, address: String, designation: String)


object Employee {


  // -- Parsers

  /**

   * Parse a Employee from a ResultSet

   */

  val employee = {

    get[Long]("employee.id") ~

      get[String]("employee.name") ~

      get[String]("employee.address") ~

      get[String]("employee.designation") map {

        case id ~ name ~ address ~ designation => Employee(id, name, address, designation)

      }

  }


  // -- Queries

  Employee

  /**

   * Retrieve a employee from the id.

   */

  def findById(id: Long): Option[Employee] = {

    DB.withConnection { implicit connection =>

      SQL("select * from employee where id = {id}").on('id -> id).as(employee.singleOpt)

    }

  }

...

}

Reply all
Reply to author
Forward
0 new messages