Skip to content

Instantly share code, notes, and snippets.

@proton0210
Created June 15, 2024 13:54
Show Gist options
  • Save proton0210/31a4cd31ad95ea6209d3bca32a23d6fb to your computer and use it in GitHub Desktop.
Save proton0210/31a4cd31ad95ea6209d3bca32a23d6fb to your computer and use it in GitHub Desktop.
Sf Update Item
// 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