Changeset 8fe7f1a for database/drizzle
- Timestamp:
- 12/28/25 17:27:19 (6 months ago)
- Branches:
- main
- Children:
- 1ac9ee4
- Parents:
- 67186d2
- Location:
- database/drizzle/queries
- Files:
-
- 2 edited
-
builds.ts (modified) (4 diffs)
-
components.ts (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
database/drizzle/queries/builds.ts
r67186d2 r8fe7f1a 10 10 import {eq, desc, and, sql, ilike, asc} from "drizzle-orm"; 11 11 import {inArray} from "drizzle-orm/sql/expressions/conditions"; 12 import {addComponentToBuild} from "./components"; 12 13 13 14 export async function getPendingBuilds(db: Database) { … … 392 393 return db.transaction(async (tx) => { 393 394 const [buildToClone] = await tx 394 .select({ 395 id: buildsTable.id, 396 userId: buildsTable.userId, 397 name: buildsTable.name, 398 description: buildsTable.description, 399 totalPrice: buildsTable.totalPrice, 400 }) 395 .select() 401 396 .from(buildsTable) 402 397 .where( … … 421 416 }); 422 417 423 const components = await tx 424 .select() 418 if(!newBuild) return null; 419 420 const existing = await tx 421 .select({ componentId: buildComponentsTable.componentId }) 425 422 .from(buildComponentsTable) 426 .where( 427 eq(buildComponentsTable.buildId, buildId) 423 .where(eq(buildComponentsTable.buildId, buildId)); 424 425 if (existing.length > 0) { 426 await tx.insert(buildComponentsTable).values( 427 existing.map((r) => ({ 428 buildId: newBuild.id, 429 componentId: r.componentId, 430 })), 428 431 ); 429 430 if(components.length) {431 await tx432 .insert(buildComponentsTable)433 .values(434 components.map(component => ({435 buildId: newBuild.id,436 componentId: component.componentId437 }))438 );439 432 } 440 433 441 const [clonedBuild] = await tx 442 .select({ 443 id: buildsTable.id, 444 userId: buildsTable.userId, 445 name: buildsTable.name, 446 createdAt: buildsTable.createdAt, 447 description: buildsTable.description, 448 totalPrice: buildsTable.totalPrice 449 }) 450 .from(buildsTable) 451 .innerJoin( 452 usersTable, 453 eq(buildsTable.userId, usersTable.id) 454 ) 455 .where( 456 eq(buildsTable.id, newBuild.id) 457 ) 458 .limit(1); 459 460 const clonedComponents = await tx 461 .select({ 462 componentId: buildComponentsTable.componentId, 463 component: componentsTable 464 }) 465 .from(buildComponentsTable) 466 .innerJoin( 467 componentsTable, 468 eq(buildComponentsTable.componentId, componentsTable.id) 469 ) 470 .where( 471 eq(buildComponentsTable.buildId, newBuild.id) 472 ); 473 474 return { 475 ...clonedBuild, 476 components: clonedComponents.map(c => c.component) 477 }; 434 return newBuild.id; 478 435 }); 479 436 } … … 513 470 } 514 471 515 export async function editBuild(db: Database, userId: number, buildId: number, name: string, description: string, componentIds: number[]) { 516 return db.transaction(async (tx) => { 517 const [build] = await tx 518 .select({ 519 id: buildsTable.id, 520 userId: buildsTable.userId, 521 isApproved: buildsTable.isApproved 472 export async function editBuild(db: Database, userId: number, buildId: number) { 473 const [buildToEdit] = await db 474 .select() 475 .from(buildsTable) 476 .where( 477 and( 478 eq(buildsTable.id, buildId), 479 eq(buildsTable.userId, userId) 480 ) 481 ) 482 .limit(1); 483 484 if (!buildToEdit || buildToEdit.isApproved) return null; 485 486 487 return buildToEdit?.id ?? null; 488 } 489 490 export async function saveBuildState(db: Database, userId: number, buildId: number, name: string, description: string ) { 491 const [build] = await db 492 .select({ 493 id: buildsTable.id, 494 isApproved: buildsTable.isApproved 495 }) 496 .from(buildsTable) 497 .where( 498 and( 499 eq(buildsTable.id, buildId), 500 eq(buildsTable.userId, userId) 501 ) 502 ) 503 .limit(1); 504 505 if (!build || build.isApproved) return null; 506 507 const [updated] = await db 508 .update(buildsTable) 509 .set({ 510 name, 511 description 512 }) 513 .where(eq(buildsTable.id, buildId)) 514 .returning({ id: buildsTable.id }); 515 516 return updated?.id ?? null; 517 } 518 519 export async function getBuildState(db: Database, userId: number, buildId: number) { 520 const [build] = await db 521 .select({ 522 id: buildsTable.id, 523 userId: buildsTable.userId, 524 isApproved: buildsTable.isApproved, 525 name: buildsTable.name, 526 description: buildsTable.description, 527 totalPrice: buildsTable.totalPrice, 528 }) 529 .from(buildsTable) 530 .where( 531 and( 532 eq(buildsTable.id, buildId), 533 eq(buildsTable.userId, userId) 534 ) 535 ) 536 .limit(1); 537 538 if (!build || build.isApproved) return null; 539 540 const components = await db 541 .select({ componentId: buildComponentsTable.componentId 522 542 }) 523 .from(buildsTable) 524 .where( 525 and( 526 eq(buildsTable.id, buildId), 527 eq(buildsTable.userId, userId) 528 ) 529 ) 530 .limit(1); 531 532 if (!build) return null; 533 if (build.isApproved) return null; 534 535 const components = await tx 536 .select({ 537 id: componentsTable.id, 538 price: componentsTable.price 539 }) 540 .from(componentsTable) 541 .where( 542 inArray(componentsTable.id, componentIds) 543 ); 544 545 const totalPrice = components.reduce((sum, c) => sum + Number(c.price), 0); 546 547 await tx 548 .update(buildsTable) 549 .set({ 550 name: name, 551 description: description, 552 totalPrice: totalPrice.toFixed(2), 553 }) 554 .where( 555 and( 556 eq(buildsTable.id, buildId), 557 eq(buildsTable.userId, userId) 558 ) 559 ); 560 561 await tx 562 .delete(buildComponentsTable) 563 .where( 564 eq(buildComponentsTable.buildId, buildId) 565 ); 566 567 if(components.length) { 568 await tx.insert(buildComponentsTable) 569 .values( 570 componentIds.map(componentId => ({ 571 buildId: buildId, 572 componentId: componentId 573 })) 574 ); 575 } 576 577 return build.id; 578 }); 579 } 543 .from(buildComponentsTable) 544 .where( 545 eq(buildComponentsTable.buildId, buildId) 546 ); 547 548 return { 549 build, 550 componentIds: components.map(c => c.componentId) 551 }; 552 } -
database/drizzle/queries/components.ts
r67186d2 r8fe7f1a 896 896 } 897 897 898 export async function addComponentToBuild(db: Database, buildId: number, componentId: number) { 899 const [build] = await db 900 .select() 901 .from(buildsTable) 902 .where( 903 eq(buildsTable.id, buildId) 904 ) 905 .limit(1); 906 907 if(!build) return null; 908 909 const [component] = await db 910 .select() 911 .from(componentsTable) 912 .where( 913 eq(componentsTable.id, componentId) 914 ) 915 .limit(1); 916 917 if(!component) return null; 918 919 const existing = await db 920 .select() 921 .from(buildComponentsTable) 922 .where( 923 and( 924 eq(buildComponentsTable.buildId, buildId), 925 eq(buildComponentsTable.componentId, componentId) 926 ) 927 ) 928 .limit(1); 929 930 if(existing.length > 0) return null; 931 932 const [result] = await db 933 .insert(buildComponentsTable) 934 .values({ 935 buildId, 936 componentId 937 }) 938 .returning({ 939 id: buildComponentsTable.buildId 940 }); 941 942 const buildComponents = await db 943 .select({ 944 price: componentsTable.price, 945 }) 946 .from(buildComponentsTable) 947 .innerJoin( 948 componentsTable, 949 eq(buildComponentsTable.componentId, componentsTable.id) 950 ) 951 .where( 952 eq(buildComponentsTable.buildId, buildId) 953 ); 954 955 const totalPrice = buildComponents.reduce((sum, c) => sum + Number(c.price), 0); 956 957 await db 958 .update(buildsTable) 959 .set({ 960 totalPrice: totalPrice.toFixed(2) 961 }) 962 .where( 963 eq(buildsTable.id, buildId) 964 ); 965 966 return result?.id ?? null; 967 } 968 969 export async function removeComponentFromBuild(db: Database, buildId: number, componentId: number) { 970 const [build] = await db 971 .select() 972 .from(buildsTable) 973 .where( 974 eq(buildsTable.id, buildId) 975 ) 976 .limit(1); 977 978 if(!build) return null; 979 980 const [component] = await db 981 .select() 982 .from(componentsTable) 983 .where( 984 eq(componentsTable.id, componentId) 985 ) 986 .limit(1); 987 988 if(!component) return null; 989 990 const result = await db 991 .delete(buildComponentsTable) 992 .where( 993 and( 994 eq(buildComponentsTable.buildId, buildId), 995 eq(buildComponentsTable.componentId, componentId) 996 ) 997 ); 998 999 if(result.rowCount === 0) return null; 1000 1001 const buildComponents = await db 1002 .select({ 1003 price: componentsTable.price, 1004 }) 1005 .from(buildComponentsTable) 1006 .innerJoin( 1007 componentsTable, 1008 eq(buildComponentsTable.componentId, componentsTable.id) 1009 ) 1010 .where( 1011 eq(buildComponentsTable.buildId, buildId) 1012 ); 1013 1014 const totalPrice = buildComponents.reduce((sum, c) => sum + Number(c.price), 0); 1015 1016 await db 1017 .update(buildsTable) 1018 .set({ 1019 totalPrice: totalPrice.toFixed(2) 1020 }) 1021 .where( 1022 eq(buildsTable.id, buildId) 1023 ); 1024 1025 return result; 1026 } 1027 898 export async function addComponentToBuild(db: Database, userId: number, buildId: number, componentId: number) { 899 return db.transaction(async (tx) => { 900 const [build] = await tx 901 .select() 902 .from(buildsTable) 903 .where( 904 and( 905 eq(buildsTable.id, buildId), 906 eq(buildsTable.userId, userId) 907 ) 908 ) 909 .limit(1); 910 911 if(!build || build.isApproved) return null; 912 913 const existing = await tx 914 .select() 915 .from(buildComponentsTable) 916 .where( 917 and( 918 eq(buildComponentsTable.buildId, buildId), 919 eq(buildComponentsTable.componentId, componentId) 920 ) 921 ) 922 .limit(1); 923 924 if (existing.length) return null; 925 926 await tx 927 .insert(buildComponentsTable) 928 .values({ 929 buildId, 930 componentId 931 }); 932 933 const buildComponents = await tx 934 .select({ 935 price: componentsTable.price, 936 }) 937 .from(buildComponentsTable) 938 .innerJoin( 939 componentsTable, 940 eq(buildComponentsTable.componentId, componentsTable.id) 941 ) 942 .where( 943 eq(buildComponentsTable.buildId, buildId) 944 ); 945 946 const totalPrice = buildComponents.reduce((sum, c) => sum + Number(c.price), 0); 947 948 await tx 949 .update(buildsTable) 950 .set({ 951 totalPrice: totalPrice.toFixed(2) 952 }) 953 .where( 954 eq(buildsTable.id, buildId) 955 ); 956 957 return buildId; 958 }) 959 } 960 961 export async function removeComponentFromBuild(db: Database, userId: number, buildId: number, componentId: number) { 962 return db.transaction(async (tx) => { 963 const [build] = await tx 964 .select() 965 .from(buildsTable) 966 .where( 967 and( 968 eq(buildsTable.id, buildId), 969 eq(buildsTable.userId, userId) 970 ) 971 ) 972 .limit(1); 973 974 if(!build || build.isApproved) return null; 975 976 const result = await tx 977 .delete(buildComponentsTable) 978 .where( 979 and( 980 eq(buildComponentsTable.buildId, buildId), 981 eq(buildComponentsTable.componentId, componentId) 982 ) 983 ); 984 985 if(result.rowCount === 0) return null; 986 987 const buildComponents = await tx 988 .select({ 989 price: componentsTable.price, 990 }) 991 .from(buildComponentsTable) 992 .innerJoin( 993 componentsTable, 994 eq(buildComponentsTable.componentId, componentsTable.id) 995 ) 996 .where( 997 eq(buildComponentsTable.buildId, buildId) 998 ); 999 1000 const totalPrice = buildComponents.reduce((sum, c) => sum + Number(c.price), 0); 1001 1002 await tx 1003 .update(buildsTable) 1004 .set({ 1005 totalPrice: totalPrice.toFixed(2) 1006 }) 1007 .where( 1008 eq(buildsTable.id, buildId) 1009 ); 1010 1011 return result; 1012 }) 1013 }
Note:
See TracChangeset
for help on using the changeset viewer.
