Skip to content

buhusa/sql-studio

Repository files navigation

SQL Studio

Generate production-ready SQL from plain-English data questions, then run a second-pass AI review for correctness, joins, performance, unsafe assumptions, and edge cases.

SQL Studio is a local Next.js app powered by the xAI Responses API. Paste a schema, describe the analysis you want, and get highlighted SQL with explanations, line numbers, copy actions, and review suggestions tied back to exact SQL lines.

SQL Studio dashboard

SQL Studio generated SQL

SQL Studio review details

Features

  • Plain-English question input
  • Schema editor with built-in export tips for PostgreSQL, MySQL, and SQLite
  • Three demo scenarios for quick verification
  • Server-side xAI API calls only
  • Syntax-highlighted SQL output with line numbers
  • Copy buttons for SQL, explanations, and reviews
  • Review workflow that asks Grok to check correctness, performance, joins, assumptions, and edge cases
  • Line-aware review suggestions such as Replace lines 9-12 with:
  • Startup preflight that refuses to run without an API key

Tech Stack

  • Next.js App Router
  • React
  • TypeScript
  • xAI Responses API
  • CSS modules via global app stylesheet
  • Node.js test runner

Requirements

Getting Started

Install dependencies:

npm install

Create .env from the example:

cp .env.example .env

Add your xAI key:

XAI_API_KEY="xai-..."

Optional model override:

XAI_MODEL="grok-4.3"

Run locally:

npm run dev

Open http://localhost:3000.

How It Works

  1. Pick a sample scenario or enter your own question.
  2. Paste database schema notes or DDL into the schema editor.
  3. Click Generate SQL.
  4. Review the SQL, explanation, and line numbers.
  5. Click Review SQL to get production-readiness feedback.

The app calls POST https://api.x.ai/v1/responses from server routes only. XAI_API_KEY is never exposed to browser code.

Schema Tips

The schema editor works best with structure, not data. Include:

  • Table names
  • Column names and data types
  • Primary keys and foreign keys
  • Important constraints
  • Business rules such as amount is stored in cents
  • Timestamp assumptions such as timestamps are UTC
  • Allowed status values such as paid, completed, or cancelled

Useful export commands:

pg_dump --schema-only --no-owner --no-privileges "$DATABASE_URL" > schema.sql
SHOW CREATE TABLE table_name;
sqlite3 database.db ".schema" > schema.sql

Scripts

npm run dev
npm run build
npm run start
npm run typecheck
npm test

Security Notes

  • Keep .env private.
  • Use .env.example as the public template.
  • All xAI calls happen server-side in API routes.
  • The xAI helper is isolated in lib/xai.ts so the endpoint or model can be changed easily.

License

MIT

About

AI-powered SQL generator and review workspace for developers.

Topics

Resources

License

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors