Added dbSearch util

This commit is contained in:
2025-12-06 20:31:59 +01:00
parent 1e71e54314
commit 765f3c42c1

27
src/utils/dbSearch.ts Normal file
View File

@@ -0,0 +1,27 @@
import { ilike, or } from "drizzle-orm"
/**
* Erzeugt eine OR-Suchbedingung über mehrere Spalten
*
* @param table - Drizzle Table Schema
* @param columns - Array der Spaltennamen (property names im schema)
* @param search - Suchbegriff
*/
export function buildSearchWhere(table: any, columns: string[], search: string) {
if (!search || !columns.length) return undefined
const term = `%${search.toLowerCase()}%`
const parts = columns
.map((colName) => {
const col = table[colName]
if (!col) return null
return ilike(col, term)
})
.filter(Boolean)
if (parts.length === 0) return undefined
// @ts-ignore
return or(...parts)
}