1

I have two Scala projects managed by SBT - models_project and client_project.

The models_project contains the DB models (Slick2-based). This project contains all the Slick tables and rows definitions. The client_project depends on the models_project to access the DB.

I want to be able to configure the driver at the client_project project.

The problem I'm facing is that I need to import the driver (for example scala.slick.driver.PostgresDriver.simple._) in the models_project to get many of the types that the model require.

I figured out how to configure those in a separate / independent project (so I'll have driver_project and both client_project and models_project will depend on it), but this requires the configuration to be made at the driver_project project. What I really want is for my models_project to be agnostic of the driver and for client_project to have a configuration of which driver to use, which it will push to models_project it depends on.

Thanks for your help!

Jacek Laskowski
  • 72,696
  • 27
  • 242
  • 420
polo
  • 1,352
  • 2
  • 16
  • 35
  • 1
    If you use a class (a driver) in a project, say `models_project`, you must declare the library of it as a dependency of the project. It is because the project uses it directly. I think it's more Slick architectural question not SBT one. – Jacek Laskowski Mar 01 '14 at 22:31

1 Answers1

1

It took me a little while to figure this out too. I have a github project which I used to test this out.

https://github.com/drstevens/slick-testing

I defined a SlickDriver trait which allows you to mix in the configured driver along with specific implementations (https://github.com/drstevens/slick-testing/blob/master/src/main/scala/com/daverstevens/sql/SlickDriver.scala)

trait SlickDriver {
  val driver: JdbcDriver
}

trait MySqlSlickDriver extends SlickDriver {
  val driver = MySQLDriver
}

trait H2SlickDriver extends SlickDriver {
  val driver = H2Driver
}

You can extend SlickDriver in a trait in which you define your model. Then you just need to mix in the appropriate SlickDriver at configuration time. Whenever you would normally need to import a driver, instead import the driver which is mixed into the instance of the model you have.

For an example, see trait SqlModel.

drstevens
  • 2,903
  • 1
  • 21
  • 30
  • Thanks for your answer and for taking the time to code the example and push to github. As far as I understand, your solution allow to configure the driver, but doesn't allow for the driver to be injected at runtime. The problem, is that I want to keep the models in an independent repo (and hence, SBT project), that anyone can use (by adding it as an sbt project dependency). – polo Mar 01 '14 at 23:59
  • I coded it up for my own benefit a while ago and just had it on githubs already. Slick only allows you to do so much at runtime. My code allows you to configure the driver at runtime as long as the driver was compiled in as a possible option to be configured. I figured this out based on this Slick mailing list thread. https://groups.google.com/forum/#!topic/scalaquery/BpPOj_NSOjc – drstevens Mar 02 '14 at 04:13