Skip to content

Instantly share code, notes, and snippets.

@shirishp
Last active August 29, 2015 14:20
Show Gist options
  • Save shirishp/476f5374d3e9129196c6 to your computer and use it in GitHub Desktop.
Save shirishp/476f5374d3e9129196c6 to your computer and use it in GitHub Desktop.
JSONCollection
object CollectionImplicits {
implicit class RichJSONCollection(val self: JSONCollection) extends AnyVal {
def insertWithTimestamp(document: JsObject) = {
self.insert(document ++ Json.obj("updatedAt" -> new java.util.Date().getTime))
}
}
}
// Usage
case class Board(name: String, sections: List[Section])
object JsonFormats {
implicit val boardFormat = Json.format[Board]
implicit val boardReads = Json.reads[Board]
}
private lazy val collection: JSONCollection = database.collection[JSONCollection]("boards")
def create(board: Board) = collection.insert(board) // now, board is writable to JSON, but I want above insertWithTimestamp to work here
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment