);
};
export default User;
```
## Generating plain SQL queries for use with TypedSQL
### How Tabnine helps
While Prisma provides a high-level API for querying, sometimes a developer may need to drop down to raw SQL for more low-level control of a query. In these cases, they can use [TypedSQL](/orm/prisma-client/using-raw-sql/typedsql) which provides full type safety for raq SQL queries in Prisma ORM.
One common use case are aggregation queries, such as calculating percentages of different subscription statuses in the schema example above.
Tabnine can assist by:
- Generating SQL queries based on your Prisma schema.
- Structuring complex aggregations without manual trial and error.
- Integrating the queries in your application code.
### Example: Calculating the percentages of active, expired, and paused subscriptions
#### Prompt
_Generate a SQL query that returns the percentage of subscriptions that are Active, Paused, and Expired._
#### Generated SQL query
```sql file=prisma/sql/subscriptionsReport.sql
SELECT
status,
COUNT(*) * 100.0 / (SELECT COUNT(*) FROM "Subscription") AS percentage
FROM "Subscription"
GROUP BY status;
```
#### Application code
```ts
import { PrismaClient } from '@prisma/client'
import { subscriptionsReport } from '@prisma/client/sql'
const prisma = new PrismaClient();
const usersWithPostCounts = await prisma.$queryRawTyped(subscriptionsReport())
console.log(usersWithPostCounts)
```
## Generating code for unit and integration tests
### How Tabnine helps
Writing tests ensures that Prisma-based applications function correctly and behave according to requirements and expectations. However, writing tests is a time-consuming activity that often follows predictable and repetitive patterns.
Tabnine can drastically speed up writing tests by:
- Generating test boilerplate for both unit and integration tests.
- Suggesting mocks and fixtures for Prisma database interactions.
- Helping structure test cases to follow best practices.
- Providing integration test scaffolding for testing Prisma with a real or in-memory database.
### Example: Writing a unit test for a Prisma service
#### Prompt
_Generate a Jest unit test for a Prisma service function that fetches all active users._
#### Generated code
```ts filename=__tests__/userService.test.ts
import { prismaMock } from '../prisma/singleton';
import { getActiveUsers } from '../services/userService';
test('should return only active users', async () => {
prismaMock.user.findMany.mockResolvedValue([
{ id: 1, name: 'Alice', email: 'alice@example.com' },
{ id: 2, name: 'Bob', email: 'bob@example.com' }
]);
const users = await getActiveUsers();
expect(users).toHaveLength(2);
expect(users[0].email).toBe('alice@example.com');
});
```
## Guiding through major refactorings
### How Tabnine helps
Thanks to Prisma ORM's type safe queries, refactoring are more safe and predictable than with other ORMs: After making schema changes, the generated Prisma Client code will be updated and the TypeScript compiler will highlight all places that need a change.
However, there may still be more difficult situations during a refactoring, e.g. when business logic or complex queries need to be adjusted.
Tabnine can assist during refactorings by:
- Identifying patterns in your code and suggesting refactorings while maintaining consistency.
- Helping update database queries after schema changes and reducing manual effort.
- Providing function rewrites to simplify and optimize logic.
- Suggesting breaking changes that need to be handled across your codebase.
### Example: Refactoring a Prisma query after a schema change
#### Prompt
_Refactor this Prisma query to support soft deletion using a `deletedAt` timestamp field in the User model._
#### Initial code
```ts
const users = await prisma.user.findMany();
```
#### Refactored code
```ts
const users = await prisma.user.findMany({
where: { deletedAt: null }
});
```
## Generating seeding scripts
### How Tabnine helps
Seeding a database with realistic test data can be cumbersome. Realistic seed data is helpful during development because it gives the developer a better impression of the application when it's used in the real-world. Tabnine can help with database seeding by:
- Generating scripts with structured seed data based on the schema and with specific requirements.
- Creating real-looking placeholder data that can be used to test the app.
- Generate specific data to cover edge cases.
### Example: Creating a seed script
#### Prompt
_Generate a Prisma seed script for populating User, Organization, and Subscription models._
#### Generated code
```ts
import { PrismaClient } from "@prisma/client";
const prisma = new PrismaClient();
async function main() {
await prisma.organization.create({
data: {
name: "Prisma Corp",
slug: "prisma-corp",
subscription: {
create: {
plan: "PRO",
status: "ACTIVE"
}
},
users: {
create: [
{ name: "Alice Doe", email: "alice@prisma.io" },
{ name: "Bob Smith", email: "bob@prisma.io" }
]
}
}
});
console.log("Seed data created successfully");
}
main()
.catch((e) => {
console.error(e);
process.exit(1);
})
.finally(async () => {
await prisma.$disconnect();
});
```
---
# Windsurf
URL: https://www.prisma.io/docs/orm/more/ai-tools/windsurf
[Windsurf Editor](https://windsurf.com/editor/) is an AI-powered code editor designed to boost productivity by automating repetitive coding tasks. When paired with Prisma, a robust and type-safe toolkit for database workflows, it becomes a powerful solution for managing and optimizing database schemas, queries, and data seeding.
This guide provides detailed instructions for effectively using Prisma with Windsurf to:
- Define project-specific best practices with `.windsurfrules`.
- Use Windsurf's context-aware capabilities.
- Generate schemas, queries, and seed data tailored to your database.
:::note
While this guide is focused on Windsurf, these patterns should work with any AI editor. [Let us know on X](https://pris.ly/x?utm_source=docs&utm_medium=inline_text) if you'd like us to create guides for your preferred tool!
:::
## Prisma MCP server
Prisma provides its own [Model Context Protocol (MCP)](https://modelcontextprotocol.io/introduction) server that lets you manage Prisma Postgres databases, model database schemas, and even chat through migrations. Learn more about how you can add it to Windsurf [here](/postgres/mcp-server#windsurf).
## Defining project-specific rules with `.windsurfrules`
The [`.windsurfrules` file](https://docs.windsurf.com/windsurf/memories#windsurfrules) in Windsurf allows you to enforce best practices and development standards tailored to your Prisma project. By defining clear and consistent rules, you can ensure that Windsurf generates clean, maintainable, and project-specific code with minimal manual adjustments.
To implement these rules, create a `.windsurfrules` file in the root of your project. Below is an example configuration: