Prisma
Prisma ORM is an open-source database toolkit that simplifies database access and management in applications by providing a type-safe query builder and an intuitive data modeling interface. Read more here.
Example Usage
Make sure you have Prisma installed and configured. Then, you can use the Prisma adapter to connect to your database.
import { betterQuery } from "better-query";
import { prismaAdapter } from "better-query/adapters/prisma";
import { PrismaClient } from "@prisma/client";
const prisma = new PrismaClient();
export const query = betterQuery({
database: prismaAdapter(prisma, {
provider: "sqlite",
}),
});If you have configured a custom output directory in your schema.prisma file (e.g., output = "../src/generated/prisma"), make sure to import the Prisma client from that location instead of @prisma/client. Learn more about custom output directories in the Prisma documentation.
Schema generation & migration
The Better Query CLI allows you to generate or migrate your database schema based on your Better Query configuration and plugins.
Prisma Schema Generation | Prisma Schema Migration |
|---|---|
| ✅ Supported | ❌ Not Supported |
npx @better-query/cli@latest generateCustom Operations
The Prisma adapter includes native custom operations that leverage Prisma's advanced features like transactions, native upserts, and aggregations.
// Example: Transaction with multiple operations
const result = await query.customOperation('transaction', {
operations: [
{
model: 'user',
operation: 'create',
data: { name: 'John', email: 'john@example.com' }
},
{
model: 'product',
operation: 'update',
data: { where: { id: '1' }, data: { stock: 10 } }
}
]
});
// Example: Upsert with relations
const user = await query.customOperation('upsert', {
model: 'user',
where: { email: 'john@example.com' },
update: { lastLogin: new Date() },
create: {
email: 'john@example.com',
name: 'John',
profile: {
create: { bio: 'Welcome!' }
}
},
include: { profile: true }
});Learn more about all available custom operations in the Custom Operations Guide.
Additional Information
If you're looking for performance improvements or tips, take a look at our guide to performance optimizations.