Created
July 14, 2014 14:22
-
-
Save maiostri/627491f56998d85a1ed1 to your computer and use it in GitHub Desktop.
Generic DAO using 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
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