---
title: Prisma Adapter
description: The Prisma adapter integrates c15t Backend with Prisma ORM, providing type-safe database access with migration support and automatic schema generation.
lastModified: 2025-04-15
deprecated: true
deprecatedReason: "@c15t/backend v1 did not deliver the flexibility we wanted and fell short of our standards. It is now deprecated as we work on a full rewrite, with v2 entering canary soon. This does not affect consent.io deployments, which remain stable."
---
## Installation

First, install Prisma and initialize your project:

```bash
npm install @prisma/client
npm install -D prisma
npx prisma init
```

## Configuration

1. Define your Prisma schema in `prisma/schema.prisma`:

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

datasource db {
  provider = "postgresql" // or "mysql", "sqlite", etc.
  url      = env("DATABASE_URL")
}

model User {
  id        String   @id @default(uuid())
  name      String
  email     String   @unique
  createdAt DateTime @default(now()) @map("created_at")
}
```

2. Generate the Prisma client:

```bash
npx prisma generate
```

3. Configure the c15t instance with the Prisma adapter:

```typescript
import { c15tInstance } from '@c15t/backend';
import { prismaAdapter } from '@c15t/backend/db/adapters/prisma';
import { PrismaClient } from '@prisma/client';

const prisma = new PrismaClient();

const instance = c15tInstance({
  baseURL: 'http://localhost:3000',
  database: prismaAdapter({ 
    client: prisma,
    // Optional: Configure logging
    logging: process.env.NODE_ENV !== 'production'
  }),
});
```

## Usage Examples

### Basic CRUD Operations

```typescript
// Create a new record
const user = await instance.database.create('User', {
  name: 'John Doe',
  email: 'john@example.com'
});

// Find records
const users = await instance.database.find('User', {
  where: { email: 'john@example.com' },
  orderBy: { createdAt: 'desc' },
  limit: 10
});

// Update a record
const updatedUser = await instance.database.update(
  'User',
  { where: { id: user.id } },
  { name: 'John Smith' }
);

// Delete a record
await instance.database.delete('User', { where: { id: user.id } });
```

### Transactions

```typescript
await instance.database.transaction(async (trx) => {
  const user = await trx.create('User', { 
    name: 'Alice',
    email: 'alice@example.com'
  });
  
  await trx.create('Profile', { 
    userId: user.id,
    bio: 'Software engineer'
  });
});
```

### Migrations

Run migrations using the Prisma CLI:

```bash
# Create a migration
npx prisma migrate dev --name add-user-model

# Apply migrations in production
npx prisma migrate deploy
```

## Type Safety

The Prisma adapter provides type safety when used with TypeScript:

```typescript
// Define your types to match your Prisma schema
type User = {
  id: string;
  name: string;
  email: string;
  createdAt: Date;
};

// Type-safe operations
const users = await instance.database.find<User>('User', {
  where: { email: 'john@example.com' }
});
```

## Best Practices

* **Define schema in Prisma format** - Use Prisma's schema format for auto-generated migration files
* **Use migrations for schema changes** - Let Prisma handle database schema migrations
* **Enable query logging in development** - Monitor query performance and debug issues
* **Consider connection pooling** - Configure connection pools for production performance

## Limitations

* Table names must match Prisma model names
* Some advanced query features may require direct Prisma client usage

## Related Resources

* [Prisma Documentation](https://www.prisma.io/docs)
* [Database Adapter Interface](../database-adapters)
* [Core Concepts](../core-concepts)
