|
| 1 | +// This is an example of an UpdateCommand using the higher level DocumentClient |
| 2 | +// for Amazon DynamoDB. It manipulates an list attribute on an item. |
| 3 | + |
| 4 | +const { DynamoDBClient } = require("@aws-sdk/client-dynamodb"); |
| 5 | +const { DynamoDBDocumentClient, UpdateCommand, PutCommand, GetCommand } = require("@aws-sdk/lib-dynamodb"); |
| 6 | + |
| 7 | +const client = new DynamoDBClient({ region: "us-west-2" }); |
| 8 | +const docClient = DynamoDBDocumentClient.from(client); |
| 9 | + |
| 10 | +async function createItem() { |
| 11 | + // We create the item first so we have a point of reference of the changes. |
| 12 | + const params = { |
| 13 | + TableName: "RetailDatabase", |
| 14 | + Item: { |
| 15 | + pk: "jim.bob@somewhere.com", |
| 16 | + sk: "metadata", |
| 17 | + name: "Jim Bob", |
| 18 | + favoriteColors: ['red', 'green', 'blue'], |
| 19 | + }, |
| 20 | + }; |
| 21 | + await docClient.send(new PutCommand(params)); |
| 22 | +} |
| 23 | + |
| 24 | +async function getFavoriteColors() { |
| 25 | + const params = { |
| 26 | + TableName: "RetailDatabase", |
| 27 | + Key: { |
| 28 | + pk: "jim.bob@somewhere.com", |
| 29 | + sk: "metadata", |
| 30 | + }, |
| 31 | + AttributesToGet: ['favoriteColors'], // No reason to get anything else |
| 32 | + }; |
| 33 | + const response = await docClient.send(new GetCommand(params)); |
| 34 | + return response.Item.favoriteColors.join(', '); |
| 35 | +} |
| 36 | + |
| 37 | +async function addYellow() { |
| 38 | + const params = { |
| 39 | + TableName: "RetailDatabase", |
| 40 | + Key: { |
| 41 | + pk: "jim.bob@somewhere.com", |
| 42 | + sk: "metadata", |
| 43 | + }, |
| 44 | + UpdateExpression: "SET #fc = list_append(#fc, :colors)", |
| 45 | + ExpressionAttributeNames: { |
| 46 | + "#fc": "favoriteColors", |
| 47 | + }, |
| 48 | + ExpressionAttributeValues: { |
| 49 | + ":colors": ["yellow"], |
| 50 | + }, |
| 51 | + ReturnValues: "ALL_NEW", |
| 52 | + }; |
| 53 | + |
| 54 | + const response = await docClient.send(new UpdateCommand(params)); |
| 55 | + return response.Attributes.favoriteColors.join(', '); |
| 56 | +} |
| 57 | + |
| 58 | +async function removeGreen() { |
| 59 | + const params = { |
| 60 | + TableName: "RetailDatabase", |
| 61 | + Key: { |
| 62 | + pk: "jim.bob@somewhere.com", |
| 63 | + sk: "metadata", |
| 64 | + }, |
| 65 | + UpdateExpression: "REMOVE #fc[1]", |
| 66 | + ExpressionAttributeNames: { |
| 67 | + "#fc": "favoriteColors", |
| 68 | + }, |
| 69 | + ReturnValues: "ALL_NEW", |
| 70 | + }; |
| 71 | + |
| 72 | + const response = await docClient.send(new UpdateCommand(params)); |
| 73 | + return response.Attributes.favoriteColors.join(', '); |
| 74 | +} |
| 75 | + |
| 76 | +async function swapRedAndBlue() { |
| 77 | + const params = { |
| 78 | + TableName: "RetailDatabase", |
| 79 | + Key: { |
| 80 | + pk: "jim.bob@somewhere.com", |
| 81 | + sk: "metadata", |
| 82 | + }, |
| 83 | + UpdateExpression: "SET #fc[0] = #fc[1], #fc[1] = #fc[0]", |
| 84 | + ExpressionAttributeNames: { |
| 85 | + "#fc": "favoriteColors", |
| 86 | + }, |
| 87 | + ReturnValues: "ALL_NEW", |
| 88 | + }; |
| 89 | + |
| 90 | + const response = await docClient.send(new UpdateCommand(params)); |
| 91 | + return response.Attributes.favoriteColors.join(', '); |
| 92 | +} |
| 93 | + |
| 94 | +async function manipulateList() { |
| 95 | + // We are creating the item for a fresh start |
| 96 | + await createItem(); |
| 97 | + |
| 98 | + let favoriteColors = await getFavoriteColors(); |
| 99 | + console.log(`These are Jim's favorite colors: ${favoriteColors}`); |
| 100 | + |
| 101 | + console.log("Yellow is one of Jim's favorite colors as well"); |
| 102 | + favoriteColors = await addYellow(); |
| 103 | + console.log(`Jim's new favorite colors: ${favoriteColors}`); |
| 104 | + |
| 105 | + console.log("Green is not one of Jim's favorite colors anymore"); |
| 106 | + favoriteColors = await removeGreen(); |
| 107 | + console.log(`Jim's new favorite colors: ${favoriteColors}`); |
| 108 | + |
| 109 | + console.log("Jim had a change of heart and he now likes blue more than red") |
| 110 | + favoriteColors = await swapRedAndBlue(); |
| 111 | + console.log(`Jim's new favorite colors: ${favoriteColors}`); |
| 112 | +} |
| 113 | + |
| 114 | +manipulateList() |
| 115 | + .then(() => console.log("Script completed")) |
| 116 | + .catch((error) => console.error(error)); |
0 commit comments