简体   繁体   English

使用 AWS Amplify 对 DynamoDB 表进行有条件的读取访问

[英]Conditional read access to DynamoDB table with AWS Amplify

I'm building an application with AWS Amplify, where I have three DynamoDB tables: Users , Posts and Subscriptions .我正在使用 AWS Amplify 构建一个应用程序,其中有三个 DynamoDB 表: UsersPostsSubscriptions

  1. users can make posts用户可以发帖
  2. users subscribe to other users用户订阅其他用户
  3. user A can only see posts by user B if user A is subscribed to user B用户A只能看到由用户张贴B如果用户A已订阅用户B

Points 1. and 2. are easy to implement with standard graphQL mutations.第 1 点和第 2 点很容易使用标准的 graphQL 突变来实现。 But I'm stuck at how to implement 3. in an elegant way.但我坚持如何以优雅的方式实现 3.。 Currently what I do is to use a lambda resolver.目前我所做的是使用 lambda 解析器。

Given inputs "user A wants to see user B ", the lambda resolver does the following:给定输入“用户A想要看到用户B ”,lambda 解析器执行以下操作:

  1. Query Subscriptions table to see if there's a document for "user A subscribed to user B "查询Subscriptions表以查看是否有“用户A订阅用户B ”的文档
  2. if such a row exists, query Posts table and return documents.如果存在这样的行,则查询Posts表并返回文档。 If not, return nothing.如果没有,则不返回任何内容。

This logic required two round trips, but since dynamo is fast I'm OK with this trade-off.这个逻辑需要两次往返,但由于 dynamo 速度很快,我可以接受这种权衡。 There are other downsides though, so I'm wondering if there's a more Amplify-native way to do this?但是还有其他缺点,所以我想知道是否有更多的 Amplify-native 方法来做到这一点? Some magic DynamoDB and @auth trickery perhaps?也许是一些神奇的 DynamoDB 和 @auth 诡计?

Thank you!谢谢!

If you are using multiple tables to store the data, the multiple query approach is your only option.如果您使用多个表来存储数据,则多查询方法是您唯一的选择。

You can use transactions when mutating items across multiple tables, which is useful when you want to perform an operation based on a condition on an item in another table(s).可以在跨多个表更改项目时使用事务,这在您希望基于另一个表中项目的条件执行操作时非常有用。 But when it comes to a read operation, you have no such option.但是当涉及到读操作时,你没有这样的选择。

Aside from re-designing your tables to support this access pattern, I don't think two reads is particularly bad.除了重新设计您的表以支持这种访问模式之外,我认为两次读取并不是特别糟糕。

If you wanted to handle authorization logic outside of DDB, you may want to look into AWS IAM and it's documentation on Fine-Grained Access Control .如果您想在 DDB 之外处理授权逻辑,您可能需要查看 AWS IAM 及其有关Fine-Grained Access Control的文档。 Among other features, IAM can restrict access to specific items in a table based on certain primary key values.在其他功能中,IAM 可以根据某些主键值限制对表中特定项目的访问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM