@hokaccha/sql-formatter
TypeScript icon, indicating that this package has built-in type declarations

1.4.0 • Public • Published

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:

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'

License

MIT

Package Sidebar

Install

npm i @hokaccha/sql-formatter

Weekly Downloads

463

Version

1.4.0

License

MIT

Unpacked Size

460 kB

Total Files

129

Last publish

Collaborators

  • hokaccha