LogoNEXTDEVKIT Docs

Overview

Learn about database setup and management in NEXTDEVKIT.

NEXTDEVKIT uses Drizzle ORM, providing a flexible and type-safe SQL query builder. It's designed to integrate smoothly with different database backends depending on your deployment environment.

🚀 Default Database Integrations

This starter kit is pre-configured to work with the most suitable database for your chosen deployment platform:

PlatformDefault DatabaseBest For
General/LocalPostgreSQLRobust features, local development, and standard deployments.
Cloudflare WorkersCloudflare D1Edge deployments and global low-latency access.
AWS (with SST)AWS RDSScalable, managed relational database for production workloads.

📚 Detailed Setup Guides

Choose your database backend and follow the detailed setup guide:

📋 Database Schema

Your database schema is the single source of truth for your data structure.

How to Modify the Schema

The schema is defined in one central location: src/database/schema.ts.

To modify your database structure, you simply edit this file. You can define tables, columns, and relationships using Drizzle's syntax. For example, if you are using PostgreSQL, your table definitions will use imports from drizzle-orm/pg-core.

// Example of a table definition for PostgreSQL
import { pgTable, text, timestamp } from "drizzle-orm/pg-core";

export const user = pgTable("user", {
  id: text("id").primaryKey(),
  name: text("name").notNull(),
  email: text("email").notNull().unique(),
  // ... other fields
});

The starter kit includes a default schema that supports user authentication, subscriptions, and more. You are free to customize it to fit your needs.

If you want to switch to a different database, you can do so by changing the database client in src/database/client.ts.

You can reference the Drizzle ORM Documentation for more information on how to use the ORM.

🔄 Migrations

After you modify the schema in src/database/schema.ts, you need to generate and apply a migration to update your database.

  1. Generate a Migration: This command compares your schema file to the state of the database and creates a new SQL migration file in the drizzle directory.

    pnpm db:generate
  2. Apply the Migration: This command executes the migration files to update your database schema.

    pnpm db:migrate

🔧 How to Use the Database

You can import and use the database client in your application code to perform queries.

Example Query

Here is how you can fetch a user from the database:

import { db } from "@/database/client";
import { user } from "@/database/schema";
import { eq } from "drizzle-orm";

// Get user by ID
const userData = await db
  .select()
  .from(user)
  .where(eq(user.id, userId))
  .limit(1);

Drizzle ORM provides a powerful API for building all types of queries, including joins, transactions, and more complex filtering.

🔍 Database Studio

Drizzle Studio provides a powerful GUI for managing your database directly from your browser.

pnpm db:studio

This command opens a web interface where you can:

  • 📊 View and browse table data
  • ✏️ Add, delete, or edit records
  • 🔍 Run and test custom queries

🔧 Environment Configuration

Environment Variables

Different databases require different environment variables:

# PostgreSQL
DATABASE_URL="postgresql://user:password@localhost:5432/database"

# Cloudflare D1 (configured in wrangler.jsonc)
# No environment variables needed

# AWS RDS (configured in aws secret manager)
# npx sst secret set NextDevKitDBPassword xxx --stage production

Configuration Files

  • PostgreSQL: .env.local or .env
  • Cloudflare D1: wrangler.jsonc
  • AWS RDS: sst.config.ts and environment variables

📚 Detailed Setup Guides

Choose your database backend and follow the detailed setup guide:


🎯 Next Steps

Now that you understand the database architecture, dive into the specific setup for your chosen platform: