Documentation
DatabaseOverview

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 .prisma files
  • 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:

packages/database/prisma/schema.prisma
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 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:

  1. Create a project at supabase.com
  2. Get your connection strings from the Connect button at the top of the project dashboard
  3. In the connection dialog, select the ORMs tab
  4. Copy the DATABASE_URL and DIRECT_URL connection strings
  5. Paste them into your .env.local file
.env.local
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:16

Then configure your environment:

.env.local
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:

packages/database/prisma/schema.prisma
datasource db {
  provider = "mysql"
  url      = env("DATABASE_URL")
}

Configure your connection string:

.env.local
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:8

MongoDB

Update your schema:

packages/database/prisma/schema.prisma
datasource db {
  provider = "mongodb"
  url      = env("DATABASE_URL")
}

Configure your connection string:

.env.local
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

When using MongoDB, you may need to adjust your schema models as MongoDB has different constraints compared to SQL databases.

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:7

Prisma ORM

Sushify Next.js uses Prisma as its ORM for database access.

Key Features:

  • Declarative schema with .prisma files
  • Auto-generated type-safe client
  • Comprehensive migration system
  • Built-in admin UI (Prisma Studio)
  • Extensive documentation and community

Learn more about Prisma →

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:

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:

  1. The provider in your Prisma schema: packages/database/prisma/schema.prisma
  2. The provider in 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_URL

2. Initialize Database

Push the schema to your database:

pnpm --filter database push

This creates all necessary tables and relationships in your database.

3. Generate Prisma Client

Generate the Prisma Client:

pnpm --filter database generate

4. 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:

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 generate

3. Apply Changes to Database

For Development (prototype and iterate quickly):

pnpm --filter database push

This pushes schema changes directly to your database without creating migration files. Ideal for rapid development.

For Production (track changes with migrations):

pnpm --filter database migrate

This 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