Skip to content

Instantly share code, notes, and snippets.

@0xdevalias
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 0xdevalias/ce0390661992ca1152e2 to your computer and use it in GitHub Desktop.
Save 0xdevalias/ce0390661992ca1152e2 to your computer and use it in GitHub Desktop.
Basic example of how a 'versioned' model might be implementable with ReactiveMongo
// Expanding on basic case from 'Use Readers to deserialize documents automatically' http://reactivemongo.org/releases/0.10/documentation/tutorial/find-documents.html
case class Person(id: BSONObjectID, firstName: String, lastName: String, age: Int)
object Person {
implicit object PersonReader extends BSONDocumentReader[Person] {
def read(doc: BSONDocument): Person = {
// Common fields
val id = doc.getAs[BSONObjectID]("_id").get
val version = doc.getAs[Int]("_version").get
// Version matching
val person = version match {
case 1 => { // version 1
val firstName = doc.getAs[String]("firstName").get
val lastName = doc.getAs[String]("lastName").get
val age = doc.getAs[Int]("age").get
Person(id, firstName, lastName, age)
}
case _ => { // default/latest version
val givenName = doc.getAs[String]("givenName").get
val surname = doc.getAs[String]("surname").get
val age = doc.getAs[Int]("age").get
Person(id, givenName, surname, age)
}
}
person // return
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment