Skip to content

Commit

Permalink
Updated for use with mongo-java-driver 1.4 and lift 2.0-M4
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim Nelson committed Apr 26, 2010
1 parent 810e1d6 commit 8110ef1
Show file tree
Hide file tree
Showing 11 changed files with 112 additions and 48 deletions.
24 changes: 24 additions & 0 deletions README.textile
@@ -1,3 +1,10 @@
README: Scamongo is being moved to the Lift project. In doing so I'm comtemplating
what to do with the scamongo-document part of this library. I'm trying to decide if I
should move that with it, discontinue support for it, or keep it as a separate project.
Please contact me if you are using this at all and let me know what you think.

Tim

See wiki for project info. See wiki for project info.


To build you will need to use sbt To build you will need to use sbt
Expand All @@ -7,9 +14,26 @@ Jar is available at:
http://dl.dropbox.com/u/1154284/scamongo-0.1.jar http://dl.dropbox.com/u/1154284/scamongo-0.1.jar
http://dl.dropbox.com/u/1154284/scamongo-0.1.1.jar http://dl.dropbox.com/u/1154284/scamongo-0.1.1.jar
http://dl.dropbox.com/u/1154284/scamongo-0.1.2.jar http://dl.dropbox.com/u/1154284/scamongo-0.1.2.jar
http://dl.dropbox.com/u/1154284/scamongo-0.1.3.jar


h2. Release Notes h2. Release Notes


*version-0.1.3*
* Updated for use with mongo-java-driver 1.4 and lift 2.0-M4
NOTE: There is a small breaking change in this version for scamongo-record users;
record companion objects must now include a createRecord method. Here's an example:

class TestRecord extends MongoRecord[TestRecord] with MongoId[TestRecord] {

def meta = TestRecord

object stringfield extends StringField(this, 32)
}

object TestRecord extends TestRecord with MongoMetaRecord[TestRecord] {
def createRecord = new TestRecord
}

*version-0.1.2* *version-0.1.2*


* Added harryh's changes to DBRef * Added harryh's changes to DBRef
Expand Down
4 changes: 2 additions & 2 deletions project/build.properties
@@ -1,8 +1,8 @@
#Project properties #Project properties
#Tue Mar 30 11:20:07 CDT 2010 #Mon Apr 26 13:31:31 CDT 2010
project.organization=com.eltimn project.organization=com.eltimn
project.name=scamongo project.name=scamongo
sbt.version=0.5.6 sbt.version=0.5.6
project.version=0.1.3 project.version=0.1.4
scala.version=2.7.7 scala.version=2.7.7
project.initialize=false project.initialize=false
10 changes: 5 additions & 5 deletions project/build/ScamongoProject.scala
Expand Up @@ -4,13 +4,13 @@ class ScamongoProject(info: ProjectInfo) extends DefaultProject(info) {


//override def compileOptions = super.compileOptions ++ Seq(Unchecked) //override def compileOptions = super.compileOptions ++ Seq(Unchecked)


val mongo = "org.mongodb" % "mongo-java-driver" % "1.3" % "compile->default" val mongo = "org.mongodb" % "mongo-java-driver" % "1.4" % "compile->default"


val liftjson = "net.liftweb" % "lift-json" % "2.0-M3" % "compile->default" val liftjson = "net.liftweb" % "lift-json" % "2.0-M4" % "compile->default"
val liftrecord = "net.liftweb" % "lift-record" % "2.0-M3" % "compile->default" val liftrecord = "net.liftweb" % "lift-record" % "2.0-M4" % "compile->default"


val junit = "junit" % "junit" % "4.5" val junit = "junit" % "junit" % "4.7"
val specs = "org.scala-tools.testing" % "specs" % "1.5.0" val specs = "org.scala-tools.testing" % "specs" % "1.6.1"


// other repositories // other repositories
//val scalaToolsSnapshots = ScalaToolsSnapshots //val scalaToolsSnapshots = ScalaToolsSnapshots
Expand Down
13 changes: 9 additions & 4 deletions src/main/scala/com/eltimn/scamongo/field/DBRefField.scala
Expand Up @@ -16,9 +16,10 @@ package com.eltimn.scamongo.field
* and limitations under the License. * and limitations under the License.
*/ */


import net.liftweb.common.{Box, Empty, Failure, Full} import _root_.net.liftweb.common.{Box, Empty, Failure, Full}
import net.liftweb.http.js.JE.Str import _root_.net.liftweb.http.js.JE.Str
import net.liftweb.record.{Field, Record} import _root_.net.liftweb.json.JsonAST.{JNothing, JObject, JValue}
import _root_.net.liftweb.record.{Field, Record}


import com.mongodb.{BasicDBObject, BasicDBObjectBuilder, DBObject, DBRef, ObjectId} import com.mongodb.{BasicDBObject, BasicDBObjectBuilder, DBObject, DBRef, ObjectId}
import com.mongodb.util.JSON import com.mongodb.util.JSON
Expand All @@ -28,7 +29,7 @@ import com.mongodb.util.JSON
*/ */
//abstract class MongoRefField[OwnerType <: MongoRecord[OwnerType]](rec: OwnerType) //abstract class MongoRefField[OwnerType <: MongoRecord[OwnerType]](rec: OwnerType)
//abstract class MongoRefField[OwnerType <: MongoRecord[OwnerType], RefType <: MongoMetaRecord[RefType]](rec: OwnerType, ref: RefType) //abstract class MongoRefField[OwnerType <: MongoRecord[OwnerType], RefType <: MongoMetaRecord[RefType]](rec: OwnerType, ref: RefType)
abstract class DBRefField[OwnerType <: MongoRecord[OwnerType], RefType <: MongoRecord[RefType]](rec: OwnerType, ref: RefType) class DBRefField[OwnerType <: MongoRecord[OwnerType], RefType <: MongoRecord[RefType]](rec: OwnerType, ref: RefType)
extends Field[DBRef, OwnerType] { extends Field[DBRef, OwnerType] {


/* /*
Expand All @@ -53,6 +54,10 @@ abstract class DBRefField[OwnerType <: MongoRecord[OwnerType], RefType <: MongoR
private var _calcedObj = false private var _calcedObj = false


def asJs = Str(toString) def asJs = Str(toString)

def asJValue = (JNothing: JValue) // not implemented

def setFromJValue(jvalue: JValue) = Empty // not implemented


def asXHtml = <div></div> def asXHtml = <div></div>


Expand Down
14 changes: 9 additions & 5 deletions src/main/scala/com/eltimn/scamongo/field/JObjectField.scala
Expand Up @@ -16,15 +16,19 @@ package com.eltimn.scamongo.field
* and limitations under the License. * and limitations under the License.
*/ */


import net.liftweb.common.{Box, Empty, Failure, Full} import _root_.net.liftweb.common.{Box, Empty, Failure, Full}
import net.liftweb.http.js.JE.Str import _root_.net.liftweb.http.js.JE.Str
import net.liftweb.json.JsonAST.JObject import _root_.net.liftweb.json.JsonAST.{JNothing, JObject, JValue}
import net.liftweb.json.JsonParser import _root_.net.liftweb.json.JsonParser
import net.liftweb.record.{Field, Record} import _root_.net.liftweb.record.{Field, Record}


class JObjectField[OwnerType <: Record[OwnerType]](rec: OwnerType) extends Field[JObject, OwnerType] { class JObjectField[OwnerType <: Record[OwnerType]](rec: OwnerType) extends Field[JObject, OwnerType] {


def asJs = Str(toString) def asJs = Str(toString)

def asJValue = (JNothing: JValue) // not implemented

def setFromJValue(jvalue: JValue) = Empty // not implemented


def asXHtml = <div></div> def asXHtml = <div></div>


Expand Down
12 changes: 8 additions & 4 deletions src/main/scala/com/eltimn/scamongo/field/MongoListField.scala
Expand Up @@ -20,10 +20,10 @@ import java.util.Date


import scala.collection.jcl.Conversions._ import scala.collection.jcl.Conversions._


import net.liftweb.common.{Box, Empty, Failure, Full} import _root_.net.liftweb.common.{Box, Empty, Failure, Full}
import net.liftweb.json.JsonAST.JObject import _root_.net.liftweb.json.JsonAST.{JNothing, JObject, JValue}
import net.liftweb.http.js.JE.Str import _root_.net.liftweb.http.js.JE.Str
import net.liftweb.record.{Field, Record} import _root_.net.liftweb.record.{Field, Record}


import com.mongodb._ import com.mongodb._
import com.mongodb.util.JSON import com.mongodb.util.JSON
Expand All @@ -37,6 +37,10 @@ class MongoListField[OwnerType <: MongoRecord[OwnerType], ListType](rec: OwnerTy
import Meta.Reflection._ import Meta.Reflection._


def asJs = Str(toString) def asJs = Str(toString)

def asJValue = (JNothing: JValue) // not implemented

def setFromJValue(jvalue: JValue) = Empty // not implemented


def asXHtml = <div></div> def asXHtml = <div></div>


Expand Down
19 changes: 12 additions & 7 deletions src/main/scala/com/eltimn/scamongo/field/MongoMapField.scala
Expand Up @@ -16,20 +16,25 @@ package com.eltimn.scamongo.field
* and limitations under the License. * and limitations under the License.
*/ */


import net.liftweb.common.{Box, Empty, Failure, Full} import _root_.net.liftweb.common.{Box, Empty, Failure, Full}
import net.liftweb.http.js.JE.Str import _root_.net.liftweb.http.js.JE.Str
import net.liftweb.record.{Field, Record} import _root_.net.liftweb.json.JsonAST.{JNothing, JValue}
import net.liftweb.util.Log import _root_.net.liftweb.record.{Field, Record}
import _root_.net.liftweb.util.Log


import com.mongodb._ import com.mongodb._


class MongoMapField[OwnerType <: MongoRecord[OwnerType], MapValueType](rec: OwnerType) class MongoMapField[OwnerType <: MongoRecord[OwnerType], MapValueType](rec: OwnerType)
extends Field[Map[String, MapValueType], OwnerType] extends Field[Map[String, MapValueType], OwnerType]
with MongoFieldFlavor[Map[String, MapValueType]] { with MongoFieldFlavor[Map[String, MapValueType]] {


def asJs = Str(toString) def asJs = Str(toString) // not implemented

def asJValue = (JNothing: JValue) // not implemented

def setFromJValue(jvalue: JValue) = Empty // not implemented


def asXHtml = <div></div> def asXHtml = <div></div> // not implemented


def defaultValue = Map[String, MapValueType]() def defaultValue = Map[String, MapValueType]()


Expand All @@ -50,7 +55,7 @@ class MongoMapField[OwnerType <: MongoRecord[OwnerType], MapValueType](rec: Owne
} }
} }


def toForm = <div></div> def toForm = <div></div> // not implemented


def owner = rec def owner = rec


Expand Down
20 changes: 15 additions & 5 deletions src/main/scala/com/eltimn/scamongo/field/ObjectIdField.scala
Expand Up @@ -16,9 +16,11 @@ package com.eltimn.scamongo.field
* and limitations under the License. * and limitations under the License.
*/ */


import net.liftweb.common.{Box, Empty, Failure, Full} import _root_.net.liftweb.common.{Box, Empty, Failure, Full}
import net.liftweb.http.js.JE.Str import _root_.net.liftweb.http.js.JE.{JsNull, Str}
import net.liftweb.record.{Field, Record} import _root_.net.liftweb.json.JsonAST.{JNothing, JNull, JString, JValue}
import _root_.net.liftweb.record.{Field, Record}
import _root_.net.liftweb.record.FieldHelpers


import com.mongodb.{ObjectId, DBRef} import com.mongodb.{ObjectId, DBRef}


Expand All @@ -27,8 +29,16 @@ import com.mongodb.{ObjectId, DBRef}
*/ */
class ObjectIdField[OwnerType <: MongoRecord[OwnerType]](rec: OwnerType) class ObjectIdField[OwnerType <: MongoRecord[OwnerType]](rec: OwnerType)
extends Field[ObjectId, OwnerType] { extends Field[ObjectId, OwnerType] {


def asJs = Str(toString) def asJs = valueBox.map(v => Str(v.toString)) openOr JsNull

def asJValue: JValue = valueBox.map(v => JString(v.toString)) openOr (JNothing: JValue)

def setFromJValue(jvalue: JValue): Box[ObjectId] = jvalue match {
case JNothing|JNull if optional_? => setBox(Empty)
case JString(s) => setFromString(s)
case other => setBox(FieldHelpers.expectedA("JString", other))
}


def asXHtml = <div></div> def asXHtml = <div></div>


Expand Down
10 changes: 5 additions & 5 deletions src/test/scala/com/eltimn/scamongo/DirectExamples.scala
Expand Up @@ -33,9 +33,9 @@ import com.mongodb.{BasicDBObject, BasicDBObjectBuilder, DBObject}


object DirectExamples extends Specification { object DirectExamples extends Specification {


doFirst { doBeforeSpec {
// define the db // define the db
MongoDB.defineDb(DefaultMongoIdentifier, MongoAddress(MongoHost("localhost", 27017), "test_direct")) MongoDB.defineDb(DefaultMongoIdentifier, MongoAddress(MongoHost(), "test_direct"))
} }


import com.mongodb.util.JSON // Mongo parser/serializer import com.mongodb.util.JSON // Mongo parser/serializer
Expand Down Expand Up @@ -228,15 +228,15 @@ object DirectExamples extends Specification {
doc2.put("type", "db") doc2.put("type", "db")
doc2.put("count", 1) doc2.put("count", 1)


doc3.put("name", "MongoDB") doc3.put("name", "MongoDB")
doc3.put("type", "db") doc3.put("type", "db")
doc3.put("count", 1) doc3.put("count", 1)


// save the docs to the db // save the docs to the db
coll.save(doc) coll.save(doc)
db.getLastError.get("err") must beNull db.getLastError.get("err") must beNull
coll.save(doc2) // this should return an error coll.save(doc2) // this should return an error
db.getLastError.get("err").toString must startWith("E11000 duplicate key errorindex") db.getLastError.get("err").toString must startWith("E11000 duplicate key error index")
coll.save(doc3) coll.save(doc3)
db.getLastError.get("err") must beNull db.getLastError.get("err") must beNull


Expand Down Expand Up @@ -290,7 +290,7 @@ object DirectExamples extends Specification {
}) })
} }


doLast { doAfterSpec {
if (!debug) { if (!debug) {
/* drop the collections */ /* drop the collections */
MongoDB.useCollection(DefaultMongoIdentifier, "testCollection") ( coll => { MongoDB.useCollection(DefaultMongoIdentifier, "testCollection") ( coll => {
Expand Down
6 changes: 3 additions & 3 deletions src/test/scala/com/eltimn/scamongo/DocumentExamples.scala
Expand Up @@ -41,7 +41,7 @@ object TestDBb extends MongoIdentifier {
//class DocumentExampleTest extends Runner(DocumentExamples) with JUnit //class DocumentExampleTest extends Runner(DocumentExamples) with JUnit
object DocumentExamples extends Specification { object DocumentExamples extends Specification {


doFirst { doBeforeSpec {
// create a Mongo instance // create a Mongo instance
val mongoHost = MongoHost("localhost", 27017) val mongoHost = MongoHost("localhost", 27017)
// define the dbs // define the dbs
Expand Down Expand Up @@ -325,7 +325,7 @@ object DocumentExamples extends Specification {
TestCollection.save(tc, db) TestCollection.save(tc, db)
db.getLastError.get("err") must beNull db.getLastError.get("err") must beNull
TestCollection.save(tc2, db) // this should return an error TestCollection.save(tc2, db) // this should return an error
db.getLastError.get("err").toString must startWith("E11000 duplicate key errorindex") db.getLastError.get("err").toString must startWith("E11000 duplicate key error index")
TestCollection.save(tc3, db) TestCollection.save(tc3, db)
db.getLastError.get("err") must beNull db.getLastError.get("err") must beNull


Expand Down Expand Up @@ -443,7 +443,7 @@ object DocumentExamples extends Specification {


} }


doLast { doAfterSpec {
if (!debug) { if (!debug) {
/** drop the collections */ /** drop the collections */
SimplePerson.drop SimplePerson.drop
Expand Down
28 changes: 20 additions & 8 deletions src/test/scala/com/eltimn/scamongo/RecordExamples.scala
Expand Up @@ -40,7 +40,7 @@ object RecordExamples extends Specification {


val debug = false val debug = false


doFirst { doBeforeSpec {
// define the db // define the db
MongoDB.defineDb(DefaultMongoIdentifier, MongoAddress(MongoHost("localhost", 27017), "test_record")) MongoDB.defineDb(DefaultMongoIdentifier, MongoAddress(MongoHost("localhost", 27017), "test_record"))
} }
Expand Down Expand Up @@ -312,7 +312,7 @@ object RecordExamples extends Specification {
} }
} }


doLast { doAfterSpec {
if (!debug) { if (!debug) {
/** drop the collections */ /** drop the collections */
TestRecord.drop TestRecord.drop
Expand Down Expand Up @@ -390,7 +390,9 @@ class TestRecord extends MongoRecord[TestRecord] {
*/ */
} }


object TestRecord extends TestRecord with MongoMetaRecord[TestRecord] object TestRecord extends TestRecord with MongoMetaRecord[TestRecord] {
def createRecord = new TestRecord
}


case class RPerson(name: String, age: Int, address: Address, children: List[Child]) case class RPerson(name: String, age: Int, address: Address, children: List[Child])
extends JsonObject[RPerson] { extends JsonObject[RPerson] {
Expand All @@ -414,12 +416,16 @@ class MainDoc extends MongoRecord[MainDoc] with MongoId[MainDoc] {
def obj = RefDoc.find(value) def obj = RefDoc.find(value)
} }
} }
object MainDoc extends MainDoc with MongoMetaRecord[MainDoc] object MainDoc extends MainDoc with MongoMetaRecord[MainDoc] {
def createRecord = new MainDoc
}


class RefDoc extends MongoRecord[RefDoc] with MongoId[RefDoc] { class RefDoc extends MongoRecord[RefDoc] with MongoId[RefDoc] {
def meta = RefDoc def meta = RefDoc
} }
object RefDoc extends RefDoc with MongoMetaRecord[RefDoc] object RefDoc extends RefDoc with MongoMetaRecord[RefDoc] {
def createRecord = new RefDoc
}


// string as id // string as id
class RefStringDoc extends MongoRecord[RefStringDoc] { class RefStringDoc extends MongoRecord[RefStringDoc] {
Expand All @@ -436,7 +442,9 @@ class RefStringDoc extends MongoRecord[RefStringDoc] {
new DBRef(db, meta.collectionName, _id.value) new DBRef(db, meta.collectionName, _id.value)
) )
} }
object RefStringDoc extends RefStringDoc with MongoMetaRecord[RefStringDoc] object RefStringDoc extends RefStringDoc with MongoMetaRecord[RefStringDoc] {
def createRecord = new RefStringDoc
}


class ListDoc extends MongoRecord[ListDoc] with MongoId[ListDoc] { class ListDoc extends MongoRecord[ListDoc] with MongoId[ListDoc] {
def meta = ListDoc def meta = ListDoc
Expand Down Expand Up @@ -505,7 +513,8 @@ class ListDoc extends MongoRecord[ListDoc] with MongoId[ListDoc] {


} }
object ListDoc extends ListDoc with MongoMetaRecord[ListDoc] { object ListDoc extends ListDoc with MongoMetaRecord[ListDoc] {
override def formats = DefaultFormats.lossless // adds .000 override def formats = DefaultFormats.lossless // adds .000 to Dates
def createRecord = new ListDoc
} }


case class JsonDoc(id: String, name: String) extends JsonObject[JsonDoc] { case class JsonDoc(id: String, name: String) extends JsonObject[JsonDoc] {
Expand All @@ -525,6 +534,7 @@ class MapDoc extends MongoRecord[MapDoc] with MongoId[MapDoc] {
} }
object MapDoc extends MapDoc with MongoMetaRecord[MapDoc] { object MapDoc extends MapDoc with MongoMetaRecord[MapDoc] {
override def formats = DefaultFormats.lossless // adds .000 override def formats = DefaultFormats.lossless // adds .000
def createRecord = new MapDoc
} }


class OptionalDoc extends MongoRecord[OptionalDoc] with MongoId[OptionalDoc] { class OptionalDoc extends MongoRecord[OptionalDoc] with MongoId[OptionalDoc] {
Expand All @@ -535,5 +545,7 @@ class OptionalDoc extends MongoRecord[OptionalDoc] with MongoId[OptionalDoc] {
override def defaultValue = "nothin" override def defaultValue = "nothin"
} }
} }
object OptionalDoc extends OptionalDoc with MongoMetaRecord[OptionalDoc] object OptionalDoc extends OptionalDoc with MongoMetaRecord[OptionalDoc] {
def createRecord = new OptionalDoc
}


0 comments on commit 8110ef1

Please sign in to comment.