Changeset e7f1bfa for database


Ignore:
Timestamp:
12/25/25 03:39:56 (7 months ago)
Author:
Tome <gjorgievtome@…>
Branches:
main
Children:
898cf68
Parents:
83fb5e2
Message:

implement forge features

Location:
database/drizzle
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • database/drizzle/config/componentFieldConfig.ts

    r83fb5e2 re7f1bfa  
     1import {
     2    cablesTable, caseMoboFormFactorsTable, casePsFormFactorsTable,
     3    caseStorageFormFactorsTable, coolerCPUSocketsTable, coolersTable,
     4    CPUTable,
     5    GPUTable, memoryCardsTable,
     6    memoryTable, motherboardsTable,
     7    networkAdaptersTable,
     8    networkCardsTable, opticalDrivesTable, pcCasesTable, powerSupplyTable,
     9    soundCardsTable, storageTable
     10} from "../schema";
     11
     12export type ComponentConfig<SelectType = any, InsertType = any> = {
     13    table: any;
     14    multiTables?: {
     15        storageFormFactors?: typeof caseStorageFormFactorsTable;
     16        psFormFactors?: typeof casePsFormFactorsTable;
     17        moboFormFactors?: typeof caseMoboFormFactorsTable;
     18        cpuSockets?: typeof coolerCPUSocketsTable;
     19    }
     20};
     21
    122export type ComponentType =
    223    | 'cpu'
     
    1435    | 'network_adapter'
    1536    | 'network_card';
     37
     38export const typeConfigMap: Record<ComponentType, ComponentConfig> = {
     39    cpu: { table: CPUTable },
     40    gpu: { table: GPUTable },
     41    memory: { table: memoryTable },
     42    storage: { table: storageTable },
     43    power_supply: { table: powerSupplyTable },
     44    motherboard: { table: motherboardsTable },
     45    case: {
     46        table: pcCasesTable,
     47        multiTables: {
     48            storageFormFactors: caseStorageFormFactorsTable,
     49            psFormFactors: casePsFormFactorsTable,
     50            moboFormFactors: caseMoboFormFactorsTable,
     51        },
     52    },
     53    cooler: {
     54        table: coolersTable,
     55        multiTables: {
     56            cpuSockets: coolerCPUSocketsTable,
     57        },
     58    },
     59    memory_card: { table: memoryCardsTable },
     60    optical_drive: { table: opticalDrivesTable },
     61    sound_card: { table: soundCardsTable },
     62    cables: { table: cablesTable },
     63    network_adapter: { table: networkAdaptersTable },
     64    network_card: { table: networkCardsTable },
     65};
    1666
    1767export const requiredFields: Record<ComponentType, string[]> = {
  • database/drizzle/queries/builds.ts

    r83fb5e2 re7f1bfa  
    1212
    1313export async function getPendingBuilds(db: Database) {
    14     const pendingBuildsList = await db
     14    const pendingBuilds = await db
    1515        .select({
    1616            id: buildsTable.id,
     
    2525        );
    2626
    27     return pendingBuildsList;
     27    return pendingBuilds;
    2828}
    2929
    3030export async function getUserBuilds(db: Database, userId: number) {
    31     const userBuildsList = await db
     31    const userBuilds = await db
    3232        .select({
    3333            id: buildsTable.id,
     
    4242        );
    4343
    44     return userBuildsList;
     44    return userBuilds;
    4545}
    4646
     
    6565
    6666export async function getFavoriteBuilds(db: Database, userId: number) {
    67     const favoriteBuildsList = await db
     67    const favoriteBuilds = await db
    6868        .select({
    6969            id: buildsTable.id,
     
    8282        );
    8383
    84     return favoriteBuildsList;
     84    return favoriteBuilds;
    8585}
    8686
     
    128128        }
    129129
    130     const approvedBuildsList = await db
     130    const approvedBuilds = await db
    131131        .select({
    132132            id: buildsTable.id,
     
    159159        .limit(limit || 100); // 100 placeholder
    160160
    161     return approvedBuildsList;
     161    return approvedBuilds;
    162162}
    163163
  • database/drizzle/queries/components.ts

    r83fb5e2 re7f1bfa  
    1212} from "../schema";
    1313import {and, desc, eq, ilike} from "drizzle-orm";
     14import {typeConfigMap, ComponentType} from "../config/componentFieldConfig";
     15import {AnyPgColumn} from "drizzle-orm/pg-core";
    1416export async function getAllComponents(db: Database, limit?: number, componentType?: string, q?: string) {
    1517    let queryConditions = [];
     
    2729    }
    2830
    29     const componentsList = await db
     31    const components = await db
    3032        .select({
    3133            id: componentsTable.id,
     
    4749        .limit(limit || 100); // 100 placeholder
    4850
    49     return componentsList;
     51    return components;
    5052}
    5153
     
    6163    if(!component) return null;
    6264
    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));
    11683    }
    11784
    11885    return {
    11986        ...component,
    120         details: details || null
     87        details: details
    12188    };
    12289}
     
    139106        const componentId = newComponent.id;
    140107
    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            );
    319149        }
    320150
     
    323153}
    324154
    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)
     155export 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)
    333191            .where(
    334                 eq(buildsTable.id, buildId)
     192                eq(config.table.componentId, comp.id)
    335193            )
    336194            .limit(1);
    337195
    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
     215export async function getCompatibleComponents(db: Database, buildId: number, componentType: string) {
     216    // TO BE IMPLEMENTED
    341217    return null;
    342     });
    343218}
    344219
Note: See TracChangeset for help on using the changeset viewer.