Personally I usually do C with a API resource. It is just cleaner that way and you control exactly how you want the data to look.
Best Practice: Normalize/transform data to frontend?
Hi.
I'm looking for the best way to transform db data, with relations from the db to the frontend..
Lets say we have two tabels:
Database Profile Table:
{
"id": 3,
"user_id": 10,
"username": "Roberta",
"online_at: "2019-08-02 10:32:33",
}
Database News Table:
{
"id": 84,
"profile_id": 3,
"content": "...",
"title": "...",
},
I can think of 3 ways this can be transformed:
A: With nesting
profiles: {
"id": 10,
"user_id": 10,
"username": "Roberta",
"online_at: "2019-08-02 10:32:33",
"news" : [
{ "id": 84,
"profile_id": 3,
"content": "...",
"title": "...",
},
]
}
B: More flat but still separate
data: [
{
profile:{
"id": 10,
"user_id": 10,
"username": "Roberta",
"online_at: "2019-08-02 10:32:33",
},
"news" :
{ "id": 84,
"profile_id": 10,
"content": "...",
"title": "...",
},
}
]
C: Merge them all together (with risk of merge conflict in big db's)
data: [
{
"id": 3,
"user_id": 10,
"username": "Roberta",
"online_at: "2019-08-02 10:32:33",
"profile_id": 3,
"content": "...",
"title": "...",
}
]
My thoughts are that B and C is the best when you want to insert the data into view components. Since the structure is the same every-time.
Where the structure of A can vary, depending on what controller you ask and what relations this controller has. In case A, the component is in most cases not designed to handle different nested objects. Also, you have to manually sit and correct the nested path in the component every-time. Not so copy paste friendly :D
Now, my question is, what do you usually do? Are there better ways to transform the data?
Thanks ;-)
Please or to participate in this conversation.