@thisalbonindu/sqlfmt-cli
v1.0.0
Published
A fast, dependency-free SQL formatter and minifier for the command line.
Maintainers
Readme
sqlfmt-cli
A fast, dependency-free SQL formatter and minifier for the command line.
Same formatting engine as the web version — pretty-prints SELECT/FROM/WHERE/JOIN
clauses, subqueries, CTEs, CASE expressions, and multi-statement scripts, with
configurable indentation, keyword case, and comma style.
No dependencies. No network calls. Works on .sql files, directories, or piped stdin.
Install
npm install -g sqlfmt-cliOr run it without installing:
npx sqlfmt-cli query.sqlUsage
sqlfmt [options] [file|dir ...]
cat query.sql | sqlfmt [options]Options
| Flag | Description | Default |
|-------------------|------------------------------------------------------------|---------|
| --indent <n> | Indent width in spaces | 4 |
| --case <mode> | Keyword case: upper | lower | capitalize | preserve | upper |
| --fn-case <mode>| Function case: upper | lower | preserve | upper |
| --comma <pos> | Comma position: after | before | after |
| --minify | Minify instead of pretty-print | |
| -w, --write | Overwrite files in place instead of printing to stdout | |
| --check | Exit 1 if any file isn't already formatted (CI mode) | |
| -v, --version | Print version | |
| -h, --help | Show help | |
Examples
# print formatted query to stdout
sqlfmt query.sql
# format every .sql file under a directory, in place
sqlfmt -w src/queries/
# CI check — fails the build if anything needs formatting
sqlfmt --check src/queries/
# pipe SQL in, lowercase keywords out
cat query.sql | sqlfmt --case lower
# minify a query for embedding in application code
sqlfmt --minify query.sqlUse it in CI
# .github/workflows/sql-lint.yml
- run: npx sqlfmt-cli --check src/queries/Use it as a pre-commit hook
npx sqlfmt-cli -w $(git diff --cached --name-only --diff-filter=ACM -- '*.sql')
git add -uUse it as a library
const { formatSql, minifySql } = require('sqlfmt-cli');
formatSql('select id,name from users where active=1;', {
keywordCase: 'upper',
indentSize: 4,
commaPosition: 'after',
});What it handles
SELECT/FROM/WHERE/JOIN(all variants) /GROUP BY/HAVING/ORDER BY/LIMITINSERT INTO ... VALUES,UPDATE ... SET,DELETE FROMWITHcommon table expressions, including multiple chained CTEs- Subqueries, recursively indented
CASE WHEN ... THEN ... ELSE ENDAND/ORchains- Line comments (
--) and block comments (/* */) - String literals, quoted identifiers (
`,",[ ]— MySQL/Postgres/SQL Server), bind variables - Multi-statement scripts (semicolon-separated)
Development
npm testLicense
MIT
