Files
portal/src/lib/svelte-admin/prisma-delegate.ts
T
2026-02-23 10:21:42 -06:00

27 lines
835 B
TypeScript

import { prisma } from '$lib/db';
import type { AdminModelConfig } from './types';
function lcfirst(str: string): string {
return str.charAt(0).toLowerCase() + str.slice(1);
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function getDelegate(config: AdminModelConfig): any {
const key = lcfirst(config.modelName);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const delegate = (prisma as any)[key];
if (!delegate) {
throw new Error(`Prisma delegate not found for model: ${config.modelName} (key: ${key})`);
}
return delegate;
}
export function parseId(config: AdminModelConfig, raw: string): string | number {
if (config.idType === 'Int') {
const parsed = parseInt(raw, 10);
if (isNaN(parsed)) throw new Error(`Invalid integer ID: ${raw}`);
return parsed;
}
return raw;
}