What is sql-formatter?
The sql-formatter npm package is a library for formatting SQL queries. It supports various SQL dialects and provides a clean and readable output for SQL statements.
What are sql-formatter's main functionalities?
Basic SQL Formatting
This feature allows you to format a basic SQL query into a more readable format.
const sqlFormatter = require('sql-formatter');
const formattedSQL = sqlFormatter.format('SELECT * FROM table WHERE column = value');
console.log(formattedSQL);
Dialect-Specific Formatting
This feature allows you to format SQL queries according to specific SQL dialects like MySQL, PostgreSQL, etc.
const sqlFormatter = require('sql-formatter');
const formattedSQL = sqlFormatter.format('SELECT * FROM table WHERE column = value', { language: 'mysql' });
console.log(formattedSQL);
Custom Indentation
This feature allows you to customize the indentation used in the formatted SQL output.
const sqlFormatter = require('sql-formatter');
const formattedSQL = sqlFormatter.format('SELECT * FROM table WHERE column = value', { indent: ' ' });
console.log(formattedSQL);
Other packages similar to sql-formatter
sql-formatter-plus
sql-formatter-plus is another SQL formatter that offers similar functionality to sql-formatter. It also supports multiple SQL dialects and provides options for customizing the output format. However, it may have different default formatting styles and additional configuration options.
prettier-plugin-sql
prettier-plugin-sql is a plugin for the Prettier code formatter that adds support for formatting SQL queries. It integrates with Prettier's ecosystem, allowing you to format SQL queries alongside other code formats supported by Prettier. This can be useful if you are already using Prettier for other parts of your codebase.
SQL Formatter
SQL Formatter is a whitespace formatter for different query languages.
See the demo.
Installation
To install the newest version from NPM:
npm install --save sql-formatter
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.
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.
Example usage
First we need to import our CommonJS module and then we can use it for formatting
standard SQL query.
import sqlFormatter from "sql-formatter";
const formattedStandardSql = sqlFormatter.format("SELECT * FROM table1");
The value of formattedStandardSql
will look like this:
SELECT
*
FROM
table1
Supported languages
sqlFormatter.format("SELECT * FROM table1 WHERE foo = bar");
sqlFormatter.format("SELECT fname, email FROM tutorial USE KEYS ['dave', 'ian'];", {language: "n1ql"});
Optional configuration
Example below has default values.
const cfg = {
language: "sql"
indent: " "
};
sqlFormatter.format("SELECT *", cfg);
Contribute
TODO
Influence
SQL Formatter core logic is influenced by a PHP version of SQL Formatter by Jeremy Dorn.
License
MIT