I have Play 2.3 appliction with reactive mongo plugin. I have base document:
trait TemporalDocument {
val created: Option[DateTime] = Some(new DateTime())
val updated: Option[DateTime] = Some(new DateTime())
}
and one of the concrete document:
case class User(name: String) extends TemporalDocument
object User{
implicit val userFormat = Json.format[User]
}
So when I persist it to mongo db using reactive mongo plugin only name is persisted, created/updated fields are not.
My repository looks like:
trait MongoDocumentRepository[T <: TemporalDocument] extends ContextHolder {
private val db = ReactiveMongoPlugin.db
def insert(document: T)(implicit writer: Writes[T]): Future[String] = {
collection.insert(document).map {
lastError => lastError.toString
} recover {
case e: Exception => sys.error(e.getMessage)
}
}
private def collection: JSONCollection = db.collection[JSONCollection](collectionName)
implicit object BSONDateTimeHandler extends BSONHandler[BSONDateTime, DateTime] {
def read(time: BSONDateTime) = new DateTime(time.value)
def write(jdtime: DateTime) = BSONDateTime(jdtime.getMillis)
}
}
Problem is that I will have many documents extended from base document and I do not want each time init those dates and probably some other fields. Is it possible to do something like this?