Corsair
Integrations

Prisma

Integrate Corsair with Prisma for type-safe database operations

Prisma

Corsair integrates natively with Prisma for type-safe database operations.

Installation

npm install corsair prisma @prisma/client
npx corsair init --orm prisma

Setup

Corsair uses your existing Prisma schema:

prisma/schema.prisma
datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
}

generator client {
  provider = "prisma-client-js"
}

model User {
  id        String   @id @default(cuid())
  email     String   @unique
  name      String?
  posts     Post[]
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
}

model Post {
  id        String   @id @default(cuid())
  title     String
  content   String
  published Boolean  @default(false)
  author    User     @relation(fields: [authorId], references: [id])
  authorId  String
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
}

Configuration

corsair.config.ts
import { defineConfig } from 'corsair'

export default defineConfig({
  orm: 'prisma',
  database: {
    type: 'postgresql',
    url: process.env.DATABASE_URL,
  },
})

Type Generation

Generate types from Prisma schema:

# Generate Prisma client
npx prisma generate

# Generate Corsair types
npx corsair generate

Natural Language to Prisma

Corsair translates natural language to Prisma queries:

// Natural language
useCorsairQuery('all published posts with authors')

// Generates Prisma query
prisma.post.findMany({
  where: { published: true },
  include: { author: true },
})

Relationships

Corsair understands Prisma relations:

// One-to-many
useCorsairQuery('user with all their posts')

// Prisma equivalent
prisma.user.findUnique({
  where: { id },
  include: { posts: true },
})
// Nested includes
useCorsairQuery('posts with authors and their other posts')

// Prisma equivalent
prisma.post.findMany({
  include: {
    author: {
      include: { posts: true },
    },
  },
})

Filtering

// Date filtering
useCorsairQuery('posts created after January 1st 2024')

// Prisma equivalent
prisma.post.findMany({
  where: {
    createdAt: { gte: new Date('2024-01-01') },
  },
})
// Complex filters
useCorsairQuery('published posts by users with email domain gmail.com')

// Prisma equivalent
prisma.post.findMany({
  where: {
    published: true,
    author: {
      email: { endsWith: '@gmail.com' },
    },
  },
})

Mutations

// Create
const { mutate } = useCorsairMutation('create post')
mutate({
  title: 'Hello World',
  content: 'My first post',
  authorId: userId,
})

// Prisma equivalent
prisma.post.create({
  data: {
    title: 'Hello World',
    content: 'My first post',
    authorId: userId,
  },
})

Transactions

Corsair supports Prisma transactions:

// Create user and post in transaction
const { mutate } = useCorsairMutation('create user with first post')

mutate({
  email: 'user@example.com',
  name: 'John Doe',
  post: {
    title: 'My First Post',
    content: 'Hello world',
  },
})

// Prisma equivalent
prisma.$transaction(async tx => {
  const user = await tx.user.create({
    data: { email, name },
  })
  await tx.post.create({
    data: { ...post, authorId: user.id },
  })
})

Migrations

Run migrations through Corsair:

# Create migration
npx corsair migrate create add_user_role

# Apply migrations
npx corsair migrate apply

# Reset database
npx corsair migrate reset

These commands use Prisma Migrate under the hood.

Raw SQL

Execute raw SQL with Prisma:

import { corsair } from '@/lib/corsair'

const result = await corsair.raw`
  SELECT * FROM users WHERE email = ${email}
`