Ember data round trip serialization for new records


#1

I want Ember data round trip serialization. It should be a no brainer. At any point, with any object, I should be able to serialize the object, push it back into the store, and end up with the same representation I started with. This is already possible, but only so long as all records have IDs.

Note: skip to bottom for TL;DR.

Why is this important?

Consider an address: conceptually an address probably does not need an ID, and many things probably have addresses. An address could in theory be a POJO, but then you lose the ability for it to have shared serialization logic.

Now I have person model, which has an address. I attempt to do round trip serialization. Because there is no ID, I use the embedded records mixin and say that address is always embedded. Fabulous, right? Of course not, the round trip is broken because embedded records must have IDs to be loaded.

This is, pardon the expression, a load of crap. It is data’s implementation, not its philosophy that is breaking this and making ember data a nightmare for the many of who have to deal with systems where not all objects have IDs.

Why is this broken?

Because of the contract between serializer and store.

At serialization time, the store gives the serializer a model and expects it to produce a POJO. Logical. Great!

At deserialization time, the store gives the serializer a payload or a POJO and expects in return, a normalized POJO. The store reserves the right to create records for itself. If this policy were really enforced, it would be impossible to deserialize relationships. To skirt the issue, records are pushed as a side effect or encountering relationships, and their IDs are kept in the POJO. No IDs, no records.

It seems clear to me that the solution conceptually is simple. Serializers should push records, not the store. The serializer’s responsibility would be clear: turn records to JSON, turn JSON into records. Why isn’t this the case?

TL;DR:

Why can’t serializers create/update records as the last step in deserialization? What functionality would this break?