|
1 | 1 | # SQL Formatter [](https://npmjs.com/package/sql-formatter) [](https://travis-ci.org/zeroturnaround/sql-formatter) [](https://coveralls.io/github/zeroturnaround/sql-formatter?branch=master)
|
2 | 2 |
|
3 |
| -**SQL Formatter** is a whitespace formatter for different query languages. |
| 3 | +**SQL Formatter** is a JavaScript library for pretty-printing SQL queries. |
| 4 | +It started as a port of a [PHP Library][], but has since considerably diverged. |
| 5 | +It supports [Standard SQL][] and [Couchbase N1QL][] dialects. |
4 | 6 |
|
5 |
| -[See the demo](https://zeroturnaround.github.io/sql-formatter/). |
| 7 | +→ [Try the demo.](https://zeroturnaround.github.io/sql-formatter/) |
6 | 8 |
|
7 |
| -## Installation |
| 9 | +## Install |
8 | 10 |
|
9 |
| -To install the newest version from NPM: |
| 11 | +Get the latest version from NPM: |
10 | 12 |
|
11 | 13 | ```
|
12 | 14 | npm install --save sql-formatter
|
13 | 15 | ```
|
14 | 16 |
|
15 |
| -The SQL Formatter source code is written in ES2015 but we precompile both CommonJS and UMD builds to ES5 so they work in any modern browser. |
16 |
| - |
17 |
| -If you don't use a module bundler then you can drop a file from `/dist` directory as a `<script>` tag on the page. This makes SQL Formatter available as a `window.sqlFormatter` global variable. |
18 |
| - |
19 |
| -## Example usage |
20 |
| - |
21 |
| -First we need to import our CommonJS module and then we can use it for formatting |
22 |
| -standard SQL query. |
| 17 | +## Usage |
23 | 18 |
|
24 | 19 | ```js
|
25 | 20 | import sqlFormatter from "sql-formatter";
|
26 | 21 |
|
27 |
| -const formattedStandardSql = sqlFormatter.format("SELECT * FROM table1"); |
| 22 | +console.log(sqlFormatter.format("SELECT * FROM table1")); |
28 | 23 | ```
|
29 | 24 |
|
30 |
| -The value of `formattedStandardSql` will look like this: |
| 25 | +This will output: |
31 | 26 |
|
32 |
| -```sql |
| 27 | +``` |
33 | 28 | SELECT
|
34 | 29 | *
|
35 | 30 | FROM
|
36 | 31 | table1
|
37 | 32 | ```
|
38 | 33 |
|
39 |
| -## Supported languages |
40 |
| - |
41 |
| -### [Standard SQL](https://en.wikipedia.org/wiki/SQL:2011) |
42 |
| - |
43 |
| -```js |
44 |
| -sqlFormatter.format("SELECT * FROM table1 WHERE foo = bar"); |
45 |
| -``` |
46 |
| - |
47 |
| -### [N1QL](http://www.couchbase.com/n1ql) |
| 34 | +You can also pass in configuration options: |
48 | 35 |
|
49 | 36 | ```js
|
50 |
| -sqlFormatter.format("SELECT fname, email FROM tutorial USE KEYS ['dave', 'ian'];", {language: "n1ql"}); |
| 37 | +sqlFormatter.format("SELECT *", { |
| 38 | + language: "n1ql" // Defaults to "sql" |
| 39 | + indent: " " // Defaults to two spaces |
| 40 | +}); |
51 | 41 | ```
|
52 | 42 |
|
53 |
| -## Optional configuration |
| 43 | +Currently just two SQL dialects are supported: |
54 | 44 |
|
55 |
| -Example below has default values. |
| 45 | +- **sql** - [Standard SQL][] |
| 46 | +- **n1ql** - [Couchbase N1QL][] |
56 | 47 |
|
57 |
| -```js |
58 |
| -const cfg = { |
59 |
| - language: "sql" // Specific query language |
60 |
| - indent: " " // Value that is used for creating indentation levels |
61 |
| -}; |
| 48 | +## Usage without NPM |
62 | 49 |
|
63 |
| -sqlFormatter.format("SELECT *", cfg); |
64 |
| -``` |
| 50 | +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. |
| 51 | +This makes SQL Formatter available as a global variable `window.sqlFormatter`. |
65 | 52 |
|
66 | 53 | ## Contributing
|
67 | 54 |
|
68 |
| -Simply `npm install` to install the dependencies, write your fix, |
69 |
| -ensure tests and linter are fine with `npm run check`, |
70 |
| -and you're ready to poke us with a pull request. |
71 |
| - |
72 |
| -## Influence |
| 55 | +```bash |
| 56 | +# run linter and tests |
| 57 | +$ npm run check |
| 58 | +``` |
73 | 59 |
|
74 |
| -SQL Formatter core logic is influenced by a PHP version of [SQL Formatter by Jeremy Dorn](https://github.com/jdorn/sql-formatter). |
| 60 | +...and you're ready to poke us with a pull request. |
75 | 61 |
|
76 | 62 | ## License
|
77 | 63 |
|
78 | 64 | [MIT](https://github.com/zeroturnaround/sql-formatter/blob/master/LICENSE)
|
| 65 | + |
| 66 | +[PHP library]: https://github.com/jdorn/sql-formatter |
| 67 | +[Standard SQL]: https://en.wikipedia.org/wiki/SQL:2011 |
| 68 | +[Couchbase N1QL]: http://www.couchbase.com/n1ql |
0 commit comments