SQL Formatter
SQL Formatter is a JavaScript library for pretty-printing SQL queries.
It started as a Javascript port of a PHP Library, but has diverged
considerably, and been forked/joined multiple times in the past. The current
formatter (@gwax/sql-formatter) forked from zeroturnaround/sql-formatter
with code consolidated from kufii/sql-formatter-plus
and a number of other forks scattered around GitHub.
SQL Formatter supports Standard SQL, Couchbase N1QL, IBM DB2,
Oracle PL/SQL, Amazon Redshift, and Spark dialects.
→ Try the demo.
Install
Get the latest version from NPM:
npm install sql-formatter
Usage
import sqlFormatter from '@gwax/sql-formatter';
console.log(sqlFormatter.format('SELECT * FROM table1'));
This will output:
SELECT
*
FROM
table1
You can also pass in configuration options:
sqlFormatter.format('SELECT *', {
language: 'n1ql',
indent: ' ',
});
Currently just six SQL dialects are supported:
Placeholders replacement
sqlFormatter.format('SELECT * FROM tbl WHERE foo = @foo', {
params: { foo: "'bar'" },
});
sqlFormatter.format('SELECT * FROM tbl WHERE foo = ?', {
params: ["'bar'"],
});
Both result in:
SELECT
*
FROM
tbl
WHERE
foo = 'bar'
Usage without NPM
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
.
Contributing
$ npm run check
...and you're ready to poke us with a pull request.
License
MIT