8

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.

Community
  • 1
  • 1
user1390113
  • 262
  • 4
  • 10
  • 1
    This tutorial from Artima is followable and uses Play2 with Squeryl in Scala. It should be exactly what you need to get going: http://www.artima.com/articles/play2_scala_squeryl.html – myyk Aug 21 '12 at 00:43

2 Answers2

8

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

James Ward
  • 29,283
  • 9
  • 49
  • 85
  • 2
    Thanks **a lot**! :) But I did run into a few problems running your project. Here's how I solved them: to clone the _correct_ branch (and not master) `git clone https://github.com/jamesward/play2bars.git -b scala-squeryl` And then to solve the Jquery dependency problem I had to alter line num 13 in `project/Build.scala` from `"com.jquery" % "jquery" % "1.7.1"` to `"com.jquery" % "jquery" % "1.7.1" from "http://code.jquery.com/jquery-1.7.1.min.js"`. – user1390113 May 12 '12 at 15:53
  • Sorry, I've fixed that in the git repo. – James Ward May 14 '12 at 20:08
  • 3
    A blog post on that example http://www.artima.com/articles/play2_scala_squeryl.html – Somatik Aug 16 '12 at 17:34
4

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

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

Somatik
  • 4,723
  • 3
  • 37
  • 49