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

Viewed 857

I am using aws-amplify in my react-native application for user authentication. it works fine.

modules used for aws amplify
aws-amplify: "^3.0.23", aws-amplify-react-native: "^4.2.4"

Now I would like to connect the app or retrieve the data from a DynamoDB table and use that data in my react native application.

How can I Access data from Dynamodb tables in react native using aws-amplify is there any get methods or rest APIs to retrieve data from dynamodb.

How can I establish a connection with dB to my react-native application.

Thanks in advance.

2 Answers

I used

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)
 }
 }

I m using graphql api and when i create any model, aws automatically creates queries and mutations for that.

Let's say i have this model in schema json.

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

here are related query and mutation:

enter image description here

in lambda function i defined the query:

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
        }
    }
`;

then I call it:

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