【问题标题】:Best approach for Posts and PostReactions in AWS Amplify and DynamoDBAWS Amplify 和 DynamoDB 中 Posts 和 PostReactions 的最佳方法
【发布时间】:2023-01-17 21:36:39
【问题描述】:

我正在使用 AWS Amplify 开发聊天功能,我的 graphql 架构中有一个简单的 Post 模型:

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
}

我想要实现的是与其他流行的聊天应用程序类似 - 每个帖子都附有表情符号的反应,所以我创建了另一个表和 PostReaction 模型。

type PostReaction
...
{
    postId: ID! @primaryKey(sortKeyFields: ["customerId", "emojiUnicode"])
    customerId: String!
    customerMeta: CustomerMeta
    emojiUnicode: String!
    createdAt: AWSDateTime
    updatedAt: AWSDateTime
}

当然,每个客户可以在一个帖子中添加多个表情符号,自定义主键用于稍后处理重复。

这里有一个缺点。 表情符号将在帖子的reactions字段中以数组形式列出,即使它是许多人添加的相同表情符号。

前端需要为每个帖子合并一系列简单的反应,最好的办法是从 AppSync 查询中为每个 Post 获取结果,例如:

...
reactions: [{
  emojiUnicode: "U+1F44D",
  customerIds: ["ID1234", "ID5678"],
  ...
}, {...}]

我以为我可以在 reactions 字段中使用 JSON 对象,但 DynamoDB 对单个项目的最大大小限制为 400KB。现在这不是问题,但接下来我将向 Post 模型添加更多属性时,当同时有许多人的许多反应时,这可能是一个问题。

有没有选择如何以最简单的方式实现这一目标?

【问题讨论】:

    标签: amazon-web-services graphql amazon-dynamodb aws-amplify aws-appsync


    【解决方案1】:

    不要让你的架构过于复杂的最好办法是强制使用最大数量的表情符号,就像 Slack 所做的那样:例如:

    您最多可以为任何消息添加 23 个表情符号反应,但每条消息最多可以添加 50 个独特的表情符号。

    除此之外,你可以为每个反应的表情符号保留一个项目

    pk sk data
    thread123 metadata metadata about thread
    thread123 post#001 First message in thread
    thread123 post#002 Second message in thread
    thread123 post#003 Third message in thread
    thread123 post#003#emoji#U+1F44D [user1, user2, user45]
    thread123 post#003#emoji#U+1F33R [user56, user8, user7, user10]

    现在,当您希望所有数据填充 UI 上的给定线程时,您只需使用 pk 作为参数发出查询:

    SELECT * FROM table WHERE PK = 'thread123'

    【讨论】:

      猜你喜欢
      • 2019-10-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-12
      • 1970-01-01
      • 2022-12-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多