askrobots / dbbasic-cli
Public
Make a complete, running app (a CRM, a shop, a blog) with one command, such as dbbasic crm.
engine.py · 16.5 KB · 469 lines
| 1 | """ |
| 2 | DBBasic Engine - Unified App Server |
| 3 | The crate engine that replaces your entire stack |
| 4 | """ |
| 5 | |
| 6 | from fastapi import FastAPI, HTTPException |
| 7 | from fastapi.responses import HTMLResponse, JSONResponse |
| 8 | import duckdb |
| 9 | import yaml |
| 10 | from pathlib import Path |
| 11 | from typing import Any, Dict, List, Optional |
| 12 | import json |
| 13 | import asyncio |
| 14 | from datetime import datetime |
| 15 | import hashlib |
| 16 | from collections import defaultdict, OrderedDict |
| 17 | import uvicorn |
| 18 | |
| 19 | # In-memory stores (will be backed by DuckDB) |
| 20 | QUEUES = defaultdict(list) |
| 21 | CACHE = OrderedDict() |
| 22 | CACHE_MAX_SIZE = 1000 |
| 23 | JOBS = [] |
| 24 | METRICS = defaultdict(int) |
| 25 | |
| 26 | class DBBasicEngine: |
| 27 | """The unified app server - one process to rule them all""" |
| 28 | |
| 29 | def __init__(self, config_file="dbbasic.yaml"): |
| 30 | self.config_file = config_file |
| 31 | self.app = FastAPI(title="DBBasic App") |
| 32 | self.db = None |
| 33 | self.config = {} |
| 34 | self.models = {} |
| 35 | |
| 36 | # Load configuration |
| 37 | self.load_config() |
| 38 | |
| 39 | # Initialize database |
| 40 | self.init_database() |
| 41 | |
| 42 | # Setup all endpoints |
| 43 | self.setup_routes() |
| 44 | self.setup_crud_routes() |
| 45 | self.setup_queue_routes() |
| 46 | self.setup_cache_routes() |
| 47 | self.setup_job_routes() |
| 48 | self.setup_ui_routes() |
| 49 | |
| 50 | def load_config(self): |
| 51 | """Load YAML configuration""" |
| 52 | config_path = Path(self.config_file) |
| 53 | if config_path.exists(): |
| 54 | with open(config_path) as f: |
| 55 | self.config = yaml.safe_load(f) or {} |
| 56 | else: |
| 57 | self.config = { |
| 58 | "project": {"name": "DBBasic App", "type": "default"}, |
| 59 | "models": {} |
| 60 | } |
| 61 | |
| 62 | def init_database(self): |
| 63 | """Initialize DuckDB with schema from config""" |
| 64 | self.db = duckdb.connect("dbbasic.db") |
| 65 | |
| 66 | # Create tables for each model |
| 67 | if "models" in self.config: |
| 68 | for model_def in self.config["models"]: |
| 69 | if isinstance(model_def, dict): |
| 70 | for model_name, model_config in model_def.items(): |
| 71 | self.create_model_table(model_name, model_config) |
| 72 | |
| 73 | def create_model_table(self, name: str, config: dict): |
| 74 | """Create a DuckDB table from model config""" |
| 75 | fields = config.get("fields", []) |
| 76 | |
| 77 | # Build CREATE TABLE statement |
| 78 | columns = ["id INTEGER PRIMARY KEY"] |
| 79 | |
| 80 | for field in fields: |
| 81 | field_name = field.get("name") |
| 82 | field_type = field.get("type", "string") |
| 83 | |
| 84 | if not field_name: |
| 85 | continue |
| 86 | |
| 87 | # Map types to DuckDB |
| 88 | type_map = { |
| 89 | "string": "VARCHAR", |
| 90 | "text": "TEXT", |
| 91 | "integer": "INTEGER", |
| 92 | "decimal": "DECIMAL(10, 2)", |
| 93 | "boolean": "BOOLEAN", |
| 94 | "date": "DATE", |
| 95 | "datetime": "TIMESTAMP", |
| 96 | "email": "VARCHAR", |
| 97 | "reference": "INTEGER" |
| 98 | } |
| 99 | |
| 100 | # Handle enum specially |
| 101 | if field_type == "enum" or "enum[" in str(field): |
| 102 | sql_type = "VARCHAR" |
| 103 | else: |
| 104 | sql_type = type_map.get(field_type, "VARCHAR") |
| 105 | |
| 106 | # Add constraints |
| 107 | constraints = [] |
| 108 | if field.get("required"): |
| 109 | constraints.append("NOT NULL") |
| 110 | if field.get("unique"): |
| 111 | constraints.append("UNIQUE") |
| 112 | |
| 113 | column_def = f"{field_name} {sql_type}" |
| 114 | if constraints: |
| 115 | column_def += " " + " ".join(constraints) |
| 116 | |
| 117 | columns.append(column_def) |
| 118 | |
| 119 | # Create table |
| 120 | create_sql = f"CREATE TABLE IF NOT EXISTS {name} ({', '.join(columns)})" |
| 121 | self.db.execute(create_sql) |
| 122 | |
| 123 | self.models[name] = config |
| 124 | |
| 125 | def setup_routes(self): |
| 126 | """Setup basic routes""" |
| 127 | |
| 128 | @self.app.get("/") |
| 129 | async def root(): |
| 130 | return HTMLResponse(self.render_home()) |
| 131 | |
| 132 | @self.app.get("/health") |
| 133 | async def health(): |
| 134 | METRICS["health_checks"] += 1 |
| 135 | return {"status": "healthy", "app": self.config.get("project", {}).get("name")} |
| 136 | |
| 137 | @self.app.get("/api/config") |
| 138 | async def get_config(): |
| 139 | return self.config |
| 140 | |
| 141 | @self.app.get("/api/metrics") |
| 142 | async def get_metrics(): |
| 143 | return dict(METRICS) |
| 144 | |
| 145 | def setup_crud_routes(self): |
| 146 | """Setup CRUD routes for all models""" |
| 147 | |
| 148 | for model_name in self.models: |
| 149 | # List/Create |
| 150 | @self.app.get(f"/api/{model_name}") |
| 151 | async def list_items(model=model_name): |
| 152 | METRICS[f"{model}_list"] += 1 |
| 153 | result = self.db.execute(f"SELECT * FROM {model}").fetchall() |
| 154 | return [dict(zip([d[0] for d in self.db.description], row)) for row in result] |
| 155 | |
| 156 | @self.app.post(f"/api/{model_name}") |
| 157 | async def create_item(data: dict, model=model_name): |
| 158 | METRICS[f"{model}_create"] += 1 |
| 159 | |
| 160 | # Build INSERT statement |
| 161 | fields = list(data.keys()) |
| 162 | values = list(data.values()) |
| 163 | placeholders = ", ".join(["?" for _ in values]) |
| 164 | |
| 165 | sql = f"INSERT INTO {model} ({', '.join(fields)}) VALUES ({placeholders})" |
| 166 | self.db.execute(sql, values) |
| 167 | |
| 168 | # Queue event |
| 169 | QUEUES[f"{model}_events"].append({ |
| 170 | "action": "created", |
| 171 | "model": model, |
| 172 | "data": data, |
| 173 | "timestamp": datetime.now().isoformat() |
| 174 | }) |
| 175 | |
| 176 | return {"success": True, "model": model, "data": data} |
| 177 | |
| 178 | # Get/Update/Delete |
| 179 | @self.app.get(f"/api/{model_name}/{{item_id}}") |
| 180 | async def get_item(item_id: int, model=model_name): |
| 181 | METRICS[f"{model}_get"] += 1 |
| 182 | result = self.db.execute(f"SELECT * FROM {model} WHERE id = ?", [item_id]).fetchone() |
| 183 | if result: |
| 184 | return dict(zip([d[0] for d in self.db.description], result)) |
| 185 | raise HTTPException(status_code=404, detail="Not found") |
| 186 | |
| 187 | def setup_queue_routes(self): |
| 188 | """Setup message queue routes (Kafka replacement)""" |
| 189 | |
| 190 | @self.app.post("/api/queue/{queue_name}/publish") |
| 191 | async def publish_message(queue_name: str, message: dict): |
| 192 | METRICS["queue_publish"] += 1 |
| 193 | QUEUES[queue_name].append({ |
| 194 | "message": message, |
| 195 | "timestamp": datetime.now().isoformat() |
| 196 | }) |
| 197 | return {"success": True, "queue": queue_name, "size": len(QUEUES[queue_name])} |
| 198 | |
| 199 | @self.app.get("/api/queue/{queue_name}/consume") |
| 200 | async def consume_message(queue_name: str): |
| 201 | METRICS["queue_consume"] += 1 |
| 202 | if QUEUES[queue_name]: |
| 203 | return QUEUES[queue_name].pop(0) |
| 204 | return None |
| 205 | |
| 206 | @self.app.get("/api/queue/{queue_name}/size") |
| 207 | async def queue_size(queue_name: str): |
| 208 | return {"queue": queue_name, "size": len(QUEUES[queue_name])} |
| 209 | |
| 210 | def setup_cache_routes(self): |
| 211 | """Setup cache routes (Redis replacement)""" |
| 212 | |
| 213 | @self.app.get("/api/cache/{key}") |
| 214 | async def get_cache(key: str): |
| 215 | METRICS["cache_get"] += 1 |
| 216 | if key in CACHE: |
| 217 | # Move to end (LRU) |
| 218 | CACHE.move_to_end(key) |
| 219 | METRICS["cache_hits"] += 1 |
| 220 | return CACHE[key] |
| 221 | METRICS["cache_misses"] += 1 |
| 222 | return None |
| 223 | |
| 224 | @self.app.post("/api/cache/{key}") |
| 225 | async def set_cache(key: str, value: dict): |
| 226 | METRICS["cache_set"] += 1 |
| 227 | |
| 228 | # Implement LRU eviction |
| 229 | if len(CACHE) >= CACHE_MAX_SIZE: |
| 230 | CACHE.popitem(last=False) |
| 231 | |
| 232 | CACHE[key] = { |
| 233 | "value": value, |
| 234 | "timestamp": datetime.now().isoformat() |
| 235 | } |
| 236 | return {"success": True, "key": key} |
| 237 | |
| 238 | @self.app.delete("/api/cache/{key}") |
| 239 | async def delete_cache(key: str): |
| 240 | if key in CACHE: |
| 241 | del CACHE[key] |
| 242 | return {"success": True} |
| 243 | return {"success": False, "error": "Key not found"} |
| 244 | |
| 245 | def setup_job_routes(self): |
| 246 | """Setup job routes (Celery replacement)""" |
| 247 | |
| 248 | @self.app.post("/api/jobs") |
| 249 | async def create_job(job: dict): |
| 250 | METRICS["jobs_created"] += 1 |
| 251 | job_id = hashlib.md5(json.dumps(job).encode()).hexdigest()[:8] |
| 252 | |
| 253 | job_record = { |
| 254 | "id": job_id, |
| 255 | "status": "pending", |
| 256 | "created": datetime.now().isoformat(), |
| 257 | **job |
| 258 | } |
| 259 | |
| 260 | JOBS.append(job_record) |
| 261 | |
| 262 | # Simulate job execution |
| 263 | asyncio.create_task(self.execute_job(job_record)) |
| 264 | |
| 265 | return {"job_id": job_id, "status": "queued"} |
| 266 | |
| 267 | @self.app.get("/api/jobs/{job_id}") |
| 268 | async def get_job_status(job_id: str): |
| 269 | for job in JOBS: |
| 270 | if job["id"] == job_id: |
| 271 | return job |
| 272 | raise HTTPException(status_code=404, detail="Job not found") |
| 273 | |
| 274 | async def execute_job(self, job: dict): |
| 275 | """Execute a job asynchronously""" |
| 276 | await asyncio.sleep(1) # Simulate work |
| 277 | job["status"] = "completed" |
| 278 | job["completed"] = datetime.now().isoformat() |
| 279 | METRICS["jobs_completed"] += 1 |
| 280 | |
| 281 | def setup_ui_routes(self): |
| 282 | """Setup UI routes""" |
| 283 | |
| 284 | @self.app.get("/ui/{model_name}") |
| 285 | async def model_ui(model_name: str): |
| 286 | return HTMLResponse(self.render_model_ui(model_name)) |
| 287 | |
| 288 | def render_home(self): |
| 289 | """Render home page""" |
| 290 | project_name = self.config.get("project", {}).get("name", "DBBasic App") |
| 291 | |
| 292 | models_html = "" |
| 293 | for model_name in self.models: |
| 294 | models_html += f""" |
| 295 | <div class="col-md-4 mb-3"> |
| 296 | <div class="card"> |
| 297 | <div class="card-body"> |
| 298 | <h5 class="card-title">{model_name.title()}</h5> |
| 299 | <p class="card-text">Manage {model_name} records</p> |
| 300 | <a href="/ui/{model_name}" class="btn btn-primary">View</a> |
| 301 | <a href="/api/{model_name}" class="btn btn-outline-secondary">API</a> |
| 302 | </div> |
| 303 | </div> |
| 304 | </div> |
| 305 | """ |
| 306 | |
| 307 | return f""" |
| 308 | <!DOCTYPE html> |
| 309 | <html> |
| 310 | <head> |
| 311 | <title>{project_name}</title> |
| 312 | <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet"> |
| 313 | </head> |
| 314 | <body> |
| 315 | <nav class="navbar navbar-dark bg-dark mb-4"> |
| 316 | <div class="container"> |
| 317 | <span class="navbar-brand mb-0 h1">{project_name}</span> |
| 318 | </div> |
| 319 | </nav> |
| 320 | |
| 321 | <div class="container"> |
| 322 | <h1>Welcome to {project_name}</h1> |
| 323 | <p class="lead">Built with DBBasic - The crate engine for web apps</p> |
| 324 | |
| 325 | <div class="row mt-4"> |
| 326 | <div class="col-md-3"> |
| 327 | <div class="card text-white bg-primary mb-3"> |
| 328 | <div class="card-body"> |
| 329 | <h5 class="card-title">Models</h5> |
| 330 | <h2>{len(self.models)}</h2> |
| 331 | </div> |
| 332 | </div> |
| 333 | </div> |
| 334 | <div class="col-md-3"> |
| 335 | <div class="card text-white bg-success mb-3"> |
| 336 | <div class="card-body"> |
| 337 | <h5 class="card-title">API Calls</h5> |
| 338 | <h2>{sum(v for k,v in METRICS.items() if k.endswith('_get') or k.endswith('_list'))}</h2> |
| 339 | </div> |
| 340 | </div> |
| 341 | </div> |
| 342 | <div class="col-md-3"> |
| 343 | <div class="card text-white bg-info mb-3"> |
| 344 | <div class="card-body"> |
| 345 | <h5 class="card-title">Cache Hit Rate</h5> |
| 346 | <h2>{int(METRICS.get('cache_hits', 0) / max(1, METRICS.get('cache_hits', 0) + METRICS.get('cache_misses', 0)) * 100)}%</h2> |
| 347 | </div> |
| 348 | </div> |
| 349 | </div> |
| 350 | <div class="col-md-3"> |
| 351 | <div class="card text-white bg-warning mb-3"> |
| 352 | <div class="card-body"> |
| 353 | <h5 class="card-title">Jobs</h5> |
| 354 | <h2>{METRICS.get('jobs_completed', 0)}/{METRICS.get('jobs_created', 0)}</h2> |
| 355 | </div> |
| 356 | </div> |
| 357 | </div> |
| 358 | </div> |
| 359 | |
| 360 | <h2 class="mt-4">Models</h2> |
| 361 | <div class="row"> |
| 362 | {models_html} |
| 363 | </div> |
| 364 | |
| 365 | <h2 class="mt-4">Stack Replacement</h2> |
| 366 | <div class="row"> |
| 367 | <div class="col-md-4"> |
| 368 | <div class="card"> |
| 369 | <div class="card-body"> |
| 370 | <h5 class="card-title">🚀 Message Queue</h5> |
| 371 | <p class="card-text">Replaces Kafka/RabbitMQ</p> |
| 372 | <code>POST /api/queue/orders/publish</code> |
| 373 | </div> |
| 374 | </div> |
| 375 | </div> |
| 376 | <div class="col-md-4"> |
| 377 | <div class="card"> |
| 378 | <div class="card-body"> |
| 379 | <h5 class="card-title">💾 Cache</h5> |
| 380 | <p class="card-text">Replaces Redis/Memcached</p> |
| 381 | <code>GET /api/cache/user:123</code> |
| 382 | </div> |
| 383 | </div> |
| 384 | </div> |
| 385 | <div class="col-md-4"> |
| 386 | <div class="card"> |
| 387 | <div class="card-body"> |
| 388 | <h5 class="card-title">⚙️ Job Queue</h5> |
| 389 | <p class="card-text">Replaces Celery/Sidekiq</p> |
| 390 | <code>POST /api/jobs</code> |
| 391 | </div> |
| 392 | </div> |
| 393 | </div> |
| 394 | </div> |
| 395 | |
| 396 | <div class="mt-4"> |
| 397 | <a href="/docs" class="btn btn-outline-primary">API Documentation</a> |
| 398 | <a href="/api/metrics" class="btn btn-outline-secondary">Metrics</a> |
| 399 | </div> |
| 400 | </div> |
| 401 | </body> |
| 402 | </html> |
| 403 | """ |
| 404 | |
| 405 | def render_model_ui(self, model_name: str): |
| 406 | """Render UI for a specific model""" |
| 407 | return f""" |
| 408 | <!DOCTYPE html> |
| 409 | <html> |
| 410 | <head> |
| 411 | <title>{model_name.title()} - DBBasic</title> |
| 412 | <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet"> |
| 413 | </head> |
| 414 | <body> |
| 415 | <nav class="navbar navbar-dark bg-dark mb-4"> |
| 416 | <div class="container"> |
| 417 | <a class="navbar-brand" href="/">DBBasic</a> |
| 418 | </div> |
| 419 | </nav> |
| 420 | |
| 421 | <div class="container"> |
| 422 | <h1>{model_name.title()}</h1> |
| 423 | <table class="table" id="dataTable"> |
| 424 | <thead> |
| 425 | <tr id="headerRow"></tr> |
| 426 | </thead> |
| 427 | <tbody id="dataBody"></tbody> |
| 428 | </table> |
| 429 | |
| 430 | <button class="btn btn-primary" onclick="addNew()">Add New</button> |
| 431 | </div> |
| 432 | |
| 433 | <script> |
| 434 | // Load data |
| 435 | fetch('/api/{model_name}') |
| 436 | .then(r => r.json()) |
| 437 | .then(data => {{ |
| 438 | if (data.length > 0) {{ |
| 439 | // Build headers |
| 440 | const headers = Object.keys(data[0]); |
| 441 | document.getElementById('headerRow').innerHTML = |
| 442 | headers.map(h => `<th>${{h}}</th>`).join(''); |
| 443 | |
| 444 | // Build rows |
| 445 | document.getElementById('dataBody').innerHTML = |
| 446 | data.map(row => |
| 447 | '<tr>' + headers.map(h => `<td>${{row[h]}}</td>`).join('') + '</tr>' |
| 448 | ).join(''); |
| 449 | }} |
| 450 | }}); |
| 451 | |
| 452 | function addNew() {{ |
| 453 | alert('Add form would go here'); |
| 454 | }} |
| 455 | </script> |
| 456 | </body> |
| 457 | </html> |
| 458 | """ |
| 459 | |
| 460 | |
| 461 | def create_app(config_file="dbbasic.yaml"): |
| 462 | """Create FastAPI application from config""" |
| 463 | engine = DBBasicEngine(config_file) |
| 464 | return engine.app |
| 465 | |
| 466 | |
| 467 | if __name__ == "__main__": |
| 468 | app = create_app() |
| 469 | uvicorn.run(app, host="0.0.0.0", port=8000) |