Teach it your schema. Ask for data. Get SQL.
ASK DATABASE is an open-source, schema-aware workspace for database-specific SQL generation. It combines DDL import, historical SELECT memory, business glossary terms, schema aliases, relationship ranking, a backend OpenAI provider boundary and Safe Mode SQL verification.
Static demo: https://milekv.github.io/ask-database/
- Import a database schema from DDL.
- Add sanitized historical SELECT queries and business terms.
- Ask a question in plain language.
- Review the generated SQL, the evidence used and Safe Mode validation results.
For example, a workspace can turn "show active students with their faculty" into a schema-checked query using the known students and faculties relationship. The public demo uses saved results. Live generation runs only with the API and PostgreSQL configured locally.
ASK DATABASE is not a keyword-to-SQL demo. The production /api/ask pipeline:
- loads a persisted workspace from PostgreSQL,
- retrieves relevant tables, columns, glossary terms and aliases,
- retrieves historical SELECT examples as evidence,
- ranks candidate relationship paths,
- asks the backend OpenAI provider for a structured interpretation,
- generates SQL as a Structured Output,
- validates tables, aliases, columns and Safe Mode,
- may run up to two controlled regeneration attempts after validation errors,
- stores a query version and returns evidence plus a decision log.
- User-created workspaces with dialect, DDL and optional historical SQL.
- PostgreSQL persistence through Fastify services and Drizzle migrations.
- Historical SQL memory with literal redaction and structural analysis.
- Business glossary and aliases influencing retrieval.
- Deterministic relationship path ranking.
- Backend-only OpenAI key handling.
- Responses API with Structured Outputs and Zod validation.
- Honest GitHub Pages static demo with saved examples only.
Schema Memory is created from DDL. ASK DATABASE persists tables, columns, primary keys, foreign keys and relationships in PostgreSQL. Retrieval does not automatically send the entire schema to the provider; it first selects candidate objects with application-calculated evidence.
Query Memory is created from historical SELECTs. Import redacts literals and stores normalized SQL, tables, columns, joins, filters, GROUP BY, ORDER BY and query structure. The most relevant sanitized examples enter SQL generation context.
Correction Memory is stored as workspace rules created from user corrections or API input. Enabled workspace rules can influence retrieval and relationship path ranking. The UI for approving memory is still limited, so advanced memory management is best exercised through the API.
pnpm install
docker compose up -d
pnpm db:migrate
pnpm dev:api
pnpm devWeb:
http://127.0.0.1:5174/
API:
http://127.0.0.1:4310/api/health
Provider secrets stay on the backend:
LLM_PROVIDER=openai
OPENAI_API_KEY=<backend-openai-api-key>
OPENAI_MODEL=gpt-4.1-mini
OPENAI_TIMEOUT_MS=45000The frontend never reads or sends OPENAI_API_KEY.
GitHub Pages cannot host Fastify or PostgreSQL. The public deployment therefore shows University Demo schema, relationships, query memory, glossary and explicitly labeled saved examples. It does not pretend that arbitrary live SQL generation is available.
flowchart TD
Web["React / Vite UI"] --> Api["Fastify API"]
Api --> Repo["WorkspaceRepository"]
Repo --> Pg["PostgreSQL"]
Api --> Core["Core Ask Pipeline"]
Core --> Retriever["Schema + History Retrieval"]
Core --> Paths["Relationship Path Ranking"]
Core --> Validator["SQL Validator / Safe Mode"]
Core --> Provider["LLMProvider"]
Provider --> OpenAI["OpenAI Responses API"]
apps/web React, Vite, Tailwind, Monaco, React Flow
apps/api Fastify, Drizzle, migrations, provider factory
packages/shared types, Zod schemas, SQL helpers
packages/schema-parser DDL parser
packages/sql-memory historical SELECT import and analysis
packages/sql-validator Safe Mode and schema validation
packages/core retrieval, prompts, ask pipeline, demo data
packages/ui shared React components
pnpm install
pnpm db:migrate
pnpm lint
pnpm typecheck
pnpm test
pnpm build
pnpm audit- Live generation requires the backend, PostgreSQL and
OPENAI_API_KEY. - GitHub Pages is static and does not generate arbitrary SQL.
- The UI has basic workspace creation; full step-by-step onboarding, SQL diff, version history UI and manual override need more work.
- The full Test Commerce browser acceptance flow is not complete without a configured provider and manual override UI.
MIT. See LICENSE.
