source: database/drizzle/queries/components.ts@ 76b980b

main
Last change on this file since 76b980b was ea09e98, checked in by Tome <gjorgievtome@…>, 7 months ago

Update and add

  • Property mode set to 100644
File size: 11.9 KB
Line 
1import type { Database } from "../db";
2import {
3 buildsTable, cablesTable, caseMoboFormFactorsTable, casePsFormFactorsTable, caseStorageFormFactorsTable,
4 componentsTable, coolerCPUSocketsTable, coolersTable,
5 CPUTable,
6 GPUTable, memoryCardsTable,
7 memoryTable, motherboardsTable,
8 networkAdaptersTable,
9 networkCardsTable, opticalDrivesTable, pcCasesTable, powerSupplyTable, soundCardsTable, storageTable
10} from "../schema";
11import {and, desc, eq, ilike} from "drizzle-orm";
12
13export async function getAllComponents(db: Database, limit?: number, q?: string, componentType?: string) {
14 let queryConditions = [];
15
16 if (q) {
17 queryConditions.push(
18 ilike(componentsTable.name, `%${q}%`)
19 );
20 }
21
22 if(componentType && componentType.trim() !== 'all') {
23 queryConditions.push(
24 eq(componentsTable.type, componentType.trim().toLowerCase())
25 );
26 }
27
28 const componentsList = await db
29 .select({
30 id: componentsTable.id,
31 name: componentsTable.name,
32 brand: componentsTable.brand,
33 price: componentsTable.price,
34 })
35 .from(componentsTable)
36 .where(
37 queryConditions.length > 0 ?
38 and (
39 ...queryConditions
40 )
41 : undefined
42 )
43 .orderBy(
44 desc(componentsTable.price)
45 )
46 .limit(limit || 100); // 100 placeholder
47
48 return componentsList;
49}
50
51export async function getComponentDetails(db: Database, componentId: number) {
52 const [component] = await db
53 .select()
54 .from(componentsTable)
55 .where(
56 eq(componentsTable.id, componentId)
57 )
58 .limit(1);
59
60 if(!component) return null;
61
62 if (!component) return null;
63
64 let details: any = {};
65
66 switch (component.type) {
67 case 'cpu':
68 [details] = await db.select().from(CPUTable).where(eq(CPUTable.componentId, componentId));
69 break;
70 case 'gpu':
71 [details] = await db.select().from(GPUTable).where(eq(GPUTable.componentId, componentId));
72 break;
73 case 'memory':
74 [details] = await db.select().from(memoryTable).where(eq(memoryTable.componentId, componentId));
75 break;
76 case 'power_supply':
77 [details] = await db.select().from(powerSupplyTable).where(eq(powerSupplyTable.componentId, componentId));
78 break;
79 case 'case':
80 [details] = await db.select().from(pcCasesTable).where(eq(pcCasesTable.componentId, componentId));
81 if (details) {
82 details.storageFormFactors = await db.select().from(caseStorageFormFactorsTable).where(eq(caseStorageFormFactorsTable.caseId, componentId));
83 details.psFormFactors = await db.select().from(casePsFormFactorsTable).where(eq(casePsFormFactorsTable.caseId, componentId));
84 details.moboFormFactors = await db.select().from(caseMoboFormFactorsTable).where(eq(caseMoboFormFactorsTable.caseId, componentId));
85 }
86 break;
87 case 'cooler':
88 [details] = await db.select().from(coolersTable).where(eq(coolersTable.componentId, componentId));
89 if (details) {
90 details.cpuSockets = await db.select().from(coolerCPUSocketsTable).where(eq(coolerCPUSocketsTable.coolerId, componentId));
91 }
92 break;
93 case 'motherboard':
94 [details] = await db.select().from(motherboardsTable).where(eq(motherboardsTable.componentId, componentId));
95 break;
96 case 'storage':
97 [details] = await db.select().from(storageTable).where(eq(storageTable.componentId, componentId));
98 break;
99 case 'memory_card':
100 [details] = await db.select().from(memoryCardsTable).where(eq(memoryCardsTable.componentId, componentId));
101 break;
102 case 'optical_drive':
103 [details] = await db.select().from(opticalDrivesTable).where(eq(opticalDrivesTable.componentId, componentId));
104 break;
105 case 'sound_card':
106 [details] = await db.select().from(soundCardsTable).where(eq(soundCardsTable.componentId, componentId));
107 break;
108 case 'cables':
109 [details] = await db.select().from(cablesTable).where(eq(cablesTable.componentId, componentId));
110 break;
111 case 'network_adapter':
112 [details] = await db.select().from(networkAdaptersTable).where(eq(networkAdaptersTable.componentId, componentId));
113 break;
114 case 'network_card':
115 [details] = await db.select().from(networkCardsTable).where(eq(networkCardsTable.componentId, componentId));
116 break;
117 }
118
119 return {
120 ...component,
121 details: details || null
122 };
123}
124
125export async function addNewComponent(db: Database, name: string, brand: string, price: number, imgUrl: string, type: string, specificData: any) {
126 return db.transaction(async (tx) => {
127 const [newComponent] = await tx
128 .insert(componentsTable)
129 .values({
130 name: name,
131 brand: brand,
132 price: price.toFixed(2),
133 imgUrl: imgUrl,
134 type: type
135 })
136 .returning({
137 id: componentsTable.id
138 });
139
140 const componentId = newComponent.id;
141
142 switch (type) {
143 case 'cpu':
144 await tx.insert(CPUTable).values({
145 componentId,
146 socket: specificData.socket,
147 cores: specificData.cores,
148 threads: specificData.threads,
149 baseClock: specificData.baseClock,
150 boostClock: specificData.boostClock,
151 tdp: specificData.tdp,
152 });
153 break;
154
155 case 'gpu':
156 await tx.insert(GPUTable).values({
157 componentId,
158 vram: specificData.vram,
159 tdp: specificData.tdp,
160 baseClock: specificData.baseClock,
161 boostClock: specificData.boostClock,
162 chipset: specificData.chipset,
163 length: specificData.length,
164 });
165 break;
166
167 case 'memory':
168 await tx.insert(memoryTable).values({
169 componentId,
170 type: specificData.type,
171 speed: specificData.speed,
172 capacity: specificData.capacity,
173 modules: specificData.modules,
174 });
175 break;
176
177 case 'power_supply':
178 await tx.insert(powerSupplyTable).values({
179 componentId,
180 type: specificData.type,
181 wattage: specificData.wattage,
182 formFactor: specificData.formFactor,
183 });
184 break;
185
186 case 'case':
187 await tx.insert(pcCasesTable).values({
188 componentId,
189 coolerMaxHeight: specificData.coolerMaxHeight,
190 gpuMaxLength: specificData.gpuMaxLength,
191 });
192
193 if (specificData.storageFormFactors) {
194 await tx.insert(caseStorageFormFactorsTable).values(
195 specificData.storageFormFactors.map((sf: any) => ({
196 caseId: componentId,
197 formFactor: sf.formFactor,
198 numSlots: sf.numSlots,
199 }))
200 );
201 }
202
203 if (specificData.psFormFactors) {
204 await tx.insert(casePsFormFactorsTable).values(
205 specificData.psFormFactors.map((pf: any) => ({
206 caseId: componentId,
207 formFactor: pf.formFactor,
208 }))
209 );
210 }
211
212 if (specificData.moboFormFactors) {
213 await tx.insert(caseMoboFormFactorsTable).values(
214 specificData.moboFormFactors.map((mf: any) => ({
215 caseId: componentId,
216 formFactor: mf.formFactor,
217 }))
218 );
219 }
220 break;
221
222 case 'cooler':
223 await tx.insert(coolersTable).values({
224 componentId,
225 type: specificData.type,
226 height: specificData.height,
227 maxTdpSupported: specificData.maxTdpSupported,
228 });
229
230 if (specificData.cpuSockets) {
231 await tx.insert(coolerCPUSocketsTable).values(
232 specificData.cpuSockets.map((socket: string) => ({
233 coolerId: componentId,
234 socket: socket,
235 }))
236 );
237 }
238 break;
239
240 case 'motherboard':
241 await tx.insert(motherboardsTable).values({
242 componentId,
243 socket: specificData.socket,
244 chipset: specificData.chipset,
245 formFactor: specificData.formFactor,
246 ramType: specificData.ramType,
247 numRamSlots: specificData.numRamSlots,
248 maxRamCapacity: specificData.maxRamCapacity,
249 });
250 break;
251
252 case 'storage':
253 await tx.insert(storageTable).values({
254 componentId,
255 type: specificData.type,
256 capacity: specificData.capacity,
257 formFactor: specificData.formFactor,
258 });
259 break;
260
261 case 'memory_card':
262 await tx.insert(memoryCardsTable).values({
263 componentId,
264 numSlots: specificData.numSlots,
265 interface: specificData.interface,
266 });
267 break;
268
269 case 'optical_drive':
270 await tx.insert(opticalDrivesTable).values({
271 componentId,
272 formFactor: specificData.formFactor,
273 type: specificData.type,
274 interface: specificData.interface,
275 writeSpeed: specificData.writeSpeed,
276 readSpeed: specificData.readSpeed,
277 });
278 break;
279
280 case 'sound_card':
281 await tx.insert(soundCardsTable).values({
282 componentId,
283 sampleRate: specificData.sampleRate,
284 bitDepth: specificData.bitDepth,
285 chipset: specificData.chipset,
286 interface: specificData.interface,
287 channel: specificData.channel,
288 });
289 break;
290
291 case 'cables':
292 await tx.insert(cablesTable).values({
293 componentId,
294 lengthCm: specificData.lengthCm,
295 type: specificData.type,
296 });
297 break;
298
299 case 'network_adapter':
300 await tx.insert(networkAdaptersTable).values({
301 componentId,
302 wifiVersion: specificData.wifiVersion,
303 interface: specificData.interface,
304 numAntennas: specificData.numAntennas,
305 });
306 break;
307
308 case 'network_card':
309 await tx.insert(networkCardsTable).values({
310 componentId,
311 numPorts: specificData.numPorts,
312 speed: specificData.speed,
313 interface: specificData.interface,
314 });
315 break;
316
317 default:
318 return null;
319 }
320
321 return componentId;
322 });
323}
Note: See TracBrowser for help on using the repository browser.