prisma-client-api
prisma/skills
Prisma Client API 参考文档,涵盖模型查询、过滤器、运算符和客户端方法。在编写数据库查询、使用 CRUD 操作、过滤数据或配置 Prisma Client 时可参考本文档。 触发器适用于“prisma query”、“findMany”、“create”、“update”、“delete”和“$transaction”。
...展开全部关于prisma-client-api
prisma-client-api 技能为使用 Prisma Client 与数据库交互提供了全面的参考指南。它简化了在 Prisma 项目中构建查询、执行 CRUD 操作、应用过滤器、管理关系以及处理事务的流程。 通过提供关于客户端实例化、模型查询、查询选项以及原始 SQL 执行的详细指导,该技能可帮助开发人员在处理关系型数据库时减少错误并提高效率。它对于确保查询结构正确,以及安全有效地处理嵌套写入和事务等高级操作尤为有用。
本技能涵盖广泛的功能,包括 findUnique、findMany、create、update、delete、upsert 等模型查询方法,以及 count、aggregate 和 groupBy 等聚合函数。它还为查询选项提供了全面支持,允许开发人员进行过滤、选择、包含、排除、排序、分页以及强制使用唯一值。 此外,还涵盖了 Prisma Client 用于生命周期管理、事件订阅、原始 SQL 执行以及客户端扩展的方法。该技能确保原始 SQL 查询的安全执行,并提供有关使用数组式和交互式事务的指导,以维护数据完整性。
目标用户包括使用 JavaScript 或 TypeScript 且需要通过 Prisma 高效与数据库交互的后端开发人员、数据库管理员和全栈工程师。 典型用例包括构建 Web 应用程序、API 和微服务,其中精确的数据检索、操作和聚合至关重要。该技能还适用于需要在生产环境中实现复杂关系逻辑或执行批处理操作时,寻求一致查询模式和最佳实践标准参考的团队。
常见问题
如何使用自定义适配器实例化 Prisma Client?
您可以通过导入 PrismaClient 和您的适配器,然后将适配器配置传递给客户端构造函数来实例化 Prisma Client,具体操作如提供的 TypeScript 示例所示。
Prisma Client 支持哪些数据库操作?
Prisma Client 支持完整的 CRUD 操作,包括 findUnique、findMany、create、createMany、update、updateMany、upsert、delete 和 deleteMany。它还支持聚合和分组方法。
Prisma Client 能否处理事务?
是的,Prisma Client 通过 $transaction 方法同时支持基于数组和交互式的事务,允许在单个事务内安全地执行多个查询。
是否支持原始 SQL,使用是否安全?
Prisma Client 提供了 $queryRaw 和 $executeRaw 方法来执行原始 SQL 查询。使用这些方法时应格外谨慎,以避免 SQL 注入;文档中提供了安全使用的指导。
使用此 Prisma Client API 技能有哪些要求?
您需要一个已生成客户端的 Prisma 项目、一个受支持的数据库以及一个合适的适配器。客户端实例化和查询执行需要 TypeScript 或 JavaScript 环境。
Prisma Client API Reference
Complete API reference for Prisma Client. This skill provides guidance on model queries, filtering, relations, and client methods for current Prisma projects.
When to Apply
Reference this skill when:
- Writing database queries with Prisma Client
- Performing CRUD operations (create, read, update, delete)
- Filtering and sorting data
- Working with relations
- Using transactions
- Configuring client options
Rule Categories by Priority
| Priority | Category | Impact | Prefix |
|---|---|---|---|
| 1 | Client Construction | HIGH | constructor |
| 2 | Model Queries | CRITICAL | model-queries |
| 3 | Query Shape | HIGH | query-options |
| 4 | Filtering | HIGH | filters |
| 5 | Relations | HIGH | relations |
| 6 | Transactions | CRITICAL | transactions |
| 7 | Raw SQL | CRITICAL | raw-queries |
| 8 | Client Methods | MEDIUM | client-methods |
Quick Reference
constructor-PrismaClientsetup, adapter wiring, logging, and SQL commenter pluginsmodel-queries- CRUD operations and bulk operationsquery-options-select,include,omit, sort, paginationfilters- scalar and logical filter operatorsrelations- relation reads and nested writestransactions- array and interactive transaction patternsraw-queries-$queryRawand$executeRawsafetyclient-methods- lifecycle methods, extensions, andsatisfiespatterns forprisma-client
Client Instantiation
import { PrismaClient } from '../generated/client'import { PrismaPg } from '@prisma/adapter-pg'const adapter = new PrismaPg({ connectionString: process.env.DATABASE_URL})const prisma = new PrismaClient({ adapter })
Model Query Methods
| Method | Description |
|---|---|
findUnique() | Find one record by unique field |
findUniqueOrThrow() | Find one or throw error |
findFirst() | Find first matching record |
findFirstOrThrow() | Find first or throw error |
findMany() | Find multiple records |
create() | Create a new record |
createMany() | Create multiple records |
createManyAndReturn() | Create multiple and return them |
update() | Update one record |
updateMany() | Update multiple records |
updateManyAndReturn() | Update multiple and return them |
upsert() | Update or create record |
delete() | Delete one record |
deleteMany() | Delete multiple records |
count() | Count matching records |
aggregate() | Aggregate values (sum, avg, etc.) |
groupBy() | Group and aggregate |
Query Options
| Option | Description |
|---|---|
where | Filter conditions |
select | Fields to include |
include | Relations to load |
omit | Fields to exclude |
orderBy | Sort order |
take | Limit results |
skip | Skip results (pagination) |
cursor | Cursor-based pagination |
distinct | Unique values only |
Client Methods
| Method | Description |
|---|---|
$connect() | Explicitly connect to database |
$disconnect() | Disconnect from database |
$transaction() | Execute transaction |
$queryRaw() | Execute raw SQL query |
$executeRaw() | Execute raw SQL command |
$on() | Subscribe to events |
$extends() | Add extensions |
Quick Examples
Find records
// Find by unique fieldconst user = await prisma.user.findUnique({ where: { email: '[email protected]' }})// Find with filterconst users = await prisma.user.findMany({ where: { role: 'ADMIN' }, orderBy: { createdAt: 'desc' }, take: 10})
Create records
const user = await prisma.user.create({ data: { email: '[email protected]', name: 'Alice', posts: { create: { title: 'Hello World' } } }, include: { posts: true }})
Update records
const user = await prisma.user.update({ where: { id: 1 }, data: { name: 'Alice Smith' }})
Delete records
await prisma.user.delete({ where: { id: 1 }})
Transactions
const [user, post] = await prisma.$transaction([ prisma.user.create({ data: { email: '[email protected]' } }), prisma.post.create({ data: { title: 'Hello', authorId: 1 } })])
Rule Files
Detailed API documentation:
references/constructor.md - PrismaClient constructor optionsreferences/model-queries.md - CRUD operationsreferences/query-options.md - select, include, omit, where, orderByreferences/filters.md - Filter conditions and operatorsreferences/relations.md - Relation queries and nested operationsreferences/transactions.md - Transaction APIreferences/raw-queries.md - $queryRaw, $executeRawreferences/client-methods.md - $connect, $disconnect, $on, $extendsFilter Operators
| Operator | Description |
|---|---|
equals | Exact match |
not | Not equal |
in | In array |
notIn | Not in array |
lt, lte | Less than |
gt, gte | Greater than |
contains | String contains |
startsWith | String starts with |
endsWith | String ends with |
mode | Case sensitivity |
Relation Filters
| Operator | Description |
|---|---|
some | At least one related record matches |
every | All related records match |
none | No related records match |
is | Related record matches (1-to-1) |
isNot | Related record doesn't match |
Resources
- Prisma Client API Reference
- CRUD Operations
- Filtering and Sorting
How to Use
Pick the category from the table above, then open the matching reference file for implementation details and examples.





首页
