проектdocs/Agents/rules/data-prefer-select-over-include.md
data-prefer-select-over-include.md
Обновлён 1 апр. 2026 г., 12:41 · 0 комментариев
title: Prefer Select Over Include in Prisma Queries impact: HIGH impactDescription: Reduces data transfer and prevents data leaks tags: prisma, database, performance, security
Паспорт документа
- Статус документа: living standard
- Актуально на: 28 марта 2026 года
- Владелец: backend/platform-команда
- Пересмотр: при изменении инженерной практики, CI/CD, архитектурных правил или локального workflow
- Область применения: внутренние rule/reference-card документы для инженерной команды
- Связанные документы:
Prefer Select Over Include in Prisma Queries
Impact: HIGH
Using select instead of include fetches only the fields you need. This improves performance and prevents accidental exposure of sensitive data.
Incorrect (include fetches all fields):
const item = await this.prisma.item.findFirst({
where: { slug },
include: {
seller: true, // Gets ALL seller fields including internal ones
reviews: true, // Gets ALL review fields
}
});
Correct (select for specific fields):
const item = await this.prisma.$replica().item.findFirst({
where: { slug, status: 'PUBLISHED' },
select: {
id: true,
title: true,
slug: true,
description: true,
price: true,
imageUrl: true,
seller: {
select: {
id: true,
name: true,
logoUrl: true,
rating: true,
}
},
reviews: {
select: {
id: true,
rating: true,
comment: true,
createdAt: true,
user: {
select: {
id: true,
name: true,
}
}
},
take: 10,
orderBy: { createdAt: 'desc' },
}
}
});
Benefits:
- Performance: Smaller payloads, faster queries
- Security: Prevents accidental exposure of sensitive fields
- Clarity: Makes data requirements explicit
- Network: Less data transferred between DB and backend
Exception: Use include only when you genuinely need ALL fields from a relation, which is rare. Always prefer select.