DEV Community

Mohd Faizal
Mohd Faizal

Posted on

I Built an AI-Powered Database Schema Explorer for the H0 Hackathon

The following content was created for the purposes of entering the H0 Hackathon. #H0Hackathon


You join a new project. The database has 30 tables. No documentation. No ERD. No one remembers why status_v2_legacy exists.

Existing tools force a tradeoff. pgAdmin gives raw access but no visualization. dbdiagram.io lets you draw schemas but requires manual input that goes stale after every migration. SchemaSpy needs CLI setup and Java. DBeaver is desktop-only — you can't share a link with a new hire.

I looked for a web tool that auto-discovers schemas, visualizes them, documents them, and lets you query them with AI. I found nothing that does all four.

So I built SchemaLens for the H0 Hackathon.

What SchemaLens Does

Paste a PostgreSQL connection string. That's it.

SchemaLens queries information_schema to pull tables, columns, types, constraints, foreign keys, and row estimates. No manual input. No DSL. No install.

From there, you get:

1. Interactive ERD

A full entity-relationship diagram rendered automatically from your real schema. Zoom, pan, click any table to see columns with types, nullability, defaults, and constraints. Foreign keys become edges. The diagram builds itself.

2. Schema Health Scoring

10 automated checks produce a weighted score with letter grade (A–F):

  • Primary keys present
  • Foreign key validity
  • Naming conventions
  • Column types
  • NOT NULL constraints
  • Timestamps
  • Oversized columns
  • Unique constraints
  • Boolean naming
  • Column count

Each finding includes a severity level and suggested fix.

3. AI Schema Agent

This is the feature I'm most proud of. An AI assistant that understands your schema and executes queries against your live database.

Ask in plain English:

"Show me all users who signed up last month"

The agent writes the query, validates it, runs it against your actual database, shows results in a table, and auto-retries on errors.

The tool-calling pipeline:

generate_sql → check_sql → execute_sql → retry loop
Enter fullscreen mode Exit fullscreen mode

You see the agent's reasoning, tool calls, and results streaming in real-time.

4. Shareable Links

One click generates a public URL. Your team sees the full ERD and docs without signing up.

The Security Model

This was the hardest part. The AI agent generates and executes SQL against a live database. That's terrifying.

Two-layer defense:

  1. Application layer — blocks destructive keywords (INSERT, UPDATE, DELETE, DROP, ALTER, TRUNCATE) and injection attempts via regex
  2. Database layer — PostgreSQL itself is set to READ ONLY mode, rejecting any write regardless of what the application misses

Even if the AI generates an INSERT, PostgreSQL rejects it. The agent cannot write data.

This is defense in depth, not just a prompt telling the AI to be nice.

How I Built It

Layer Technology
Framework Next.js 16, React, TypeScript
UI Tailwind v4, shadcn/ui, React Flow
Auth better-auth (email/password)
ORM Drizzle ORM
Database Amazon Aurora PostgreSQL 16
LLM OpenAI-compatible (DeepSeek V4 Flash)
AI SDK Vercel AI SDK — streamText + tool calling
Deployment Vercel

Aurora PostgreSQL is doing the heavy lifting:

  • Auto-discovers schemas from the system catalog
  • Stores schema snapshots as JSONB
  • Handles user data, sessions, chat history, and conversations
  • Serverless scaling — no always-on servers

The Vercel + Aurora combo is legit. Not just a hackathon shortcut — clean integration, fast deploys, real scaling potential.

Challenges I Hit

SSL for Aurora on Vercel

Aurora requires SSL but the database driver rejects self-signed certs. Connection pool failed silently until I configured it to accept the certificate chain. Silent failures are the worst.

Better-auth Drizzle Adapter

Table key naming mismatch caused silent auth failures. The handler swallowed errors and returned empty responses. Had to debug the handler output to find the real error.

AI Agent SQL Safety

Getting the self-correcting retry loop to work reliably with real-time streaming took significant effort. When the AI generates invalid SQL, it reads the database error, fixes the query, and retries automatically.

Schema Health Tuning

What looks good on paper (check every table for primary keys) produces noise on real schemas. Each check needed iteration to balance sensitivity with specificity.

What I Learned

  1. PostgreSQL's system catalog is rich — the database already knows everything about itself. You just have to ask.

  2. AI agents need guardrails, not just prompts — the schema chat works because of defense in depth.

  3. Self-correcting agents need boundaries — the generate → validate → execute → retry loop lets the agent fix its own mistakes, but you need clear retry limits.

  4. Vercel + Aurora is production-ready — not just a hackathon shortcut.

Try It Yourself

  1. Go to schemalens-alpha.vercel.app
  2. Sign up for a free account
  3. Click "Connect database"
  4. Paste your PostgreSQL connection string (any PostgreSQL DB works — Aurora, Railway, Supabase, etc.)

  5. The ERD renders automatically

  6. Open Schema Chat and try:

    • "list all tables"
    • "which tables have no foreign keys?"
    • "show me the 5 largest tables by row count"
    • "if I delete a connector, which related data would be affected?"
  7. Click "Share" to generate a public link

What's Next

  • Schema diff — re-introspect to see what changed
  • Export — SQL migrations, dbdiagram.io DSL, markdown docs
  • Team features — shared schema libraries, RBAC, Slack integration
  • Multi-database — MySQL, SQLite, MongoDB
  • Schema monitoring — scheduled re-introspection with drift alerts

Source Code

GitHub: github.com/faizalmy/schemalens


Built with ❤️ for the #H0Hackathon. Thanks to AWS Aurora and Vercel for the platforms.

Top comments (0)