Created
June 15, 2024 13:54
-
-
Save proton0210/31a4cd31ad95ea6209d3bca32a23d6fb to your computer and use it in GitHub Desktop.
Sf Update Item
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// handler.ts | |
import { DynamoDBClient, UpdateItemCommand } from "@aws-sdk/client-dynamodb"; | |
const client = new DynamoDBClient({}); | |
export const handler = async ({ | |
item: { itemId, quantity }, | |
}: { | |
item: { itemId: string; quantity: number }; | |
}): Promise<void> => { | |
const tableName = process.env.TABLE_NAME; | |
await client.send( | |
new UpdateItemCommand({ | |
TableName: tableName, | |
Key: { | |
ProductID: { S: itemId }, | |
}, | |
UpdateExpression: "SET stock = stock - :quantity", | |
ExpressionAttributeValues: { | |
":quantity": { N: quantity.toString() }, | |
}, | |
}) | |
); | |
}; | |
---- | |
construct | |
--- | |
const updateItemStock = new cdk.aws_lambda_nodejs.NodejsFunction( | |
this, | |
"updateItemStock", | |
{ | |
entry: path.join(__dirname, "updateItemStock", "handler.ts"), | |
handler: "handler", | |
runtime: cdk.aws_lambda.Runtime.NODEJS_20_X, | |
environment: { | |
TABLE_NAME: ProductTable.tableName, | |
}, | |
} | |
); | |
ProductTable.grantReadWriteData(updateItemStock); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment