Skip to content

holistics/sql-formatter

This branch is 19 commits ahead of, 2781 commits behind sql-formatter-org/sql-formatter:master.

Folders and files

NameName
Last commit message
Last commit date
Jun 4, 2020
Jun 3, 2020
Jun 3, 2020
Jun 3, 2020
Jun 3, 2020
Sep 20, 2016
Nov 8, 2016
Sep 14, 2016
Dec 1, 2016
Dec 13, 2017
Sep 20, 2016
Sep 14, 2016
Jun 17, 2017
Jun 17, 2017
Mar 1, 2019
Jun 1, 2020
Sep 15, 2016
Dec 13, 2017

Repository files navigation

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 and IBM DB2 dialects.

Try the demo.

Install

Get the latest version from NPM:

npm install --save 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 three 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

About

A whitespace formatter for different query languages

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 96.5%
  • HTML 3.5%