@hokaccha/sql-formatter
v1.4.0
Published
Format whitespace in a SQL query to make it more readable
Downloads
1,136
Maintainers
Readme
SQL Formatter
Forked from zeroturnaround/sql-formatter but with improvements and ported Typescript.
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.
SQL formatter supports the following dialects:
- sql - Standard SQL
- mariadb - MariaDB
- mysql - MySQL
- postgresql - PostgreSQL
- db2 - IBM DB2
- plsql - Oracle PL/SQL
- n1ql - Couchbase N1QL
- redshift - Amazon Redshift
- spark - Spark
- tsql - SQL Server Transact-SQL
- bigquery - Google BigQuery
It does not support:
- Stored procedures.
- Changing of the delimiter type to something else than
;
.
Install
Get the latest version from NPM:
npm install @hokaccha/sql-formatter
Usage as library
import { format } from "sql-formatter";
console.log(format("SELECT * FROM tbl"));
This will output:
SELECT
*
FROM
tbl
You can also pass in configuration options:
format("SELECT * FROM tbl", {
language: "spark", // Defaults to "sql" (see the above list of supported dialects)
indent: " ", // Defaults to two spaces
keywordCase: "upper", // Defaults to preserve ("upper" | "lower" | "preserve")
linesBetweenQueries: 2, // Defaults to 1
});
Placeholders replacement
// Named placeholders
format("SELECT * FROM tbl WHERE foo = @foo", {
params: {foo: "'bar'"}
}));
// Indexed placeholders
format("SELECT * FROM tbl WHERE foo = ?", {
params: ["'bar'"]
}));
Both result in:
SELECT
*
FROM
tbl
WHERE
foo = 'bar'