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
query.py · 1.9 KB · 73 lines
1"""
2API endpoint: GET /admin/api/query
3Query TSV table with filters and limits
4"""
5
6import json
7from pathlib import Path
8
9from dbbasic.tsv import TSV
10
11
12def GET(request):
13 """Query a TSV table"""
14 params = request.get('params', {})
15
16 table_name = params.get('table')
17 where_clause = params.get('where', '')
18 limit = int(params.get('limit', 100))
19
20 if not table_name:
21 return json_response({'error': 'table parameter required'}, 400)
22
23 try:
24 # Open table
25 tsv = TSV(table_name, data_dir="data")
26
27 # Parse where clause (simple key=value format)
28 filters = {}
29 if where_clause:
30 if '=' in where_clause:
31 key, val = where_clause.split('=', 1)
32 filters[key.strip()] = val.strip()
33
34 # Query with filters
35 if filters:
36 results = list(tsv.query(**filters))
37 else:
38 results = list(tsv.all())
39
40 # Apply limit
41 results = results[:limit]
42
43 # Get columns from first result or table schema
44 columns = []
45 if results:
46 columns = list(results[0].keys())
47 else:
48 # Try to get columns from file header
49 tsv_path = Path("data") / f"{table_name}.tsv"
50 if tsv_path.exists():
51 with open(tsv_path, 'r') as f:
52 first_line = f.readline().strip()
53 columns = first_line.split('\t')
54
55 return json_response({
56 'columns': columns,
57 'rows': results,
58 'count': len(results)
59 })
60
61 except FileNotFoundError:
62 return json_response({'error': f'Table {table_name} not found'}, 404)
63 except Exception as e:
64 return json_response({'error': str(e)}, 500)
65
66
67def json_response(data, status=200):
68 """Helper to return JSON response"""
69 return {
70 'status': status,
71 'headers': {'Content-Type': 'application/json'},
72 'body': json.dumps(data)
73 }