How does DataStore work? - a2n-seattle/rms-app GitHub Wiki
TL;DR
Call the .query() function on DataStore to do Reads (read from the database).
- Import the DataStore API in any file you want to do Reads (Queries) from the Database
import { DataStore } from 'aws-amplify';
-
Currently, the only supported Query is to get the whole table (for any of the 5 tables in our schema). See here for how to do that: https://docs.amplify.aws/lib/datastore/getting-started/q/platform/js#reading-from-the-database
-
What will the queries look like when you get back? This is a GraphQL query under the hood so you will get a GraphQL formatted JSON back. For example, if this is the query:
{
main {
name
displayName
description
tags
items
}
}
This should be the response (not exact here, just an idea):
{
"data": {
"main": [
{
"name": "Camera",
"displayName": "Cameras",
"description": "All cameras",
"tags": ["video", "trips"],
"items": ["AJs Camera", "John's Camera"]
},
{
"name": "tripods",
"displayName": "Tripods",
"description": "All Tripods",
"tags": ["video, "trips"],
"items": ["Monopod", "Small Tripod"]
},
]
}
}
-
See here for more info on GraphQL (though you shouldn't have to know much besides what responses will look like): https://graphql.org/learn/queries/
-
Here's a link on working with JSONs if you're not familiar with it: https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/JSON
-
See here for an example React Native App that calls DataStore. Note that any "mutations" (Update, Delete) operations they do through calling DataStore is not something we will be using. https://docs.amplify.aws/lib/datastore/examples/q/platform/js
(NOT CURRENTLY SUPPORTED). Maybe we will support querying for a row in a table by the ID of that row in the future. Here is how you do it: https://docs.amplify.aws/lib/datastore/data-access/q/platform/js#querying-for-a-single-item