Formats whitespaces in a SQL query to make it more readable
npm install @rickbergfalk/sql-formatterSQL 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.
Get the latest version from NPM:
```
npm install sql-formatter
`js
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:
`js`
sqlFormatter.format("SELECT *", {
language: "n1ql", // Defaults to "sql"
indent: " " // Defaults to two spaces
});
Currently just four SQL dialects are supported:
- sql - [Standard SQL][]
- n1ql - [Couchbase N1QL][]
- db2 - [IBM DB2][]
- pl/sql - [Oracle PL/SQL][]
`js
// 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'
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