based on SQL-Formatter, Bravo Formatter is a JavaScript library for pretty-printing SQL queries. It supports various SQL dialects: Hive、Clickhouse.
基于 SQL-Formatter 开发,目前长期维护 Hive、Clickhouse 语法,若不明确具体的语法可以不配置 language 参数。会采用兼容语法进行格式化
Get the latest version from NPM:
npm install bravo-formatter
Also available with yarn:
yarn add bravo-formatter
import { format } from 'sql-formatter';
console.log(format('SELECT * FROM tbl'));
This will output:
SELECT
*
FROM
tbl
You can also pass in configuration options:
format('SELECT id, case when name = '1' then 1 else 2 end FROM tbl', {
language: 'hive',
tabWidth: 2,
indentStyle: 'tabularLeft',
tabulateAlias: true,
keywordCase: 'upper',
});
SELECT id,
CASE
WHEN name = '1' THEN 1
WHEN name = '2' THEN 2
ELSE 3
END
FROM tbl;
-
language
the SQL dialect to use,格式化的语言. -
tabWidth
amount of indentation to use,配置缩进的空格数. -
useTabs
to use tabs for indentation, 使用 Tab 作为缩进. -
keywordCase
uppercases or lowercases keywords,关键词是否改为大写、小写. -
indentStyle
defines overall indentation style,格式化风格. -
logicalOperatorNewline
newline before or after boolean operator (AND, OR, XOR),连接词(AND、OR、XOR)是否换行展示. -
tabulateAlias
aligns column aliases vertically,是否开启别名对齐. -
expressionWidth
maximum number of characters in parenthesized expressions to be kept on single line,括号内表达式最大长度,超出则换行展示. -
linesBetweenQueries
how many newlines to insert between queries,多段 SQL 之间的空行数,默认为 1. -
denseOperators
packs operators densely without spaces,运算符中间是否保留空格,默认保留. -
newlineBeforeSemicolon
places semicolon on separate line,SQL 结尾分号是否启用新行展示.
If you don't use a module bundler, clone the repository, run npm install
and grab a file from /dist
directory to use inside a <script>
tag.
This makes SQL Formatter available as a global variable window.sqlFormatter
.
提供 commonjs 的接入方式,提取 dist 目录下的 sql-formatter.min.cjs 文件,通过<script src="XXX/sql-formatter.min.cjs">
引入。window.sqlFormatter
方式调用