How do I updateItem an element nested in Dynamodb?

Asked

Viewed 31 times

0

I have a Map format structure stored in Dynamodb, and would like to add another attribute within the school object, something like:

{
  name: 'Felipe'
  uid: 112233,
  data: {
    structure: {
      school: {
        name: 'beta'
      }
    }      
  }
}

previously the add_year was not part of the structure, so this part is new

school: {
  name: 'beta'
  add_year: '2020'
}

How can I get it?

I tried the following solutions, but without success

(async ()=>{
  try {
    let teste =  await dynamoDb.updateItem({
      TableName: 'test',
      Key: { 
        uid: "112233"
      },
      UpdateExpression: "SET data.#structure.#school = list_append(#structure, :attrValue)",
      ExpressionAttributeNames: {
        "#data": "data",
        "#structure": "structure",
        "#school": "school",
      },
      ExpressionAttributeValues: {
        ":school":{  
          "add_year": 2020
        }
      },
      ReturnValues:"UPDATED_NEW "
    })
    console.log('update',teste)
  } catch (error) {
    console.log(error)
  }

1 answer

0


const { DocumentClient } = require('aws-sdk/clients/dynamodb');

const documentClient = new DocumentClient({
  region: 'us-east-1',
});

try {
    let add_year= '2020'
    let teste = documentClient.update({
      TableName: 'test',
      Key: { 
        uid: "112233"
      },     
      UpdateExpression: `SET #data.structure.school=  :add_year`,
      ExpressionAttributeValues: {
        ':add_year':  add_year
      },
      ExpressionAttributeNames: {
        "#data": "data"   
      },
      ReturnValues:"ALL_NEW"
    }).promise()

    teste.then(t => console.log(t));   

  } catch (error) {
    console.log(error)
  }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.