formated project with prettier
This commit is contained in:
@@ -1,47 +1,47 @@
|
||||
'use server';
|
||||
import { PrismaClient } from '@repo/db';
|
||||
"use server";
|
||||
import { PrismaClient } from "@repo/db";
|
||||
|
||||
const prisma = new PrismaClient();
|
||||
|
||||
export async function getData(
|
||||
model: keyof PrismaClient,
|
||||
limit: number,
|
||||
offset: number,
|
||||
searchTerm: string,
|
||||
searchFields: string[],
|
||||
filter?: Record<string, any>,
|
||||
include?: Record<string, boolean>
|
||||
model: keyof PrismaClient,
|
||||
limit: number,
|
||||
offset: number,
|
||||
searchTerm: string,
|
||||
searchFields: string[],
|
||||
filter?: Record<string, any>,
|
||||
include?: Record<string, boolean>,
|
||||
) {
|
||||
if (!model || !prisma[model]) {
|
||||
return { data: [], total: 0 };
|
||||
}
|
||||
if (!model || !prisma[model]) {
|
||||
return { data: [], total: 0 };
|
||||
}
|
||||
|
||||
const formattedId = searchTerm.match(/^VAR(\d+)$/)?.[1];
|
||||
const formattedId = searchTerm.match(/^VAR(\d+)$/)?.[1];
|
||||
|
||||
const where = searchTerm
|
||||
? {
|
||||
OR: [
|
||||
formattedId ? { id: formattedId } : undefined,
|
||||
...searchFields.map((field) => ({
|
||||
[field]: { contains: searchTerm },
|
||||
})),
|
||||
].filter(Boolean),
|
||||
...filter,
|
||||
}
|
||||
: { ...filter };
|
||||
const where = searchTerm
|
||||
? {
|
||||
OR: [
|
||||
formattedId ? { id: formattedId } : undefined,
|
||||
...searchFields.map((field) => ({
|
||||
[field]: { contains: searchTerm },
|
||||
})),
|
||||
].filter(Boolean),
|
||||
...filter,
|
||||
}
|
||||
: { ...filter };
|
||||
|
||||
if (!prisma[model]) {
|
||||
return { data: [], total: 0 };
|
||||
}
|
||||
if (!prisma[model]) {
|
||||
return { data: [], total: 0 };
|
||||
}
|
||||
|
||||
const data = await (prisma[model] as any).findMany({
|
||||
where,
|
||||
take: limit,
|
||||
skip: offset,
|
||||
include,
|
||||
});
|
||||
const data = await (prisma[model] as any).findMany({
|
||||
where,
|
||||
take: limit,
|
||||
skip: offset,
|
||||
include,
|
||||
});
|
||||
|
||||
const total = await (prisma[model] as any).count({ where });
|
||||
const total = await (prisma[model] as any).count({ where });
|
||||
|
||||
return { data, total };
|
||||
return { data, total };
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user