Created
June 15, 2024 13:58
-
-
Save proton0210/765e061d9fc73c356adc6755fdd6fe6e to your computer and use it in GitHub Desktop.
11-CreatOrder
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, PutItemCommand } from "@aws-sdk/client-dynamodb"; | |
import { ulid } from "ulid"; | |
const client = new DynamoDBClient({}); | |
export const handler = async ({ | |
order, | |
}: { | |
order: { itemId: string; quantity: number }[]; | |
}): Promise<void> => { | |
const tableName = process.env.TABLE_NAME; | |
const id = ulid(); | |
await client.send( | |
new PutItemCommand({ | |
TableName: tableName, | |
Item: { | |
OrderID: { S: id }, | |
order: { | |
L: order.map(({ itemId, quantity }) => ({ | |
M: { itemId: { S: itemId }, quantity: { N: quantity.toString() } }, | |
})), | |
}, | |
}, | |
}) | |
); | |
}; | |
--- | |
construct | |
-- | |
const createOrder = new cdk.aws_lambda_nodejs.NodejsFunction( | |
this, | |
"createOrder", | |
{ | |
entry: path.join(__dirname, "createOrder", "handler.ts"), | |
handler: "handler", | |
runtime: cdk.aws_lambda.Runtime.NODEJS_20_X, | |
environment: { | |
TABLE_NAME: OrderTable.tableName, | |
}, | |
} | |
); | |
OrderTable.grantReadWriteData(createOrder); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment