# Persistent Data

Two ways to persist data across rebuilds.

## `/data` directory (backend apps)

Every dynamic container (Node.js, Python, Go) has a persistent `/data` directory. Use the `DATA_DIR` environment variable to reference it.

### Node.js

```javascript
const path = require('path');
const dbPath = path.join(process.env.DATA_DIR, 'app.db');
```

### Python

```python
import os
db_path = os.path.join(os.environ["DATA_DIR"], "app.db")
```

This directory survives code updates and rebuilds. It’s deleted when the deployment is destroyed.

## `/_data/*` REST API (any deployment)

A built-in document store accessible via `fetch()` from any deployed page. No backend code needed — works with static HTML.

All requests go to `/_data/` on the deployment’s own domain:

### JavaScript

```javascript
// Create a document
await fetch('/_data/votes', {
  method: 'POST',
  headers: {'Content-Type': 'application/json'},
  body: JSON.stringify({option: 'pizza'})
});

// List documents
const {documents} = await fetch('/_data/votes').then(r => r.json());

// Get a document
const doc = await fetch('/_data/votes/cs1abc').then(r => r.json());

// Update a document
await fetch('/_data/votes/cs1abc', {
  method: 'PUT',
  headers: {'Content-Type': 'application/json'},
  body: JSON.stringify({option: 'pizza', count: 2})
});

// Delete a document
await fetch('/_data/votes/cs1abc', {method: 'DELETE'});
```

## Endpoints

| Method | Path | Description |
| --- | --- | --- |
| `GET` | `/_data` | List collections |
| `POST` | `/_data/{collection}` | Create a document |
| `GET` | `/_data/{collection}` | List documents (`?limit=10&offset=20`) |
| `GET` | `/_data/{collection}/{id}` | Get a document |
| `PUT` | `/_data/{collection}/{id}` | Update a document |
| `DELETE` | `/_data/{collection}/{id}` | Delete a document |
| `DELETE` | `/_data/{collection}` | Delete entire collection |

## Limits

| Limit | Value |
| --- | --- |
| Max document size | 100 KB |
| Max documents per collection | 10,000 |
| Max collections per deployment | 100 |
| Max total storage | 50 MB |
