This document explains how Prisma is integrated into the Hasura Blog project as an additional migration tool and ORM.
This project uses both Hasura and Prisma for database management:
- Hasura Migrations: Primary migration tool for database schema
- Prisma: Type-safe ORM and alternative migration tool
- Hasura provides instant GraphQL APIs and real-time subscriptions
- Prisma provides type-safe database access in server-side code
- Both can work together without conflicts
npm installThis installs both prisma (CLI) and @prisma/client (runtime).
Add to your .env file:
DATABASE_URL=postgresql://postgres:postgrespassword@localhost:5432/hasuranpm run prisma:generateThis generates the type-safe Prisma Client in lib/generated/prisma.
Create a Prisma client instance:
// lib/prisma.ts
import { PrismaClient } from '@/lib/generated/prisma';
const globalForPrisma = global as unknown as { prisma: PrismaClient };
export const prisma = globalForPrisma.prisma || new PrismaClient();
if (process.env.NODE_ENV !== 'production') globalForPrisma.prisma = prisma;Use it in your server-side code:
import { prisma } from '@/lib/prisma';
// Get all published posts
export async function getPosts() {
return await prisma.post.findMany({
where: { status: 'published' },
include: {
author: true,
category: true,
postTags: {
include: {
tag: true
}
}
},
orderBy: {
publishedAt: 'desc'
}
});
}
// Get a single post by slug
export async function getPostBySlug(slug: string) {
return await prisma.post.findUnique({
where: { slug },
include: {
author: true,
category: true,
comments: {
where: { status: 'approved' },
include: {
author: true
}
},
postTags: {
include: {
tag: true
}
}
}
});
}
// Create a new post
export async function createPost(data: {
title: string;
slug: string;
content: string;
authorId: string;
categoryId?: string;
}) {
return await prisma.post.create({
data: {
...data,
status: 'draft'
}
});
}The primary migration tool is Hasura CLI:
cd hasura
hasura migrate create "add_new_table"
# Edit the generated SQL files
hasura migrate applyAfter applying Hasura migrations, update Prisma schema:
npm run prisma:db:pull
npm run prisma:generateYou can also use Prisma Migrate:
# Create a new migration
npm run prisma:migrate:dev
# Apply migrations in production
npm run prisma:migrate:deploycd hasura
hasura metadata reload| Command | Description |
|---|---|
npm run prisma:generate |
Generate Prisma Client |
npm run prisma:studio |
Open Prisma Studio (GUI) |
npm run prisma:db:push |
Push schema to database (dev) |
npm run prisma:db:pull |
Pull schema from database |
npm run prisma:migrate:dev |
Create and apply migration (dev) |
npm run prisma:migrate:deploy |
Apply migrations (production) |
npm run prisma:migrate:reset |
Reset database ( |
- Schema Sync: Keep Prisma schema in sync with Hasura migrations
- Type Safety: Use Prisma Client for all server-side database operations
- GraphQL API: Use Hasura GraphQL API for client-side queries
- Migrations: Prefer Hasura migrations for schema changes
- Generated Code: Don't commit generated Prisma Client files
- Schema file:
prisma/schema.prisma - Generated client:
lib/generated/prisma/(gitignored) - Migrations:
prisma/migrations/(if using Prisma Migrate)
npm run prisma:generatenpm run prisma:db:pull
npm run prisma:generatenpm run prisma:migrate:reset