[英]Best approach for Posts and PostReactions in AWS Amplify and DynamoDB
I am working on chat functionality using AWS Amplify and I have a simple Post model in my graphql schema:我正在使用 AWS Amplify 开发聊天功能,我的 graphql 架构中有一个简单的 Post model:
type Post
...
{
id: ID!
channelId: ID @index(
name: "byChannel", sortKeyFields: ["createdAt"],
queryField: "listPostsByChannel"
)
customerId: ID @index(
name: "byCustomer", sortKeyFields: ["postType", "createdAt"]
)
text: String!
postTempId: String
postType: String
reactions: [PostReaction] @hasMany(fields: ["id"])
createdAt: AWSDateTime
updatedAt: AWSDateTime
}
What I want to achieve is to have similar to other popular chat apps - reactions with emojis attached to each post, so I've created another table and the PostReaction model.我想要实现的是与其他流行的聊天应用程序类似 - 每个帖子都附有表情符号的反应,所以我创建了另一个表和 PostReaction model。
type PostReaction
...
{
postId: ID! @primaryKey(sortKeyFields: ["customerId", "emojiUnicode"])
customerId: String!
customerMeta: CustomerMeta
emojiUnicode: String!
createdAt: AWSDateTime
updatedAt: AWSDateTime
}
Of course, each customer could add multiple emojis to a single post, the custom primary key is for handling duplicates later.当然,每个客户可以在一个帖子中添加多个表情符号,自定义主键用于稍后处理重复。
There is one disadvantage here.这里有一个缺点。 Emojis will be listed in an array in the reactions
field in the post, even if it's the same emoji added by many people.表情符号将列在帖子的reactions
字段中的数组中,即使它是许多人添加的相同表情符号也是如此。
Instead of a simple array of reactions that frontend would need to merge for each post, the best would be to get a result from the AppSync query for each Post
like:而不是前端需要为每个帖子合并的简单反应数组,最好的方法是从每个Post
的 AppSync 查询中获取结果,例如:
...
reactions: [{
emojiUnicode: "U+1F44D",
customerIds: ["ID1234", "ID5678"],
...
}, {...}]
I thought that I can use a JSON object in the reactions
field, but the DynamoDB has the max size limit for a single item which is 400KB.我以为我可以在reactions
字段中使用 JSON object,但 DynamoDB 对单个项目的最大大小限制为 400KB。 That's not a problem for now, but next when I will add more attributes to the Post model, and when there will be many reactions from many people at the same time, this might be an issue.这暂时不是问题,但是当我给model这个帖子增加更多的属性,当很多人同时有很多反应的时候,这可能是一个问题。
Is there an option how to achieve this in the simplest way?有没有选择如何以最简单的方式实现这一目标?
Best thing to not over-complicate your schema would be to enforce a maximum number of emojis just as Slack does for example:不要让你的架构过于复杂的最好办法是强制使用最大数量的表情符号,就像 Slack 所做的那样:例如:
You can add up to 23 emoji reactions to any message, but the maximum per message is 50 unique emoji.您最多可以为任何消息添加 23 个表情符号反应,但每条消息最多可以添加 50 个独特的表情符号。
Other than that, you could keep an item for each emoji reacted除此之外,你可以为每个反应的表情符号保留一个项目
pk pk | sk斯克 | data数据 |
---|---|---|
thread123线程123 | metadata元数据 | metadata about thread关于线程的元数据 |
thread123线程123 | post#001帖子#001 | First message in thread线程中的第一条消息 |
thread123线程123 | post#002帖子#002 | Second message in thread线程中的第二条消息 |
thread123线程123 | post#003帖子#003 | Third message in thread线程中的第三条消息 |
thread123线程123 | post#003#emoji#U+1F44D帖子#003#emoji#U+1F44D | [user1, user2, user45] [用户 1、用户 2、用户 45] |
thread123线程123 | post#003#emoji#U+1F33R帖子#003#emoji#U+1F33R | [user56, user8, user7, user10] [用户 56、用户 8、用户 7、用户 10] |
Now when you want all the data to populate a given thread on your UI, you just issue a query with the pk as a parameter:现在,当您希望所有数据填充 UI 上的给定线程时,您只需使用 pk 作为参数发出查询:
SELECT * FROM table WHERE PK = 'thread123'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.