source: database/drizzle/queries/components.ts@ 5ce5904

main
Last change on this file since 5ce5904 was c4dd17d, checked in by Tome <gjorgievtome@…>, 7 months ago

update queries

  • Property mode set to 100644
File size: 1.4 KB
RevLine 
[1ebac59]1import type { Database } from "../db";
[c4dd17d]2import {buildsTable, componentsTable } from "../schema";
3import {and, desc, eq, ilike} from "drizzle-orm";
[1ebac59]4
[c4dd17d]5export async function getAllComponents(db: Database, limit?: number, q?: string, componentType?: string) {
6 let queryConditions = [];
[1ebac59]7
[c4dd17d]8 if (q) {
9 queryConditions.push(
10 ilike(componentsTable.name, `%${q}%`)
11 );
12 }
13
14 if(componentType && componentType.trim() !== 'all') {
15 queryConditions.push(
16 eq(componentsTable.type, componentType.trim().toLowerCase())
17 );
18 }
[1ebac59]19
[c4dd17d]20 const componentsList = await db
21 .select({
22 id: componentsTable.id,
23 name: componentsTable.name,
24 brand: componentsTable.brand,
25 price: componentsTable.price,
26 })
27 .from(componentsTable)
28 .where(
29 queryConditions.length > 0 ?
30 and (
31 ...queryConditions
32 )
33 : undefined
34 )
35 .orderBy(
36 desc(componentsTable.price)
37 )
38 .limit(limit || 100); // 100 placeholder
[1ebac59]39
[c4dd17d]40 return componentsList;
[1ebac59]41}
42
[c4dd17d]43export async function getComponentDetails(db: Database, componentId: number) {
44 const [componentDetails] = await db
45 .select()
46 .from(componentsTable)
47 .where(
48 eq(componentsTable.id, componentId)
49 )
50 .limit(1);
[1ebac59]51
[c4dd17d]52 return componentDetails ?? null;
[1ebac59]53}
Note: See TracBrowser for help on using the repository browser.