From harness-claude
Filters and sorts Prisma queries using where with AND/OR/NOT/relations, orderBy, cursor/offset pagination, and aggregations for dynamic searches and lists.
npx claudepluginhub intense-visions/harness-engineering --plugin harness-claudeThis skill uses the workspace's default tool permissions.
> Filter and sort Prisma queries with where, AND/OR/NOT, orderBy, and cursor/offset pagination
Executes type-safe Prisma Client queries: findUnique/findMany for reads, create/update/delete/upsert for writes, select fields, include relations. Use for database CRUD operations.
Generates optimized SQL queries for PostgreSQL, MySQL, SQLite and NoSQL for MongoDB, DynamoDB, Redis; supports ORMs like Prisma. Explains plans, indexes, and performance optimizations.
Provides expert guidance on Prisma ORM schema design, migrations, query optimization, relations modeling, and database operations for PostgreSQL, MySQL, SQLite.
Share bugs, ideas, or general feedback.
Filter and sort Prisma queries with where, AND/OR/NOT, orderBy, and cursor/offset pagination
where:const users = await prisma.user.findMany({
where: { role: 'ADMIN', isActive: true },
});
Multiple top-level fields are implicitly AND-ed.
equals, not, gt, gte, lt, lte, in, notIn:const recentPosts = await prisma.post.findMany({
where: {
createdAt: { gte: new Date('2024-01-01') },
viewCount: { gt: 100 },
},
});
contains, startsWith, endsWith. Add mode: 'insensitive' for case-insensitive matching (PostgreSQL only):const results = await prisma.user.findMany({
where: { name: { contains: 'john', mode: 'insensitive' } },
});
AND, OR, NOT for complex logic:const filtered = await prisma.post.findMany({
where: {
OR: [{ title: { contains: search } }, { content: { contains: search } }],
AND: { published: true },
NOT: { authorId: blockedUserId },
},
});
some, every, none for to-many relations and is, isNot for to-one:const usersWithPublishedPosts = await prisma.user.findMany({
where: { posts: { some: { published: true } } },
});
orderBy — pass one or multiple sort fields:const sorted = await prisma.post.findMany({
orderBy: [{ pinned: 'desc' }, { createdAt: 'desc' }],
});
const byPostCount = await prisma.user.findMany({
orderBy: { posts: { _count: 'desc' } },
});
skip and take:const page = await prisma.post.findMany({
skip: (pageNumber - 1) * pageSize,
take: pageSize,
orderBy: { createdAt: 'desc' },
});
cursor with skip: 1 to exclude the cursor record:const nextPage = await prisma.post.findMany({
cursor: { id: lastPostId },
skip: 1,
take: 20,
orderBy: { createdAt: 'desc' },
});
aggregate, groupBy, and count:const stats = await prisma.post.aggregate({
_avg: { viewCount: true },
_max: { viewCount: true },
where: { published: true },
});
Prisma's filtering and sorting are fully type-safe. The available filter operators depend on the field type — contains only appears on String fields, gt/lt only on numeric and DateTime fields.
Offset vs cursor pagination: Offset (skip/take) is simpler to implement but degrades at high offsets because the database still scans skipped rows. Cursor pagination (cursor/take) uses an indexed value (usually id or createdAt) and performs consistently regardless of position. Use cursor pagination for infinite scroll or large datasets.
Dynamic filters: Build where objects conditionally to avoid unnecessary filters:
const where: Prisma.PostWhereInput = { published: true };
if (search) where.title = { contains: search, mode: 'insensitive' };
if (authorId) where.authorId = authorId;
const posts = await prisma.post.findMany({ where });
null filtering: Use equals: null to find null values and not: null to exclude them. With strictNullChecks, Prisma distinguishes between "field is null" and "field is not provided in the filter."
Aggregation limitations:
groupBy requires all non-aggregated fields in the by arraycount returns number, not BigInt, even on tables with BigInt IDsPerformance notes:
mode: 'insensitive' on PostgreSQL uses ILIKE which cannot use standard B-tree indexes — create a GIN/GiST trigram index or a functional index with lower() for production workloadshttps://prisma.io/docs/orm/prisma-client/queries/filtering-and-sorting