How to make Squeryl work with the Play! Framework?

北城余情 提交于 2019-12-05 22:45:37

问题


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.


回答1:


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




回答2:


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

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



来源:https://stackoverflow.com/questions/10559562/how-to-make-squeryl-work-with-the-play-framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!