UNPKG

2.48 kBMarkdownView Raw
1# SQL Formatter [![NPM version](https://img.shields.io/npm/v/sql-formatter.svg)](https://npmjs.com/package/sql-formatter) [![Build Status](https://travis-ci.org/zeroturnaround/sql-formatter.svg?branch=master)](https://travis-ci.org/zeroturnaround/sql-formatter) [![Coverage Status](https://coveralls.io/repos/github/zeroturnaround/sql-formatter/badge.svg?branch=master)](https://coveralls.io/github/zeroturnaround/sql-formatter?branch=master)
2
3**SQL Formatter** is a JavaScript library for pretty-printing SQL queries.
4It started as a port of a [PHP Library][], but has since considerably diverged.
5It supports [Standard SQL][], [Couchbase N1QL][], [IBM DB2][] and [Oracle PL/SQL][] dialects.
6
7→ [Try the demo.](https://zeroturnaround.github.io/sql-formatter/)
8
9## Install
10
11Get the latest version from NPM:
12
13```
14npm install --save sql-formatter
15```
16
17## Usage
18
19```js
20import sqlFormatter from "sql-formatter";
21
22console.log(sqlFormatter.format("SELECT * FROM table1"));
23```
24
25This will output:
26
27```
28SELECT
29 *
30FROM
31 table1
32```
33
34You can also pass in configuration options:
35
36```js
37sqlFormatter.format("SELECT *", {
38 language: "n1ql", // Defaults to "sql"
39 indent: " " // Defaults to two spaces
40});
41```
42
43Currently just four SQL dialects are supported:
44
45- **sql** - [Standard SQL][]
46- **n1ql** - [Couchbase N1QL][]
47- **db2** - [IBM DB2][]
48- **pl/sql** - [Oracle PL/SQL][]
49
50### Placeholders replacement
51
52```js
53// Named placeholders
54sqlFormatter.format("SELECT * FROM tbl WHERE foo = @foo", {
55 params: {foo: "'bar'"}
56}));
57
58// Indexed placeholders
59sqlFormatter.format("SELECT * FROM tbl WHERE foo = ?", {
60 params: ["'bar'"]
61}));
62```
63
64Both result in:
65
66```
67SELECT
68 *
69FROM
70 tbl
71WHERE
72 foo = 'bar'
73```
74
75## Usage without NPM
76
77If 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.
78This makes SQL Formatter available as a global variable `window.sqlFormatter`.
79
80## Contributing
81
82```bash
83# run linter and tests
84$ npm run check
85```
86
87...and you're ready to poke us with a pull request.
88
89## License
90
91[MIT](https://github.com/zeroturnaround/sql-formatter/blob/master/LICENSE)
92
93[PHP library]: https://github.com/jdorn/sql-formatter
94[Standard SQL]: https://en.wikipedia.org/wiki/SQL:2011
95[Couchbase N1QL]: http://www.couchbase.com/n1ql
96[IBM DB2]: https://www.ibm.com/analytics/us/en/technology/db2/
97[Oracle PL/SQL]: http://www.oracle.com/technetwork/database/features/plsql/index.html