Datastore not syncing to dynamo on first attempt & DataStore not allowing public user assuming IAM role
See original GitHub issueI 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:
- Created 3 years ago
- Comments:13 (2 by maintainers)
Top GitHub Comments
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.
This might help: https://github.com/aws-amplify/amplify-js/issues/5174