Database Queries

Common Prisma query patterns and optimization techniques including filtering, selecting, pagination, aggregation, sorting, raw SQL, and search.

What's Included#

  • Basic queries (findUnique, findFirst, findMany)
  • Filtering patterns (AND, OR, NOT, nested relation filters)
  • Field selection with select and include for relations
  • Offset and cursor-based pagination with total counts
  • Aggregation functions (count, sum, avg, min, max, groupBy)
  • Single and multi-field sorting, including relation ordering
  • Raw SQL queries and executions with tagged templates
  • Query optimization (select vs include, avoiding N+1 queries)
  • Search patterns (case-insensitive, multi-field, full-text)
  • Reusable query function pattern with composable options

Usage#

Via CLI#

Loading code block...

Via AI Assistant#

Ask your AI assistant:

  • "Use the database queries pattern from Bootspring"
  • "Apply the Bootspring query patterns to my project"

Key Considerations#

  • Add @@index on frequently filtered and joined columns to improve performance
  • Use select instead of include when you only need specific fields to reduce data transfer
  • Avoid N+1 queries by using include or select with relations in a single query
  • Prefer cursor-based pagination over offset for large datasets
  • Enable query logging in development to identify slow queries