Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make Squeryl work with the Play! Framework?

I'm trying to learn how to make a simple database app with Play and Squeryl. I've made the Tasks app from the Play tutorial, but I want to change the model / schema so that it uses Squeryl instead of Anorm. I've been looking at different tutorials, examples and answers, but I haven't really figured out how to do this.

So, given the source code from the Play Tutorial (ScalaTodoList); how do I proceed to make it work with Squeryl?

More specifically:

  • How do I implement the all(), create(), and delete() methods in my model? (I'd like to use auto-incrementing ID's for the Tasks)
  • Which database adapter to use is currently hard coded in Build.scala and Global.scala (see below). How can I make it such that it automatically uses H2 for dev/testing and Postgres on Heroku, like it does for Anorm in the Play tutorial?
  • How do I make sure that it automatically creates my tables?

This is what I've done thus far

I've completed the Play ScalaTodoList Tutorial.

In project/Build.scala, object ApplicationBuild, I've added the 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've added imports and removed the 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 rest of the files are left as they were at the end of the Play tutorial.

like image 500
user1390113 Avatar asked May 11 '12 22:05

user1390113


2 Answers

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

like image 68
James Ward Avatar answered Sep 17 '22 12:09

James Ward


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

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

like image 45
Somatik Avatar answered Sep 17 '22 12:09

Somatik