2011年7月25日月曜日

LiftとMongoDBとRogueを使ってみる。(その2:Rogue設定編)

以前の続きです。


Rogueを使ってみます。


Rogueについてはこちらを参照。

Rogue: A Type-Safe Scala DSL for querying MongoDB

Foursquareが公開しているMongoDBクエリ用のライブラリ。型安全なScala-DSLだそうで。

型安全なクエリを実行できるのは素晴らしいですね。
正直あまりよくわかっていないのですが、ひとまず開発環境の足がかりを作ります。

※赤字は追記・変更点。


まずは2011年7月段階で最新安定版のLift 2.3をダウンロード。

ダウンロードしたファイルを解答し、「lift_basic」フォルダをどこかに展開。

project/build/LiftProject.scalaを下記の通り変更。


import sbt._

class LiftProject(info: ProjectInfo) extends DefaultWebProject(info) {

 val liftVersion = "2.3"

 // uncomment the following if you want to use the snapshot repo
 val scalatoolsSnapshot = ScalaToolsSnapshots

 // If you're using JRebel for Lift development, uncomment
 // this line
 // override def scanDirectories = Nil

 override def libraryDependencies = Set(
   "net.liftweb" %% "lift-webkit" % liftVersion % "compile",
   "net.liftweb" %% "lift-mapper" % liftVersion % "compile",
   "org.mortbay.jetty" % "jetty" % "6.1.22" % "test",
   "junit" % "junit" % "4.5" % "test",
   "ch.qos.logback" % "logback-classic" % "0.9.26",
   "org.scala-tools.testing" %% "specs" % "1.6.6" % "test",
   "com.h2database" % "h2" % "1.2.138",
   "com.foursquare" %% "rogue" % "1.0.14-SNAPSHOT" withSources()
  ) ++ super.libraryDependencies

}



sbtを起動し、updateして必要なライブラリをすべてダウンロードできているようであればOKです。

"com.foursquare" %% "rogue" % "1.0.14-SNAPSHOT" withSources()
の"1.0.14-SNAPSHOT"については、下記のリポジトリを参考にその時々のバージョンを指定して下さい。
http://scala-tools.org/repo-snapshots/com/foursquare/


続いてMongoDBへの接続設定。
正直なところ下記を参照して適当に設定しています。
http://www.assembla.com/spaces/liftweb/wiki/Mongo_Configuration


src/main/scala/code/model/MongoConfig.scalaを下記内容で作成。



package code.model
import net.liftweb._
import mongodb._
import util.Props
import com.mongodb.{Mongo, ServerAddress}

object MongoConfig {
  def init: Unit = {
    val srvr = new ServerAddress("127.0.0.1", 27017)

    MongoDB.defineDb(DefaultMongoIdentifier, new Mongo(srvr), "foo")
  }
}



別マシンでmongodを実行している場合や、DB名を指定する場合にどういうことをすれば良いのかは不明ですが、まぁ気にしないことにします。 




そしてsrc/main/scala/bootstrap/liftweb/Boot.scalaにて、上で定義した設定処理を実行するように下記を追記。


class Boot {
  def boot {
    if (!DB.jndiJdbcConnAvailable_?) {
     //MongoDb boot
     MongoConfig.init
(後略)





ひとまず設定はこんなところです。




********

TODO
  1. Lift+RogueでCRUDしてみる。
********
サンプルはgithubにて公開。
https://github.com/RKTM/Lift-MongoDb-Rogue-Sample

0 件のコメント:

コメントを投稿