I'm new to DynamoDb and generally new to NoSql databases. I'm currently playing arround with a simple webapplication which is written in asp.net core 6 an run on aws lambda.
I have posts which can be in different channels. Users can make comments on posts. Posts and Comments can be up/down voted.
When trying to implement voting on comments I started getting the feeling, that my design is quite bad after reading for example the following question: Update nested map dynamodb
The issue is that:
- I can't update votes of a comment without knowing the array index
- the array index is prone to race conditions when an older comment (with lower index therefore) is deleted
Current data structur (3 tables):
guids are saved as strings
post: {
pk -> id
sk -> id
id : guid
createdAt : date
createdBy : guid // user.id
comments: [
{
id : guid
text : string
createdAt : date
createdBy : guid // user.id
upvotedBy : guid[] // user ids
downvotedBy : guid[] // user ids
upvotes : int // aggregated count of above list
downvotes : int
},
{ ... },
{ ... },
{ ... },
]
commentCount : int
upvotedBy : guid[] // user ids
downvotedBy : guid[] // user ids
upvotes : int // aggregated count of above list
downvotes : int
channel : guid // channel.id
hidden : bool
}
user: {
pk -> id
sk -> id
id : guid
username : string
password : string
createdAt : date
subscribed_channels : guid[] // channel ids
}
channel: {
pk -> id
sk -> id
id : guid / string
channelname : string
}
I have additional keys for:
- posts
- ChannelIndex : pk = channelname, sk = createdAt
- CreatedAtIndex : pk = pk, sk = createdAt
- users
- UsernameIndex : pk = username, sk = sk
- channels
- ChannelnameIndex : pk = channelname, sk = sk
Usecases (ordered by frequency):
- Access all posts without comments
- from multiple channels
- from single channels
- sorted by date
- sorted by most comments
- sorted by most likes
- from all channels
- Access single post with (all) comments
- vote on comment
- vote on post
- create comment
- create post
- delete comment
- delete post
Question:
How to improve this design from a performance but also from the ease of implementation side?
My ideas:
I thought about removing the nesting for comments making them a own table with a key beeing postId
and createdAt
However this feels like the design for a relational database.