Prisma Adapter
The Prisma adapter integrates c15t Backend with Prisma ORM, providing type-safe database access with migration support and automatic schema generation.
Deprecated Feature
@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:
npm install @prisma/client
npm install -D prisma
npx prisma init
Configuration
- Define your Prisma schema in
prisma/schema.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")
}
- Generate the Prisma client:
npx prisma generate
- Configure the c15t instance with the Prisma adapter:
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
// 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
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:
# 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:
// 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