> ## Documentation Index
> Fetch the complete documentation index at: https://docs.anotherwrapper.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Database Overview

> Understand the main tables, Drizzle schema, and data responsibilities

<Info>
  The database isn't just there for auth. It stores chats, generations, purchases, credits, recordings, documents, embeddings, and more. It's the backbone of every app in the repo.
</Info>

## The Mental Model

Here's how to think about the data layer:

* **Better Auth** owns the auth tables and browser session flow
* **PostgreSQL + Drizzle** own the application data layer
* **Supabase PostgreSQL** is the recommended managed host

Application queries run through Drizzle, while authentication lives inside the app through Better Auth -- not through provider-specific database primitives.

## Schema Workflow

<Steps>
  <Step title="Edit schema files">
    Define tables, indexes, and relationships in `lib/db/schema/*`.
  </Step>

  <Step title="Generate SQL migrations">
    Run `pnpm db:generate` to produce migration files from your schema changes.
  </Step>

  <Step title="Apply migrations">
    Run `pnpm db:migrate` to apply the generated SQL to your database.
  </Step>
</Steps>

<Tip>
  The `drizzle/*` folder is the SQL history produced by this workflow. The current baseline starts with `drizzle/0000_better_auth_baseline.sql` and `drizzle/0001_rag_functions.sql`.
</Tip>

## Table Families

<AccordionGroup>
  <Accordion title="Auth & Profiles">
    **Schema files:** `lib/db/schema/auth.ts`, `lib/db/schema/profiles.ts`

    Handles:

    * Better Auth users, sessions, accounts, and verifications
    * User profile data
    * Credit tracking
    * Connecting app-owned records to the authenticated user ID
  </Accordion>

  <Accordion title="Generations">
    **Schema file:** `lib/db/schema/generations.ts`

    Handles:

    * Outputs from AI apps (images, videos, audio, structured data)
    * Input/output payloads
    * Reusable generation history across multiple apps
  </Accordion>

  <Accordion title="Chat">
    **Schema file:** `lib/db/schema/chat.ts`

    Handles:

    * Chat conversations
    * Messages within conversations
    * Chat document versioning
  </Accordion>

  <Accordion title="PDF & RAG">
    **Schema file:** `lib/db/schema/pdf.ts`

    Handles:

    * Uploaded PDFs
    * Embeddings (via `pgvector`)
    * Document-to-chat links
    * Similarity search functions
  </Accordion>

  <Accordion title="Audio Notes">
    **Schema file:** `lib/db/schema/audio.ts`

    Handles:

    * Recordings
    * Transcripts
    * AI-generated summaries
  </Accordion>

  <Accordion title="Purchases">
    **Schema file:** `lib/db/schema/purchases.ts`

    Handles:

    * Purchase records
    * Provider metadata
    * Credit packs and plan purchases
  </Accordion>
</AccordionGroup>

## Important Database Behaviors

### Ownership Checks

Most user-owned data access is enforced through authenticated server helpers plus user-scoped queries inside the `lib/db/*` modules. Records are filtered or updated by the current user ID -- not by database-vendor-specific auth helpers.

### Credits

Credit logic isn't just a frontend number. The repo uses **transactional update queries** and ownership checks in the database layer to keep credit changes safe.

### Vector Search

The PDF system uses `pgvector` plus retrieval functions so the chat app can do document-aware answers with citations. The extension setup and retrieval function live in focused custom Drizzle migrations.

<Note>
  Important details about `pgvector`:

  * It's enabled at the **database level**, not per-table
  * Once the extension exists, any table can define `vector(...)` columns
  * Your database host still needs to support the extension
</Note>

## Good First Files to Read

* `lib/db/schema/auth.ts`
* `lib/db/schema/profiles.ts`
* `lib/db/schema/generations.ts`
* `lib/db/schema/chat.ts`
* `lib/db/schema/pdf.ts`
* `lib/db/schema/audio.ts`
* `lib/db/schema/purchases.ts`
* `drizzle/0000_better_auth_baseline.sql`
* `drizzle/0001_rag_functions.sql`

<Info>
  When customizing the product, understanding these table families helps you avoid a common mistake: bolting new features onto random existing tables instead of working with the repo's actual data model.
</Info>

<CardGroup cols={2}>
  <Card title="Better Auth + PostgreSQL" icon="database" href="/services/better-auth-postgresql">
    Learn how auth and persistence fit together in the default setup.
  </Card>

  <Card title="Vector RAG" icon="database" href="/services/vector-rag">
    Learn how embeddings and retrieval use the database.
  </Card>
</CardGroup>
