Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,17 @@ const postItemsHandler: Handler = {
({ rdsDataClient }) =>
async (req, res) => {
const { description, guide, status, name }: Item = req.body;
const values = {
description: { StringValue: description },
guide: { StringValue: guide },
status: { StringValue: status },
name: { StringValue: name },
};
const command = buildStatementCommand(
`insert into items (iditem, description, guide, status, username, archived)\nvalues ("${uuidv4()}", "${description}", "${guide}", "${status}", "${name}", 0)`,
`insert into items (iditem, description, guide, status, username, archived)
values ("${uuidv4()}", ":description", ":guide", ":status", ":name", 0)`,
values,
);

await rdsDataClient.send(command);
res.status(200).send({});
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,14 @@ const putItemsArchiveHandler: Handler = {
({ rdsDataClient }) =>
async (req, res) => {
const { itemId } = req.params;

const values = {
itemId: { StringValue: itemId },
};
const command = buildStatementCommand(
`update items\nset archived = 1\nwhere iditem = "${itemId}"`,
`update items
set archived = 1
where iditem = ":itemId"`,
values,
);

await rdsDataClient.send(command);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import { ExecuteStatementCommand } from "@aws-sdk/client-rds-data";
import env from "../../env.json" assert { type: "json" };
import env from "../../env.json" with { type: "json" };

const buildStatementCommand = (sql: string) => {
const buildStatementCommand = (
sql: string,
parameters?: { [key: string]: { [key: string]: unknown } },
) => {
return new ExecuteStatementCommand({
resourceArn: env.CLUSTER_ARN,
secretArn: env.SECRET_ARN,
database: env.DB_NAME,
sql,
[parameters ? "parameters" : ""]: [parameters],
});
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,14 @@ describe("command-helper", () => {
expect(command.input.sql).toBe(sql);
});
});
it("should create an ExecuteStatementCommand with the provided SQL statement and parameters", () => {
const sql = "select * from some_table where id = :id";
const parameters = {
id: { StringValue: "123" },
};
const command = buildStatementCommand(sql, parameters);
expect(command.constructor.name).toBe("ExecuteStatementCommand");
expect(command.input.sql).toBe(sql);
expect(command.input.parameters).toEqual([parameters]);
});
});
Loading