AgentSkillsCN

database-patterns

数据库访问模式(ORM 使用、存储库、事务处理与性能优化)。关键词:数据库、ORM、Prisma、事务、查询、N+1 问题。

SKILL.md
--- frontmatter
name: database-patterns
description: "Database access patterns (ORM usage, repositories, transactions, and performance). Keywords: database, ORM, prisma, transactions, queries, N+1."

Database Patterns

This skill provides practical patterns for database access in backend services.


Core rules

  1. Keep ORM usage behind a repository boundary when queries become non-trivial.
  2. Prefer explicit pagination and filtering.
  3. Use transactions for multi-write operations.
  4. Capture slow queries with spans/traces (where supported).

ORM client lifecycle

Create the DB client once per process and reuse it.


Transactions (example)

ts
await db.$transaction(async (tx) => {
  await tx.user.create({ data: input });
  await tx.auditLog.create({ data: { action: "user.create" } });
});

Performance notes

  • Avoid N+1 query patterns (use joins/includes carefully).
  • Add indexes for high-cardinality query patterns.
  • Prefer selecting only needed fields for large tables.

Related Skills

  • services-and-repositories
  • sentry-and-monitoring