# Infrastructure

This project has provisioned database and cache access.

## Database (CockroachDB)

PostgreSQL-compatible distributed SQL database.

### Connection

| Environment | Variable |
|-------------|----------|
| Production  | `DATABASE_URL` |
| Staging     | `DATABASE_URL_STAGING` |

### Usage

**Go (sqlx):**
```go
import "github.com/jmoiron/sqlx"
import _ "github.com/lib/pq"

db, err := sqlx.Connect("postgres", os.Getenv("DATABASE_URL"))
```

**Node.js (pg):**
```javascript
import pg from 'pg';
const pool = new pg.Pool({ connectionString: process.env.DATABASE_URL });
```

**Python (psycopg2):**
```python
import psycopg2
conn = psycopg2.connect(os.environ['DATABASE_URL'])
```

### Schema Migrations

Use any PostgreSQL migration tool. Recommended:
- Go: `golang-migrate/migrate`
- Node.js: `node-pg-migrate`
- Python: `alembic`

## Cache (Redis)

Redis cache with project-isolated key prefix.

### Connection

| Environment | Variable |
|-------------|----------|
| Production  | `REDIS_URL` |
| Staging     | `REDIS_URL_STAGING` |
| Key Prefix  | `REDIS_PREFIX` |

**Important:** Always prefix your keys with `REDIS_PREFIX` to ensure isolation.

### Usage

**Go (go-redis):**
```go
import "github.com/redis/go-redis/v9"

opt, _ := redis.ParseURL(os.Getenv("REDIS_URL"))
client := redis.NewClient(opt)

prefix := os.Getenv("REDIS_PREFIX")
client.Set(ctx, prefix+"users:123", data, time.Hour)
```

**Node.js (ioredis):**
```javascript
import Redis from 'ioredis';

const redis = new Redis(process.env.REDIS_URL);
const prefix = process.env.REDIS_PREFIX;

await redis.set(`${prefix}session:abc`, JSON.stringify(data), 'EX', 3600);
```

## Environment Variables

These are automatically injected into your deployment:

| Variable | Description |
|----------|-------------|
| `DATABASE_URL` | CockroachDB production connection |
| `DATABASE_URL_STAGING` | CockroachDB staging connection |
| `REDIS_URL` | Redis production connection |
| `REDIS_URL_STAGING` | Redis staging connection |
| `REDIS_PREFIX` | Key prefix for Redis isolation |
