amplify-cli icon indicating copy to clipboard operation
amplify-cli copied to clipboard

Sort of data items

Open marcschroeter opened this issue 7 years ago • 2 comments

Note: If your question is regarding the AWS Amplify Console service, please log it in the official AWS Amplify Console forum

** Which Category is your question related to? ** api

** What AWS Services are you utilizing? ** dynamodb

** Provide additional details e.g. code snippets ** We want to develop a social media app. How can we show the latest posts first when its not possible to have sort keys at the moment?

marcschroeter avatar Apr 20 '19 09:04 marcschroeter

Until we have better support for custom indexes (#1062 will be updated again with new designs), you have a few options:

  1. Use custom resources to define your DynamoDB tables with the sort keys of your choosing. This will also require your to configure an IAM role, AppSync data source, and AppSync resolvers manually. You can learn more about the API categories custom resources functionality here https://aws-amplify.github.io/docs/cli/graphql#api-category-project-structure. When building many-to-many relationships which it seems this might be, you can use an adjacency list pattern in DynamoDB https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/bp-adjacency-graphs.html.

  2. Use @connection and a bit of indirection.

type UserFeed @model {
  id: ID!

  # Will configure the GSI such that the edges are partitioned by their "userFeedId" and sorted
  # by the date they were added in the feed.
  edges: [PostEdge] @connection(name: "UserFeedPostEdge", keyField: "userFeedId", sortField: "addedToFeedDate")
}
# When you create a PostEdge, you specify a "addedToFeedDate" that is effectively your posted date.
type PostEdge @model {
  id: ID!
  userFeedId: ID!
  addedToFeedDate: String
  post: Post @connection(name: "PostEdgePost")
  feed: UserFeed @connection(name: "UserFeedPostEdge", keyField: "userFeedId", sortField: "addedToFeedDate")
}
# The post itself might have its own date and can be distributed to people's feeds at a different time.
type Post @model {
  id: ID!
  title: String
  edges: [PostEdge] @connection(name: "PostEdgePost")
}

The custom index work aims to simplify these patterns as there are some known issues with defining GSIs based on @connection.

  1. If the number of posts in a users feed is small you can consider storing the list of post ids on the User object and then configure a BatchGetItem resolver on User.posts to fetch the posts for the user.

mikeparisstuff avatar Apr 26 '19 19:04 mikeparisstuff

This issue has been automatically locked since there hasn't been any recent activity after it was closed. Please open a new issue for related bugs.

Looking for a help forum? We recommend joining the Amplify Community Discord server *-help channels for those types of questions.

github-actions[bot] avatar May 27 '21 10:05 github-actions[bot]