Skip to content

Commit e9a9854

Browse files
authored
javascriptv3: update aurora-serverless-app to use paramaterized sql statements, Aurora Serverless (#7559)
1 parent ba2dea7 commit e9a9854

File tree

4 files changed

+32
-6
lines changed

4 files changed

+32
-6
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/handlers/put-items-archive-handler.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,14 @@ const putItemsArchiveHandler: Handler = {
99
({ rdsDataClient }) =>
1010
async (req, res) => {
1111
const { itemId } = req.params;
12-
12+
const values = {
13+
itemId: { StringValue: itemId },
14+
};
1315
const command = buildStatementCommand(
14-
`update items\nset archived = 1\nwhere iditem = "${itemId}"`,
16+
`update items
17+
set archived = 1
18+
where iditem = ":itemId"`,
19+
values,
1520
);
1621

1722
await rdsDataClient.send(command);

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
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 = (
7+
sql: string,
8+
parameters?: { [key: string]: { [key: string]: unknown } },
9+
) => {
710
return new ExecuteStatementCommand({
811
resourceArn: env.CLUSTER_ARN,
912
secretArn: env.SECRET_ARN,
1013
database: env.DB_NAME,
1114
sql,
15+
[parameters ? "parameters" : ""]: [parameters],
1216
});
1317
};
1418

javascriptv3/example_code/cross-services/aurora-serverless-app/tests/command-helper.unit.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,14 @@ describe("command-helper", () => {
1212
expect(command.input.sql).toBe(sql);
1313
});
1414
});
15+
it("should create an ExecuteStatementCommand with the provided SQL statement and parameters", () => {
16+
const sql = "select * from some_table where id = :id";
17+
const parameters = {
18+
id: { StringValue: "123" },
19+
};
20+
const command = buildStatementCommand(sql, parameters);
21+
expect(command.constructor.name).toBe("ExecuteStatementCommand");
22+
expect(command.input.sql).toBe(sql);
23+
expect(command.input.parameters).toEqual([parameters]);
24+
});
1525
});

0 commit comments

Comments
 (0)