Search code examples
reactjsamazon-web-servicesaws-sdkaws-amplify

AWS Amplify - updating a column in a DynamoDB


I am trying to figure out how to update a colums in a Dynamo table, using the 'aws-amplify' API.

Without Amplify, (just using the AWS SDK), that could be done like this:

const docClient = new AWS.DynamoDB.DocumentClient();
let params = {
        TableName:table,
        Key:{
            "year": year,
            "title": title
        },
        UpdateExpression: "set info.rating = :r, info.plot=:p, info.actors=:a",
        ExpressionAttributeValues:{
            ":r":5.5,
            ":p":"Everything happens all at once.",
            ":a":["Larry", "Moe", "Curly"]
        },
        ReturnValues:"UPDATED_NEW"
    };

    docClient.update(params, function(err, data) { ....

I set up a backend/api using the docs here to ( enable cloud API to do CRUD operations) https://docs.aws.amazon.com/aws-mobile/latest/developerguide/web-access-databases.html

Everything works fine as far as 'put/get' methods that create new records,etc

import Amplify, { API } from 'aws-amplify';
....
const path = '/MyTable';
const newRecord = {.......}
const apiResponse = await API.put('MyTableCRUD', path, newRecord);

But there is so little documentation on more advanced techniques like the update above, that I don't know how if/how this can be achieved using Amplify.

Hoping someone has already done this! Thank you


Solution

  • You can create a custom route in the cloud API of your database (which is usually located under awsmobilejs/backend/cloud-api/INSERT_TABLE_NAME). Unless you modified the cloud API of your database in the past, the main file where all of the routes of the API are specified should be awsmobilejs/backend/cloud-api/INSERT_TABLE_NAME/app.js, or in your case awsmobilejs/backend/cloud-api/MyTable/app.js.

    For more information about creating custom routes in your cloud API and some examples, check out the example app's cloud API and the express API reference.