sql-formatter

A JavaScript/TypeScript library and CLI that pretty-prints SQL queries across 20+ dialects with configurable style options.

Library
npm
v15.8.2
2,884stars
MIT License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
84/100Excellent
Development Activity76
Maintenance92
Community68
Maturity60
Momentum40

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
86/100Excellent
Architecture85
Code Quality88
Innovation84
Learning Curve88

sql-formatter is a whitespace formatter for SQL that turns cramped or inconsistently styled queries into readable, indented output. It ships as both an importable library (format(query, options)) and a standalone CLI, and understands the syntactic quirks of over twenty SQL dialects — from MySQL, PostgreSQL, and SQLite to BigQuery, Snowflake, Redshift, and Trino — so formatting decisions respect each engine’s actual grammar rather than applying one generic SQL style to everything.

Under the hood it tokenizes and parses each query into an AST using a Nearley-generated grammar, then walks that tree to lay out keywords, indentation, and line breaks according to caller-supplied options (keyword case, tab width, indent style, and more). It also supports placeholder/parameter substitution for prepared statements and disable/enable comment blocks for skipping SQL the parser can’t yet handle, making it a practical fit for SQL linters, query builders, ORMs, database GUIs, and editor/IDE tooling that need to render SQL consistently.

What You Get

  • A format() / formatDialect() API that formats a SQL string in one call, with a SqlLanguage union type covering 20+ dialects for TypeScript autocompletion.
  • A sql-formatter CLI (npx sql-formatter) that reads from stdin or a file, supports --fix in-place rewriting, and accepts a JSON/.sql-formatter.json config file.
  • Fine-grained style controls — keyword case, identifier/data-type/function case, tab width vs. tabs, indent style, expression width, and where logical operators break onto new lines.
  • Prepared-statement placeholder replacement (params option) so parameterized queries can be rendered with real values substituted in for readability.
  • /* sql-formatter-disable */ / /* sql-formatter-enable */ comment markers that let a section of SQL bypass parsing entirely, so one unsupported construct doesn’t block formatting the rest of a file.

Common Use Cases

  • Formatting SQL embedded in application code (template literals, ORM query builders) as part of a lint-staged or pre-commit hook.
  • Powering the ‘format query’ button in SQL editors, database GUIs, or internal admin tools.
  • Normalizing SQL style across a team or codebase via CI checks, independent of which dialect each service’s database uses.
  • Pretty-printing generated SQL (from query builders, migrations, or LLM output) before displaying it to a developer or end user.

Under The Hood

Architecture: The pipeline is a classic tokenize → parse → format chain. src/lexer/Tokenizer.ts and TokenizerEngine.ts turn raw SQL text into tokens using dialect-specific TokenizerOptions; src/parser/grammar.ne (a Nearley grammar, compiled to grammar.ts via the grammar build step) parses those tokens into an AST defined in src/parser/ast.ts. src/formatter/Formatter.ts and ExpressionFormatter.ts then walk the AST, applying Layout/Indentation/InlineLayout rules to produce the final string. Each of the 20+ entries under src/languages/<dialect>/ supplies a DialectOptions object (reserved keywords, operators, tokenizer quirks) that dialect.ts and allDialects.ts assemble into the dialect registry consumed by sqlFormatter.ts’s format()/formatDialect() entry points — new dialects are added by implementing this one config object rather than forking the formatter logic. Tech Stack: Written in strict TypeScript ("strict": true, noImplicitReturns) targeting ES6, built with tsc for dual CJS/ESM output plus a Webpack UMD bundle (dist/sql-formatter.min.js) for browser/unpkg use. Runtime dependencies are minimal — just nearley (parser runtime) and argparse (CLI argument parsing) — keeping the installed footprint small for a library with 4M+ weekly downloads. The build is orchestrated with pnpm workspaces and npm-run-all for parallel CJS/ESM/Webpack builds. Code Quality: The test suite (76 test files under test/) uses a shared-behavior pattern — behavesLikeMariaDbFormatter.ts, behavesLikeSqlFormatter.ts, etc. — so dialects that share grammar (e.g. MySQL/MariaDB, Redshift/PostgreSQL) reuse one behavior spec instead of duplicating assertions, plus feature-scoped modules under test/features/ and test/options/ (joins, window functions, CTEs, case sensitivity, etc.) that are mixed into every applicable dialect’s test file. CI runs ts:check, pretty:check, eslint, and the full Jest suite with coverage collection on every change (pnpm run check). API Design: The public surface is intentionally small — one format(query, options) call for the common case, with formatDialect() for consumers that want to pass a DialectOptions object directly instead of a string language name. Every dialect and option type is re-exported from src/index.ts for full TypeScript inference, defaults are sensible (keywordCase: 'preserve' avoids surprising rewrites), and each configuration option has its own dedicated doc page under docs/ with before/after examples, keeping the learning curve low despite the breadth of dialects supported.

Used by 20 apps in this directory

TypeScript
99%
Other

Activepieces

Automation · AI Assistants

23,887

Open-source AI automation platform that converts 280+ workflow integrations into MCP servers for LLMs, with no-code builders and TypeScript extensibility.

View details
92
Repo Health
85
Technical
65
Dependency
Built with
TypeScript99%
Updated today
TypeScript
66%
Apache 2.0

Appsmith

Developer Tools · Automation · No Code Platforms

40,707

Open-source low-code platform to build admin panels, dashboards, and internal tools connected to any database or API.

View details
94
Repo Health
79
Technical
68
Dependency
Built with
TypeScript66%
Java22%
Updated today
Go
52%
MIT

Bytebase

Devops

14,396

An open-source database CI/CD and DevSecOps platform — schema migration review, GitOps-driven changes, data masking, and access control across MySQL, PostgreSQL, Oracle, Snowflake, MongoDB, and more.

View details
92
Repo Health
73
Technical
69
Dependency
Built with
Go52%
TypeScript39%
Updated today
Go
62%
Apache 2.0

Coroot

Analytics · Monitoring

7,877

eBPF-powered observability with AI root cause analysis — zero code changes required, full-stack visibility out of the box.

View details
88
Repo Health
80
Technical
65
Dependency
Built with
Go62%
Vue36%
Updated yesterday
Java
34%
Apache 2.0

Enso

Analytics · Data Engineering · Low Code Platforms

7,437

A visual and textual programming platform for data prep and analysis where the node graph and the underlying Enso code are always perfectly in sync, built by an Alteryx co-founder on a GraalVM engine.

View details
70
Repo Health
90
Technical
62
Dependency
Built with
Java34%
TypeScript27%
Scala26%
Updated 1 weeks ago
TypeScript
97%
Other

GrowthBook

Developer Tools · Analytics · Monitoring

8,138

Open source feature flags, A/B testing, and warehouse-native experimentation that queries your existing data infrastructure—no data movement required.

View details
92
Repo Health
85
Technical
66
Dependency
Built with
TypeScript97%
Updated today
TypeScript
95%
MIT

HyperDX

Developer Tools · Analytics · Monitoring

9,841

Open source observability platform that unifies logs, traces, metrics, and session replays on ClickHouse — now the core of ClickStack.

View details
87
Repo Health
83
Technical
70
Dependency
Built with
TypeScript95%
Updated today
Vue
50%
GPL 3.0

IT-Tools

Developer Tools

40,279

A unified collection of 88 web-based developer utilities — from JSON formatting to subnet calculation — all self-hostable, keyboard-searchable, and offline-ready.

View details
60
Repo Health
74
Technical
67
Dependency
Built with
Vue50%
TypeScript48%
Updated 3 days ago
TypeScript
96%
Other

Lightdash

Analytics · Data Engineering

6,056

The open-source Looker alternative that turns your dbt project's metrics and dimensions into governed, self-serve charts and dashboards — no license key required.

View details
93
Repo Health
84
Technical
66
Dependency
Built with
TypeScript96%
Updated today

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers

Search