Working with databases containing more than 22 fields in Play 2.x

226 views Asked by At

I am trying to migrate an application from Rails/Mongoid to Play/Reactivemongo with reactivemongo-extensions. Many of my documents have more than 22 fields. Play's JSON library does not seem to be able to handle > 22 fields. What is the preferred pattern to deal with database schemas having more than 22 fields in play/scala/reactivemongo?

I feel like I must be missing a common design pattern because this seems like a very common use case. A web framework that cannot work with even moderately sized database tables would not be very useful so I think I must be missing an obvious solution.

1

There are 1 answers

0
dingdong On

You can map your flat json structure to a hierarchical object structure. Then you don't need to deal with huge objects and you can have more than 22 fields.

case class SubObject(field4: String, field5:String)
case class MainObject(field1: String, field2: String, field3: String, 
subObject: SubObject, field6: String, field7: String)

implicit val mainObjectFormat: Format[MainObject] = (
    (__ \ "field1").format[String] and
    (__ \ "field2").format[String] and
    (__ \ "field3").format[String] and
    (
      (__ \ "field4").format[String] and
      (__ \ "field5").format[String]
    )(SubObject.apply, unlift(SubObject.unapply)) and
    (__ \ "field6").format[String] and
    (__ \ "field7").format[String]    
(MainObject.apply, unlift(MainObject.unapply))