Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Append item to list using AWS AppSync to DynamoDB

This might be a stupid question, but I really cannot find a way to do that.

So, I have DynamoDB tables and I have schema in AppSync api. In a table, for each row, there is a field which has a list as its value. So how can I append multiple items into this list without replacing the existing items? How should I write the resolver of that mutation?

Here is the screenshot of my table:

table pic

And you can see there are multiple programs in the list.

How can I just append two more programs.

Here is a new screenshot of my resolver: screenshot of resolver I want to add a existence check method in UpdateItem operation. But the current code does not work. The logic I want is that use the "contains" method to see whether the "toBeAddedProgramId" already exists. But the question is, how to extract the current program id list from User table and how to make the program id list a "list" type (since the contains method only take String set and String).

I hope this question makes sense. Thanks so much guys.

Best, Harrison

like image 632
Harrison Song Avatar asked Jun 22 '18 16:06

Harrison Song


People also ask

What operation inserts an item into DynamoDB?

With the DynamoDB API, you use the PutItem operation to add an item to a table.

How do you update multiple items in a DynamoDB table?

(If you want to modify multiple items, you must use multiple operations.) With the DynamoDB API, you use the UpdateItem action to modify a single item. You must specify the Key attributes of the item to be modified and an UpdateExpression to specify attribute values.


1 Answers

To append items to a list, you should use the DynamoDB UpdateItem operation. Here is an example if you're using DynamoDB directly

In AWS AppSync, you can use the DynamoDB data source and specify the DynamoDB UpdateItem operation in your request mapping template.

Your UpdateItem request template could look like the following (modify it to serve your needs):

{
    "version" : "2017-02-28",
    "operation" : "UpdateItem",
    "key" : {
        "id" : { "S" : "${context.arguments.id}" }
    },
    "update" : {
        "expression" : "SET #progs = list_append(#progs, :vals)",
        "expressionNames": {
            "#progs" : "programs"
        },
        "expressionValues": {
            ":vals" : {
                "L": [
                    { "M" : { "id": { "S": "49f2c...." }}},
                    { "M" : { "id": { "S": "931db...." }}}
                ]
            }
        }
    }
}

We have a tutorial here that goes into more details if you are interested in learning more

like image 88
Tinou Avatar answered Oct 20 '22 13:10

Tinou