Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feedback]Datastore documentation is lacking. #2102

Closed
SeanRenet opened this issue Jul 12, 2020 · 7 comments
Closed

[Feedback]Datastore documentation is lacking. #2102

SeanRenet opened this issue Jul 12, 2020 · 7 comments
Assignees
Labels
amplify/js Issues tied to JS

Comments

@SeanRenet
Copy link

Page: /lib/datastore/getting-started/q/platform/js

Feedback:

It appears that in order to query the datastore you need an association table to retrieve the record you need.

For instance here is a GraphQL user profile where the profile id is the cognito user sub (3722f35e-de54-45b2-b26e-676e2cc48160) which makes the user profile easy to access by Auth.currentUserPoolUser()

schema looks like this:

type User @model  {
  id: ID!
  username: String
  userPhone: String
  createdAt: String
  updatedAt: String
}

user profile loaded into DataStore is

  Object {
  "createdAt": 1593774031,
  "id": "3722f35e-de54-45b2-b26e-676e2cc48160",
  "userPhone": "",
  "username": "One Two",
}
let input = {
                id: id,
                username: username,
                userPhone: userPhone,
                createdAt: createdAt,
            }
 let myDataStore = await DataStore.save(new User({input}));

once it is saved to the Datastore it looks like this.

dataStore is   User {
  "_deleted": undefined,
  "_lastChangedAt": undefined,
  "_version": undefined,
  "id": "f86d3252-7b1d-44f6-b4cd-b8a7a3a88def",
  "input": Object {
    "createdAt": 1593774031,
`    "id": "3722f35e-de54-45b2-b26e-676e2cc48160",`
    "userPhone": "",
    "username": "One Two",
  },
}

In order to query the Datastore and get this record I need to query the datastore with the datastore id:

DataStore.query(User, c => c.id("eq", "f86d3252-7b1d-44f6-b4cd-b8a7a3a88def"));

However the only way to know what the datastore id is to create another table that associates the datastore id with the user profile id which in this case is the user's cognito sub.

This does NOT work

DataStore.query(User, c => c.input.id("eq", "3722f35e-de54-45b2-b26e-676e2cc48160"));

This does NOT work

DataStore.query(User, c => c.id.id("eq", "3722f35e-de54-45b2-b26e-676e2cc48160"));

Is the recommended architecture to create an association table that connects a user's cognito sub with every associated datastore id or is there another way to access the properties of the datastore record?

I've read through the documentation and all of it seems to just explicitly pass the datastore record id which isn't all that helpful in practical use.
.
.

@mauerbac mauerbac added the amplify/js Issues tied to JS label Jul 13, 2020
@j1mr10rd4n
Copy link
Contributor

j1mr10rd4n commented Jul 14, 2020

I'm just getting to grips with DataStore myself, but shouldn't the query by id be:

DataStore.query(User, "3722f35e-de54-45b2-b26e-676e2cc48160");

I found this documented in the DataStore.query API Reference but it should definitely also be mentioned in the Manipulating Data page in the Library documentation. #1903 already has a lot of work on revamping the DataStore docs - perhaps this could be added in.

@SeanRenet
Copy link
Author

@j1mr10rd4n thanks I will give that a shot.

@mauerbac
Copy link
Member

Thanks for taking the time to provide this feedback @SeanRenet ! I've asked @manueliglesias to process this feedback.

@mauerbac
Copy link
Member

+1 request to improve DS Docs: #2077

@mauerbac
Copy link
Member

#2145

@mauerbac
Copy link
Member

#2294

@iartemiev
Copy link
Member

@SeanRenet If your User object needs to be flat like you have in the schema you've defined, I wouldn't pass new User( {input}) to save, i.e., with curly braces around input, since that will create a nested object inside your model with a key of input, in other words, you're saving

{
  input: {
    "createdAt": 1593774031,
    "id": "3722f35e-de54-45b2-b26e-676e2cc48160",
    "userPhone": "",
    "username": "One Two",
  }
}

That doesn't match the shape of the schema you've defined, since there's no input field there.
I think the following would be the easiest way for you to achieve the result you're looking for:

type User @model  {
  id: ID!
  userSub: String
  username: String
  userPhone: String
  createdAt: String
  updatedAt: String
}

And this app code:

const input = {
  userSub: id,
  username: username,
  userPhone: userPhone,
  createdAt: createdAt,
}

await DataStore.save(new User(input)); // no { } around input here, to prevent it from getting nested

You could then query by the user sub with the following:

const user = await DataStore.query(User, c => c.userSub("eq", "3722f35e-de54-45b2-b26e-676e2cc48160"))

@mauerbac mauerbac closed this as completed Oct 1, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
amplify/js Issues tied to JS
Projects
None yet
Development

No branches or pull requests

5 participants