A full-stack web application that allows users to execute SQL queries in real-time with a modern, intuitive interface. Built with FastAPI (Python) backend and Next.js (React) frontend.
- User registration and login with JWT tokens
- Secure session management
- Protected routes and API endpoints
- Read-only mode for safe query execution
- Execute SQL queries in real-time
- View results in formatted tables
- Support for complex queries (JOINs, GROUP BY, etc.)
- Query history tracking
- Export results to CSV or JSON
- Browse all available database tables
- View table schemas (column names and types)
- Preview sample data from each table
- Quick query templates (SELECT, COUNT, etc.)
- Track all executed queries
- Replay previous queries with one click
- Delete individual queries or clear all history
- Relative timestamps for easy reference
- Clean, responsive design with Tailwind CSS
- Three-panel layout (Tables, Editor, History)
- Loading states and error handling
- Keyboard shortcuts (Ctrl+Enter to run)
- SQL query formatting
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Frontend (Next.js) β
β ββββββββββββββββ ββββββββββββββββ ββββββββββββββββ β
β β Auth UI β β Query Editor β βTable Explorerβ β
β ββββββββββββββββ ββββββββββββββββ ββββββββββββββββ β
βββββββββββββββββββββββββββββ¬ββββββββββββββββββββββββββββββββββ
β REST API (JWT Auth)
βββββββββββββββββββββββββββββ΄ββββββββββββββββββββββββββββββββββ
β Backend (FastAPI) β
β ββββββββββββββββ ββββββββββββββββ ββββββββββββββββ β
β β Auth Routes β βQuery Executorβ βTable Manager β β
β ββββββββββββββββ ββββββββββββββββ ββββββββββββββββ β
βββββββββββββββββββββββββββββ¬ββββββββββββββββββββββββββββββββββ
β SQLAlchemy ORM
βββββββββββββββββββββββββββββ΄ββββββββββββββββββββββββββββββββββ
β SQLite Database β
β ββββββββββββββββ ββββββββββββββββ ββββββββββββββββ β
β β Users β βQueryHistory β βSample Tables β β
β ββββββββββββββββ ββββββββββββββββ ββββββββββββββββ β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
- Framework: FastAPI 0.109
- Database: SQLite with SQLAlchemy ORM
- Authentication: JWT (python-jose) + bcrypt
- Validation: Pydantic schemas
- Server: Uvicorn ASGI server
- Framework: Next.js 14 (Pages Directory)
- Language: JavaScript/TypeScript
- Styling: Tailwind CSS
- HTTP Client: Native Fetch API
- State Management: React Context + useState
- Containerization: Docker & Docker Compose
- Database: SQLite (persistent volume)
- Docker Desktop 20.10+ (Windows/Mac) or Docker Engine (Linux)
- Docker Compose 2.0+
- 4GB RAM minimum
- 2GB free disk space
- Python 3.11+
- Node.js 18+
- npm or yarn
- SQLite3
git clone https://github.com/DarshD22/SQL_Runner.git
cd SQL_RunnerCreate a .env file in the root directory:
# Backend Configuration
SECRET_KEY=your-super-secret-key
READ_ONLY_MODE=True
DEBUG=False
# Frontend Configuration
NEXT_PUBLIC_API_URL=http://localhost:8000# Build and start all services
docker-compose up --build
First build takes 5-10 minutes. Subsequent starts take ~30 seconds.
- Frontend: http://localhost:3000
- Backend API: http://localhost:8000
- API Documentation: http://localhost:8000/docs
- Alternative API Docs: http://localhost:8000/redoc
- Navigate to http://localhost:3000/register
- Create an account (username, password)
- Login with your credentials
- Start running SQL queries!
# Stop containers (keep data)
docker-compose down
# Stop and remove volumes (delete data)
docker-compose down -v-
Navigate to backend directory
cd backend -
Create virtual environment
python -m venv venv # Activate on Windows venv\Scripts\activate # Activate on Mac/Linux source venv/bin/activate
-
Install dependencies
pip install -r requirements.txt
-
Create .env file
# Create backend/.env SECRET_KEY=your-secret-key READ_ONLY_MODE=True DEBUG=True -
Run the backend
python -m app.main # or uvicorn app.main:app --reloadBackend will start at: http://localhost:8000
-
Navigate to frontend directory
cd frontend -
Install dependencies
npm install # or yarn install -
Create .env.local file
# Create frontend/.env.local NEXT_PUBLIC_API_URL=http://localhost:8000 -
Run the frontend
npm run dev # or yarn devFrontend will start at: http://localhost:3000
sql-runner/
βββ backend/
β βββ app/
β β βββ main.py # FastAPI application entry point
β β βββ config.py # Configuration settings
β β βββ db.py # Database connection and helpers
β β βββ models.py # SQLAlchemy models (User, QueryHistory)
β β βββ schemas.py # Pydantic schemas for validation
β β βββ auth.py # Authentication helpers (JWT, bcrypt)
β β βββ utils.py # Utility functions
β β βββ routes/
β β β βββ auth_routes.py # Authentication endpoints
β β β βββ query_routes.py # Query execution endpoints
β β β βββ tables_routes.py # Table information endpoints
β β β βββ history_routes.py# Query history endpoints
β βββ requirements.txt # Python dependencies
β βββ Dockerfile # Backend Docker configuration
β βββ .dockerignore
β
βββ frontend/
β βββ src/
β β βββ pages/
β β β βββ _app.jsx # Next.js app wrapper
β β β βββ index.jsx # Main SQL runner page
β β β βββ login.jsx # Login page
β β β βββ register.jsx # Registration page
β β βββ components/
β β β βββ Layout.jsx # Page layout wrapper
β β β βββ QueryEditor.jsx # SQL query input component
β β β βββ ResultsTable.jsx # Query results display
β β β βββ TablesSidebar.jsx# Database tables list
β β β βββ TablePreview.jsx # Table schema preview
β β β βββ RecentQueries.jsx# Query history sidebar
β β β βββ Loading.jsx # Loading spinner
β β β βββ Error.jsx # Error display
β β βββ services/
β β β βββ api.ts # Generic API client
β β β βββ auth.ts # Authentication service
β β β βββ sqlRunner.ts # SQL execution service
β β βββ hooks/
β β β βββ useAuth.js # Authentication hook
β β βββ styles/
β β βββ globals.css # Global styles
β βββ package.json # Node dependencies
β βββ next.config.js # Next.js configuration
β βββ tailwind.config.js # Tailwind CSS configuration
β βββ Dockerfile # Frontend Docker configuration
β βββ .dockerignore
β
βββ docker-compose.yml # Docker Compose orchestration
βββ .env # Environment variables
βββ README.md # This file
CREATE TABLE Customers (
customer_id INTEGER PRIMARY KEY AUTOINCREMENT,
first_name VARCHAR(100),
last_name VARCHAR(100),
age INTEGER,
country VARCHAR(100)
);CREATE TABLE Orders (
order_id INTEGER PRIMARY KEY AUTOINCREMENT,
item VARCHAR(100),
amount INTEGER,
customer_id INTEGER,
FOREIGN KEY (customer_id) REFERENCES Customers(customer_id)
);CREATE TABLE Shippings (
shipping_id INTEGER PRIMARY KEY AUTOINCREMENT,
status VARCHAR(100),
customer INTEGER
);CREATE TABLE users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
username VARCHAR(100) UNIQUE NOT NULL,
password_hash VARCHAR(255) NOT NULL,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
);CREATE TABLE query_history (
id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id INTEGER NOT NULL,
sql_text TEXT NOT NULL,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES users(id)
);POST /auth/register- Register new userPOST /auth/login- Login user (returns JWT token)POST /auth/logout- Logout userGET /auth/me- Get current user info
POST /api/query/run- Execute SQL query (Protected)
GET /api/tables- List all tables (Protected)GET /api/tables/{table_name}- Get table schema and preview (Protected)
GET /api/history- Get query history (Protected)DELETE /api/history/{id}- Delete specific query (Protected)DELETE /api/history- Clear all history (Protected)
GET /- API health checkGET /health- Health status
Full API Documentation: http://localhost:8000/docs
SELECT * FROM Customers LIMIT 10;SELECT c.first_name, c.last_name, o.item, o.amount
FROM Customers c
JOIN Orders o ON c.customer_id = o.customer_id
WHERE c.country = 'USA';SELECT country, COUNT(*) as customer_count
FROM Customers
GROUP BY country
ORDER BY customer_count DESC;SELECT
c.country,
COUNT(DISTINCT c.customer_id) as customers,
COUNT(o.order_id) as orders,
SUM(o.amount) as total_revenue
FROM Customers c
LEFT JOIN Orders o ON c.customer_id = o.customer_id
GROUP BY c.country
HAVING COUNT(o.order_id) > 0
ORDER BY total_revenue DESC;# Start services
docker-compose up
# Start in background
docker-compose up -d
# Stop services
docker-compose down
# View logs
docker-compose logs
# Follow logs in real-time
docker-compose logs -f
# Rebuild and restart
docker-compose up --build
# Reset everything
docker-compose down -v
docker-compose up --build# Build backend
cd backend
docker build -t sql-runner-backend .
# Run backend
docker run -d -p 8000:8000 --name backend sql-runner-backend
# Build frontend
cd frontend
docker build -t sql-runner-frontend .
# Run frontend
docker run -d -p 3000:3000 --name frontend sql-runner-frontend- β JWT-based authentication
- β Password hashing with bcrypt
- β Secure session management
- β Protected API endpoints
- β Read-only mode (blocks INSERT, UPDATE, DELETE, DROP)
- β Input validation and sanitization
- β Query length limits
- β CORS configuration
- β HTTP security headers
- β Non-root Docker containers
- β Environment variable management
# Database
DATABASE_URL = "sqlite:///./sql_runner.db"
# Security
SECRET_KEY = "your-secret-key" # Change in production!
ALGORITHM = "HS256"
ACCESS_TOKEN_EXPIRE_MINUTES = 1440 # 24 hours
# SQL Safety
READ_ONLY_MODE = True # Blocks write operations
BLOCKED_SQL_KEYWORDS = ["INSERT", "UPDATE", "DELETE", "DROP", "ALTER"]
# Limits
MAX_QUERY_LENGTH = 5000
MAX_PREVIEW_ROWS = 10
MAX_HISTORY_ITEMS = 50# API URL
NEXT_PUBLIC_API_URL=http://localhost:8000
# Environment
NODE_ENV=developmentcd backend
# Test health endpoint
curl http://localhost:8000/health
# Test registration (using curl)
curl -X POST http://localhost:8000/auth/register \
-H "Content-Type: application/json" \
-d '{"username":"testuser","password":"testpass123"}'
# Test login
curl -X POST http://localhost:8000/auth/login \
-H "Content-Type: application/json" \
-d '{"username":"testuser","password":"testpass123"}'cd frontend
# Run build
npm run build
# Test production build
npm startVisit http://localhost:8000/docs for interactive API testing with Swagger UI.