Loading...
Loading...
Query PostgreSQL with Prisma 7 efficiently, using Prisma Client for mutations and selectively using raw SQL for complex/performant reads (SELECT/COUNT) when appropriate.
npx skill4agent add madsnyl/t3-template prisma-database-queryingfindMany/findFirst/findUniqueinclude/select$queryRaw$executeRawselectincludetakeskipdistinctgroupByincludein$queryRaw$executeRawreferences/PRISMA7_CORE_REFERENCES.md$transaction// Input: { workspaceId, cursorId?: string, take?: number }
const take = Math.min(input.take ?? 50, 200);
const items = await prisma.project.findMany({
where: { workspaceId: input.workspaceId },
orderBy: { createdAt: "desc" },
take: take + 1,
...(input.cursorId
? { cursor: { id: input.cursorId }, skip: 1 }
: {}),
select: {
id: true,
name: true,
slug: true,
createdAt: true,
},
});
const hasNextPage = items.length > take;
const page = hasNextPage ? items.slice(0, take) : items;
const nextCursor = hasNextPage ? page[page.length - 1]!.id : null;import { Prisma } from "@prisma/client";
const rows = await prisma.$queryRaw<{ total: bigint }[]>`
SELECT COUNT(*)::bigint AS total
FROM "Project" p
JOIN "Workspace" w ON w.id = p."workspaceId"
WHERE w.id = ${input.workspaceId}
AND p."createdAt" >= ${input.since}
`;
const total = Number(rows[0]?.total ?? 0n);await prisma.project.update({
where: { id: input.projectId },
data: { name: input.name, slug: input.slug },
});selectinclude$executeRawskiptake