relationships with unique IDs.
Bad approach:
student{
name: "siivaji",
City: "Amalapuram ",
College:{
Id:"cg1",
name:"aditya College of Engineering ",
Pincode :5454643,
}
}
Good approach:
student {
name: "siivaji",
City: "Amalapuram ",
College: cg-id
}
College {
Id:"cg1",
name:"aditya College of Engineering ",
Pincode :5454643,
}
Why?
without Normalization :
const state = {
users: [
{
id: 65577,
name: "shivaji897",
posts: [
{
id: 101,
title: "post1",
},
{
id: 102,
title: "post2",
},
],
},
{
id: 65577,
name: "vyshnavi34",
posts: [
{
id: 101,
title: "post1",
},
{
id: 102,
title: "post2",
},
],
},
],
};
console.log(state.users[1].name);
console.log(state.users[1].posts[1].title);
normalized data :
const normalizedState = {
users: {
byIds: {
1: {
id: 65577,
name: "shivaji897",
},
2: {
id: 65577,
name: "vyshnavi34",
},
},
allIds: [1, 2, 3, 4],
},
posts: {
byIds: {
101: {
id: 101,
title: "post1",
userId: 1232,
},
102: {
id: 102,
title: "post2",
userId: 2556,
},
103: {
id: 103,
title: "post3",
userId: 1656,
},
},
allIds: [101, 102, 103],
},
};
// O(1) complexity and no redundancy, objects.keys don't give the proper order(random).