Repository navigation
Conversation
|
We do use transforms quite a lot in our apps, and I don't really see how it would be easy to move this to serializers. Sure, it would be possible, but would be much more verbose. Some examples: Our server can sometimes return null for array fields, so we have an Then, we also have some common objects, which the server sends as inlined JSON objects, and we convert to actual class instances in transforms (and back to JSON when serializing). Again, this is used many times, and could theoretically be done in a serializer, but would be much more verbose. Currently, we only have special serializers for really custom, one-off functionality, and all generic functionality that we need multiple times in different models can be handled in transforms. Removing transforms and requiring to do all of this in serializers seems very un-DRY to me. It would also mean that we'd basically need a custom serializer for each and every of our models (which we currently don't). Maybe I'm missing something here - but what would the actual migration strategy be for a generic transform that's used in multiple models? |
|
I would change the syntax instead, keeping string, number, boolean, and date (basically making them types) and making custom transform syntax as: import transform from 'app/transforms/moment';
//...
// just a suggested API
dateUpdated: attr('date', {
transform: transform({
format: 'YYYY-MM-DD'
})
}),
// OR
const options = {
format: 'YYYY-MM-DD'
};
// ..
dateUpdated: attr('date', {
transform: {
serialize(value) {
// use options
},
deserialize(value) {
// use options
}
}
}),A transform be a function that takes options and returns a class/object with |
|
@knownasilya What about non-standard types (i.e. custom classes) and arrays? |
Basically the issue here is that one bad pattern (per-type serializers) is endorsing another bad pattern as a compromise (transforms). That said, another way of looking at this is that you've already done exactly the thing you say you fear, just on the model instead of in the serializer where it belongs.
You could do this once in the
It would likely be less verbose if done in the application serializer, especially if each class implements
Transforms are anything but DRY, as they require you to do extensive additional work to be able to ensure mutations conform to a hidden and unofficial "type" that they lead you to believe exists, and must be repeated on every
A single application serializer will do the trick. The serializer is always given the information of the model being serialized / deserialized.
To use your array example. const EMPTY_ARR_ATTRS = {
'dashboard-user': ['settings', 'friends']
};
function normalizedEmptyArrAttrs(modelName, srcHash) {
let attrsToNormalize = EMPTY_ARR_ATTRS[modelName];
let attributes = {};
if (attrsToNormalize !== undefined) {
attrsToNormalize.forEach(attr => {
attributes[attr] = srcHash[attr] === null ? [] : srcHash[attr];
});
}
return attributes;
}
class Serializer {
extractAttributes(modelClass, resourceHash) {
let attributes = {};
let { modelName } = modelClass;
let srcAttributes = resourceHash.attributes;
// ... do other things as necessary ...
Object.assign(attributes, normalizeEmptyArrAttrs(modelName, srcAttributes));
// ... do other things as necessary ...
}
} |
|
@Alonski thanks for putting this together to get the conversation started! We discussed this recently on the team meeting and it seems likely that instead of deprecating transforms we just leave them as a feature of the current model layer (yes, this implies a different model layer might exist, this was something enabled by the We will be splitting ember-data into multiple packages soon (RFCs coming), and while deprecating transforms would allow us to keep |
|
We use transforms a fair bit as well. Testing as it pertains to a transform is really nice, and does a nice job performing a "separation of duties" task. I always feel like the serializers end up with a LOT of code (this may just be because I work with APIs that don't conform to JSONAPI), but when that happens it becomes cumbersome to test/maintain. So, I'm not big advocate of letting serializers bear the brunt of this workload, especially considering there is already a reasonable pattern in place. Reading the RFC, it seems the main issue is the confusion regarding types and I believe this confusion stems mostly from the design of the export default DS.Model.extend({
name: DS.attr('string'), // seems like a type
age: DS.attr('number'), // seems like a type
profile: DS.attr('markdown'), // custom transform, is it "markdown" a type?
});Perhaps as an alternative, transforms become options with a type of "object". So in practice, it could work along these lines: export default DS.Model.extend({
name: DS.attr('string'), // seems like a type
age: DS.attr('number'), // seems like a type
profile: DS.attr('object', {
transform: 'markdown'
}), // Clearly and object, but uses a transform.
});Again, most of my apprehension here is to avoid shoving more responsibilities onto the serializers. However, I can appreciate the delicate nature of transforms and their impact on performance if used incorrectly. |
|
Effective use of transforms has provided a great developer experience in our applications. We use a custom transform when dealing with units on a model that provides access to things like conversion rates and translated names. All a developer has to do is declare |
|
Long time Ember user here. Transforms are one of my favorite parts of Ember Data and one of the main reasons I haven't moved to something else. Key benefits:
One of my most often used custom transforms is I do believe there are strong reasons to keep transforms. If transforms did not exist, we could solve the same problem today through custom serializers, though the concept of an isolated, reusable attribute transformation seems like a good abstraction. For me, it would help a lot to see what the proposed "new way" would be to replace a transform. Usually the RFCs have an "old way" vs. "proposed new way". Would someone be able to update the RFC to include that? |
|
Closing as it seems folks would prefer to leave transforms on the current model layer and as mentioned above we feel it would be less disruptive to enable folks to drop that model layer all at once vs in pieces. I would encourage these folks to investigate the powers of serializers more thoroughly, as these sorts of transformations will nearly always be more clear when done there: this includes the case where one only uses one or a small handful of serializers (such as one per API). |
|
@runspired maybe a blog post or stream would help guide people in how to do this properly, in a testable way? |
|
@Alonski Thanks for bringing this up for discussion! 🏅 I have three main comments:
|
|
We breathe transforms. |
Maybe a silly question, but can someone explain why this is? If a transform can guarantee that a property will be a string or number or someCustomThing, why is it wrong to think of it as a type? edit: for what it's worth, I like transforms for documentation mostly. Looking at a model and knowing what a property is expected to be is a nice alternative to having a schema file. An equivalent API could probably exist in serializers though, so I'm not particularly attached to transforms in their current form. |
|
@mehulkar transforms have zero guarantees. They are only used by the serializer, not the model (so calls to |
Rendered