I am using Salat with MongoDB and I am trying to convert to natural keys to avoid duplication in the database. The case class that I use looks something like this:
case class Foo(someRelatedId: String, email: String ...)
I would like to add a natural key that consists of some RelatedId + email address and use MongoDB instead of the default ObjectId. From the documentation, I get the feeling that this is possible, but I'm still groping for a working solution. This is largely due to the fact that I'm not sure of myself with Scala, I'm sure.
Update: I now have a working solution, but I'm still wondering if this is the best way.
case class Foo(someRelatedId: String, email: String, naturalKey: String) object Foo { def apply((someRelatedId: String, email: String) { apply(someRelatedId, email, someRelatedId+email) } }
And then in package.scala I map the custom salad context :
implicit val ctx = new Context() { val name = Some("Custom Context") } ctx.registerGlobalKeyOverride(remapThis = "naturalKey", toThisInstead = "_id")
This way, I avoid having a required (meaningless) _id field in my domain classes, but I need to overload apply () in the companion object, which seems a bit awkward.
iwein source share