Access Your Database

Important

The following content applies if you are already using the AWS Mobile CLI to configure
your backend. If you are building a new mobile or web app, or you're adding cloud
capabilities to your existing app, use the new AWS Amplify CLI instead. With the new Amplify CLI, you can use all of the features described in Announcing the AWS Amplify CLI toolchain, including AWS CloudFormation functionality that provides additional workflows.

Set Up Your Backend

BEFORE YOU BEGIN

The steps on this page assume you have already completed the steps on Get Started.

AWS Mobile database feature enables you to create tables customized to your needs. The CLI then guides
you to create a custom API to access your database.

Create a table

To specify and create a table

In your app root folder, run:

awsmobile database enable --prompt

Design your table when prompted by the CLI.

The CLI will prompt you for the table and other table configurations such as columns.

Welcome to NoSQL database wizard
You will be asked a series of questions to help determine how to best construct your NoSQL database table.
? Should the data of this table be open or restricted by user? Open
? Table name Notes
You can now add columns to the table.
? What would you like to name this column NoteId
? Choose the data type string
? Would you like to add another column Yes
? What would you like to name this column NoteTitle
? Choose the data type string
? Would you like to add another column Yes
? What would you like to name this column NoteContent
? Choose the data type string
? Would you like to add another column No

Choose a Primary Key that will uniquely identify each item. Optionally, choose a column to be a Sort Key
when you will commonly use those values in combination with the Primary Key for sorting
or searching your data. You can additional sort keys by adding a Secondary Index for each column you will want to sort by.

Before you create the database, you must specify how items in your table are uniquely organized. This is done by specifying a Primary key. The primary key uniquely identifies each item in the table, so that no two items can have the same key.
This could be and individual column or a combination that has "primary key" and a "sort key".
To learn more about primary key:
http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/HowItWorks.CoreComponents.html#HowItWorks.CoreComponents.PrimaryKey
? Select primary key NoteId
? Select sort key (No Sort Key)
You can optionally add global secondary indexes for this table. These are useful when running queries defined by a different column than the primary key.
To learn more about indexes:
http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/HowItWorks.CoreComponents.html#HowItWorks.CoreComponents.SecondaryIndexes
? Add index No
Table Notes added

When prompted, choose Create CRUD API for existing Dynamo table, select the table name from the previous steps, choose the access permissions for
the table. Using the example table from the previous section:

? Select from one of the choices below.
Create a new API
❯ Create CRUD API for an existing Amazon DynamoDB table

The prompt response will be:

Path to be used on API for get and remove an object should be like:
/Notes/object/:NoteId
Path to be used on API for list objects on get method should be like:
/Notes/:NoteId
JSON to be used as data on put request should be like:
{
"NoteTitle": "INSERT VALUE HERE",
"NoteContent": "INSERT VALUE HERE",
"NoteId": "INSERT VALUE HERE"
}
To test the api from the command line (after awsmobile push) use this commands
awsmobile cloud-api invoke NotesCRUD <method> <path> [init]
Api NotesCRUD saved

Copy and keep the path of your API and the JSON for use in your app code.

This feature will create an API using Amazon API Gateway and AWS Lambda. You can optionally
have the lambda function perform CRUD operations against your Amazon DynamoDB table.

Update your backend.

To create the API you have configured, run:

awsmobile push

Until deployment of API to the cloud the has completed, the CLI displays the message:
cloud-api update status: CREATE_IN_PROGRESS. Once deployed a successful creation message cloud-api update status: CREATE_COMPLETE is displayed.

You can view the API that the CLI created by running awmobile console and then choosing Cloud Logic in the Mobile Hub console.

Save an item (create or update)

To save an item

In the part of your app where you access the database, such as an event handler in
your React component, call the put method. Use the JSON and the root path (/Notes) of your API that you copied from the CLI prompt response earlier.