Reputation: 577
I am creating an application to store bookmarks and every bookmark might have many labels. I am new to NoSQL databases and can't figure out how to structure my table. For now I have the following model:
PK SK data
USER#1 USER some data
USER#1 BOOKMARK#1 bookmarkDetails
USER#1 LABLE#1 labelDetails
LABEL#1 BOOKMARK#1 bookmarkDetails
With it I can query all user bookmarks and all user labels. But I need to store in the bookmark details which labels are assigned including title and color for example. But what happens when I want to update details of the label and change the name? Should I go through all bookmarks and update the title of the label where it exists? And in the case of DynamoDb that would mean I need to send many request to fetch the bookmarks that I need to update and then send update request.
Is it okay to store the labels that are assigned to a bookmark in bookmarkDetails and store the bookmarks that have the given label in labelDetails? This will lead to the need to update many rows if I change the title of the label or title of the bookmark. Coming from an RDS world this seems overwhelming and huge development effort. Is there another way to represent ManyToMany relationship? In this case I need to be able to fetch all bookmarks by label and that's why I am following this approach.
EDIT: I am adding the access patterns that I will need here to be more clear.
Entities:
User: ID and email
Bookmark: ID and url
Label: ID and title
Relations:
User to Bookmark = OneToMany
Bookmark to Label = ManyToMany
And the update actions are to be able to change the label title and bookmark url
Upvotes: 2
Views: 141
Reputation: 2096
Storing labels data in bookmarkDetails and bookmarks data in labelDetails will complicate the solution. It might be better to separate out the details of bookmarks and labels into separate tables and just keep the mappings of label-bookmark-user in one table.
Whether or not the following solution works will depend on the number of distinct bookmarks you have, number of distinct labels, number of distinct users, number of bookmarks per user and number of labels per bookmark.
Table 1: Bookmark Details
PK
----------
BookmarkID BookMarkDetails (Name, title etc)
Table 2: Label Details
PK
-------
LabelID LabelDetails (Name, title etc)
Table 3: UserBookmarkLabelMapping
PK SK
----------------- --------
UserID#BookmarkID LabelID(GSI) UserID (GSI) BookmarkID (GSI)
Write Operations
Read Operations
Get all user labels: Same as above.
Get all bookmarks for a label.
The drawback is that unlike the relational world we will be running two sequential queries - one to get the Ids and second one to get the details of these IDs. This can be overcome if the number of distinct bookmarks/labels is small, we can cache this data in memory. Or if you are using DynamoDB, then maybe using something like DAX to speed up the getBookmarkDetails, getLabelDetails calls.
Upvotes: 1