A database in TSV text files: data you can read, fast inserts, and an optional Rust speed-up.

0 downloads

Download zip
Clone
Clone with git
git clone https://gitstable.com/git/askrobots/dbbasic-tsv.git
HTTPS
https://gitstable.com/git/askrobots/dbbasic-tsv.git
SSH
ssh://git@gitstable.com:2222/askrobots/dbbasic-tsv.git

Cloning needs no sign-in. Pushing over HTTPS asks for your username and an access token. SSH needs your key under SSH keys.

danq9 Add launch announcement for dbbasic-tsv v1.0.1 1c9b0b8 1 year, 1 month ago History
tables.py · 1.6 KB · 64 lines
1"""
2API endpoint: GET /admin/api/tables
3Returns list of all TSV tables with metadata
4"""
5
6import json
7from pathlib import Path
8
9
10def GET(request):
11 """List all TSV tables in data/ directory"""
12 data_dir = Path("data")
13
14 if not data_dir.exists():
15 return json_response([])
16
17 tables = []
18
19 for tsv_file in data_dir.glob("*.tsv"):
20 try:
21 # Get file stats
22 stat = tsv_file.stat()
23 size_kb = stat.st_size / 1024
24
25 # Read first line to get columns
26 with open(tsv_file, 'r') as f:
27 first_line = f.readline().strip()
28 columns = first_line.split('\t') if first_line else []
29
30 # Count rows (excluding header)
31 row_count = sum(1 for line in f)
32
33 # Format size
34 if size_kb < 1:
35 size_str = f"{stat.st_size} B"
36 elif size_kb < 1024:
37 size_str = f"{size_kb:.1f} KB"
38 else:
39 size_str = f"{size_kb/1024:.1f} MB"
40
41 tables.append({
42 'name': tsv_file.stem,
43 'columns': columns,
44 'rows': row_count,
45 'size': size_str,
46 'size_bytes': stat.st_size
47 })
48 except Exception as e:
49 print(f"Error reading {tsv_file}: {e}")
50 continue
51
52 # Sort by name
53 tables.sort(key=lambda t: t['name'])
54
55 return json_response(tables)
56
57
58def json_response(data, status=200):
59 """Helper to return JSON response"""
60 return {
61 'status': status,
62 'headers': {'Content-Type': 'application/json'},
63 'body': json.dumps(data)
64 }