Package Exports
- @hokaccha/sql-formatter
- @hokaccha/sql-formatter/dist/cjs/sqlFormatter.js
- @hokaccha/sql-formatter/dist/esm/sqlFormatter.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@hokaccha/sql-formatter) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
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-formatterUsage as library
import { format } from "sql-formatter";
console.log(format("SELECT * FROM tbl"));This will output:
SELECT
*
FROM
tblYou 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'