Skip to content

Commit

Permalink
chore: remove deleteItem() (denoland#616)
Browse files Browse the repository at this point in the history
This function is not used anywhere in the codebase.
  • Loading branch information
iuioiua authored Oct 3, 2023
1 parent ddc8fbd commit 7e40f27
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 45 deletions.
35 changes: 0 additions & 35 deletions utils/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,41 +86,6 @@ export async function createItem(item: Item) {
if (!res.ok) throw new Error("Failed to create item");
}

/**
* Deletes the item from the database. Throws if the item doesn't exist.
*
* @example
* ```ts
* import { deleteItem } from "@/utils/db.ts";
*
* await deleteItem({
* id: "01H9YD2RVCYTBVJEYEJEV5D1S1",
* userLogin: "john_doe",
* });
* ```
*/
export async function deleteItem(item: Pick<Item, "id" | "userLogin">) {
const itemsKey = ["items", item.id];
const itemsByUserKey = ["items_by_user", item.userLogin, item.id];
const [itemsRes, itemsByUserRes] = await kv.getMany<Item[]>([
itemsKey,
itemsByUserKey,
]);
if (itemsRes.value === null) throw new Deno.errors.NotFound("Item not found");
if (itemsByUserRes.value === null) {
throw new Deno.errors.NotFound("Item by user not found");
}

const res = await kv.atomic()
.check(itemsRes)
.check(itemsByUserRes)
.delete(itemsKey)
.delete(itemsByUserKey)
.commit();

if (!res.ok) throw new Error("Failed to delete item");
}

/**
* Gets the item with the given ID from the database.
*
Expand Down
10 changes: 0 additions & 10 deletions utils/db_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
createItem,
createUser,
createVote,
deleteItem,
deleteUserSession,
getAreVotedByUser,
getItem,
Expand Down Expand Up @@ -40,7 +39,6 @@ Deno.test("[db] items", async () => {
assertEquals(await getItem(item2.id), null);
assertEquals(await collectValues(listItems()), []);
assertEquals(await collectValues(listItemsByUser(user.login)), []);
await assertRejects(async () => await deleteItem(item1), "Item not found");

await createItem(item1);
await createItem(item2);
Expand All @@ -53,14 +51,6 @@ Deno.test("[db] items", async () => {
item1,
item2,
]);

await deleteItem(item1);
await deleteItem(item2);

assertEquals(await getItem(item1.id), null);
assertEquals(await getItem(item1.id), null);
assertEquals(await collectValues(listItems()), []);
assertEquals(await collectValues(listItemsByUser(user.login)), []);
});

Deno.test("[db] user", async () => {
Expand Down

0 comments on commit 7e40f27

Please sign in to comment.