DEV Community

Cover image for Adding and removing data in Astra tables
BetterBotz
BetterBotz

Posted on • Updated on

Adding and removing data in Astra tables

This post is a part of a tutorial series for learning how to build an app with DataStax's Astra database. See all of the posts in this Better Botz series to start using Astra now.

In the last post, you used the DataStax Astra API to generate data for our Better Botz database. When DataStax Astra was made generally available, GraphQL was added as a feature for creating, reading, updating, and deleting data.

GraphQL uses queries and mutations to retrieve and change data.

Goal 1: See the status of current products

Better Botz has created a new and improved Heavy Lift Arms. While planning when to add Heavy Lift Arms II, Better Botz needs to know if the existing Heavy Lift Arms are already listed.

  1. If necessary, get a fresh authorization token using the DataStax Astra API. Replace {auth-token} with the value for your database:

    ```
    curl --request POST \
      --url https://{databaseid}-{region}.apps.astra.datastax.com/api/rest/v1/auth \
      --header 'accept: */*' \
      --header 'content-type: application/json' \
      --header 'x-cassandra-request-id: {auth-token}'
    ```
    
  2. Using GraphQL, run a query to see if you already have the Heavy Lift Arms II.

    ```
    curl --location --request POST 'https://{databaseid}-{region}.apps.astra.datastax.com/api/graphql' \
      --header 'accept: */*' \
      --header 'content-type: application/json' \
      --header 'x-cassandra-request-id: {unique-UUID}' \
      --header 'X-Cassandra-Token: {auth-token}' \
      --header 'Content-Type: application/json' \
      --data-raw '{"query":"query {products(value: {id:\"Heavy Lift Arms\"}) {values {id name price description created}}}"}'
    ```
    
  3. Replace the following values with the values for your database:

    • Replace {databaseid} with the UUID of your database, copied from the Astra URL.
    • Replace {region} with the cloud region where your database is located, as listed on the Database Details page in Astra. For example, us-east1.
    • Enter a {unique-UUID} for the request and the {auth-token} you created earlier.
  4. Check for an existing “Heavy Lift Arms II”. This should verify that you do not have an existing “Heavy Lift Arms II” product.

Goal 2: Add a new product

Now that you know there aren’t any Heavy Lift Arms in the products table, you want to add the new and improved product to our table for customers to be able to order.

  1. Use the following cURL command:

    ```
    curl --request POST \
      --url https://{databaseid}-{region}.apps.astra.datastax.com/api/rest/v1/keyspaces/{my_keyspace}/tables/{table_name}/rows \
      --header 'accept: application/json' \
      --header 'content-type: application/json' \
      --header 'x-cassandra-request-id: {unique-UUID}' \
      --header 'x-cassandra-token: {auth-token}' \
      --data-raw '{"query":"mutation {superarms: insertProducts(value:{id:\"65cad0df-4fc8-42df-90e5-4effcd221ef7\"\n name:\"Arm Spec A2\" description:\"Heavy Lift Arms II\"price: \"9999.99\" created: \"2012-04-23T18:25:43.511Z\"}){value {name description price created}}}","variables":{}}'
    ```
    
  2. Replace the following values with the values for your database:

    • Replace {databaseid} with the UUID of your database, copied from the Astra URL.
    • Replace {region} with the cloud region where your database is located, as listed on the Database Details page in Astra. For example, us-east1.
    • Modify {my_keyspace} to match the name of your keyspace.
    • Replace {table_name} with the name of the table you want to create. In this example, enter products as the table name.
    • Enter a {unique-UUID} for the request and the {auth-token} you created earlier.
    • The --data-raw option defines the new product, including a description, price, and created timestamp.
  3. Using a GraphQL query, confirm the new product is available in the database:

    ```
    curl --location --request POST 'https://{databaseid}-{region}.apps.astra.datastax.com/api/graphql' \
      --header 'accept: */*' \
      --header 'content-type: application/json' \
      --header 'x-cassandra-request-id: {unique-UUID}' \
      --header 'X-Cassandra-Token: {auth-token}' \
      --header 'Content-Type: application/json' \
      --data-raw '{"query":"query {products(value: {id:\"Heavy Lift Arms II\"}) {values {id name price description created}}}"}'
    ```
    
  4. Replace the following values with the values for your database:

    • Replace {databaseid} with the UUID of your database, copied from the Astra URL.
    • Replace {region} with the cloud region where your database is located, as listed on the Database Details page in Astra. For example, us-east1.
    • Enter a {unique-UUID} for the request and the {auth-token} you created earlier.

In the next post, Better Botz you will learn to create a new table for orders that have shipped and explore further data modeling!

Top comments (0)