This repository has been archived by the owner on Jan 27, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 52
Home
leon edited this page Apr 10, 2012
·
10 revisions
all models need to be case classes for salat to work.
A simple model
package models
import play.api.Play.current
import java.util.{Date}
import com.novus.salat._
import com.mongodb.casbah.Imports._
import se.radley.plugin.mongodb._
import se.radley.plugin.mongodb.salat._
case class User(
id: ObjectId = new ObjectId,
username: String,
password: String,
address: Option[Address] = None,
added: Date = new Date(),
updated: Option[Date] = None,
@Key("company_id")company: Option[ObjectId] = None
)
case class Address(
street: String,
zip: String,
country: String
)
object User extends SalatDAO[User, ObjectId](collection = getCollection("users")) {
def all = find(MongoDBObject())
def findOneByUsername(username: String): Option[User] = findOne(MongoDBObject("username" -> username))
def findByCountry(country: String) = find(MongoDBObject("address.country" -> country)).toList
}
-
import com.novus.salat._
makes all the salat stuff work -
import com.mongodb.casbah.Imports._
makes all the implicit convertions between the mongodb javadriver and scala work -
import se.radley.plugin.mongodb._
makes getCollection() available -
import se.radley.plugin.mongodb.salat._
and most important of all this one import the salat context
I've choosen to have the DAO in the companion object, but some may say that you should separate the repository logic, in that case your could simply rename it to UserDAO
or UserRepository
object User extends SalatDAO[User, ObjectId](collection = getCollection("users", "myothersource")) {
and make sure you've added it to your application.conf
the SalatDAO
class contains all the logic for saving, updating, removing entities.
User.save(User(username = "leon", password = "1234"))