Overview
Learn about Sushify Next.js's database architecture and setup with Prisma
Sushify Next.js provides a flexible and powerful database layer with Prisma ORM for type-safe database access. The database is configured to use PostgreSQL by default, but can be easily changed to support other databases like MySQL, MongoDB, SQLite, and more.
Architecture
The database package is located at packages/database and provides:
- Type-safe database client with Prisma
- Declarative schema with
.prismafiles - Pre-configured queries for common operations
- Zod schemas for runtime validation
- Migration tools for database schema management
Database Provider
Sushify Next.js uses PostgreSQL as its default database provider, but supports multiple database systems through Prisma. You can configure the provider in your schema.prisma file:
datasource db {
provider = "postgresql" // Can be: postgresql, mysql, mongodb, sqlite, sqlserver, cockroachdb
url = env("DATABASE_URL")
}Supported Database Providers
Prisma supports the following database providers:
- postgresql - PostgreSQL (default)
- mysql - MySQL or MariaDB
- mongodb - MongoDB
- sqlite - SQLite
- sqlserver - Microsoft SQL Server
- cockroachdb - CockroachDB
PostgreSQL Providers (Default)
You can use any PostgreSQL provider:
Supabase (Recommended)
Supabase provides a fully managed PostgreSQL database with additional features like:
- Real-time subscriptions
- Built-in authentication
- Storage and CDN
- Auto-generated APIs
- Database backups
Setup:
- Create a project at supabase.com
- Get your connection strings from the
Connectbutton at the top of the project dashboard - In the connection dialog, select the
ORMstab - Copy the
DATABASE_URLandDIRECT_URLconnection strings - Paste them into your
.env.localfile
DATABASE_URL="postgresql://postgres.xxx:[YOUR-PASSWORD]@aws-0-ap-southeast-1.pooler.supabase.com:6543/postgres?pgbouncer=true"
DIRECT_URL="postgresql://postgres.xxx:[YOUR-PASSWORD]@aws-0-ap-southeast-1.pooler.supabase.com:5432/postgres"Local Development
For local development, you can run PostgreSQL using Docker:
docker run --name Project-Name \
-e POSTGRES_PASSWORD=password \
-e POSTGRES_DB=your-project-name \
-p 5432:5432 \
-d postgres:16Then configure your environment:
DATABASE_URL="postgresql://postgres:password@localhost:5432/your-project-name"
DIRECT_URL="postgresql://postgres:password@localhost:5432/your-project-name"Using Other Database Providers
To switch to a different database provider, update your schema.prisma file and adjust your connection string accordingly.
MySQL / MariaDB
Update your schema:
datasource db {
provider = "mysql"
url = env("DATABASE_URL")
}Configure your connection string:
DATABASE_URL="mysql://user:password@localhost:3306/your-project-name"
DIRECT_URL="mysql://user:password@localhost:3306/your-project-name"Local MySQL with Docker:
docker run --name Project-Name \
-e MYSQL_ROOT_PASSWORD=password \
-e MYSQL_DATABASE=your-project-name \
-p 3306:3306 \
-d mysql:8MongoDB
Update your schema:
datasource db {
provider = "mongodb"
url = env("DATABASE_URL")
}Configure your connection string:
DATABASE_URL="mongodb://user:password@localhost:27017/your-project-name?authSource=admin"
DIRECT_URL="mongodb://user:password@localhost:27017/your-project-name?authSource=admin"Note
Local MongoDB with Docker:
docker run --name Project-Name \
-e MONGO_INITDB_ROOT_USERNAME=admin \
-e MONGO_INITDB_ROOT_PASSWORD=password \
-e MONGO_INITDB_DATABASE=your-project-name \
-p 27017:27017 \
-d mongo:7Prisma ORM
Sushify Next.js uses Prisma as its ORM for database access.
Key Features:
- Declarative schema with
.prismafiles - Auto-generated type-safe client
- Comprehensive migration system
- Built-in admin UI (Prisma Studio)
- Extensive documentation and community
Connection Strings Explained
DATABASE_URL: Used for application queries. Supports connection pooling for better performance under load.DIRECT_URL: Used for migrations and schema operations. Bypasses connection pooling to ensure migrations execute properly.
If you're not using Supabase, both variables can use the same connection string.
Important: Authentication Adapter Configuration
The authentication system (Better Auth) must be configured with the same database provider as your Prisma schema. This is already set up correctly in packages/auth/auth.ts:
export const auth = betterAuth({
database: prismaAdapter(db, {
provider: "postgresql", // ⚠️ Must match your Prisma datasource provider
}),
// ... other config
});Critical: If you ever change your database provider (e.g., to MySQL or SQLite), you must update both:
- The
providerin your Prisma schema:packages/database/prisma/schema.prisma - The
providerin the Better Auth configuration:packages/auth/auth.ts
Both configurations must match for authentication to work correctly. A mismatch will cause authentication failures.
Quick Start
1. Set Up Database Connection
Copy your database connection string to .env.local:
cp .env.local.example .env.local
# Edit .env.local and add your DATABASE_URL and DIRECT_URL2. Initialize Database
Push the schema to your database:
pnpm --filter database pushThis creates all necessary tables and relationships in your database.
3. Generate Prisma Client
Generate the Prisma Client:
pnpm --filter database generate4. Start Using the Database
Import and use the database client in your application:
import { db } from "@repo/database";
const users = await db.user.findMany();For more detailed Prisma Client query syntax and advanced features, refer to the official Prisma documentation.
Modifying the Database Schema
When you need to add or modify database tables and fields, follow these steps:
1. Update the Schema File
Edit your Prisma schema file at packages/database/prisma/schema.prisma:
model Post {
id String @id @default(cuid())
title String
content String?
published Boolean @default(false)
authorId String
author User @relation(fields: [authorId], references: [id])
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}
model User {
id String @id @default(cuid())
name String
email String @unique
posts Post[]
}2. Generate Prisma Client
After modifying the schema, regenerate the Prisma Client to get updated TypeScript types:
pnpm --filter database generate3. Apply Changes to Database
For Development (prototype and iterate quickly):
pnpm --filter database pushThis pushes schema changes directly to your database without creating migration files. Ideal for rapid development.
For Production (track changes with migrations):
pnpm --filter database migrateThis creates a migration file and applies it to your database. Migration files should be committed to version control.
Package Commands
The database package includes these npm scripts:
# Generate Prisma Client
pnpm --filter database generate
# Push schema to database (development)
pnpm --filter database push
# Create and run migrations (production)
pnpm --filter database migrate
# Open Prisma Studio (visual database editor)
pnpm --filter database studio
# Type check
pnpm --filter database type-check