Changeset 83fb5e2 for database/drizzle/queries
- Timestamp:
- 12/25/25 01:49:51 (7 months ago)
- Branches:
- main
- Children:
- e7f1bfa
- Parents:
- d4842f4
- Location:
- database/drizzle/queries
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
database/drizzle/queries/builds.ts
rd4842f4 r83fb5e2 46 46 47 47 export async function setBuildApprovalStatus(db: Database, buildId: number, isApproved: boolean){ 48 const result= await db48 const [result] = await db 49 49 .update(buildsTable) 50 50 .set({ … … 61 61 }) 62 62 63 return result .length;63 return result?.id ?? null; 64 64 } 65 65 … … 135 135 created_at: buildsTable.createdAt, 136 136 total_price: buildsTable.totalPrice, 137 avgRating: sql<number>` AVG(${ratingBuildsTable.value}::float)`137 avgRating: sql<number>`COALESCE(AVG(${ratingBuildsTable.value}::float),0)` 138 138 }) 139 139 .from(buildsTable) … … 160 160 161 161 return approvedBuildsList; 162 }163 164 export async function getHighestRankedBuilds(db: Database, limit? : number) {165 const highestRankedBuildsList = await db166 .select({167 id: buildsTable.id,168 user_id: buildsTable.userId,169 name: buildsTable.name,170 created_at: buildsTable.createdAt,171 total_price: buildsTable.totalPrice,172 avgRating: sql<number>`AVG(${ratingBuildsTable.value}::float)`173 })174 .from(buildsTable)175 .innerJoin(176 ratingBuildsTable,177 eq(buildsTable.id, ratingBuildsTable.buildId)178 )179 .where(180 eq(buildsTable.isApproved, true)181 )182 .groupBy(buildsTable.id)183 .orderBy(184 desc(sql<number>`AVG(${ratingBuildsTable.value}::float)`)185 )186 .limit(limit || 100); // 100 placeholder187 188 return highestRankedBuildsList;189 162 } 190 163 … … 335 308 .limit(1); 336 309 337 if (existing.length ) {310 if (existing.length > 0) { 338 311 await db 339 312 .delete(favoriteBuildsTable) … … 345 318 ); 346 319 347 return { favorite: false };320 return null; 348 321 } 349 322 … … 355 328 }); 356 329 357 return { favorite: true };330 return true; 358 331 } 359 332 … … 378 351 }) 379 352 380 return result ;353 return result ?? null; 381 354 } 382 355 … … 404 377 }) 405 378 406 return result ;379 return result ?? null; 407 380 } 408 381 … … 498 471 499 472 export async function deleteBuild(db: Database, userId: number, buildId: number) { 500 const result= await db473 const [result] = await db 501 474 .delete(buildsTable) 502 475 .where( … … 510 483 }) 511 484 512 return result .length;485 return result?.id ?? null; 513 486 } 514 487 … … 550 523 } 551 524 552 return newBuild .id;525 return newBuild?.id ?? null; 553 526 }); 554 527 } -
database/drizzle/queries/components.ts
rd4842f4 r83fb5e2 1 1 import type { Database } from "../db"; 2 2 import { 3 buildComponentsTable, 3 4 buildsTable, cablesTable, caseMoboFormFactorsTable, casePsFormFactorsTable, caseStorageFormFactorsTable, 4 5 componentsTable, coolerCPUSocketsTable, coolersTable, … … 7 8 memoryTable, motherboardsTable, 8 9 networkAdaptersTable, 9 networkCardsTable, opticalDrivesTable, pcCasesTable, powerSupplyTable, soundCardsTable, storageTable 10 networkCardsTable, opticalDrivesTable, pcCasesTable, powerSupplyTable, 11 ratingBuildsTable, soundCardsTable, storageTable 10 12 } from "../schema"; 11 13 import {and, desc, eq, ilike} from "drizzle-orm"; 12 13 export async function getAllComponents(db: Database, limit?: number, q?: string, componentType?: string) { 14 export async function getAllComponents(db: Database, limit?: number, componentType?: string, q?: string) { 14 15 let queryConditions = []; 15 16 … … 59 60 60 61 if(!component) return null; 61 62 if (!component) return null;63 62 64 63 let details: any = {}; … … 247 246 numRamSlots: specificData.numRamSlots, 248 247 maxRamCapacity: specificData.maxRamCapacity, 248 pciExpressSlots: specificData.pciExpressSlots, 249 249 }); 250 250 break; … … 322 322 }); 323 323 } 324 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) 333 .where( 334 eq(buildsTable.id, buildId) 335 ) 336 .limit(1); 337 338 if(!build) return null; 339 340 341 return null; 342 }); 343 } 344 345 export async function addComponentToBuild(db: Database, buildId: number, componentId: number) { 346 const [build] = await db 347 .select() 348 .from(buildsTable) 349 .where( 350 eq(buildsTable.id, buildId) 351 ) 352 .limit(1); 353 354 if(!build) return null; 355 356 const [component] = await db 357 .select() 358 .from(componentsTable) 359 .where( 360 eq(componentsTable.id, componentId) 361 ) 362 .limit(1); 363 364 if(!component) return null; 365 366 const existing = await db 367 .select() 368 .from(buildComponentsTable) 369 .where( 370 and( 371 eq(buildComponentsTable.buildId, buildId), 372 eq(buildComponentsTable.componentId, componentId) 373 ) 374 ) 375 .limit(1); 376 377 if(existing.length > 0) return null; 378 379 const [result] = await db 380 .insert(buildComponentsTable) 381 .values({ 382 buildId, 383 componentId 384 }) 385 .returning({ 386 id: buildComponentsTable.buildId 387 }); 388 389 const buildComponents = await db 390 .select({ 391 price: componentsTable.price, 392 }) 393 .from(buildComponentsTable) 394 .innerJoin( 395 componentsTable, 396 eq(buildComponentsTable.componentId, componentsTable.id) 397 ) 398 .where( 399 eq(buildComponentsTable.buildId, buildId) 400 ); 401 402 const totalPrice = buildComponents.reduce((sum, c) => sum + Number(c.price), 0); 403 404 await db 405 .update(buildsTable) 406 .set({ 407 totalPrice: totalPrice.toFixed(2) 408 }) 409 .where( 410 eq(buildsTable.id, buildId) 411 ); 412 413 return result?.id ?? null; 414 } 415 416 export async function removeComponentFromBuild(db: Database, buildId: number, componentId: number) { 417 const [build] = await db 418 .select() 419 .from(buildsTable) 420 .where( 421 eq(buildsTable.id, buildId) 422 ) 423 .limit(1); 424 425 if(!build) return null; 426 427 const [component] = await db 428 .select() 429 .from(componentsTable) 430 .where( 431 eq(componentsTable.id, componentId) 432 ) 433 .limit(1); 434 435 if(!component) return null; 436 437 const result = await db 438 .delete(buildComponentsTable) 439 .where( 440 and( 441 eq(buildComponentsTable.buildId, buildId), 442 eq(buildComponentsTable.componentId, componentId) 443 ) 444 ); 445 446 if(result.rowCount === 0) return null; 447 448 const buildComponents = await db 449 .select({ 450 price: componentsTable.price, 451 }) 452 .from(buildComponentsTable) 453 .innerJoin( 454 componentsTable, 455 eq(buildComponentsTable.componentId, componentsTable.id) 456 ) 457 .where( 458 eq(buildComponentsTable.buildId, buildId) 459 ); 460 461 const totalPrice = buildComponents.reduce((sum, c) => sum + Number(c.price), 0); 462 463 await db 464 .update(buildsTable) 465 .set({ 466 totalPrice: totalPrice.toFixed(2) 467 }) 468 .where( 469 eq(buildsTable.id, buildId) 470 ); 471 472 return result; 473 } 474 -
database/drizzle/queries/users.ts
rd4842f4 r83fb5e2 4 4 5 5 export async function createUser(db: Database, username: string, email: string, passwordHash: string) { 6 await db6 const [newUser] = await db 7 7 .insert(usersTable) 8 8 .values({ … … 10 10 email: email, 11 11 passwordHash: passwordHash, 12 }) 13 .returning({ 14 id: usersTable.id, 12 15 }); 16 17 return newUser?.id ?? null; 13 18 } 14 19 … … 29 34 30 35 export async function isAdmin(db: Database, userId: number) { 31 const admin= await db36 const [admin] = await db 32 37 .selectDistinct() 33 38 .from(adminsTable) … … 37 42 .limit(1); 38 43 39 return admin.length ?? null;44 return !!admin; 40 45 } 41 46 … … 55 60 56 61 export async function setComponentSuggestionStatus(db: Database, suggestionId: number, adminId: number, status: string, adminComment: string) { 57 const result= await db62 const [result] = await db 58 63 .update(suggestionsTable) 59 64 .set({ … … 64 69 .where( 65 70 eq(suggestionsTable.id, suggestionId) 66 ); 71 ) 72 .returning({ 73 id: suggestionsTable.id 74 }); 67 75 68 return result .rowCount?? null;76 return result?.id ?? null; 69 77 } 70 78 … … 78 86 componentType: componentType 79 87 }) 80 .returning({ id: suggestionsTable.id }); 88 .returning({ 89 id: suggestionsTable.id 90 }); 81 91 82 return newSuggestion .id ?? null;92 return newSuggestion?.id ?? null; 83 93 }
Note:
See TracChangeset
for help on using the changeset viewer.
