热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

AWSAppSync-使用GSI排序键将全局二级索引添加到DynamoDB和分页

如何解决《AWSAppSync-使用GSI排序键将全局二级索引添加到DynamoDB和分页》经验,为你挑选了1个好方法。

我正在尝试创建一种结构,该结构列出具有降序postId排列的帖子评论lastChangeTime

模式中的模型在下面共享。

type Comment {
  id: ID!
  postId: String!
  user: String!
  lastChangeTime: String
  commentBody: String
}

它已经具有支持DynamoDB表和通用CRUD解析器。而id字段是表的主键。

我计划建立一个查询,如下所示:

{
  "version": "2017-02-28",
  "operation" : "Query",
  "index" : "postId-index",
  "query" : {
    "expression": "post = :postId",
    "expressionValues" : {
      ":postId" : {
        "S" : "${ctx.args.postId}"
      }
    }
  },
  "limit": $util.defaultIfNull($ctx.args.first, 20),
  "nextToken": $util.toJson($util.defaultIfNullOrEmpty($ctx.args.after, null)),
  "scanIndexForward": false
}

为了使其正常工作,我该如何在(即)上添加全球二级索引(GSI)?postIdpostId-index

我应该在定义时在其上添加排序键lastChangeTime吗?还是lastChangeTime字段需要对自己的单独索引进行排序?



1> Ula..:

这很容易。您可以通过两种不同的方式来做到这一点,也可以同时使用两种方式来获得更好的灵活性。(如果您已经解决了该问题,希望对您有所帮助)。

这样,您可以使用查询参数动态设置sortDirection。

详细代码如下。在此之前,请注意这一点。

    第一点是您的评论类型-您正在使用

    type Comment {
      id: ID!
      postId: String!
      ## rest of your type definition
    }
    

这不是设置链接到帖子的评论类型的最佳方法。

更好的方法是:

type Comment {
    postID: ID!         ## select this as Primary key in DataSource in AppSync console
    commentID: String!    ## select this as Sort key in DataSource in AppSync console
    ## rest of your type definition
}

如果执行此操作,则DynamoDB表的结构将类似于下面显示的结构(来自此AWS网页)。

(在您的情况下,UserId将为PostId,而GameTitle将为CommentID)

这样,因为所有注释都将彼此相邻记录(在同一PostId下),因此AppSync响应时间将更快。

在AppSync文档页面中,他们还使用了以下示例:

    正如@Lisa M Shon所提到的,您可以在CommentTable上启动GSI,其中PostId是分区键,而addTime是排序键。如果要使用下面提供的解析器,则称其为“ postID-addedTime-index”。确保您在GSI中的addedTime上选择了“ Number”。

然后,您可以在架构中定义以下类型:

type Comment {
    postID: ID!
    commentID: String!
    content: String!
    addedTime: Int!
}

type CommentConnection {
    items: [Comment]
    nextToken: String
}

type Post {
    id: ID!
    postContent: String!
    addedTime: Int!
    ## Option 1. Gets Post details with all related Comments. 
    ## If 'startFromTime' is provided it will fetch all Comments starting from that timestamp.
    ## If 'startFromTime' is not provided it will fetch all Comments.
    comments(
        filter: TableCommentFilterInput,
        sortDirection: SortDirection,
        startFromTime: Int,
        limit: Int,
        nextToken: String
    ): CommentConnection
}

type Query {
    ## Option 2. It will fetch Comments only for a given PostId.
    ## If 'startFromTime' is provided it will fetch all Comments starting from that timestamp.
    ## If 'startFromTime' is not provided it will fetch all Comments.
    postCommentsByAddTime(
        postID: String!,
        startFromTime: Int!,
        sortDirection: SortDirection,
        filter: TableCommentFilterInput,
        count: Int,
        nextToken: String
    ): PaginatedComments
   ## your other queries
}

    ## rest of your schema definition

您可以同时使用-选项1和选项2并同时使用。

完整的架构代码在这里(展开下面的代码片段):

type Comment {
  id: ID!
  postId: String!
  ## rest of your type definition
}

推荐阅读
author-avatar
丹洋2012_757
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有