Skip to content

Repository files navigation

SQL Formatter NPM versionBuild StatusCoverage Status

SQL Formatter is a JavaScript library for pretty-printing SQL queries. It started as a port of a PHP Library, but has since considerably diverged. It supports Standard SQL, Couchbase N1QL and IBM DB2 dialects.

Try the demo.

Install

Get the latest version from NPM:

npm install --save sql-formatter

Usage

importsqlFormatterfrom"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",// Defaults to "sql"indent: " "// Defaults to two spaces});

Currently just three SQL dialects are supported:

Placeholders replacement

// Named placeholderssqlFormatter.format("SELECT * FROM tbl WHERE foo = @foo",{params: {foo: "'bar'"}}));// Indexed placeholderssqlFormatter.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

# run linter and tests
$ npm run check

...and you're ready to poke us with a pull request.

License

MIT

About

A whitespace formatter for different query languages

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages