Skip to content

Commit 0b008a9

Browse files
committed
feat: Initialize Elysia backup demo, replacing SQLite with JSON data files and adding documentation.
1 parent 258db3e commit 0b008a9

File tree

9 files changed

+278
-12
lines changed

9 files changed

+278
-12
lines changed

demo/.env.example

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ R2_SECRET_ACCESS_KEY=your-secret-key
55
R2_ENDPOINT=https://your-account.r2.cloudflarestorage.com
66

77
# Backup Source Directory
8-
BACKUP_SOURCE_DIR=./demo/data
8+
BACKUP_SOURCE_DIR=./data

demo/README.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Elysia Backup Demo
2+
3+
Demo completo mostrando o uso do plugin `@riligar/elysia-backup` com um sistema de dados JSON.
4+
5+
## Instalação
6+
7+
```bash
8+
cd demo
9+
bun install
10+
```
11+
12+
## Configuração
13+
14+
Copie o arquivo de exemplo e configure suas credenciais R2/S3:
15+
16+
```bash
17+
cp .env.example .env
18+
```
19+
20+
Edite o `.env` com suas credenciais:
21+
22+
```env
23+
R2_BUCKET=your-bucket-name
24+
R2_ACCESS_KEY_ID=your-access-key
25+
R2_SECRET_ACCESS_KEY=your-secret-key
26+
R2_ENDPOINT=https://your-account.r2.cloudflarestorage.com
27+
```
28+
29+
## Executar
30+
31+
```bash
32+
bun run start
33+
# ou para desenvolvimento com hot reload:
34+
bun run dev
35+
```
36+
37+
## Endpoints
38+
39+
| Endpoint | Descrição |
40+
| -------------------- | ---------------- |
41+
| `GET /` | Página inicial |
42+
| `GET /backup` | Painel de backup |
43+
| `GET /api/users` | Listar usuários |
44+
| `POST /api/users` | Criar usuário |
45+
| `GET /api/products` | Listar produtos |
46+
| `POST /api/products` | Criar produto |
47+
| `GET /api/orders` | Listar pedidos |
48+
| `POST /api/orders` | Criar pedido |
49+
50+
## Estrutura de Dados
51+
52+
Os dados são armazenados em arquivos JSON no diretório `./data`:
53+
54+
- `users.json` - Dados de usuários
55+
- `products.json` - Catálogo de produtos
56+
- `orders.json` - Pedidos
57+
58+
Estes arquivos são automaticamente incluídos no backup.

demo/bun.lock

Lines changed: 96 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

demo/data/example.sqlite

Lines changed: 0 additions & 1 deletion
This file was deleted.

demo/data/orders.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"orders": []
3+
}

demo/data/products.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"products": [
3+
{
4+
"id": 1,
5+
"name": "Laptop",
6+
"price": 2999.99,
7+
"stock": 10
8+
},
9+
{
10+
"id": 2,
11+
"name": "Mouse",
12+
"price": 79.99,
13+
"stock": 50
14+
},
15+
{
16+
"id": 3,
17+
"name": "Teclado",
18+
"price": 149.99,
19+
"stock": 30
20+
}
21+
]
22+
}

demo/data/users.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"users": [
3+
{
4+
"id": 1,
5+
"name": "João Silva",
6+
"email": "joao@example.com",
7+
"createdAt": "2025-12-14T17:47:35.248Z"
8+
},
9+
{
10+
"id": 2,
11+
"name": "Maria Santos",
12+
"email": "maria@example.com",
13+
"createdAt": "2025-12-14T17:47:35.249Z"
14+
}
15+
]
16+
}

demo/index.js

Lines changed: 68 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,21 @@
11
/**
2-
* Demo server for elysia-backup plugin
3-
* Run with: bun run demo/index.js
2+
* Demo server for @riligar/elysia-backup plugin
3+
*
4+
* Run with: bun run start
45
*/
56

67
import { Elysia } from 'elysia'
7-
import { r2Backup } from '../src/index.js'
8+
import { r2Backup } from '@riligar/elysia-backup'
9+
import { readFileSync, writeFileSync } from 'fs'
10+
11+
// JSON file paths
12+
const USERS_FILE = './data/users.json'
13+
const PRODUCTS_FILE = './data/products.json'
14+
const ORDERS_FILE = './data/orders.json'
15+
16+
// Helper functions
17+
const read = file => JSON.parse(readFileSync(file, 'utf-8'))
18+
const write = (file, data) => writeFileSync(file, JSON.stringify(data, null, 2))
819

920
const app = new Elysia()
1021
.use(
@@ -13,15 +24,62 @@ const app = new Elysia()
1324
accessKeyId: process.env.R2_ACCESS_KEY_ID || '',
1425
secretAccessKey: process.env.R2_SECRET_ACCESS_KEY || '',
1526
endpoint: process.env.R2_ENDPOINT || 'https://example.r2.cloudflarestorage.com',
16-
sourceDir: process.env.BACKUP_SOURCE_DIR || './demo/data',
27+
sourceDir: './data',
1728
prefix: 'backups/',
18-
extensions: ['.db', '.sqlite', '.json'],
19-
cronSchedule: '0 * * * *', // Every hour
20-
cronEnabled: false, // Disabled by default for demo
29+
extensions: ['.json'],
30+
cronSchedule: '0 * * * *',
31+
cronEnabled: false,
2132
})
2233
)
23-
.get('/', () => 'Elysia Backup Demo - Go to /backup for the UI')
34+
.get(
35+
'/',
36+
() => `<pre style="font-family:monospace;color:#374151;line-height:1.4;padding:3rem">
37+
🦊 Elysia Backup Demo
38+
39+
<b style="color:#111827">Backup UI</b>
40+
/backup
41+
42+
<b style="color:#111827">Users</b>
43+
GET /api/users
44+
POST /api/users { name, email }
45+
46+
<b style="color:#111827">Products</b>
47+
GET /api/products
48+
POST /api/products { name, price, stock }
49+
50+
<b style="color:#111827">Orders</b>
51+
GET /api/orders
52+
POST /api/orders { userId, productId, quantity }
53+
</pre>`
54+
)
55+
// Users API
56+
.get('/api/users', () => read(USERS_FILE))
57+
.post('/api/users', ({ body }) => {
58+
const data = read(USERS_FILE)
59+
const user = { id: Date.now(), ...body, createdAt: new Date().toISOString() }
60+
data.users.push(user)
61+
write(USERS_FILE, data)
62+
return user
63+
})
64+
// Products API
65+
.get('/api/products', () => read(PRODUCTS_FILE))
66+
.post('/api/products', ({ body }) => {
67+
const data = read(PRODUCTS_FILE)
68+
const product = { id: Date.now(), ...body }
69+
data.products.push(product)
70+
write(PRODUCTS_FILE, data)
71+
return product
72+
})
73+
// Orders API
74+
.get('/api/orders', () => read(ORDERS_FILE))
75+
.post('/api/orders', ({ body }) => {
76+
const data = read(ORDERS_FILE)
77+
const order = { id: Date.now(), ...body, createdAt: new Date().toISOString(), status: 'pending' }
78+
data.orders.push(order)
79+
write(ORDERS_FILE, data)
80+
return order
81+
})
2482
.listen(3000)
2583

26-
console.log(`🦊 Elysia Backup Demo running at http://localhost:${app.server?.port}`)
27-
console.log(`📦 Backup UI available at http://localhost:${app.server?.port}/backup`)
84+
console.log(`🦊 Elysia Backup Demo: http://localhost:${app.server?.port}`)
85+
console.log(`📦 Backup UI: http://localhost:${app.server?.port}/backup`)

demo/package.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "elysia-backup-demo",
3+
"version": "1.0.0",
4+
"description": "Demo application for @riligar/elysia-backup plugin",
5+
"type": "module",
6+
"scripts": {
7+
"start": "bun run index.js",
8+
"dev": "bun --watch run index.js"
9+
},
10+
"dependencies": {
11+
"@riligar/elysia-backup": "^1.0.0",
12+
"elysia": "^1.4.19"
13+
}
14+
}

0 commit comments

Comments
 (0)