Changeset e7f1bfa for database/drizzle/queries/components.ts
- Timestamp:
- 12/25/25 03:39:56 (7 months ago)
- Branches:
- main
- Children:
- 898cf68
- Parents:
- 83fb5e2
- File:
-
- 1 edited
-
database/drizzle/queries/components.ts (modified) (6 diffs)
Legend:
- Unmodified
- Added
- Removed
-
database/drizzle/queries/components.ts
r83fb5e2 re7f1bfa 12 12 } from "../schema"; 13 13 import {and, desc, eq, ilike} from "drizzle-orm"; 14 import {typeConfigMap, ComponentType} from "../config/componentFieldConfig"; 15 import {AnyPgColumn} from "drizzle-orm/pg-core"; 14 16 export async function getAllComponents(db: Database, limit?: number, componentType?: string, q?: string) { 15 17 let queryConditions = []; … … 27 29 } 28 30 29 const components List= await db31 const components = await db 30 32 .select({ 31 33 id: componentsTable.id, … … 47 49 .limit(limit || 100); // 100 placeholder 48 50 49 return components List;51 return components; 50 52 } 51 53 … … 61 63 if(!component) return null; 62 64 63 let details: any = {}; 64 65 switch (component.type) { 66 case 'cpu': 67 [details] = await db.select().from(CPUTable).where(eq(CPUTable.componentId, componentId)); 68 break; 69 case 'gpu': 70 [details] = await db.select().from(GPUTable).where(eq(GPUTable.componentId, componentId)); 71 break; 72 case 'memory': 73 [details] = await db.select().from(memoryTable).where(eq(memoryTable.componentId, componentId)); 74 break; 75 case 'power_supply': 76 [details] = await db.select().from(powerSupplyTable).where(eq(powerSupplyTable.componentId, componentId)); 77 break; 78 case 'case': 79 [details] = await db.select().from(pcCasesTable).where(eq(pcCasesTable.componentId, componentId)); 80 if (details) { 81 details.storageFormFactors = await db.select().from(caseStorageFormFactorsTable).where(eq(caseStorageFormFactorsTable.caseId, componentId)); 82 details.psFormFactors = await db.select().from(casePsFormFactorsTable).where(eq(casePsFormFactorsTable.caseId, componentId)); 83 details.moboFormFactors = await db.select().from(caseMoboFormFactorsTable).where(eq(caseMoboFormFactorsTable.caseId, componentId)); 84 } 85 break; 86 case 'cooler': 87 [details] = await db.select().from(coolersTable).where(eq(coolersTable.componentId, componentId)); 88 if (details) { 89 details.cpuSockets = await db.select().from(coolerCPUSocketsTable).where(eq(coolerCPUSocketsTable.coolerId, componentId)); 90 } 91 break; 92 case 'motherboard': 93 [details] = await db.select().from(motherboardsTable).where(eq(motherboardsTable.componentId, componentId)); 94 break; 95 case 'storage': 96 [details] = await db.select().from(storageTable).where(eq(storageTable.componentId, componentId)); 97 break; 98 case 'memory_card': 99 [details] = await db.select().from(memoryCardsTable).where(eq(memoryCardsTable.componentId, componentId)); 100 break; 101 case 'optical_drive': 102 [details] = await db.select().from(opticalDrivesTable).where(eq(opticalDrivesTable.componentId, componentId)); 103 break; 104 case 'sound_card': 105 [details] = await db.select().from(soundCardsTable).where(eq(soundCardsTable.componentId, componentId)); 106 break; 107 case 'cables': 108 [details] = await db.select().from(cablesTable).where(eq(cablesTable.componentId, componentId)); 109 break; 110 case 'network_adapter': 111 [details] = await db.select().from(networkAdaptersTable).where(eq(networkAdaptersTable.componentId, componentId)); 112 break; 113 case 'network_card': 114 [details] = await db.select().from(networkCardsTable).where(eq(networkCardsTable.componentId, componentId)); 115 break; 65 const config = typeConfigMap[component.type as ComponentType]; 66 67 const [details] = await db 68 .select() 69 .from(config.table) 70 .where( 71 eq(config.table.componentId, componentId) 72 ) 73 .limit(1); 74 75 if (component.type === 'case') { 76 details.storageFormFactors = await db.select().from(caseStorageFormFactorsTable).where(eq(caseStorageFormFactorsTable.caseId, componentId)); 77 details.psFormFactors = await db.select().from(casePsFormFactorsTable).where(eq(casePsFormFactorsTable.caseId, componentId)); 78 details.moboFormFactors = await db.select().from(caseMoboFormFactorsTable).where(eq(caseMoboFormFactorsTable.caseId, componentId)); 79 } 80 81 if (component.type === 'cooler') { 82 details.cpuSockets = await db.select().from(coolerCPUSocketsTable).where(eq(coolerCPUSocketsTable.coolerId, componentId)); 116 83 } 117 84 118 85 return { 119 86 ...component, 120 details: details || null87 details: details 121 88 }; 122 89 } … … 139 106 const componentId = newComponent.id; 140 107 141 switch (type) { 142 case 'cpu': 143 await tx.insert(CPUTable).values({ 144 componentId, 145 socket: specificData.socket, 146 cores: specificData.cores, 147 threads: specificData.threads, 148 baseClock: specificData.baseClock, 149 boostClock: specificData.boostClock, 150 tdp: specificData.tdp, 151 }); 152 break; 153 154 case 'gpu': 155 await tx.insert(GPUTable).values({ 156 componentId, 157 vram: specificData.vram, 158 tdp: specificData.tdp, 159 baseClock: specificData.baseClock, 160 boostClock: specificData.boostClock, 161 chipset: specificData.chipset, 162 length: specificData.length, 163 }); 164 break; 165 166 case 'memory': 167 await tx.insert(memoryTable).values({ 168 componentId, 169 type: specificData.type, 170 speed: specificData.speed, 171 capacity: specificData.capacity, 172 modules: specificData.modules, 173 }); 174 break; 175 176 case 'power_supply': 177 await tx.insert(powerSupplyTable).values({ 178 componentId, 179 type: specificData.type, 180 wattage: specificData.wattage, 181 formFactor: specificData.formFactor, 182 }); 183 break; 184 185 case 'case': 186 await tx.insert(pcCasesTable).values({ 187 componentId, 188 coolerMaxHeight: specificData.coolerMaxHeight, 189 gpuMaxLength: specificData.gpuMaxLength, 190 }); 191 192 if (specificData.storageFormFactors) { 193 await tx.insert(caseStorageFormFactorsTable).values( 194 specificData.storageFormFactors.map((sf: any) => ({ 195 caseId: componentId, 196 formFactor: sf.formFactor, 197 numSlots: sf.numSlots, 198 })) 199 ); 200 } 201 202 if (specificData.psFormFactors) { 203 await tx.insert(casePsFormFactorsTable).values( 204 specificData.psFormFactors.map((pf: any) => ({ 205 caseId: componentId, 206 formFactor: pf.formFactor, 207 })) 208 ); 209 } 210 211 if (specificData.moboFormFactors) { 212 await tx.insert(caseMoboFormFactorsTable).values( 213 specificData.moboFormFactors.map((mf: any) => ({ 214 caseId: componentId, 215 formFactor: mf.formFactor, 216 })) 217 ); 218 } 219 break; 220 221 case 'cooler': 222 await tx.insert(coolersTable).values({ 223 componentId, 224 type: specificData.type, 225 height: specificData.height, 226 maxTdpSupported: specificData.maxTdpSupported, 227 }); 228 229 if (specificData.cpuSockets) { 230 await tx.insert(coolerCPUSocketsTable).values( 231 specificData.cpuSockets.map((socket: string) => ({ 232 coolerId: componentId, 233 socket: socket, 234 })) 235 ); 236 } 237 break; 238 239 case 'motherboard': 240 await tx.insert(motherboardsTable).values({ 241 componentId, 242 socket: specificData.socket, 243 chipset: specificData.chipset, 244 formFactor: specificData.formFactor, 245 ramType: specificData.ramType, 246 numRamSlots: specificData.numRamSlots, 247 maxRamCapacity: specificData.maxRamCapacity, 248 pciExpressSlots: specificData.pciExpressSlots, 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; 108 const config = typeConfigMap[type as ComponentType]; 109 110 await tx 111 .insert(config.table) 112 .values({ 113 componentId: componentId, 114 ...specificData 115 }); 116 117 if (type === 'case') { 118 if (specificData.storageFormFactors) { 119 await tx.insert(caseStorageFormFactorsTable).values( 120 specificData.storageFormFactors.map((sf: any) => ({ 121 caseId: componentId, 122 formFactor: sf.formFactor, 123 numSlots: sf.numSlots, 124 })) 125 ); 126 } 127 if (specificData.psFormFactors) { 128 await tx.insert(casePsFormFactorsTable).values( 129 specificData.psFormFactors.map((pf: any) => ({ 130 caseId: componentId, 131 formFactor: pf.formFactor, 132 })) 133 ); 134 } 135 if (specificData.moboFormFactors) { 136 await tx.insert(caseMoboFormFactorsTable).values( 137 specificData.moboFormFactors.map((mf: any) => ({ 138 caseId: componentId, 139 formFactor: mf.formFactor, 140 })) 141 ); 142 } 143 } 144 145 if (type === 'cooler' && specificData.cpuSockets) { 146 await tx.insert(coolerCPUSocketsTable).values( 147 specificData.cpuSockets.map((socket: any) => ({ coolerId: componentId, socket })) 148 ); 319 149 } 320 150 … … 323 153 } 324 154 325 export async function getCompatibleComponents(db: Database, buildId: number, componentType: string) { 326 return db.transaction(async (tx) => { 327 const [build] = await tx 328 .select({ 329 buildId: buildsTable.id, 330 userId: buildsTable.userId, 331 }) 332 .from(buildsTable) 155 export async function getBuildComponents(db: Database, buildId: number) { 156 const [build] = await db 157 .select({ 158 buildId: buildsTable.id, 159 userId: buildsTable.userId, 160 }) 161 .from(buildsTable) 162 .where( 163 eq(buildsTable.id, buildId) 164 ) 165 .limit(1); 166 167 if(!build) return null; 168 169 const components = await db 170 .select({ 171 id: componentsTable.id, 172 type: componentsTable.type, 173 }) 174 .from(buildComponentsTable) 175 .where( 176 eq(buildComponentsTable.buildId, buildId) 177 ) 178 .innerJoin( 179 componentsTable, 180 eq(buildComponentsTable.componentId, componentsTable.id) 181 ); 182 183 const componentsDetails = []; 184 185 for (const comp of components) { 186 const config = typeConfigMap[comp.type as ComponentType]; 187 188 const [details] = await db 189 .select() 190 .from(config.table) 333 191 .where( 334 eq( buildsTable.id, buildId)192 eq(config.table.componentId, comp.id) 335 193 ) 336 194 .limit(1); 337 195 338 if(!build) return null; 339 340 196 if (comp.type === 'case') { 197 details.storageFormFactors = await db.select().from(caseStorageFormFactorsTable).where(eq(caseStorageFormFactorsTable.caseId, comp.id)); 198 details.psFormFactors = await db.select().from(casePsFormFactorsTable).where(eq(casePsFormFactorsTable.caseId, comp.id)); 199 details.moboFormFactors = await db.select().from(caseMoboFormFactorsTable).where(eq(caseMoboFormFactorsTable.caseId, comp.id)); 200 } 201 202 if (comp.type === 'cooler') { 203 details.cpuSockets = await db.select().from(coolerCPUSocketsTable).where(eq(coolerCPUSocketsTable.coolerId, comp.id)); 204 } 205 206 componentsDetails.push({ 207 ...comp, 208 details: details 209 }); 210 } 211 212 return componentsDetails; 213 } 214 215 export async function getCompatibleComponents(db: Database, buildId: number, componentType: string) { 216 // TO BE IMPLEMENTED 341 217 return null; 342 });343 218 } 344 219
Note:
See TracChangeset
for help on using the changeset viewer.
