Skip to content
A whitespace formatter for different query languages
Branch: master
Clone or download
nene Merge pull request #58 from rafaelrpinto/master
feat: support multiple statements
Latest commit 15e1188 Dec 21, 2018
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src Fixed incorrect trim usage Dec 21, 2018
test Fixed incorrect trim usage Dec 21, 2018
.babelrc
.editorconfig Clean up editor config Nov 8, 2016
.eslintignore Ignore coverage dir from linting Sep 14, 2016
.eslintrc
.gitignore
.travis.yml Replace Karma+Jasmine with Jest test framework Sep 20, 2016
LICENSE
README.md Drop --save flag from install docs Dec 12, 2018
index.html Update README and demo Jun 17, 2017
package-lock.json
package.json
webpack.config.js

README.md

SQL Formatter NPM version Build Status Coverage 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, IBM DB2 and Oracle PL/SQL dialects.

Try the demo.

Install

Get the latest version from NPM:

npm install sql-formatter

Usage

import sqlFormatter from "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 four SQL dialects are supported:

Placeholders replacement

// Named placeholders
sqlFormatter.format("SELECT * FROM tbl WHERE foo = @foo", {
    params: {foo: "'bar'"}
}));

// Indexed placeholders
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

# run linter and tests
$ npm run check

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

License

MIT

You can’t perform that action at this time.