如何使用 aws-amplify 访问 Dynamo 数据库表中的数据

How to Access data from Dynamo db tables in react native using aws-amplify

我在我的 react-native 应用程序中使用 aws-amplify 进行用户身份验证。它工作正常。

用于 aws 放大的模块
aws-amplify: "^3.0.23", aws-amplify-react-native: "^4.2.4"

现在我想连接应用程序或从 DynamoDB 检索数据 table 并在我的 React Native 应用程序中使用该数据。

我如何使用 aws-amplify tables 在 react native 中访问来自 Dynamodb 的数据 是否有任何 get 方法或 rest API 可以从 dynamodb 检索数据。

如何与 dB 建立连接到我的 react-native 应用程序。

提前致谢。

我用过

aws-sdk/dist/aws-sdk-react-native

var AWS = require('aws-sdk/dist/aws-sdk-react-native');

AWS.config.update({
  region: 'your region',
  dynamoDbCrc32: false
});
AWS.config.credentials = new AWS.CognitoIdentityCredentials({
  IdentityPoolId: 'your identityPool Id'
});
const gp = AWS.config.credentials.getPromise();

gp.then(() => {
    const documentClient = new AWS.DynamoDB.DocumentClient();
    const params = {
      TableName: "table name",
      Key: "your key",
    };
documentClient.get(params, function(err, data) {
 if (err){
   console.log(err)
  } else {
   console.log(data)
 }
 }

我正在使用 graphql api,当我创建任何模型时,aws 会自动为其创建查询和变更。

假设我在模式 json 中有这个模型。

type ExpoTicketsObject
    @model
    @auth(rules: [{ allow: private, provider: iam, operations: [read, create, update, delete] }]) {
    id: ID!
    tickets: AWSJSON!
}

这里是相关查询和变异:

在 lambda 函数中我定义了查询:

const appsync = require("aws-appsync");
const gql = require("graphql-tag");
// inorder to use fetch api in node, we need this polyfill
require("cross-fetch/polyfill");

const getExpoToken = gql`
    query getExpoToken($token: String!) {
        # token is the primary key.
        getExpoToken(token: $token) {
            id
            token
            playerUsername
        }
    }
`;

那我就叫它:

const tokenRes = await graphqlClient.query({
        query: getExpoToken,
        variables: { token: event.arguments.token }
    });