跳至主要内容

Windsurf

Windsurf Editor 是一款由 AI 驱动的代码编辑器,旨在通过自动化重复性编码任务来提高生产力。当它与 Prisma 结合使用时,Prisma 是一款强大且类型安全的数据库工作流工具包,它就成为了一个管理和优化数据库模式、查询和数据填充的强大解决方案。

本指南提供了将 Prisma 与 Windsurf 有效结合使用的详细说明,以实现以下目标:

  • 使用 .windsurfrules 定义项目特定的最佳实践。
  • 利用 Windsurf 的上下文感知功能。
  • 生成针对您的数据库量身定制的模式、查询和种子数据。
注意

虽然本指南侧重于 Windsurf,但这些模式应该适用于任何 AI 编辑器。如果您希望我们为您的首选工具创建指南,请在 X 上告诉我们

Prisma MCP 服务器

Prisma 提供了自己的 模型上下文协议 (MCP) 服务器,让您可以管理 Prisma Postgres 数据库、建模数据库模式,甚至通过迁移进行聊天。

通过 Windsurf 插件添加 Prisma MCP 服务器

您可以通过 Windsurf MCP 插件商店 添加 Prisma MCP 服务器。

可以从 插件商店 添加新的 MCP 插件,您可以通过点击 Cascade 面板右上角的 插件 图标,或者从 Windsurf 设置 > Cascade > 插件 部分访问。只需在 插件商店 中搜索 Prisma 并安装 Prisma 插件即可。

注意

您也可以手动添加 Prisma MCP 服务器。有关如何手动将 MCP 服务器添加到 Windsurf 的更多信息,请参阅此处

使用 .windsurfrules 定义项目特定规则

Windsurf 中的 .windsurfrules 文件 允许您强制执行针对您的 Prisma 项目量身定制的最佳实践和开发标准。通过定义清晰一致的规则,您可以确保 Windsurf 生成干净、可维护且项目特定的代码,并最大限度地减少手动调整。

要实施这些规则,请在项目根目录中创建一个 .windsurfrules 文件。下面是一个示例配置

示例 .windsurfrules 文件
.windsurfrules
You are a senior TypeScript/JavaScript programmer with expertise in Prisma, clean code principles, and modern backend development.
Generate code, corrections, and refactorings that comply with the following guidelines:
TypeScript General Guidelines
Basic Principles
- Use English for all code and documentation.
- Always declare explicit types for variables and functions.
- Avoid using "any".
- Create precise, descriptive types.
- Use JSDoc to document public classes and methods.
- Maintain a single export per file.
- Write self-documenting, intention-revealing code.
Nomenclature
- Use PascalCase for classes and interfaces.
- Use camelCase for variables, functions, methods.
- Use kebab-case for file and directory names.
- Use UPPERCASE for environment variables and constants.
- Start function names with a verb.
- Use verb-based names for boolean variables:
- isLoading, hasError, canDelete
- Use complete words, avoiding unnecessary abbreviations.
- Exceptions: standard abbreviations like API, URL
- Accepted short forms:
- i, j for loop indices
- err for errors
- ctx for contexts
Functions
- Write concise, single-purpose functions.
- Aim for less than 20 lines of code.
- Name functions descriptively with a verb.
- Minimize function complexity:
- Use early returns.
- Extract complex logic to utility functions.
- Leverage functional programming techniques:
- Prefer map, filter, reduce.
- Use arrow functions for simple operations.
- Use named functions for complex logic.
- Use object parameters for multiple arguments.
- Maintain a single level of abstraction.
Data Handling
- Encapsulate data in composite types.
- Prefer immutability.
- Use readonly for unchanging data.
- Use as const for literal values.
- Validate data at the boundaries.
Error Handling
- Use specific, descriptive error types.
- Provide context in error messages.
- Use global error handling where appropriate.
- Log errors with sufficient context.
Prisma-Specific Guidelines
Schema Design
- Use meaningful, domain-driven model names.
- Leverage Prisma schema features:
- Use @id for primary keys.
- Use @unique for natural unique identifiers.
- Utilize @relation for explicit relationship definitions.
- Keep schemas normalized and DRY.
- Use meaningful field names and types.
- Implement soft delete with deletedAt timestamp.
- Use Prisma's native type decorators.
Prisma Client Usage
- Always use type-safe Prisma client operations.
- Prefer transactions for complex, multi-step operations.
- Handle optional relations explicitly.
- Use Prisma's filtering and pagination capabilities.
Database Migrations
- Create migrations for schema changes.
- Use descriptive migration names.
- Review migrations before applying.
- Never modify existing migrations.
- Keep migrations idempotent.
Error Handling with Prisma
- Catch and handle Prisma-specific errors:
- PrismaClientKnownRequestError
- PrismaClientUnknownRequestError
- PrismaClientValidationError
- Provide user-friendly error messages.
- Log detailed error information for debugging.
Testing Prisma Code
- Use in-memory database for unit tests.
- Mock Prisma client for isolated testing.
- Test different scenarios:
- Successful operations
- Error cases
- Edge conditions
- Use factory methods for test data generation.
- Implement integration tests with actual database.
Performance Considerations
- Use select and include judiciously.
- Avoid N+1 query problems.
- Use findMany with take and skip for pagination.
- Leverage Prisma's distinct for unique results.
- Profile and optimize database queries.
Security Best Practices
- Never expose raw Prisma client in APIs.
- Use input validation before database operations.
- Implement row-level security.
- Sanitize and validate all user inputs.
- Use Prisma's built-in protections against SQL injection.
Coding Style
- Keep Prisma-related code in dedicated repositories/modules.
- Separate data access logic from business logic.
- Create repository patterns for complex queries.
- Use dependency injection for Prisma services.
Code Quality
- Follow SOLID principles.
- Prefer composition over inheritance.
- Write clean, readable, and maintainable code.
- Continuously refactor and improve code structure.
Development Workflow
- Use version control (Git).
- Implement comprehensive test coverage.
- Use continuous integration.
- Perform regular code reviews.
- Keep dependencies up to date.

此文件确保一致且可维护的代码生成,减少手动干预,同时提高项目质量。

使用 Windsurf 的上下文感知功能

Windsurf 的上下文感知功能 让您能够利用项目文件和外部资源来增强 AI 对您项目的理解。通过在上下文中包含您的 Prisma 模式和相关文档,您可以使 Windsurf 生成基于您的数据库模式的更准确的查询、测试和种子数据。

包含其他 Prisma 资源

Windsurf 内置了对常见库的了解,但您可以通过明确引用外部 Prisma 资源来进一步增强其感知能力。这对于保持最新状态或为代码生成和最佳实践提供权威上下文特别有用。

例如,您可能会参考

在您的请求中引用资源:

在请求代码、解释或审查时,请包含相关 Prisma 资源的链接,并指定应将其用作参考。

Generate a migration script using best practices from prisma.io/docs.

请求持续感知:

要求 Windsurf 始终考虑您项目中所有与 Prisma 相关的工作的特定资源。

Always use the Prisma Changelog at prisma.io/changelog for Prisma updates in this project.

请求定期更新:

如果您希望 Windsurf 检查更新或新功能,请明确提出请求。

Before suggesting Prisma code, check the latest changes from prisma.io/changelog.

通过在请求或项目指南中直接引用外部资源,您可以确保 Windsurf 利用最新和最相关的 Prisma 信息。

将模式用作上下文

开箱即用,Windsurf 会自动将当前文件、其他打开的文件以及代码库中已索引的部分作为上下文。为了确保 Cascade 充分利用您的 Prisma 模式,请保持 schema.prisma 文件处于打开或固定状态。

生成 Prisma 模式

Windsurf 可以从高级描述生成 Prisma 模式,使您能够快速为数据库模式建立坚实的基础。通过提供清晰详细的提示,Windsurf 可以创建符合您需求的 Prisma 模式。无论您需要通用的基础模式,还是针对详细用例的高度特定模式,Windsurf 都能精确交付。以下是一个示例提示及其相应结果

注意

LLM 每次可能会产生不同的结果,即使使用相同的提示。

"Create a Prisma schema for a SaaS app using PostgreSQL as a provider with `User`, `Organization`, and `Subscription` models, ensuring all models include `createdAt` and `updatedAt` DateTime fields with defaults, a soft-delete `deletedAt` field, and proper relationships between entities."

生成 Prisma 查询

Windsurf 可以帮助您创建针对您的 Prisma 模式量身定制的查询,无论是用于基本数据检索还是复杂、优化的操作。为了获得最佳结果,请专注于编写清晰、具体的提示,定义您的要求,例如要包含的字段、要应用的条件和要遍历的关系。这确保 Windsurf 生成准确、高性能且类型安全的查询。以下是一个示例提示及其生成的查询

"Generate a query to retrieve all User records associated with an Organization that has an `active` Subscription, while excluding any records where the `deletedAt` field is set."

使用 Windsurf 创建种子文件

编写种子脚本可能是一个繁琐的过程,但 Windsurf 通过快速生成结构化和有组织的种子代码来简化它。通过提供清晰的说明,您可以指导 Windsurf 创建脚本,用真实数据填充您的数据库,包括处理模式中定义的关系和复杂结构。以下是一个示例提示及其生成的种子脚本

"Generate code to populate the Prisma schema with realistic data for the User, Organization, and Subscription models, ensuring all necessary fields are included."

使用 Prisma VS Code 扩展管理数据库

Prisma VS Code 扩展 是一个强大的工具,用于使用 Prisma Postgres 构建应用程序。您也可以通过安装此扩展在 Windsurf 中使用它。它提供了一个专用的 UI,用于管理本地和远程 Prisma Postgres 实例,从而可以轻松查看、创建和删除实例、将本地数据库推送到云端以及可视化您的模式。

数据库管理 UI

凭借其内置的数据库管理界面,Prisma VS Code 扩展让您可以在编辑器中轻松地使用本地和远程 Prisma Postgres 实例。

工作流

此 UI 支持以下工作流:

  • 使用以下方式进行身份验证:
  • 查看、创建和删除 Prisma Postgres 实例(本地和远程)
  • “推送到云端”:轻松部署本地 Prisma Postgres 实例
  • 通过嵌入式Prisma Studio查看和编辑数据
  • 可视化您的数据库schema

用法

通过 Prisma VS Code 扩展中的 UI 管理 Prisma Postgres 实例

  1. 确保您已安装最新版本的 Prisma VS Code 扩展
  2. 活动栏中找到 Prisma 徽标。
  3. 点击登录以开始按钮。
  4. 使用以下方式进行身份验证:使用登录提示,然后选择一个目标工作区

Prisma Studio 内置

除了管理数据库实例之外,Prisma VS Code 扩展还将 Prisma Studio 直接嵌入到您的编辑器中,使您可以在 Windsurf 中轻松地对数据库执行创建、更新和删除操作。请按照这些简单步骤开始使用。

其他资源

将 Windsurf 与 Prisma 结合使用可以加快开发速度,同时确保干净且可维护的数据库代码。继续学习


与 Prisma 保持联系

通过以下方式与我们保持联系,继续你的 Prisma 之旅: 我们的活跃社区。保持信息灵通,参与其中,并与其他开发人员协作。

我们真诚地感谢你的参与,并期待你成为我们社区的一部分!

© . This site is unofficial and not affiliated with Prisma Data, Inc.