1

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.

imagio
  • 1,390
  • 2
  • 16
  • 27
  • Which Scala version are you using? I think that is a limitation of Scala itself but it was already fixed in Scala 2.11... – Salem Nov 23 '14 at 19:30
  • 2
    Normalize your data. A class with more than 22 fields (or close to that) is not really a common use case. – Michael Zajac Nov 23 '14 at 20:11
  • In Slick, problem is resolved by HCons list, http://stackoverflow.com/questions/20555304/how-can-i-use-the-new-slick-2-0-hlist-to-overcome-22-column-limit – Mon Calamari Nov 24 '14 at 08:32
  • @m-z I am using mongodb where documents with lots of fields are a common use case. – imagio Nov 24 '14 at 14:56
  • 1
    I am using Scala 2.11 and it does indeed lift the limit but the Play framework apparently still has code in place that behaves as if the limit exists because they support Scala 2.10. I have found a workaround via [this question](https://stackoverflow.com/questions/23571677/22-fields-limit-in-scala-2-11-play-framework-2-3-case-classes-and-functions) using the Shapeless library but that feels more like a hack than a good solution. – imagio Nov 24 '14 at 15:03

1 Answers1

0

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))
dingdong
  • 169
  • 10