April 5, 2022
Simple CRUD w/ CF & DynamoDB
Comments
(0)
April 5, 2022
Simple CRUD w/ CF & DynamoDB
ColdFusion developer for 20+ years, professional experience in 10 other languages & frameworks. Artist, nerd, Jeep enthusiast.
Newbie 35 posts
Followers: 26 people
(0)

I’ve been playing with building out a Connect pod (blog entry & webinar coming soon ™) but in the meantime thought I’d share a little code for writing to and reading from a DynamoDB.

(Just as a reminder, you can find info about getting a DynamoDB object up and running here: https://helpx.adobe.com/coldfusion/using/integrate-coldfusion-dynamodb.html)

So first a few assumptions. I will assume you have created a DynamoDB object connected to DynamoDB in AWS with a table named “DemoTable”. I will further assume that your AWS connector is named “aws” and your dynamo object is named DynamoConnector.

So let’s begin by instantiating the object, and naming it “dynamoObject”.

dynamoObject = getCloudService("aws", "ConnectScheduler");

We will be using “dynamoObject” to both read from and write to the DB.

Now that we have an object to target, let’s build the query that returns the data from the db.

tableName = "DemoTable";
scanStruct = {
    "TableName": "#tableName#"
}
scanresponse = dynamoObject.scan(scanStruct);

OK so what did we do? First we set a variable to the name of the table, “DemoTable”, then we create a struct that sets the value “TableName” to that value, which is then passed into Dynamo using the scan() function. So now the variable “scanresponse” will hold the data returned from the db.

Now, something to consider here is that the return from DynamoDB is very “noisy”. The full return includes things like http codes and other things which we may or may not care about, as well as an array which includes structures which themselves include the data we want.

If we assume that the data which returns from DynamoDB includes first_name, last_name, and favorite_cake, you would return it in the following fashion:

for (item in scanresponse.items){ // Output values from DB
        writeOutput(
          "<b>First Name: </b>"
          &item.first_name.S
          &" | "
          &"<b>Last Name: </b>"
          &item.last_name.S
          &" | "
          & "<b>Favorite Cake: </b>"
          & item.favorite_cake.S
          & "<br>") ;
      }
Note there that not only am I targeting the .items object returned from Dyanamo (which contains the data structures I want) I then have to pull in the .S position inside each key’s struct. The other positions are BS, L, M, NS & SS.
Now of course, if you just hit that page right off, the return would be… nothing, since we don’t have any data in the table.
So let’s write a quick form entry field.
First, build the form you want. I am assuming the page is called “dynamo-example.cfm”.
<form name="aform" method="post" action="dynamo-example.cfm">
First Name: <input type="text" name="first_name" id="first_name" value="" /><br />
Last Name: <input type="text" name="last_name" id="last_name"  value="" /><br />
Favorite Cake: <input type="text" name="favorite_cake" id="favorite_cake" value="" /><br />
<input type="submit" name="submit" value="submit" />
</form>
Then, at the top of the page, build a simple form processing workflow
if(isDefined("FORM.submit")){
         putItemStruct = {
      "TableName":"#TableName#",
      "Item":{ "SampleKey": "#createUUID()#",
                "first_name": FORM.first_name,
                "last_name": FORM.last_name,
                "favorite_cake": FORM.favorite_cake
              },
      "ReturnValues":"NONE"
  }
try {
    putItemResponse=dynamoObject.putItem(putItemStruct,{"hasType": false})
    writeOutput("Item inserted successfully in the table.")                      writeDump(putItemResponse)
}
    catch (any e){
    writeDump(e)
      }
    }
}
So there’s a lot going on here, let’s break it down.
First we check to see if FORM.submit exists. That tells us the form was submitted. On first load, the FORM object would not exist, so this section would be skipped.
Then we are going to build the payload to insert. That struct includes the tablename variable we created before, and then creates an “item” struct that includes the values we will push into the DynamoDB.
Once that is built, we use the putItem() function to push the contents into the DB.
A very simple example, and of course you will want to do things like test the values before submitting, but this should get you started with the C & the R of CRUD using ColdFusion and DynamoDB.
0 Comments
Add Comment