Reputation: 332
I am writing a react app using AWS amplify datastore library, I want to read a whole column and put it in the drop-down select menu. I have completed designing the UI but I don't how to get only one column rather than having the whole table
Currently my query statement looks something like this
await Datastore.query(myTable);
This returns the whole table. I want to know if I can get myTable.id where 'id' is column name
Upvotes: 0
Views: 562
Reputation: 46
Because Amplify Datastore is already maintaining a local replica of your data, the recommendation from Samuel above is reasonable. Writing something like const ids = await Datastore.query(myTable).map(record => record.id)
to get the id
fields for all records in your table should work, and unless your data is really massive (in which case I'd imagine you're not trying to do that to fill a dropdown in UI) this should be reasonably fast.
The other alternative would be to query using the API
category https://docs.amplify.aws/lib/graphqlapi/query-data/q/platform/js/ with a custom graphql doc that included only id
in the selection set, but that's not really recommended, since you're going to need to hit your backend API unnecessarily in that case, rather than relying on the local data which Datastore maintains.
Upvotes: 3