Skip to content

Database Setup

Discord Forum API supports multiple database backends. Choose based on your deployment needs.

Database Options

DatabaseBest ForFeatures
SQLiteDevelopment, small deploymentsZero config, file-based
TursoProduction, edge deploymentsMulti-region, edge-compatible
PostgreSQLHigh-scale deploymentsComing soon

SQLite (Default)

SQLite is the default database—perfect for development and small to medium deployments.

Configuration

No configuration needed! The database file is created automatically at ./data/discord-forum.db.

To customize the path:

DATABASE_TYPE=sqlite
DATABASE_PATH=./data/my-custom-name.db

Initialize Schema

Push the schema to create tables:

Terminal window
pnpm db:push

Or use migrations for production:

Terminal window
# Generate migration files
pnpm db:generate
# Run migrations
pnpm db:migrate

SQLite Pros & Cons

Advantages:

  • Zero configuration
  • No external dependencies
  • Fast for read-heavy workloads
  • Easy to backup (just copy the file)

Limitations:

  • Single writer at a time
  • Not suitable for multi-instance deployments
  • No built-in replication

Turso

Turso is a SQLite-compatible database optimized for edge deployments.

Why Turso?

  • Edge-compatible: Deploy data close to users globally
  • Free tier: 500 databases, 9GB storage
  • Serverless: No servers to manage
  • SQLite compatible: Same queries, zero migration needed

Setup

  1. Install Turso CLI

    Terminal window
    # macOS/Linux
    curl -sSfL https://get.tur.so/install.sh | bash
    # Windows (WSL)
    curl -sSfL https://get.tur.so/install.sh | bash
  2. Sign up and authenticate

    Terminal window
    turso auth signup
    # or
    turso auth login
  3. Create a database

    Terminal window
    turso db create discord-forum-api
  4. Get your database URL

    Terminal window
    turso db show discord-forum-api --url
    # Output: libsql://discord-forum-api-username.turso.io
  5. Create an auth token

    Terminal window
    turso db tokens create discord-forum-api
    # Output: eyJhbGci...
  6. Update .env

    DATABASE_TYPE=turso
    TURSO_DATABASE_URL=libsql://discord-forum-api-username.turso.io
    TURSO_AUTH_TOKEN=eyJhbGci...
  7. Push schema

    Terminal window
    pnpm db:push

Turso CLI Commands

CommandDescription
turso db listList all databases
turso db show <name>Show database details
turso db shell <name>Interactive SQL shell
turso db tokens create <name>Create auth token
turso db destroy <name>Delete database

Turso Pricing

TierPriceIncludes
StarterFree500 DBs, 9GB storage, 1B reads/month
Scaler$29/moUnlimited DBs, 24GB storage, higher limits
EnterpriseCustomSLA, dedicated support

Database Management

Drizzle Studio

Open a visual database browser:

Terminal window
pnpm db:studio

This opens a web interface where you can:

  • Browse tables
  • View and edit data
  • Run SQL queries
  • Export data

Schema Changes

When you modify the schema:

Push changes directly (fast, may lose data):

Terminal window
pnpm db:push

Backup & Restore

SQLite

Terminal window
# Backup
cp ./data/discord-forum.db ./data/discord-forum.db.backup
# Restore
cp ./data/discord-forum.db.backup ./data/discord-forum.db

Turso

Turso handles backups automatically. For manual exports:

Terminal window
# Export to SQL
turso db shell discord-forum-api ".dump" > backup.sql
# Restore (create new db and import)
turso db create discord-forum-api-restored
turso db shell discord-forum-api-restored < backup.sql

Schema Overview

The database schema includes these main tables:

TablePurpose
serversDiscord server metadata
channelsForum channel information
threadsForum threads
messagesThread messages
usersDiscord user profiles
reactionsMessage reactions
tagsThread tags

Explore the full schema in packages/db/src/schema.ts.

Troubleshooting

”Database is locked”

SQLite can lock if multiple processes access it:

  • Ensure only one bot instance is running
  • Check for zombie processes: ps aux | grep node
  • Increase busy timeout (if custom client)

“Table does not exist”

Schema wasn’t pushed:

Terminal window
pnpm db:push

Turso connection errors

  • Verify TURSO_DATABASE_URL format: libsql://...
  • Check token hasn’t expired: turso db tokens create <name>
  • Ensure network allows outbound HTTPS

Slow queries

  • Check indexes exist (schema includes them by default)
  • Use EXPLAIN QUERY PLAN in Drizzle Studio
  • Consider pagination for large result sets