Changeset 59f8f5a for database/drizzle/queries/builds.ts
- Timestamp:
- 12/24/25 23:41:42 (7 months ago)
- Branches:
- main
- Children:
- 2c6d75a
- Parents:
- 3fca46c
- File:
-
- 1 edited
-
database/drizzle/queries/builds.ts (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
-
database/drizzle/queries/builds.ts
r3fca46c r59f8f5a 8 8 reviewsTable, usersTable 9 9 } from "../schema"; 10 import {eq, desc, and, sql, ilike } from "drizzle-orm";10 import {eq, desc, and, sql, ilike, asc} from "drizzle-orm"; 11 11 import {inArray} from "drizzle-orm/sql/expressions/conditions"; 12 12 … … 85 85 } 86 86 87 export async function getApprovedBuilds(db: Database, limit?: number, q?: string) {87 export async function getApprovedBuilds(db: Database, limit?: number, sort?: string, q?: string) { 88 88 let queryConditions = [eq(buildsTable.isApproved, true)]; 89 let sortConditions = []; 89 90 90 91 if (q) { … … 93 94 ); 94 95 } 96 97 switch(sort) { 98 case 'price_asc': 99 sortConditions.push( 100 asc(buildsTable.totalPrice) 101 ); 102 break; 103 case 'price_desc': 104 sortConditions.push( 105 desc(buildsTable.totalPrice) 106 ); 107 break; 108 case 'rating_desc': 109 sortConditions.push( 110 desc(sql<number>`COALESCE(AVG(${ratingBuildsTable.value}::float),0)`) 111 ); 112 break; 113 case 'oldest': 114 sortConditions.push( 115 asc(buildsTable.createdAt) 116 ); 117 break; 118 case 'newest': 119 sortConditions.push( 120 desc(buildsTable.createdAt) 121 ); 122 break; 123 default: 124 sortConditions.push( 125 desc(buildsTable.createdAt) 126 ); 127 break; 128 } 95 129 96 130 const approvedBuildsList = await db … … 104 138 }) 105 139 .from(buildsTable) 140 .leftJoin( 141 ratingBuildsTable, 142 eq(buildsTable.id, ratingBuildsTable.buildId) 143 ) 144 .groupBy( 145 buildsTable.userId, 146 buildsTable.name, 147 buildsTable.createdAt, 148 buildsTable.totalPrice 149 ) 106 150 .where( 107 151 and ( … … 110 154 ) 111 155 .orderBy( 112 desc(buildsTable.totalPrice)156 ...sortConditions 113 157 ) 114 158 .limit(limit || 100); // 100 placeholder
Note:
See TracChangeset
for help on using the changeset viewer.
