How to update object in Mongo with an immutable Salat case class

无人久伴 提交于 2019-12-02 04:06:11

You want partial lenses for updating your JSON data structure. http://dropbox.tmorris.net/media/doc/lenses.pdf

Salat handles JSON using lift-json - see https://github.com/novus/salat/wiki/SalatWithPlay2.

Play itself uses Jerkson, which is another way to decode your model objects - see http://blog.xebia.com/2012/07/22/play-body-parsing-with-jerkson/ for an example.

Feel free to make a small sample Github project that demonstrates your issue and post to the Salat mailing list at https://groups.google.com/group/scala-salat for help.

There are really two problems in your question:

  1. How do I use Play Salat.
  2. How do I prevent updates to certain fields.

The answer to your first question lies in the Play Salat documentation. Your second question could be answered a few ways.

a. When the update is pushed to the server from Backbone, you could grab the object id and find it in the database. At that point you have both copies of the object. At that point, you can fire a business rule to make sure the sender didn't attempt to change those fields.

or

b. You could put some of your fields in another document of an embedded document. The client would have access to them for rendering purposes but your API wouldn't allow them to be pushed back to Mongo.

or

c. You could write a custom update query that ignores the fields you don't want changed.

Actually the answer is pretty simple: I didn't know there was a built-in copy method on case classes that allows to copy an immutable case class while changing some data.

I don't have nested case class structures but the Tony Morris suggestion of using Lenses seems nice too.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!