Last active
August 29, 2015 14:05
-
-
Save 0xdevalias/ce0390661992ca1152e2 to your computer and use it in GitHub Desktop.
Basic example of how a 'versioned' model might be implementable with ReactiveMongo
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
// 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