Jim Lynch Codes
  • Blog
  • About Jim

Writings about one coder's stories & experiences.

DynamoDB Put Operation Doesn't Return the Data Inserted

5/19/2018

0 Comments

 
This is something that tripped me up a little bit when I was recently playing around with DynamoDB. I was very confused because in the callback function the data argument was just coming back as an empty object. "Surely that can't be right", I thought, but indeed it was right!

Some Code

Here I have a snippet of some Node.js code that puts some data into DynamoDB. This code does work in that it actually puts stuff into the DynamoDB table, but at the end it logs,  "got some data: {}".
const AWS = require('aws-sdk')
const docClient = new AWS.DynamoDB.DocumentClient({ region: 'eu-central-1' })

const createDocument = (text, callback) => {
  const createParams = {
    Item: {
      text: text
    },
    TableName: 'ToDoItems'
  }

  docClient.put(createParams, (err, data) => {
    if(err) {
          console.log('uh oh! ', err);
    } else {
          console.log('put some data: ', data);
    }
  })
}

Some Answers

After some Googling I stumbled upon this stack overflow question. The OP was asking the exact question I had. In the first answer I can see that I needed to add the ReturnValues property to my query object. However, in the answer we see that 'ALL_NEW' is not even an option for ReturnValues when doing a put operation! The third answer here is also pretty interesting. In this solution we use an overall update operation that contains many PUT actions- pretty clever.
const createParams = { 
  TableName: "table",
      Key: {
          id: randomId
      },
      AttributeUpdates: {
          authorId: {Action: "PUT", Value: event.authorId},
          date: {Action: "PUT", Value: event.date},
          description: {Action: "PUT", Value: event.description},
          title: {Action: "PUT", Value: event.title}
      },
      ReturnValues: "ALL_NEW"
}

Know Whatcha Got

The key to choosing the correct command for the task at hand is to know what's available to you. That means understanding how the basic CRUD (create, read, update, delete) operations work in DynamoDB but also being aware of Dynamo-specific things like working with change streams or commands for managing an entire table a time (here's a really useful "HowItWorks" dynamoDB Api docs page). As a coder you've got to love learning because there will always be a useful library you haven't touched yet that could make your and your projects that much better. Don't be afraid. Just dive straight in!
0 Comments

Your comment will be posted after it is approved.


Leave a Reply.

    ​Author

    Picture
    The posts on this site are written and maintained by Jim Lynch. About Jim...
    Picture
    Follow @JimLynchCodes
    Follow @JimLynchCodes

    Categories

    All
    Actionscript 3
    Angular
    AngularJS
    Automated Testing
    AWS Lambda
    Behavior Driven Development
    Blockchain
    Blogging
    Business Building
    C#
    C / C++
    ClojureScript / Clojure
    Coding
    Community Service
    CS Philosophy
    Css / Scss
    Dev Ops
    Firebase
    Fitness
    Flash
    Front End
    Functional Programming
    Git
    Go Lang
    Haskell
    Illustrations
    Investing
    Java
    Javascript
    Lean
    Life
    Linux
    Logic Pro
    Music
    Node.js
    Planning
    Productivity
    Professionalism
    Python
    React
    Redux / Ngrx
    Refactoring
    Reusable Components
    Rust
    Security
    Serverless
    Shell Scripting
    Swift
    Test Driven Development
    Things
    TypeScript
    Useful Sites
    Useful Tools
    Video
    Website Development
    WebStorm
    Writing

    Archives

    March 2023
    August 2021
    February 2021
    January 2021
    October 2020
    September 2020
    May 2020
    April 2020
    February 2020
    January 2020
    December 2019
    October 2019
    September 2019
    August 2019
    July 2019
    June 2019
    May 2019
    April 2019
    March 2019
    February 2019
    January 2019
    December 2018
    November 2018
    October 2018
    September 2018
    August 2018
    June 2018
    May 2018
    April 2018
    March 2018
    February 2018
    January 2018
    December 2017
    November 2017
    October 2017
    September 2017
    August 2017
    July 2017
    May 2017
    April 2017
    March 2017
    February 2017
    January 2017
    December 2016
    November 2016
    October 2016
    September 2016
    August 2016
    July 2016
    June 2016
    May 2016
    April 2016
    March 2016
    February 2016
    January 2016
    December 2015
    November 2015
    October 2015

    RSS Feed

  • Blog
  • About Jim
JimLynchCodes © 2023