Skip to content

Commit 749298b

Browse files
author
Brian
committed
feat: add Docker development environment
- Add Docker setup with Dockerfile and docker-compose.yml - Add example projects and documentation - Add remote access and server components for Docker environment
1 parent 857e27f commit 749298b

32 files changed

Lines changed: 3096 additions & 3 deletions

docker/.env.example

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# BRI Docker Configuration
2+
3+
# Server port
4+
PORT=3000
5+
6+
# Maximum memory for LRU cache (in MB)
7+
MAX_MEMORY_MB=256
8+
9+
# Optional: AES-256-GCM encryption key for data at rest
10+
# Generate with: openssl rand -hex 32
11+
# ENCRYPTION_KEY=your-64-char-hex-key-here
12+
13+
# Require authentication for all operations
14+
AUTH_REQUIRED=false

docker/Dockerfile

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# BRI + api-ape Docker Image
2+
# Plug-and-play database server with WebSocket RPC interface
3+
4+
FROM oven/bun:1-alpine
5+
6+
WORKDIR /app
7+
8+
# Copy server files
9+
COPY server/package.json ./
10+
11+
# Install dependencies
12+
RUN bun install --production
13+
14+
# Copy server source
15+
COPY server/ ./
16+
17+
# Create data directory
18+
RUN mkdir -p /data
19+
20+
# Environment defaults
21+
ENV PORT=3000
22+
ENV DATA_DIR=/data
23+
ENV MAX_MEMORY_MB=256
24+
25+
# Expose WebSocket/HTTP port
26+
EXPOSE 3000
27+
28+
# Health check
29+
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
30+
CMD wget --no-verbose --tries=1 --spider http://localhost:${PORT}/api/ape/ping || exit 1
31+
32+
# Run the server
33+
CMD ["bun", "run", "index.js"]

docker/README.md

Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
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

Comments
 (0)