简体   繁体   中英

How to sync data to AWS DynamoDB using Amplify DataStore?

I've setup a React Amplify project. I have successfully got Auth working using Cognito User Pools but can't seem to figure out DataStore/API.

I currently use DataStore to save data locally but it doesn't seem to update in the backend. I do have the aws_appsync_graphqlEndpoint in the aws-exports.js .

Not sure how to enable the Sync Engine in this guide .

You can use DataStore.query to query the data.

DataStore.save to save the record etc.

Below example should be good starting point.

https://github.com/dabit3/amplify-datastore-example

You must import Amplify from @aws-amplify/core and NOT from aws-amplify

The following code example worked well in App.js, at the BOTTOM of all your imports:

import Amplify from "@aws-amplify/core";
import { DataStore, Predicates } from "@aws-amplify/datastore";
import { Post, PostStatus } from "./models";

//Use next two lines only if syncing with the cloud
import awsconfig from "./aws-exports";
Amplify.configure(awsconfig);

Source: https://docs.amplify.aws/lib/datastore/examples/q/platform/js

If I understand the question correctly everything is set up with DataStore and working locally. The problem is that it doesn't sync to the cloud. One of the answers suggested using Apollo. You don't need Apollo. By default, and the suggested development mode of using DataStore, is local only. When you are ready to deploy to the cloud you use the command

amplify push

which provisions all of your cloud resources for the app. For more details refer to DataStore docs .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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