question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Datastore not syncing to dynamo on first attempt & DataStore not allowing public user assuming IAM role

See original GitHub issue

I have not been able to get DS working on two entirely separate production apps. I have however been able to get it functional using a test application given the following schema.

type User @auth(rules: [{ allow: groups, groups: ["Admin"] }, { allow: owner, ownerField: "owner" }]) @model {
  id: ID!
  owner: String!
  email: String!
  orders: [Order] @connection(name: "UserOrders")
}

type Order @auth(rules: [{ allow: groups, groups: ["Admin"] }, { allow: owner, ownerField: "owner" }]) @model {
  id: ID!
  owner: String!
  cost: Int!
  status: String!
  user: User @connection(name: "UserOrders")
}

type Test @model {
  id: ID!
  test: String!
}

But datastore does not work properly when using this schema:

type User
  @auth(rules: [{allow: owner}, {allow: groups, groups: ["Admin"]}])
  @key(fields: ["email"])
  @model {
  email: String!
  name: String
  owner: String!
  phone: String
  ownedAuthorities: [String]
  orders: [Order] @connection(name: "UserOrders")
}

type Order
  @searchable
  @auth(
    rules: [
      {allow: groups, groups: ["Admin"]}
      {allow: owner, ownerField: "owner", operations: [read]}
    ]
  )
  @model(subscriptions: {level: off}) {
  id: ID!
  user: User @connection(name: "UserOrders")
  date: String!
  datetime: String!
  owner: String!
  items: [OrderItem] @connection(name: "OrderItems")
  authorities: [String]
  amount: Int!
  refundAmount: Int!
  email: String
  stripeId: String
  status: String
}

type OrderItem
  @auth(
    rules: [
      {allow: groups, groups: ["Admin"]}
      {allow: owner, ownerField: "owner", operations: [read]}
    ]
  )
  # @key(name: "byStatusByDate", fields: ["status", "date"])
  @model {
  id: ID!
  mcNumber: String
  date: String!
  type: String!
  owner: String!
  status: String!
  cost: Int!
  mcRequestNumber: String
  grantDate: String
  applicantName: String
  email: String
  userName: String
  backupContact: String
  specialInstructions: String
  order: Order @connection(name: "OrderItems")
}

type Contact
  @model
  @auth(
    rules: [
      {allow: groups, groups: ["Admin"], operations: [read, create, update, delete]}
      {allow: public, provider: iam, operations: [create]}
      {allow: groups, groups: ["everyone"], operations: [create]}
    ]
  ) 
  {
  id: ID!
  name: String!
  email: String!
  content: String!
}

type Authority
  @searchable
  @auth(
    rules: [
      {allow: groups, groups: ["Admin"], operations: [read, create, update, delete]}
      {allow: public, provider: iam, operations: [read]}
      {allow: groups, groups: ["everyone"], operations: [read]}
    ]
  )
  @model {
  id: ID!
  authorityDate: String!
  dot: String
}

It syncs fine locally. If I create a mutation using the API.graphql it writes to dynamo with all of the DataStore fields (ie: __typenmae, __version, ___lastChangedAt). But when writing the following code I can sync locally but not to dynamo.

import {Order, User, Contact} from '../../models';
import {API, Auth, DataStore} from 'aws-amplify';
....
const ds = async () => {
    try {
      const owner = (await Auth.currentAuthenticatedUser()).username;
      const user = await DataStore.save(
        new User({
          email: 'test@email.com',
          name: 'testing',
          owner,
        }),
      );
      console.log(user);

      const contact = await DataStore.save(
        new Contact({
          name: 'TEST CONTACT',
          email: 'test@email.com',
          content: 'TEST CONTENT',
        }),
      );
      console.log(contact);
    } catch (e) {
      console.log(e);
    }

I have read through various threads and have tried the following: 1.) Removing all Auth on the contact schema and testing that way, still doesn’t sync to the cloud. 2.) Pull down and rebuild the api twice. 3.) Run through the update to make sure Auto Merge has been selected, and then also trying optimistic concurrency to see if there has been a change. 4.) Attempt to create the models using both “amplify codegen models” and I also tried installing amplify-app and running “npm run amplify-modelgen” 5.) Importing DataStore from the latest aws-amplify, also tried importing from here @aws-amplify/datastore

here is a list of my dependencies

    "dependencies": {
        "@aws-amplify/datastore": "^2.0.9",
        "@date-io/date-fns": "1.3.13",
        "@material-ui/core": "^4.9.5",
        "@material-ui/icons": "^4.9.1",
        "@material-ui/lab": "^4.0.0-alpha.49",
        "@material-ui/pickers": "^3.2.10",
        "@testing-library/jest-dom": "^4.2.4",
        "@testing-library/react": "^9.3.2",
        "@testing-library/user-event": "^7.1.2",
        "aws-amplify": "^3.0.9",
        "aws-amplify-react": "^3.1.7",
        "aws-appsync": "^3.0.2",
        "axios": "^0.19.2",
        "clsx": "^1.1.0",
        "date-fns": "^2.11.1",
        "dotenv": "^8.2.0",
        "lodash": "^4.17.15",
        "moment": "^2.24.0",
        "react": "^16.13.0",
        "react-dom": "^16.13.0",
        "react-drop-zone": "^3.0.7",
        "react-dropzone": "^10.2.1",
        "react-google-button": "^0.7.1",
        "react-responsive-picture": "^3.2.2",
        "react-router-dom": "^5.1.2",
        "react-scripts": "^3.4.0",
        "react-stripe-checkout": "^2.6.3",
        "stripe": "^8.39.0"
    },

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:13 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
blydewrightcommented, May 2, 2020

Hi 😃 I’m experiencing the same issue of the first object not being synced upon a DataStore.save(). Subsequent DataStore.save()'s correctly sync. Both first and subsequent objects are correctly saved to local storage (in this case, my browser’s indexedDb).

I am executing these saves within a click handler. This happens in both mock environment, and cloud using the auto-generated schema (Blog, Post, Comment) from amplify api add using authentication as API key.

It seems like the first save() method does some kind of initialization without synchronizing that object, but allows subsequent saves to work correctly. When additional saves are made in other handlers, they work correctly. So it’s the first save after initializing.

Let me know if more info is needed.

1reaction
rpostulartcommented, Apr 30, 2020
Read more comments on GitHub >

github_iconTop Results From Across the Web

Amplify DataStore best practices - AWS Documentation
Models in DataStore are immutable. Manually forcing a synchronization with the backend is not possible. The following example shows how to create a...
Read more >
AWS Solutions Architect Associate Exam Questions for FREE
Preparing for the new AWS Certified Solutions Architect Associate exam (SAA-C03)? Try these AWS Solutions Architect Associate exam questions for Free!
Read more >
An iOS app uses AWS Amplify DataStore and it does not sync ...
I have an app that uses AWS Cognito to authenticate a user. ... In fact, the app DOES NOT sync the date saved...
Read more >
Build a Serverless Data API with AppSync and DynamoDB
You need to create a number of AWS IAM Policies and Roles to allow AppSync to access both the DynamoDB table and log...
Read more >
AWS Solutions Architecture Associate Practice Questions Part 6
You have been tasked with choosing a datastore to persist GPS coordinates ... Your company does not allow the creation of IAM users...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found