Skip to content
Snippets Groups Projects
Commit 4e86f581 authored by Prathviraj Suryakant Thokal's avatar Prathviraj Suryakant Thokal
Browse files

Derived enum value from DB

parent c8894c3f
Branches fSearch
No related tags found
1 merge request!4Added status logic in fuzzySearch
......@@ -139,6 +139,49 @@ function inferModelName(relationKey: string): string {
return relationKey.charAt(0).toUpperCase() + relationKey.slice(1);
}
const enumTypeCache: Record<string, Record<string, string>> = {};
/**
* Retrieves a mapping of column names to enum types for a given table.
* Caches the result to avoid repeated database queries.
*
* @param prisma - PrismaClient instance
* @param tableName - Name of the table (without schema)
* @returns A record mapping column names to their enum type names
*/
export async function getEnumTypes(
prisma: PrismaClient,
tableName: string
): Promise<Record<string, string>> {
if (enumTypeCache[tableName]) {
return enumTypeCache[tableName];
}
const query = `
SELECT a.attname AS column_name, t.typname AS enum_type
FROM pg_attribute a
JOIN pg_class c ON c.oid = a.attrelid
JOIN pg_namespace n ON n.oid = c.relnamespace
JOIN pg_type t ON a.atttypid = t.oid
JOIN pg_enum e ON t.oid = e.enumtypid
WHERE c.relname = $1
AND a.attnum > 0
AND NOT a.attisdropped
GROUP BY a.attname, t.typname
`;
const rows = await prisma.$queryRawUnsafe(query, tableName) as {
column_name: string;
enum_type: string;
}[];
const enumTypes = Object.fromEntries(
rows.map(({ column_name, enum_type }) => [column_name, enum_type])
);
enumTypeCache[tableName] = enumTypes;
return enumTypes;
}
interface FuzzySearchArgs {
searchFields: string[];
......@@ -218,10 +261,7 @@ interface FuzzySearchArgs {
const extraValues: any[] = [];
let paramIndex = 2;
// Map of enum fields to Postgres enum types for casting
const enumTypeMap: Record<string, string> = {
status: 'hospital_status', // Add more if you have other enum fields
};
const enumTypeMap = await getEnumTypes(prisma, model);
for (const [key, value] of Object.entries(where)) {
if (key === 'OR_CONDITION' && typeof value === 'object' && value !== null) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment