Skip to content

Latest commit

 

History

History
197 lines (139 loc) · 9.97 KB

querying-dynamodb.md

File metadata and controls

197 lines (139 loc) · 9.97 KB

DynamoDB

DynamoDB is a serverless, fully managed, key-value NoSQL database designed to handle high-performance applications of any size.

{% hint style="warning" %} The following document assumes that you understand the basics of connecting to databases on Appsmith. If not, please go over them before reading further. {% endhint %}

On Appsmith, it's pretty straightforward to establish a connection with any datasource, including DynamoDB. With this integration, you can perform different operations using a custom UI built on Appsmith, with minimal configurations.

Create DynamoDB Datasource

To add an DynamoDB datasource, navigate to Explorer >> Click plus sign (+) next to Datasources >> Select DynamoDB under Databases.

{% embed url="https://youtu.be/LNNhWh30wK4" %} How to create DynamoDB Datasource? {% endembed %}

Connection Settings

Appsmith needs the following parameters for connecting to a DynamoDB instance:

How to create DynamoDB Datasource?

{% hint style="success" %} All required fields are suffixed with an asterisk (*). {% endhint %}

  • Region*: Select the region where your DynamoDB instance exists
  • AWS Access Key Id*: AWS access key that enables a program, script, or developer to access the resources on my AWS account programmatically.
  • AWS Secret Access Key*: Secret Access keys are like your password. This value is accessible from your AWS security credentials page.

You can get all the above details from your AWS account:

Key Concepts

When working with DynamoDB, you should be familiar with the following fundamental concepts:

****Tables: Tables are a collection of items.

****Items: In a table, an item is a single data record. The table's defined primary key serves as a unique identifier for each item in the table.

Attributes: Attributes are pieces of data attached to a single item.

****Partition key: A simple primary key, composed of one attribute known as the partition key. Attributes in DynamoDB are similar in many ways to fields or columns in other database systems.

****Composite primary key: This is a combination of partition key, and sort key, this type of key is composed of two attributes. The first attribute is the partition key, and the second attribute is the sort key. DynamoDB uses the partition key value as input to an internal hash function.

{% hint style="info" %} No matter what type of primary key you choose, the primary key must be unique for each item in the table. You can read more about it. {% endhint %}

Create Queries

You can add queries to DynamoDB datasource by selecting the New API + button available on the datasource page or by navigating to Explorer >> Click plus sign (+) next to Queries/JS >> Select the datasource name (DynamoDB). You can query DynamoDB databases using any of the officially supported operations.

Query

You can create queries to fetch, update and delete data from a datasource using the Appsmith query editor. Here we have listed some of the most basic operations to get started with using a DynamoDB API:

Query Name Description
ListTables ListTables command gets a list of all the tables that are currently accessible at the endpoint.
DescribeTable DescribeTable command returns metadata about the table that is queried using the JSON body.
GetItem GetItem command retrieves a single item on the basis of its primary key.
PutItem PutItem command is used to insert or replace an entire item object.
UpdateItem**** UpdateItem can be used for conditionally updating parts of an item.

You can check the Query Settings Guide to learn more about queries.

ListTables

The ListTables command can be used to retrieve all the tables available at the current endpoint. This request can be carried out even without a body.

  • Click on the + icon next to the queries/js and choose your DynamoDB datasource.
  • From the Commands drop-down, Select the method List Tables.
  • Next, add your code in the body section.

A sample request looks like this:

{
   "ExclusiveStartTableName": "string",
   "Limit": number
}

{% hint style="info" %} The output from ListTables is paginated, with each page returning a maximum of 100 table names. {% endhint %}

DescribeTable

The DescribeTable command returns metadata about the table that is queried using the JSON body. It returns information about the table, including the current status of the table, when it was created, the primary key schema, and any indexes on the table.

  • Click on the + icon next to the queries/js and choose your DynamoDB datasource.
  • From the Commands drop-down, Select the method Describe Tables.
  • Next, add your code in the body section.

A sample request will look like this:

{
    "TableName" : "four"
}

GetItem

The GetItem operation returns a set of attributes for the item with the given primary key. If there is no matching item, GetItem does not return any data, and there will be no Item element in the response.

  • Click on the + icon next to the queries/js and choose your DynamoDB datasource.
  • From the Commands drop-down, Select the method GetItem.
  • Next, add your code in the body section.

The request would use this specified type in the JSON body. In the following example, the primary key is called pkey and has a value of a.

{
    "TableName" : "four",
    "Key": {
        "pkey" : "a"
    }
}

PutItem

The PutItem command is used to insert or replace an entire item object. This request can be configured to return with the inserted/updated item using the ReturnValues parameter.

{% hint style="info" %} If an item with the same primary key as the new item already exists in the specified table, the new item completely replaces the existing item. {% endhint %}

  • Click on the + icon next to the queries/js and choose your DynamoDB datasource.
  • From the Commands drop-down, Select the method PutItem.
  • Next, add your code in the body section.

Here, we use the PutItem command for a simple insert.

{
    "TableName" : "four",
    "Item" : {
        "pkey" : {
            "S" : "a"
        },
        "name": {
            "S" : "Irene"
        },
        "friends": {
            "SS" : ["Sherlock"]
        }
    }
}

UpdateItem

The UpdateItem can be used for conditionally updating parts of an item. Edits an existing item's attributes or adds a new item to the table if it does not already exist. You can put, delete, or add attribute values. You can also return the item's attribute values in the same UpdateItem operation using the ReturnValues parameter.

  • Click on the + icon next to the queries/js and choose your DynamoDB datasource.
  • From the Commands drop-down, Select the method UpdateItem.
  • Next, add your code in the body section.

A sample request might have the following body:

{
    "TableName" : "four",
    "Key" : {
        "pkey" : {
            "S" : "a"
        }
    },
    "UpdateExpression" : "set friends = :new_friends",
    "ExpressionAttributeValues" : {
        ":new_friends" : {
            "SS" : ["Mycroft", "Watson", "Irene"]
        }
    },
    "ReturnValues" : "ALL_NEW"
}

With DynamoDB integration, it is possible to create apps that seamlessly connect with the DynamoDB database and provide additional flexibility for updating and analyzing data.

Using Queries in applications

Once you have successfully run a Query, you can use it in your application to