@guojc1/sql-formatter-compact
v1.1.0
Published
A compact, highly customizable SQL formatter with flush-left keywords support.
Maintainers
Readme
Note: This is a highly customized fork of
sql-formatterdesigned for extreme compactness and relative flush-left alignment (especially forWHERE,AND,FROMclauses, andWITHstatement bug fixes). It supports placing commas before fields (commaPosition: 'before') without breaking trailing inline comments.Installation:
npm install @guojc1/sql-formatter-compact
SQL Formatter Compact
SQL Formatter Compact is a JavaScript library and CLI tool for strict, clean, and highly customizable pretty-printing of SQL queries.
Install
Get the latest version from NPM:
npm install -g @guojc1/sql-formatter-compactUsage
Usage as library
import { format } from '@guojc1/sql-formatter-compact';
console.log(
format('SELECT * FROM tbl', {
language: 'mysql',
commaPosition: 'before',
newlineAfterClause: false,
})
);Magic Comments (Ignore Formatting)
You can disable formatting for specific sections of your SQL code by wrapping them in these magic comments:
/* sql-formatter-compact-disable */
SELECT * FROM tbl1;
/* sql-formatter-compact-enable */Usage from command line (CLI)
The CLI tool will be installed as sql-formatter-compact:
sql-formatter-compact -husage: sql-formatter-compact-cli.cjs [-h] [-o OUTPUT] [--fix] [-l {bigquery,clickhouse,db2,hive,mysql,postgresql,spark,sql,sqlite,trino...}] [-c CONFIG] [--version] [FILE]
positional arguments:
FILE Input SQL file (defaults to stdin)
optional arguments:
-h, --help show this help message and exit
-o OUTPUT, --output OUTPUT
File to write SQL output (defaults to stdout)
--fix Update the file in-place
-l ..., --language ...
SQL Formatter dialect (defaults to basic sql)
-c CONFIG, --config CONFIG
Path to config JSON file or json string (will find a file named '.sql-formatter-compact.json' or use default configs if unspecified)
--version show program's version number and exitFormatting in-place
To format a file and overwrite its contents directly, use the --fix flag along with your configuration file (-c):
sql-formatter-compact -c ./sqlfmt.json --fix ./src/queries/my_query.sqlBy default, the tool takes queries from stdin and processes them to stdout.
Configuration options
The tool accepts a JSON config file named .sql-formatter-compact.json in the current or any parent directory. It supports all standard options from the original library, plus our custom commaPosition and newlineAfterClause options.
{
"language": "spark",
"tabWidth": 2,
"keywordCase": "upper",
"commaPosition": "before",
"newlineAfterClause": false
}License
MIT License. Core tokenization engine and parsing rules are based on the original sql-formatter by ZeroTurnaround LLC and Rene Saarsoo.
