BsonRecord inside of a case class

19 views
Skip to first unread message

Emmanuel Eytan

unread,
Mar 18, 2015, 9:29:36 PM3/18/15
to lif...@googlegroups.com
My Lift site uses Mongo as a database. Most of what's stored in Mongo as a JavaScript object was represented in Lift as a case class. But recently, I've had to change such an object to a BsonRecord.

This worked wonderfully. I can now use Rogue to search inside of the BsonRecord, which is why I made the change in the first place.

However, today we noticed a very annoying side effect: the content of that original case class was also saved somewhere else as part of another polymorphous case class. So, basically, we have:

class Collection extends MongoRecord[...] {
 
// ...
 
// That used to be a MongoCaseClassListField:
 
object assets extends BsonRecordListField(this, MainItemType)
 
// ...
}

// This used to be a case class:
class MainItemType extends BsonRecord[...]

And elsewhere:

abstract trait GeneralItemType

class Preset extends MongoRecord[...] {
 
// ... metadata
 
object items extends MongoCaseClassListField[Preset, GeneralItemType](this) {override formats = ...}
 
// ... metadata
}

case class ItemType1(
  item
: MainItemType, // used to be a case class; is now a BsonRecord
 
// metadata....
) extends GeneralItemType

case class ItemType2(
  thing
: OtherItemType, // Completely different type.
 
// metadata....
) extends GeneralItemType

The MainItemType data inside of ItemType1 is supposed to be a copy, not a reference. We need it because the original may be gone by the time we look it up from within a Preset. When MainItemType was a case class, this was easy, as case classes are always serializable and easy to copy.

We weren't really looking at the presets, until today when someone noticed something that was not working. None of the MainItemTypes within presets were saving properly Now, based on what I understand:
  • I cannot save the item field from inside of an ItemType1 instance.
  • I cannot have a polymorphous BsonRecord inside of Preset
Now, tomorrow, I'm going to try to work around those limitations, but does some glaring omission or error on my part? I'm thinking of creating a "shadow" MainItemType as a case class and transfer the contents via JSON. I know it's ugly and dirty, but this is all i have so far.

Thanks!

Tim Nelson

unread,
Mar 19, 2015, 5:52:42 AM3/19/15
to lif...@googlegroups.com
Hi Emmanuel,

I've never tried this, but if you add a CustomSerializer to the Formats for Preset.items for turning a BsonRecord into JValue, it should work.

For the CustomSerializer, you just need to call asJValue and fromJValue on the BsonRecord.

Tim

Emmanuel Eytan

unread,
Mar 19, 2015, 11:57:54 AM3/19/15
to lif...@googlegroups.com
That is WAY better than what I was about to do. It should totally work! I'll try it and let you know. Thanks!

Emmanuel Eytan

unread,
Mar 19, 2015, 1:30:47 PM3/19/15
to lif...@googlegroups.com
Apart from a silly mistake I made early on and that took me forever to track (stupid me!), it worked like a charm.

Tim Nelson

unread,
Mar 19, 2015, 4:59:09 PM3/19/15
to lif...@googlegroups.com
Good to hear.
Reply all
Reply to author
Forward
0 new messages