# Database Configuration

Configure databases for verification sandboxes.

## Overview

Many apps need database connections. Verification sandboxes need predictable data without connecting to production.

## Strategy Overview

| Layer | Strategy | Complexity | Best For |
|-------|----------|------------|----------|
| 1 | Fixture | Simplest | Mock API layer, no DB needed |
| 2 | Memory | Easy | In-memory databases (SQLite) |
| 3 | Seed | Medium | File-based DB with seed data |
| 4 | Docker | Complex | Real database containers |
| 5 | Remote | Advanced | Connect to staging/test DB |

**Recommendation**: Start with Fixture for most frontend apps.

## Layer 1: Fixture (Default)

Don't use a database - mock the API layer:

```json
{
  "database": { "strategy": "fixture" },
  "fixtures": [
    { "pattern": "/api/users", "source": "file://fixtures/users.json" }
  ]
}
```

## Layer 2: Memory

In-memory database (resets between runs):

```json
{
  "database": {
    "strategy": "memory",
    "env": { "DATABASE_URL": "sqlite::memory:" }
  }
}
```

## Layer 3: Seed

File-based DB with migrations and seed data:

```json
{
  "database": {
    "strategy": "seed",
    "env": { "DATABASE_URL": "file:./test.db" },
    "migrate_command": "pnpm db:migrate",
    "seed_command": "pnpm db:seed"
  }
}
```

## Layer 4: Docker

Real database containers:

```json
{
  "database": {
    "strategy": "docker",
    "services": [{
      "image": "postgres:15",
      "port": 5432,
      "env": { "POSTGRES_PASSWORD": "test" },
      "health_check": "pg_isready"
    }],
    "env": { "DATABASE_URL": "postgres://postgres:test@localhost:5432/postgres" }
  }
}
```

## Layer 5: Remote

Connect to staging database (use with caution):

```json
{
  "database": {
    "strategy": "remote",
    "env": { "DATABASE_URL": "$STAGING_DATABASE_URL" },
    "read_only": true
  }
}
```

## Fallback Strategies

```json
{
  "database": {
    "strategy": "docker",
    "services": [...],
    "fallback": {
      "strategy": "memory",
      "env": { "DATABASE_URL": "sqlite::memory:" }
    }
  }
}
```

## Security Notes

- Never use production credentials
- Store secrets with `haystack secrets set`
- Use `read_only: true` for remote connections
- Prefer fixtures or memory for simplicity
