Created
March 8, 2011 13:41
-
-
Save ReSTARTR/860266 to your computer and use it in GitHub Desktop.
Salat test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.novus.salat._ | |
import com.novus.salat.global._ | |
import com.mongodb.casbah.Imports._ | |
case class User(id: Int, name: String, age: Int) | |
case class UserA(id: Int, name: String, age: Int) | |
case class UserB(id: Int, name: String, salary: Int) | |
case class Group(group_id: Int, name: String, leader: User, members: List[User]) | |
object SalatSample { | |
val collection = MongoConnection()("salat_test")("sample") | |
def singleLayerClassMapping { | |
// Casbah style | |
collection += MongoDBObject("id"->1, "name"->"me", "age"->27) | |
val me2 = collection.findOne( MongoDBObject("id"->1)).get | |
println( me2.getClass ) // class com.mongodb.BasicDBObject | |
println( me2.get("name") ) // me | |
// Salat style | |
val g = grater[User] | |
val me = User(id=2, name="me2", age=54) | |
collection += g.asDBObject(me) | |
val meInDB = collection.findOne( MongoDBObject("id"->2)).get | |
println( meInDB.getClass ) | |
// class com.mongodb.BasicDBObject | |
println( meInDB ) | |
// { "_id" : { "$oid" : "4d764830e10d23dc4758c29a"} , "_typeHint" : "User" , "id" : 2 , "name" : "me2" , "age" : 54} | |
println( g.asObject(meInDB) ) | |
// User(2,me2,54) | |
println( grater[UserA].asObject(meInDB) ) | |
println( grater[UserB].asObject(meInDB) ) | |
} | |
def doubleLayerClassMapping { | |
val me = User(id=11, name="me", age=27) | |
val you = User(id=12, name="you", age=30) | |
val members = MongoDBList.newBuilder | |
members += me | |
members += you | |
val group = Group(group_id=1, name="you and me", leader=me, members=List(me, you)) | |
println(group) | |
// Group(1,you and me,List(User(11,me,27), User(12,you,30))) | |
println(grater[Group].asDBObject(group)) | |
// { "_typeHint" : "Group" , "group_id" : 1 , "name" : "you and me" , "leader" : [ 11 , "me" , 27] , "members" : [ [ 11 , "me" , 27] , [ 12 , "you" , 30]]} | |
collection += grater[Group].asDBObject(group) | |
val groupInDB = collection.findOne( MongoDBObject("group_id"->1) ).get | |
println(groupInDB.getClass) | |
// class com.mongodb.BasicDBObject | |
println(groupInDB) | |
// { "_id" : { "$oid" : "4d764bc9e10d23dc15a97164"} , "_typeHint" : "Group" , "group_id" : 1 , "name" : "you and me" , "members" : [ [ 11 , "me" , 27] , [ 12 , "you" , 30]]} | |
println(grater[Group].asObject(groupInDB)) | |
} | |
def main(args: Array[String]) { | |
singleLayerClassMapping | |
doubleLayerClassMapping | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sbt._ | |
class SalatTestProject(info: ProjectInfo) extends DefaultProject(info) { | |
val casbah = "com.mongodb.casbah" % "casbah_2.8.1" % "2.0.2" | |
val novusRels = "repo.novus rels" at "http://repo.novus.com/releases/" | |
val novusSnaps = "repo.novus snaps" at "http://repo.novus.com/snapshots/" | |
val salat = "com.novus" %% "salat" % "0.0.5" | |
//val salat = "com.novus" %% "salat" % "0.0.6-SNAPSHOT" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment