How to make Squeryl work with Play! Framework

I am trying to learn how to create a simple database using Play and Squeryl. I made the Tasks application from the Playback tutorial, but I want to change the model / layout so that it uses Squeryl instead of Anorm. I watched various tutorials , examples, and answers , but I'm glad I really understood how to do this.

So, given the source code from the Play Tutorial (ScalaTodoList) ; How can I continue with Squeryl?

More specific:

  • How to implement all() , create() and delete() methods in my model? (I would like to use the auto-increment id for tasks)
  • Which database adapter to use is currently hardcoded in Build.scala and Global.scala (see below). How can I make it automatically use H2 for dev / testing and Postgres on Heroku, how is it done for Anorm in the Play tutorial?
  • How can I make sure it automatically creates my tables?

This is what I have done so far.

I have completed the Play ScalaTodoList tutorial.

In project/Build.scala , object ApplicationBuild I added dependencies:

 // From the "Squeryl Getting Started tutorial" val posgresDriver = "postgresql" % "postgresql" % "8.4-702.jdbc4" val h2 = "com.h2database" % "h2" % "1.2.127" // From the "Squeryl Getting Started tutorial" libraryDependencies ++= Seq( "org.squeryl" %% "squeryl" % "0.9.5", h2 ) // From the Play tutorial val appDependencies = Seq( // Add your project dependencies here, "org.squeryl" %% "squeryl" % "0.9.5", // Copied from above so that it compiles (?) "postgresql" % "postgresql" % "8.4-702.jdbc4" ) 

added app/Global.scala (taken from the SO answer mentioned above, just changed the adapter to H2):

 import play.db.DB import play.api.Application import play.api.GlobalSettings import org.squeryl._ import org.squeryl.adapters._ object Global extends GlobalSettings { override def onStart(app: Application): Unit = { SessionFactory.concreteFactory = Some( () => Session.create(DB.getDataSource().getConnection(), dbAdapter)); } override def onStop(app: Application): Unit = { } val dbAdapter = new H2Adapter(); // Hard coded. Not good. } 

in app/models/Task.scala I added import and deleted Anorm implemetations in all() , create() and delete() . The controller from the Play tutorial expects the all() method to return List[Task] .

 import org.squeryl.PrimitiveTypeMode._ import org.squeryl.Schema import org.squeryl.annotations.Column case class Task(id: Long, label: String) object Task extends Schema { val tasks = table[Task] // Inspired by Squeryl tutorial def all(): List[Task] = { List[Task]() // ?? } def create(label: String) { // ?? } def delete(id: Long) { // ?? } } 

The remaining files are left as they were at the end of the Play tutorial.

+8
scala playframework squeryl
source share
2 answers

Here is an example of a Play 2 project with Squeryl:
https://github.com/jamesward/play2bars/tree/scala-squeryl

+8
source share

Play for Scala (MEAP) has a chapter on Squeryl integration

http://www.manning.com/hilton/

+4
source share

All Articles