Skip to content

Commit 4a95c46

Browse files
committed
chore: use paramaterized sql statements
1 parent 708c376 commit 4a95c46

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

javascriptv3/example_code/cross-services/aurora-serverless-app/src/handlers/post-items-handler.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,17 @@ const postItemsHandler: Handler = {
1111
({ rdsDataClient }) =>
1212
async (req, res) => {
1313
const { description, guide, status, name }: Item = req.body;
14+
const values = {
15+
description: { StringValue: description },
16+
guide: { StringValue: guide },
17+
status: { StringValue: status },
18+
name: { StringValue: name },
19+
}
1420
const command = buildStatementCommand(
15-
`insert into items (iditem, description, guide, status, username, archived)\nvalues ("${uuidv4()}", "${description}", "${guide}", "${status}", "${name}", 0)`,
21+
`insert into items (iditem, description, guide, status, username, archived)
22+
values ("${uuidv4()}", ":description", ":guide", ":status", ":name", 0)`,
23+
values
1624
);
17-
1825
await rdsDataClient.send(command);
1926
res.status(200).send({});
2027
},

javascriptv3/example_code/cross-services/aurora-serverless-app/src/statement-commands/command-helper.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0
33
import { ExecuteStatementCommand } from "@aws-sdk/client-rds-data";
4-
import env from "../../env.json" assert { type: "json" };
4+
import env from "../../env.json" with { type: "json" };
55

6-
const buildStatementCommand = (sql: string) => {
6+
const buildStatementCommand = (sql: string, parameters?: { [key: string]: { [key: string]: unknown}}) => {
77
return new ExecuteStatementCommand({
88
resourceArn: env.CLUSTER_ARN,
99
secretArn: env.SECRET_ARN,
1010
database: env.DB_NAME,
1111
sql,
12+
[parameters ? "parameters" : ""]: [parameters]
1213
});
1314
};
1415

0 commit comments

Comments
 (0)