Changeset e03e6fb
- Timestamp:
- 01/28/26 12:12:43 (5 months ago)
- Branches:
- main
- Children:
- a744c90
- Parents:
- ad211d1
- Location:
- database
- Files:
-
- 5 edited
- 1 moved
-
drizzle/queries/builds.ts (modified) (6 diffs)
-
drizzle/queries/components.ts (modified) (14 diffs)
-
drizzle/schema/builds.ts (modified) (2 diffs)
-
migrations/0000_shallow_darkhawk.sql (moved) (moved from database/migrations/0000_regular_kulan_gath.sql ) (1 diff)
-
migrations/meta/0000_snapshot.json (modified) (4 diffs)
-
migrations/meta/_journal.json (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
database/drizzle/queries/builds.ts
rad211d1 re03e6fb 200 200 .select({ 201 201 componentId: buildComponentsTable.componentId, 202 component: componentsTable 202 component: componentsTable, 203 quantity: buildComponentsTable.numComponents 203 204 }) 204 205 .from(buildComponentsTable) … … 292 293 return { 293 294 ...buildDetails, 294 components: components.map(c => c.component), 295 components: components.map(c => ({ 296 ...c.component, 297 quantity: c.quantity 298 })), 295 299 reviews: reviews.map(r => ({ 296 300 username: r.username, … … 419 423 420 424 const existing = await tx 421 .select({ componentId: buildComponentsTable.componentId }) 425 .select({ 426 componentId: buildComponentsTable.componentId, 427 numComponents: buildComponentsTable.numComponents 428 }) 422 429 .from(buildComponentsTable) 423 430 .where(eq(buildComponentsTable.buildId, buildId)); … … 428 435 buildId: newBuild.id, 429 436 componentId: r.componentId, 437 numComponents: r.numComponents 430 438 })), 431 439 ); … … 545 553 const components = await tx 546 554 .select({ 547 componentId: buildComponentsTable.componentId 555 componentId: buildComponentsTable.componentId, 556 quantity: buildComponentsTable.numComponents 548 557 }) 549 558 .from(buildComponentsTable) … … 554 563 return { 555 564 build, 556 componentIds: components.map(c => c.componentId) 565 components: components.map(c => ({ 566 id: c.componentId, 567 quantity: c.quantity 568 })) 557 569 }; 558 570 }); -
database/drizzle/queries/components.ts
rad211d1 re03e6fb 196 196 id: componentsTable.id, 197 197 type: componentsTable.type, 198 quantity: buildComponentsTable.numComponents 198 199 }) 199 200 .from(buildComponentsTable) … … 231 232 componentsDetails.push({ 232 233 ...comp, 234 quantity: comp.quantity, 233 235 details: details 234 236 }); … … 256 258 257 259 if(!existingComponents) return null; 258 259 260 260 261 261 const existing = { … … 278 278 ...existing.networkAdapters, 279 279 ...existing.soundCards 280 ].filter(Boolean).length; 280 ].reduce((sum: number, c: any) => { 281 if (!c) return sum; 282 return sum + (c.quantity || 1); 283 }, 0); 281 284 282 285 const existingTDP = existingComponents.reduce((sum, c) => { 283 286 const tdp = c.details?.tdp ? Number(c.details.tdp) : 0; 284 return sum + tdp;287 return sum + (tdp * (c.quantity || 1)); 285 288 }, 0); 286 289 … … 456 459 if (existing.memory.length > 0) { 457 460 const totalModules = existing.memory.reduce((sum: number, m: any) => 458 sum + Number(m.details.modules), 0461 sum + (Number(m.details.modules) * m.quantity), 0 459 462 ); 460 463 const totalCapacity = existing.memory.reduce((sum: number, m: any) => 461 sum + Number(m.details.capacity), 0464 sum + (Number(m.details.capacity) * m.quantity), 0 462 465 ); 463 466 … … 704 707 if (!caseStorageFormFactor) return false; 705 708 706 const usedSlots = existing.storage .filter(707 (s: any) => s.details.formFactor === stor.formFactor708 ).length;709 const usedSlots = existing.storage 710 .filter((s: any) => s.details.formFactor === stor.formFactor) 711 .reduce((sum: number, s: any) => sum + s.quantity, 0); 709 712 710 713 return usedSlots < Number(caseStorageFormFactor.numSlots); … … 821 824 if (!storageFF) return false; 822 825 823 const usedSlots = existing.storage .filter(824 (s: any) => s.details.formFactor === stor.details.formFactor825 ).length;826 const usedSlots = existing.storage 827 .filter((s: any) => s.details.formFactor === stor.details.formFactor) 828 .reduce((sum: number, s: any) => sum + s.quantity, 0);; 826 829 827 830 return usedSlots <= Number(storageFF.numSlots); … … 963 966 .values({ 964 967 buildId, 965 componentId 968 componentId, 969 numComponents: 1 970 }) 971 .onConflictDoUpdate({ 972 target: [buildComponentsTable.buildId, buildComponentsTable.componentId], 973 set: { 974 numComponents: sql`${buildComponentsTable.numComponents} + 1` 975 } 966 976 }); 967 977 … … 969 979 .select({ 970 980 price: componentsTable.price, 981 quantity: buildComponentsTable.numComponents 971 982 }) 972 983 .from(buildComponentsTable) … … 979 990 ); 980 991 981 const totalPrice = buildComponents.reduce((sum, c) => sum + Number(c.price), 0); 992 const totalPrice = buildComponents.reduce((sum, c) => 993 sum + (Number(c.price) * c.quantity), 0 994 ); 982 995 983 996 await tx … … 1009 1022 if(!build || build.isApproved) return null; 1010 1023 1011 const result = await tx 1012 .delete(buildComponentsTable) 1024 const [existing] = await tx 1025 .select({ 1026 quantity: buildComponentsTable.numComponents 1027 }) 1028 .from(buildComponentsTable) 1013 1029 .where( 1014 1030 and( … … 1016 1032 eq(buildComponentsTable.componentId, componentId) 1017 1033 ) 1018 ); 1019 1020 if(result.rowCount === 0) return null; 1034 ) 1035 .limit(1); 1036 1037 if (!existing) return null; 1038 1039 if (existing.quantity > 1) { 1040 await tx 1041 .update(buildComponentsTable) 1042 .set({ 1043 numComponents: sql`${buildComponentsTable.numComponents} - 1` 1044 }) 1045 .where( 1046 and( 1047 eq(buildComponentsTable.buildId, buildId), 1048 eq(buildComponentsTable.componentId, componentId) 1049 ) 1050 ); 1051 } else { 1052 await tx 1053 .delete(buildComponentsTable) 1054 .where( 1055 and( 1056 eq(buildComponentsTable.buildId, buildId), 1057 eq(buildComponentsTable.componentId, componentId) 1058 ) 1059 ); 1060 } 1021 1061 1022 1062 const buildComponents = await tx 1023 1063 .select({ 1024 1064 price: componentsTable.price, 1065 quantity: buildComponentsTable.numComponents 1025 1066 }) 1026 1067 .from(buildComponentsTable) … … 1033 1074 ); 1034 1075 1035 const totalPrice = buildComponents.reduce((sum, c) => sum + Number(c.price), 0); 1076 const totalPrice = buildComponents.reduce((sum, c) => 1077 sum + (Number(c.price) * c.quantity), 0 1078 ); 1036 1079 1037 1080 await tx … … 1044 1087 ); 1045 1088 1046 return result;1089 return componentId; 1047 1090 }) 1048 1091 } -
database/drizzle/schema/builds.ts
rad211d1 re03e6fb 17 17 18 18 export const buildComponentsTable = pgTable("build_component", { 19 id: serial("id").primaryKey(),20 19 buildId: integer("build_id") 21 20 .notNull() … … 24 23 .notNull() 25 24 .references(() => componentsTable.id, { onDelete: "cascade", onUpdate: "cascade" }), 25 numComponents: integer("num_components") 26 .notNull() 27 .default(1), 26 28 }, 29 (t) => ({ 30 pk: primaryKey({ columns: [t.buildId, t.componentId] }), 31 }), 27 32 ); 28 33 -
database/migrations/0000_shallow_darkhawk.sql
rad211d1 re03e6fb 1 1 CREATE TABLE "build_component" ( 2 "id" serial PRIMARY KEY NOT NULL, 3 "build_id" integer NOT NULL, 4 "component_id" integer NOT NULL 2 "build_id" integer NOT NULL, 3 "component_id" integer NOT NULL, 4 "num_components" integer DEFAULT 1 NOT NULL, 5 CONSTRAINT "build_component_build_id_component_id_pk" PRIMARY KEY("build_id","component_id") 5 6 ); 6 7 --> statement-breakpoint -
database/migrations/meta/0000_snapshot.json
rad211d1 re03e6fb 1 1 { 2 "id": " 779a3882-651b-446d-8967-3728aeea2888",2 "id": "d1ab4ce8-e387-4d74-8d5b-511fdbe0a032", 3 3 "prevId": "00000000-0000-0000-0000-000000000000", 4 4 "version": "7", … … 9 9 "schema": "", 10 10 "columns": { 11 "id": {12 "name": "id",13 "type": "serial",14 "primaryKey": true,15 "notNull": true16 },17 11 "build_id": { 18 12 "name": "build_id", … … 26 20 "primaryKey": false, 27 21 "notNull": true 22 }, 23 "num_components": { 24 "name": "num_components", 25 "type": "integer", 26 "primaryKey": false, 27 "notNull": true, 28 "default": 1 28 29 } 29 30 }, … … 57 58 } 58 59 }, 59 "compositePrimaryKeys": {}, 60 "compositePrimaryKeys": { 61 "build_component_build_id_component_id_pk": { 62 "name": "build_component_build_id_component_id_pk", 63 "columns": [ 64 "build_id", 65 "component_id" 66 ] 67 } 68 }, 60 69 "uniqueConstraints": {}, 61 70 "policies": {}, -
database/migrations/meta/_journal.json
rad211d1 re03e6fb 6 6 "idx": 0, 7 7 "version": "7", 8 "when": 1769 188258136,9 "tag": "0000_ regular_kulan_gath",8 "when": 1769598141213, 9 "tag": "0000_shallow_darkhawk", 10 10 "breakpoints": true 11 11 }
Note:
See TracChangeset
for help on using the changeset viewer.
