Reputation: 17
I'm working on a project and was trying to create data models for it. We've a usecase where a user can host an event and add members to it.
class Event {
event_name: String
}
class User {
username: String
}
I wanted to know which of the following way to store the event members in the Event class.
//v1
class Event {
event_name: String,
event_members: Array<String> // List of usernames
}
//v2
class Event {
event_name: String,
event_members: Array<User> // List of user objects
}
By using v2, I feel I'll be able to move the logic to get user information, from DB, from client side to my server.
Latency is also something that I'm considering. If I go with v1, then I need to make multiple calls to the server to fetch all the information about event members, resulting in increase in wait time. Whereas, in v2, the response payload is increasing which might impact our network calls.
I wanted to know which will be a better way to store my model among the two and if there's a different and more efficient way then please let me know.
Upvotes: 0
Views: 130
Reputation: 599011
There is no singular "better" data model here. When modeling data in a NoSQL database, it always depends on your use-cases. As you add more use-cases to your app, you'll expand and modify the data model to fit your needs.
That said, I typically store both directions of a many-to-many relationship, so both v1 and v2. This allows fast lookup of the related items in both directions, at the cost of some extra storage - so a typical time-vs-space trade-off.
But as said: there is no singular best data model, and when you're just getting started I typically focus on getting a simple model working quickly, and on securing access to that data.
For a good introduction to the topic in general, I recommend reading NoSQL data modeling, and for Firestore specifically watch Todd's excellent Getting to know Cloud Firestore series.
Upvotes: 2