AgentSkillsCN

Backend Queries

遵循SQL注入防范、N+1查询优化以及性能提升等最佳实践,编写高效且安全的数据库查询。无论是在PostgreSQL(Bun.sql、Prisma、Supabase)还是Firestore中,此技能都适用。在编写或修改数据库查询、实现数据获取逻辑、使用ORM(Prisma、TypeORM、Entity Framework)、调用Bun.sql原生驱动、查询Firestore集合,或实施缓存策略时,均可运用此技能。适用于处理服务文件(services/*.ts、repositories/*.ts、*Service.cs)、查询构建器实现、数据访问层,或任何涉及数据获取与操作的代码。此技能确保采用参数化查询以防范SQL注入(绝不直接插入用户输入),通过预加载避免N+1问题,仅选择性地获取所需列(杜绝SELECT *),在WHERE/JOIN/ORDER BY列上合理部署索引,对相关操作使用事务处理,为提升性能设定查询超时时间,对高耗时查询实施缓存,对于重复查询使用Bun.sql的预编译语句,并针对Firestore采用查询驱动建模,从而避免复杂的OR查询。

SKILL.md
--- frontmatter
name: Backend Queries
description: Write efficient and secure database queries following best practices for SQL injection prevention, N+1 query optimization, and performance for PostgreSQL (Bun.sql, Prisma, Supabase) and Firestore. Use this skill when writing or modifying database queries, implementing data fetching logic, working with ORMs (Prisma, TypeORM, Entity Framework), using Bun.sql native driver, querying Firestore collections, or implementing caching strategies. Apply when working on service files (services/*.ts, repositories/*.ts, *Service.cs), query builder implementations, data access layers, or any code that fetches or manipulates data. This skill ensures parameterized queries to prevent SQL injection (never interpolate user input), eager loading to prevent N+1 problems, selective column fetching (no SELECT *), strategic indexing on WHERE/JOIN/ORDER BY columns, transactions for related operations, query timeouts for performance, caching expensive queries, prepared statements with Bun.sql for repeated queries, and query-driven modeling for Firestore to avoid complex OR queries.

Backend Queries

When to use this skill:

  • When writing new database queries or data fetching operations
  • When working with ORM query builders (Prisma, TypeORM, Sequelize, Entity Framework)
  • When using Bun.sql native PostgreSQL driver for high-performance queries
  • When implementing service layer methods that query databases
  • When working on repository pattern implementations
  • When writing raw SQL queries for complex operations
  • When optimizing slow queries or addressing N+1 query problems
  • When implementing eager loading or query joins
  • When adding database indexes to improve query performance
  • When wrapping related operations in database transactions
  • When implementing caching strategies for frequently-accessed data
  • When setting query timeouts to prevent long-running queries
  • When working on files that contain data access logic (services/.ts, repositories/.ts, Service.cs, models/.py)
  • When querying Firestore collections and designing queries based on data model
  • When using prepared statements for repeated queries in Bun.sql

This Skill provides Claude Code with specific guidance on how to adhere to coding standards as they relate to how it should handle backend queries.

Instructions

For details, refer to the information provided in this file: backend queries