简体   繁体   English

AWS Amplify 和 DynamoDB 中 Posts 和 PostReactions 的最佳方法

[英]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.

相关问题 使用 AWS Amplify 更新 DynamoDB 架构 - DynamoDB schema updates with AWS Amplify 使用 AWS Amplify 从 DynamoDB 查询数据 - Querying data from DynamoDB using AWS Amplify AWS Amplify:在保留 AppSync 的同时,是否可以使用 Aurora 而不是 DynamoDB? - AWS Amplify: While retaining AppSync, Is it possible to use Aurora instead of DynamoDB? iOS 应用程序使用 AWS Amplify DataStore,它不与后端 (DynamoDB) 同步 - An iOS app uses AWS Amplify DataStore and it does not sync with backend (DynamoDB) AWS Amplify:在 confirmSignUp 之后,自动登录用户的最佳做法是什么? - AWS Amplify: After confirmSignUp, what is the best practice to automatically Sign In a user? aws amplify update my Vuex state 使用触发器更新 DynamoDB 表 - aws amplify update my Vuex state on DynamoDB table update using triggers AWS CognitoUser class 中用于 DynamoDB 中主键的最佳属性 - Best attribute to use from AWS CognitoUser class for primary key in DynamoDB 将 AWS S3 数据添加到现有 Android 应用程序的最佳方法是什么 - AWSJDK、Amplify 或没有 Amplify 框架的 AWS Android JDK? - What is best way to add AWS S3 data gets to an existing Android app - AWSJDK, Amplify, or AWS Android JDK without Amplify framework? 在 AWS 中等待并收到 SSM.SendCommand 完成通知的最佳方法是什么 - What is the best approach to wait and be notified of completion of SSM.SendCommand in AWS dynamoDB / aws-amplify - 使用“limit”+“filter”进行查询 - 返回一半为空或完全为空的页面,并且不包含数量为“limit”的项目 - dynamoDB / aws-amplify - Query with 'limit' + 'filter' - returns half empty or completely empty pages and not full with items with 'limit' amount
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM