home / skills / tlabs-xyz / tbtc-v2-performance / backend-queries

backend-queries skill

/.claude/skills/backend-queries

This skill helps you write secure, efficient backend queries using parameterized queries, ORM builders, and proper optimization strategies.

npx playbooks add skill tlabs-xyz/tbtc-v2-performance --skill backend-queries

Review the files below or copy the command above to add this skill to your agents.

Files (1)
SKILL.md
1.9 KB
---
name: Backend Queries
description: Write efficient, secure database queries using parameterized queries, eager loading, and performance optimizations. Use this skill when writing database queries, implementing data retrieval logic, using ORM query builders, writing raw SQL queries, implementing eager loading or joins, preventing N+1 query problems, implementing query filters or search functionality, using transactions for data consistency, implementing pagination queries, optimizing slow queries, implementing query caching, working with query builder methods, or writing repository/DAO layer code. Use when working with SQL queries, ORM query builders like Sequelize, TypeORM, Prisma, SQLAlchemy, ActiveRecord, Django ORM query APIs, or raw database access code.
---

## When to use this skill

- When writing database queries using ORM query builders
- When writing raw SQL queries
- When implementing data retrieval or search logic
- When implementing eager loading or joins to prevent N+1 queries
- When optimizing slow or inefficient queries
- When implementing query filters, sorting, or pagination
- When using transactions to ensure data consistency
- When implementing query caching for expensive queries
- When working with query builder methods (where, select, join, etc.)
- When writing repository, service, or DAO layer code
- When preventing SQL injection with parameterized queries
- When working with complex queries involving multiple tables
- When implementing query timeouts or performance monitoring
- When working with ORMs like Sequelize, TypeORM, Prisma, SQLAlchemy, ActiveRecord, Django ORM

# Backend Queries

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](../../../agent-os/standards/backend/queries.md)

Overview

This skill guides writing efficient, secure database queries and data-access logic in TypeScript backends. It emphasizes parameterized queries, eager loading, transaction safety, and query performance optimizations to reduce latency and prevent common pitfalls like N+1 queries and SQL injection. Use it to standardize repository/DAO code and ORM usage across services.

How this skill works

The skill inspects query intent and suggests concrete query patterns: parameterized SQL, ORM query-builder calls, eager-loading or join strategies, and pagination/caching techniques. It recommends specific performance improvements such as indexing hints, selecting only needed columns, using transactions where required, and applying query timeouts or monitoring. Outputs include code snippets, refactor suggestions, and checklist-style validations tailored to TypeScript ORMs like Prisma, TypeORM, and Sequelize.

When to use it

  • Writing or refactoring SQL or ORM-based data retrieval logic
  • Implementing eager loading or joins to avoid N+1 problems
  • Adding filters, sorting, pagination, or search features
  • Protecting queries against SQL injection with parameterization
  • Optimizing slow queries, adding indexes, or caching results

Best practices

  • Always use parameterized queries or ORM parameter binding to prevent SQL injection
  • Load only required columns and prefer eager loading or explicit joins to prevent N+1 queries
  • Wrap multi-step updates in transactions and handle rollbacks on failure
  • Add pagination, limits, and sensible defaults for list endpoints
  • Measure query plans and add indexes based on real slow-query evidence, not guesses
  • Cache expensive, read-heavy queries and invalidate on writes

Example use cases

  • Refactor a REST endpoint from multiple per-record queries into a single eager-loaded ORM query to eliminate N+1
  • Implement cursor-based pagination for large result sets with stable sort keys
  • Convert string interpolation SQL into parameterized prepared statements to remove injection risk
  • Add a transactional update across multiple tables with rollback on any constraint failure
  • Instrument and optimize a slow JOIN by analyzing EXPLAIN output and adding an index

FAQ

Which is better: eager loading or lazy loading?

Prefer eager loading when you know related data is required for the response to avoid N+1 queries. Use lazy loading only when related data is rarely needed to reduce unnecessary joins.

How do I choose pagination style?

Use offset/limit for simple UIs and small datasets. Use cursor-based pagination for large or frequently changing datasets to ensure stable and performant paging.