From 83cd33e4397c3468c65fff28bd07a2035e2d472d Mon Sep 17 00:00:00 2001 From: syntaxbullet Date: Thu, 18 Dec 2025 16:51:22 +0100 Subject: [PATCH] refactor: Optimize item autocomplete by moving name filtering to the database query and increasing the limit. --- src/commands/inventory/use.ts | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/src/commands/inventory/use.ts b/src/commands/inventory/use.ts index 1c5bff0..3283782 100644 --- a/src/commands/inventory/use.ts +++ b/src/commands/inventory/use.ts @@ -60,20 +60,24 @@ export const use = createCommand({ const focusedValue = interaction.options.getFocused(); const userId = interaction.user.id; - // Fetch owned items that are usable - const userInventory = await DrizzleClient.query.inventory.findMany({ - where: eq(inventory.userId, BigInt(userId)), - with: { - item: true - }, - limit: 10 - }); + // Fetch owned items that match the search query + // We join with items table to filter by name directly in the database + const entries = await DrizzleClient.select({ + quantity: inventory.quantity, + item: items + }) + .from(inventory) + .innerJoin(items, eq(inventory.itemId, items.id)) + .where(and( + eq(inventory.userId, BigInt(userId)), + like(items.name, `%${focusedValue}%`) + )) + .limit(20); // Fetch up to 20 matching items - const filtered = userInventory.filter(entry => { - const matchName = entry.item.name.toLowerCase().includes(focusedValue.toLowerCase()); + const filtered = entries.filter(entry => { const usageData = entry.item.usageData as ItemUsageData | null; const isUsable = usageData && usageData.effects && usageData.effects.length > 0; - return matchName && isUsable; + return isUsable; }); await interaction.respond(