Skip to content

Instantly share code, notes, and snippets.

@Jacoby6000
Forked from maiostri/MongoDAO.scala
Last active August 29, 2015 14:23
Show Gist options
  • Save Jacoby6000/de6e9ffa559b7ed43f02 to your computer and use it in GitHub Desktop.
Save Jacoby6000/de6e9ffa559b7ed43f02 to your computer and use it in GitHub Desktop.
package models.dao
import scala.concurrent.ExecutionContext
import scala.concurrent.Future
import play.api.Play.current
import play.modules.reactivemongo.ReactiveMongoPlugin
import reactivemongo.api.collections.default.BSONCollection
import reactivemongo.bson.BSONDocument
import reactivemongo.bson.BSONDocumentIdentity
import reactivemongo.bson.BSONDocumentReader
import reactivemongo.bson.BSONDocumentWriter
import reactivemongo.core.commands.LastError
trait MongoDAO[T] {
implicit val ec: ExecutionContext
implicit val reader: BSONDocumentReader[T]
val collectionName: String
def db = ReactiveMongoPlugin.db
def collection = db[BSONCollection](collectionName)
def insert[T](document: T)(implicit writer: BSONDocumentWriter[T]): Future[LastError] = {
collection.insert(document)
}
def findAll(implicit reader: BSONDocumentReader[T]): Future[List[T]] = {
collection.find(BSONDocument()).cursor[T].collect[List]()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment