sql-fmt-cli
v1.2.0
Published
Format and prettify SQL queries from the command line
Downloads
262
Maintainers
Readme
sql-fmt-cli
Format and prettify SQL queries from the command line — no dependencies, works instantly.
Install
npm install -g sql-fmt-cliUsage
# Format an inline query
sql-fmt "select id, name from users where active=1 order by name"
# Format a file, print to stdout
sql-fmt --file query.sql
# Format a file and write back in place
sql-fmt --file query.sql --write
# Use 4-space indent
sql-fmt --indent 4 "select * from orders"
# Uppercase all keywords
sql-fmt --uppercase "select id from users"
# Pipe from stdin
cat query.sql | sql-fmtOutput Example
Input:
select u.id, u.name, o.total from users u inner join orders o on u.id=o.user_id where u.active=1 and o.total > 100 group by u.id, u.name order by o.total desc limit 10Output:
Select u.id,
u.name,
o.total
From users u
Inner join orders o
On u.id = o.user_id
Where u.active = 1
And o.total > 100
Group by u.id,
u.name
Order by o.total desc
Limit 10With --uppercase:
SELECT u.id,
u.name,
o.total
FROM users u
INNER JOIN orders o
ON u.id = o.user_id
WHERE u.active = 1
AND o.total > 100
GROUP BY u.id,
u.name
ORDER BY o.total DESC
LIMIT 10Options
| Flag | Short | Description |
|------|-------|-------------|
| --file <path> | -f | Read SQL from a file |
| --write | -w | Write formatted SQL back to the file (requires --file) |
| --indent <n> | -i | Number of spaces for indentation (default: 2) |
| --uppercase | -u | Uppercase all SQL keywords |
| --help | -h | Show help |
Supported SQL
SELECT,FROM,WHERE,JOIN(all variants:INNER,LEFT,RIGHT,FULL,CROSS)INSERT INTO ... VALUES,UPDATE ... SET,DELETE FROMCREATE TABLE,ALTER TABLE,DROP TABLEGROUP BY,ORDER BY,HAVING,LIMIT,OFFSETUNION,UNION ALL,INTERSECT,EXCEPTWITH(CTEs)CASE / WHEN / THEN / ELSE / END- Subqueries with deeper indentation
- Single-quoted string literals (
'hello') --and/* */comments preserved
License
MIT — Wilson Xu
