Search code examples
angularamazon-web-servicesaws-amplifyaws-amplify-sdk-js

AWS Amplify Datastore AppSync not synchronize IndexDB with DynamoDB


I followed this tutorial. It worked just fine before I updated aws-amplify to v3.3.26. After the update it stopped to synchronize IndexDB with DynamoDB. I didn't have any errors when I set Amplify.Logger.LOG_LEVEL = 'DEBUG'.

After tutorial in main.ts I had:

import Amplify from "@aws-amplify/core";
import {DataStore} from "@aws-amplify/datastore";
import awsExports from "./aws-exports";

Amplify.configure(awsExports);

After that I pulled data from Amplify UI I got generated GraphQl Models and src/aws-exports.js

const awsmobile = {
    "aws_project_region": "REGION",
    "aws_appsync_graphqlEndpoint": "https://xxxxxxxxxxx.appsync-api.REGION.amazonaws.com/graphql",
    "aws_appsync_region": "REGION",
    "aws_appsync_authenticationType": "API_KEY",
    "aws_appsync_apiKey": "xxx-xxxxxxxxxxxxxxxxxx",
    "aws_cognito_identity_pool_id": "REGION:xxxxx-xxxxxx-xxxxx-xxxxxxx-xxxxxxx",
    "aws_cognito_region": "REGION",
    "aws_user_pools_id": "REGION_xxxxxxxx",
    "aws_user_pools_web_client_id": "xxxxxxxxxxxxxxxxx",
    "oauth": {},
    "aws_content_delivery_bucket": "BUCKET",
    "aws_content_delivery_bucket_region": "us-east-2",
    "aws_content_delivery_url": "http://BUCKET.s3-website.REGION.amazonaws.com"
};```

And I tried to update data like this:

await DataStore.save(User.copyOf(data, (item:MutableModel) => { item.phone = data.phone; }));```


Solution

  • In order to fix it I changed main.ts to this:

    src/main.ts

    import awsconfig from './aws-exports'
    import PubSub from '@aws-amplify/pubsub';
    import API from '@aws-amplify/api';
    import Amplify, { Logger } from 'aws-amplify';
    import {DataStore} from '@aws-amplify/datastore';
    
    API.configure(awsconfig);
    PubSub.configure(awsconfig);
    Amplify.configure(awsconfig);
    DataStore.configure(awsconfig);
    Logger.LOG_LEVEL = 'ERROR';
    DataStore.start();
    

    And now I need to have subscription to Model changes any time I edit data.

    src/app/user/edit-user.component.ts

    import {DataStore} from '@aws-amplify/datastore';
    import {User} from '../models';
    
    
    @Component({
      selector: 'app-edit-page',
      templateUrl: './edit-page.component.html',
      styleUrls: ['./edit-page.component.scss']
    })
    export class EditPageComponent implements OnInit {
      subscription;
    
      ngOnInit() {
        this.subscription = DataStore.observe<User>(User).subscribe(() => {});
      }
      async update(data) {
        await DataStore.save(User.copyOf(data, (item:MutableModel<User>) => {
          item.phone = data.phone;
        }));
      }
      ngOnDestroy() {
        if (this.subscription) this.subscription.unsubscribe();
      }
    }