{
"$type": "site.standard.document",
"canonicalUrl": "https://numergent.com/2016-01/Clojure-and-DynamoDB-with-Faraday-part-4.html",
"path": "/2016-01/Clojure-and-DynamoDB-with-Faraday-part-4.html",
"publishedAt": "2016-01-13T08:24:14.000Z",
"site": "at://did:plc:cf6futaebyc2k4wgzsr4v42k/site.standard.publication/3mp2ewx43js2g",
"tags": [
"aws",
"clojure",
"document databases",
"dynamodb",
"faraday",
"nosql"
],
"textContent": "Update and friends\n\nThis is the final part of the series where I convert Amazon's Getting Started to Clojure with Faraday. If you've just arrived, the previous parts were:\n\n- Basic table operations and writing data\n- Getting querying and scanning\n- Working with secondary indexes\n\nThis part will correspond to steps 7 and 8 of the Getting Started guide. Let's move on to updating and deleting data!\n\n\n\nUpdate an item\n\nUp date allows us to modify an item's attributes, add new ones, or remove existing attributes. We'll use update expressions, which have the same restrictions as the expressions we saw on part 2.\n\nWe also can specify which values we want returned after the update:\n\n- :all-old returns all attribute values as they appeared before the update\n- :updated-old returns only the updated attributes as they appeared before the update\n- :all-new returns all attribute values as they appear after the update\n- :updated-new returns only the updated attributes, as they appear after the update\n\nFirst we'll add a few new attributes to our existing :music table:\n\nSince we requested :all-new, we'll get the entire item back.\n\nBut we're not limited to one update per expression - we can (for example) both set a price and remove an element using path notation.\n\n So long, Mr. Davis.\n\nSpecify a conditional write\n\nMuch like we added a conditional expression to put-item back on part 1, we can also send a condition expression to our update-item call.\n\nWhich will get us a ConditionalCheckFailedException since the attribute \"label\" - which we specified on :cond-expr shouldn't exist - is already there.\n\nSpecify an atomic counter\n\nWe'll now kill two birds with one stone: we'll demo how to do an atomic counter, and retrieve only the updated attributes for the item.\n\nLet's first set a new plays attribute to zero.\n\nWe can now use a function on update-item's update expression to increase it.\n\nIf we execute that same call multiple times, the item is updated as expected.\n\nDelete an item\n\nWe can also permanently delete an item using its full hash key.\n\nAnd poof! It's gone, forever. \n\nSpecify a conditional delete\n\nYou won't be surprised to find that deletes also support conditional expressions. We can add a check that, if it goes unsatisfied, will let us know by raising an exception.\n\nWhich, as before, gets us a ConditionalCheckFailedException because the item does not have the expected price.\n\nDeleting a table\n\nAnd finally, we can delete a table on its entirety.\n\nThis will return the table's last status.\n\nAnd we're done!\n\nThat's it! As you can see, Faraday provides a significantly more succinct interface to DynamoDB than even Amazon's unceremonious Javascript API. It's a growing project, but a great companion to Amazon's document store.",
"title": "Clojure and DynamoDB with Faraday, Part 4"
}