Database Patterns
This skill provides practical patterns for database access in backend services.
Core rules
- •Keep ORM usage behind a repository boundary when queries become non-trivial.
- •Prefer explicit pagination and filtering.
- •Use transactions for multi-write operations.
- •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