Last active
October 3, 2016 15:29
-
-
Save alexanderjarvis/5141288 to your computer and use it in GitHub Desktop.
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 daos | |
import scala.concurrent.ExecutionContext | |
import scala.concurrent.Future | |
import play.api.Play.current | |
import play.modules.reactivemongo._ | |
import reactivemongo.api._ | |
import reactivemongo.api.indexes._ | |
import reactivemongo.bson._ | |
import reactivemongo.bson.handlers.BSONReader | |
import reactivemongo.bson.handlers.DefaultBSONHandlers.DefaultBSONReaderHandler | |
import reactivemongo.bson.handlers.DefaultBSONHandlers.DefaultBSONDocumentWriter | |
import reactivemongo.bson.handlers.BSONWriter | |
import reactivemongo.core.commands.LastError | |
import reactivemongo.core.commands.GetLastError | |
import reactivemongo.bson.handlers.RawBSONWriter | |
trait MongoDAO[T] { | |
implicit val ec: ExecutionContext | |
implicit val reader: BSONReader[T] | |
def collection: reactivemongo.api.DefaultCollection | |
def db = ReactiveMongoPlugin.db | |
val ID = "_id" | |
def ensureIndexes: Future[List[Boolean]] | |
def ensureIndex( | |
key: List[(String, IndexType)], | |
name: Option[String] = None, | |
unique: Boolean = false, | |
background: Boolean = false, | |
dropDups: Boolean = false, | |
sparse: Boolean = false, | |
version: Option[Int] = None, | |
options: BSONDocument = BSONDocument()) = { | |
collection.indexesManager.ensure(Index(key, name, unique, background, dropDups, sparse, version, options)) | |
} | |
def findHeadOption(attribute: String, value: String) = { | |
collection.find(BSONDocument(attribute -> BSONString(value))).headOption | |
} | |
def findById(id: String) = { | |
collection.find(BSONDocument("_id" -> BSONObjectID(id))).headOption | |
} | |
def findById(id: BSONObjectID) = { | |
collection.find(BSONDocument("_id" -> id)).headOption | |
} | |
def remove(id: String) = { | |
collection.remove(BSONDocument("_id" -> BSONObjectID(id))) | |
} | |
def remove(id: BSONObjectID) = { | |
collection.remove(BSONDocument("_id" -> id)) | |
} | |
def removeWith(attribute: String, value: String) = { | |
collection.remove(BSONDocument(attribute -> BSONString(value))) | |
} | |
/* Functions from reactivemongo.api.Collection */ | |
def drop = collection.drop | |
def insert[T](document: T)(implicit writer: BSONWriter[T]): Future[LastError] = { | |
collection.insert(document) | |
} | |
def insert[T](document: T, writeConcern: GetLastError)(implicit writer: BSONWriter[T]): Future[LastError] = { | |
collection.insert(document, writeConcern) | |
} | |
def uncheckedInsert[T](document: T)(implicit writer: BSONWriter[T]): Unit = { | |
collection.uncheckedInsert(document) | |
} | |
def update[S, U](selector: S, update: U, writeConcern: GetLastError = GetLastError(), upsert: Boolean = false, multi: Boolean = false)(implicit selectorWriter: RawBSONWriter[S], updateWriter: RawBSONWriter[U], ec: ExecutionContext): Future[LastError] = { | |
collection.update(selector, update, writeConcern, upsert, multi) | |
} | |
} |
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 | |
import reactivemongo.bson._ | |
import reactivemongo.bson.handlers._ | |
import org.joda.time.DateTime | |
import daos.UserDAO | |
case class User( | |
id: Option[BSONObjectID], | |
email: String, | |
firstName: String, | |
lastName: String, | |
updated: Option[DateTime]) | |
object User extends UserDAO { | |
val EMAIL = "email" | |
val FIRSTNAME = "firstName" | |
val LASTNAME = "lastName" | |
val UPDATED = "updated" | |
implicit object UserBSONReader extends BSONReader[User] { | |
def fromBSON(document: BSONDocument): User = { | |
val doc = document.toTraversable | |
User( | |
doc.getAs[BSONObjectID](ID), | |
doc.getAs[BSONString](EMAIL).get.value, | |
doc.getAs[BSONString](FIRSTNAME).get.value, | |
doc.getAs[BSONString](LASTNAME).get.value, | |
doc.getAs[BSONDateTime](UPDATED).map(dt => new DateTime(dt.value)) | |
) | |
} | |
} | |
implicit object UserBSONWriter extends BSONWriter[User] { | |
def toBSON(user: User) = { | |
val bson = BSONDocument( | |
ID -> user.id.getOrElse(BSONObjectID.generate), | |
EMAIL -> BSONString(user.email), | |
FIRSTNAME -> BSONString(user.firstName), | |
LASTNAME -> BSONString(user.lastName), | |
UPDATED -> user.updated.map(date => BSONDateTime(date.getMillis)) | |
) | |
bson | |
} | |
} | |
} |
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 daos | |
import scala.concurrent.ExecutionContext | |
import scala.concurrent.Future | |
import reactivemongo.api._ | |
import reactivemongo.api.indexes._ | |
import reactivemongo.api.indexes.IndexType._ | |
import reactivemongo.bson._ | |
import reactivemongo.bson.handlers.DefaultBSONHandlers.DefaultBSONDocumentWriter | |
import reactivemongo.bson.handlers.DefaultBSONHandlers.DefaultBSONReaderHandler | |
import models.User | |
trait UserDAO extends MongoDAO[User] { | |
override implicit val ec: ExecutionContext = ExecutionContext.Implicits.global | |
override implicit val reader = User.UserBSONReader | |
override def collection = db("users") | |
override def ensureIndexes = { | |
Future.sequence(List( | |
ensureIndex(List(User.EMAIL -> Ascending), unique = true) | |
)) | |
} | |
def findByEmail(email: String) = findHeadOption(User.EMAIL, email) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Could you update this to reactivemongo v0.10 ? That would be nice.