|
| 1 | +# BRI Docker Server |
| 2 | + |
| 3 | +A plug-and-play Docker container that exposes BRI database operations via WebSocket RPC. Clients can interact with BRI remotely using the **exact same API** as local BRI. |
| 4 | + |
| 5 | +## Quick Start |
| 6 | + |
| 7 | +```bash |
| 8 | +# Clone and enter the docker directory |
| 9 | +cd docker |
| 10 | + |
| 11 | +# Start the server |
| 12 | +docker-compose up -d |
| 13 | + |
| 14 | +# The server is now running at ws://localhost:3000/api/ape |
| 15 | +``` |
| 16 | + |
| 17 | +## Client Usage |
| 18 | + |
| 19 | +```javascript |
| 20 | +import { createRemoteDB } from 'bri/remote'; |
| 21 | + |
| 22 | +// Connect to the server |
| 23 | +const db = await createRemoteDB('ws://localhost:3000'); |
| 24 | + |
| 25 | +// Now use the SAME API as local BRI! |
| 26 | + |
| 27 | +// Create |
| 28 | +const user = await db.add.user({ name: 'Alice', email: 'alice@example.com' }); |
| 29 | +console.log(user.$ID); // USER_abc1234 |
| 30 | + |
| 31 | +// Read |
| 32 | +const alice = await db.get.user(user.$ID); |
| 33 | +const allUsers = await db.get.userS(); |
| 34 | +const admins = await db.get.userS({ role: 'admin' }); |
| 35 | + |
| 36 | +// Update (reactive) |
| 37 | +alice.name = 'Alice Smith'; |
| 38 | +const updated = await alice.save(); |
| 39 | + |
| 40 | +// Population chaining |
| 41 | +const post = await db.get.post(postId); |
| 42 | +const withAuthor = await post.and.author; |
| 43 | +const withComments = await withAuthor.and.comments; |
| 44 | + |
| 45 | +// Subscriptions |
| 46 | +const unsubscribe = await db.sub.user((change) => { |
| 47 | + console.log('User changed:', change); |
| 48 | +}); |
| 49 | + |
| 50 | +// Transactions |
| 51 | +db.rec(); |
| 52 | +await db.add.order({ items: [...] }); |
| 53 | +await db.add.payment({ amount: 100 }); |
| 54 | +await db.fin(); // Commit both |
| 55 | + |
| 56 | +// Disconnect |
| 57 | +await db.disconnect(); |
| 58 | +``` |
| 59 | + |
| 60 | +## Configuration |
| 61 | + |
| 62 | +### Environment Variables |
| 63 | + |
| 64 | +| Variable | Default | Description | |
| 65 | +|----------|---------|-------------| |
| 66 | +| `PORT` | 3000 | Server port | |
| 67 | +| `DATA_DIR` | /data | Persistent storage path | |
| 68 | +| `MAX_MEMORY_MB` | 256 | LRU cache size in MB | |
| 69 | +| `ENCRYPTION_KEY` | (none) | AES-256-GCM encryption key (64 hex chars) | |
| 70 | +| `AUTH_REQUIRED` | false | Require authentication | |
| 71 | + |
| 72 | +### docker-compose.yml |
| 73 | + |
| 74 | +```yaml |
| 75 | +version: '3.8' |
| 76 | + |
| 77 | +services: |
| 78 | + bri: |
| 79 | + build: . |
| 80 | + ports: |
| 81 | + - "3000:3000" |
| 82 | + volumes: |
| 83 | + - bri-data:/data |
| 84 | + environment: |
| 85 | + - PORT=3000 |
| 86 | + - DATA_DIR=/data |
| 87 | + - MAX_MEMORY_MB=256 |
| 88 | + # - ENCRYPTION_KEY=your-64-char-hex-key |
| 89 | + |
| 90 | +volumes: |
| 91 | + bri-data: |
| 92 | +``` |
| 93 | +
|
| 94 | +### Generate Encryption Key |
| 95 | +
|
| 96 | +```bash |
| 97 | +openssl rand -hex 32 |
| 98 | +``` |
| 99 | + |
| 100 | +## API Mapping |
| 101 | + |
| 102 | +| Local BRI | Remote BRI | Description | |
| 103 | +|-----------|------------|-------------| |
| 104 | +| `db.get.user(id)` | `db.get.user(id)` | Get by ID | |
| 105 | +| `db.get.userS()` | `db.get.userS()` | Get all | |
| 106 | +| `db.get.userS({...})` | `db.get.userS({...})` | Query | |
| 107 | +| `db.add.user(data)` | `db.add.user(data)` | Create | |
| 108 | +| `db.set.user(data)` | `db.set.user(data)` | Replace | |
| 109 | +| `db.del.user(id)` | `db.del.user(id)` | Delete | |
| 110 | +| `entity.and.field` | `entity.and.field` | Populate | |
| 111 | +| `entity.save()` | `entity.save()` | Save changes | |
| 112 | +| `db.sub.user(fn)` | `db.sub.user(fn)` | Subscribe | |
| 113 | +| `db.rec()` | `db.rec()` | Start transaction | |
| 114 | +| `db.fin()` | `db.fin()` | Commit | |
| 115 | +| `db.nop()` | `db.nop()` | Cancel | |
| 116 | + |
| 117 | +## Architecture |
| 118 | + |
| 119 | +``` |
| 120 | +┌─────────────────────────────────────────────────────────┐ |
| 121 | +│ Docker Container │ |
| 122 | +│ │ |
| 123 | +│ Bun Server (api-ape WebSocket) │ |
| 124 | +│ ├── BRI Database Instance │ |
| 125 | +│ │ ├── In-memory LRU cache │ |
| 126 | +│ │ ├── WAL (Write-Ahead Log) │ |
| 127 | +│ │ └── Cold storage (JSON files) │ |
| 128 | +│ │ │ |
| 129 | +│ └── Volume mount: /data │ |
| 130 | +│ │ |
| 131 | +│ Port: 3000 │ |
| 132 | +└─────────────────────────────────────────────────────────┘ |
| 133 | + ↕ WebSocket (ws://host:3000/api/ape) |
| 134 | +┌─────────────────────────────────────────────────────────┐ |
| 135 | +│ Your Application │ |
| 136 | +│ import { createRemoteDB } from 'bri/remote' │ |
| 137 | +└─────────────────────────────────────────────────────────┘ |
| 138 | +``` |
| 139 | + |
| 140 | +## Health Check |
| 141 | + |
| 142 | +The server exposes a health check endpoint: |
| 143 | + |
| 144 | +```bash |
| 145 | +curl http://localhost:3000/api/ape/ping |
| 146 | +# {"ok":true,"timestamp":1234567890} |
| 147 | +``` |
| 148 | + |
| 149 | +## Development |
| 150 | + |
| 151 | +```bash |
| 152 | +# Build without running |
| 153 | +docker-compose build |
| 154 | + |
| 155 | +# Run with logs |
| 156 | +docker-compose up |
| 157 | + |
| 158 | +# Run in background |
| 159 | +docker-compose up -d |
| 160 | + |
| 161 | +# View logs |
| 162 | +docker-compose logs -f |
| 163 | + |
| 164 | +# Stop |
| 165 | +docker-compose down |
| 166 | + |
| 167 | +# Stop and remove volumes |
| 168 | +docker-compose down -v |
| 169 | +``` |
| 170 | + |
| 171 | +## Persistence |
| 172 | + |
| 173 | +Data is stored in a Docker volume (`bri-data`) at `/data` inside the container. This includes: |
| 174 | + |
| 175 | +- **WAL files**: Write-ahead log for durability |
| 176 | +- **Cold storage**: JSON files for persistent data |
| 177 | +- **Snapshots**: Periodic snapshots for faster recovery |
| 178 | + |
| 179 | +To backup: |
| 180 | + |
| 181 | +```bash |
| 182 | +docker run --rm -v bri-data:/data -v $(pwd):/backup alpine tar czf /backup/bri-backup.tar.gz /data |
| 183 | +``` |
| 184 | + |
| 185 | +To restore: |
| 186 | + |
| 187 | +```bash |
| 188 | +docker run --rm -v bri-data:/data -v $(pwd):/backup alpine tar xzf /backup/bri-backup.tar.gz -C / |
| 189 | +``` |
0 commit comments