I always had this doubt when I think about user data. If I created a site, for example a marketplace, when I’m going to store the data in the backend should I simply create a big array like this?
const user = [
userName: "john",
userWallet: 122,
userProfilePic: "https://blbabablabl.com/ewxase"
userCart: [
{
productName: "Air Jordan 1",
price: 280,
productImage: https://www.blablabla.com/image
}
{
productName: "Louis Vuitton Bag",
price: 900,
productImage: https://www.blablabla.com/image
}
...
]
]
and then for every user I create an array?
is this right?
what companies do in this situation?
where can I learn more about storing things in the backend?
I’m really lost when it comes to backend.
(I’m using firebase in my project cuz I don’t have the interest to study back-end. For now, I’m focusing more on the front-end)
2
Answers
The best way to do that would be to create separate sub-collection name
carts
under document of given user and store all necessary attributes for each cart item as document of that subcollection.eg. You have user documents under
users
collection, thencart document path would be as
/users/3y1x1Dy6FYOGnSW0Sa4f1EUhiIo1/carts/WADVd2AZ91d7m9kSq3GP
See screenshots below:
Alternatively, you can create same level collection for carts and store userId field on cart’s document.
For smaller use cases, you can definitely use arrays. However, this isn’t scalable – you’ll find that this approach can become very inefficient as your user base grows. Most companies store their user data in a database, such as MongoDB or MySQL. Databases allow you to create a "users" collection with columns for each of the attributes such as username, wallet, profile pic, etc. For more additional info about databases: https://www.w3schools.in/sql/database-concepts