<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Srinivas A</title>
    <description>The latest articles on Forem by Srinivas A (@srinivas_a_a6d0e7a3288710).</description>
    <link>https://forem.com/srinivas_a_a6d0e7a3288710</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3827258%2F3bee4dfc-291d-4d73-aa8e-5e1504f97501.jpg</url>
      <title>Forem: Srinivas A</title>
      <link>https://forem.com/srinivas_a_a6d0e7a3288710</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/srinivas_a_a6d0e7a3288710"/>
    <language>en</language>
    <item>
      <title>Building a Lightweight SQL Formatter with FastAPI and sqlparse</title>
      <dc:creator>Srinivas A</dc:creator>
      <pubDate>Mon, 16 Mar 2026 12:29:27 +0000</pubDate>
      <link>https://forem.com/srinivas_a_a6d0e7a3288710/building-a-lightweight-sql-formatter-with-fastapi-and-sqlparse-2il9</link>
      <guid>https://forem.com/srinivas_a_a6d0e7a3288710/building-a-lightweight-sql-formatter-with-fastapi-and-sqlparse-2il9</guid>
      <description>&lt;h2&gt;
  
  
  Why I Built a SQL Formatter
&lt;/h2&gt;

&lt;p&gt;I often run into messy SQL queries while working with databases. Sometimes queries come from logs, dashboards, or teammates and they’re hard to read because everything is on one line.&lt;/p&gt;

&lt;p&gt;Instead of opening an IDE just to clean up the query, I wanted a quick tool where I could paste SQL and instantly get a readable version.&lt;/p&gt;

&lt;p&gt;So I built a small SQL formatter.&lt;/p&gt;

&lt;p&gt;The tool is available here:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://sql-formatter.dev" rel="noopener noreferrer"&gt;https://sql-formatter.dev&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  What the Tool Does
&lt;/h2&gt;

&lt;p&gt;The formatter takes a SQL query and returns a formatted version with proper indentation and readable structure.&lt;/p&gt;

&lt;p&gt;Current features include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;indentation control&lt;/li&gt;
&lt;li&gt;keyword case toggle (UPPER / lower / preserve)&lt;/li&gt;
&lt;li&gt;SQL syntax highlighting&lt;/li&gt;
&lt;li&gt;copy formatted SQL&lt;/li&gt;
&lt;li&gt;download formatted SQL as &lt;code&gt;.sql&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;automatic formatting on paste&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Architecture
&lt;/h2&gt;

&lt;p&gt;I wanted to keep the architecture extremely simple.&lt;/p&gt;

&lt;h3&gt;
  
  
  Backend
&lt;/h3&gt;

&lt;p&gt;The backend is a small Python service built with FastAPI.&lt;/p&gt;

&lt;p&gt;Formatting is handled using the &lt;code&gt;sqlparse&lt;/code&gt; library:&lt;br&gt;
&lt;code&gt;&lt;br&gt;
python&lt;br&gt;
formatted = sqlparse.format(&lt;br&gt;
    sql,&lt;br&gt;
    reindent=True,&lt;br&gt;
    indent_width=indent_size,&lt;br&gt;
    keyword_case=keyword_case&lt;br&gt;
)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The API exposes a single endpoint:&lt;br&gt;
POST /format&lt;/p&gt;

&lt;p&gt;which receives SQL and formatting options and returns the formatted query.&lt;/p&gt;

&lt;p&gt;Validation&lt;/p&gt;

&lt;p&gt;One interesting challenge was validation.&lt;/p&gt;

&lt;p&gt;sqlparse will happily accept almost any text and return it unchanged. That means a string like:&lt;br&gt;
would still be “formatted”.&lt;/p&gt;

&lt;p&gt;To avoid this, I added a lightweight validation layer that checks whether the input actually resembles a SQL statement before formatting.&lt;/p&gt;

&lt;p&gt;⸻&lt;/p&gt;

&lt;p&gt;Frontend&lt;/p&gt;

&lt;p&gt;The frontend is intentionally minimal:&lt;br&gt;
    • vanilla HTML + JavaScript&lt;br&gt;
    • syntax highlighting using highlight.js&lt;br&gt;
    • copy and download buttons&lt;br&gt;
    • simple formatting controls&lt;/p&gt;

&lt;p&gt;Keeping the frontend lightweight makes the tool load very quickly.&lt;/p&gt;

&lt;p&gt;⸻&lt;/p&gt;

&lt;p&gt;Deployment&lt;/p&gt;

&lt;p&gt;The service is containerized with Docker and deployed on Railway.&lt;/p&gt;

&lt;p&gt;The domain is managed via Cloudflare:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://sql-formatter.dev" rel="noopener noreferrer"&gt;https://sql-formatter.dev&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;⸻&lt;/p&gt;

&lt;p&gt;Next Features&lt;/p&gt;

&lt;p&gt;Some improvements I’m considering:&lt;br&gt;
    • dialect-specific formatting (PostgreSQL / MySQL / Spark SQL)&lt;br&gt;
    • SQL linting&lt;br&gt;
    • share formatted SQL via URL&lt;/p&gt;

&lt;p&gt;⸻&lt;/p&gt;

&lt;p&gt;Feedback Welcome&lt;/p&gt;

&lt;p&gt;If you use SQL regularly, I’d love to hear what features would make a formatter like this more useful.&lt;/p&gt;

&lt;p&gt;You can try the tool here:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://sql-formatter.dev" rel="noopener noreferrer"&gt;https://sql-formatter.dev&lt;/a&gt;&lt;/p&gt;

</description>
      <category>sql</category>
      <category>python</category>
      <category>fastapi</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
