Format whitespace in a SQL query to make it more readable with support for multiple SQL dialects
npx @tessl/cli install tessl/npm-sql-formatter@15.6.0SQL Formatter is a JavaScript library for pretty-printing SQL queries with support for various SQL dialects. It provides comprehensive formatting options, parameter replacement capabilities, and supports 19 different SQL dialects including BigQuery, MySQL, PostgreSQL, and many others.
npm install sql-formatterimport { format, supportedDialects } from "sql-formatter";For CommonJS:
const { format, supportedDialects } = require("sql-formatter");import { format } from "sql-formatter";
// Basic formatting with default options
const formatted = format("SELECT * FROM users WHERE id = 1");
// With dialect-specific formatting
const formatted = format("SELECT * FROM users WHERE id = 1", {
language: "mysql",
keywordCase: "upper",
indentStyle: "standard"
});
// With parameter replacement
const formatted = format("SELECT * FROM users WHERE id = ?", {
language: "postgresql",
params: ["123"]
});SQL Formatter is built around several key components:
format() and formatDialect() functions for SQL formattingPrimary SQL formatting functionality with dialect detection and comprehensive formatting options. Handles whitespace, indentation, keyword casing, and query structure.
function format(query: string, cfg?: FormatOptionsWithLanguage): string;
function formatDialect(query: string, cfg: FormatOptionsWithDialect): string;
const supportedDialects: string[];
type SqlLanguage =
| "bigquery"
| "db2"
| "db2i"
| "duckdb"
| "hive"
| "mariadb"
| "mysql"
| "n1ql"
| "plsql"
| "postgresql"
| "redshift"
| "spark"
| "sqlite"
| "sql"
| "tidb"
| "trino"
| "transactsql"
| "tsql"
| "singlestoredb"
| "snowflake";Comprehensive formatting configuration including indentation, casing, operators, and layout options.
interface FormatOptions {
tabWidth: number;
useTabs: boolean;
keywordCase: KeywordCase;
identifierCase: IdentifierCase;
dataTypeCase: DataTypeCase;
functionCase: FunctionCase;
indentStyle: IndentStyle;
logicalOperatorNewline: LogicalOperatorNewline;
expressionWidth: number;
linesBetweenQueries: number;
denseOperators: boolean;
newlineBeforeSemicolon: boolean;
params?: ParamItems | string[];
paramTypes?: ParamTypes;
}
type KeywordCase = "preserve" | "upper" | "lower";
type IndentStyle = "standard" | "tabularLeft" | "tabularRight";
type LogicalOperatorNewline = "before" | "after";Individual dialect formatters for 19 SQL dialects, each with dialect-specific syntax rules, keywords, and formatting options.
// Dialect objects for direct use with formatDialect()
const bigquery: DialectOptions;
const mysql: DialectOptions;
const postgresql: DialectOptions;
const sqlite: DialectOptions;
// ... and 15 more dialects
interface DialectOptions {
name: string;
tokenizerOptions: TokenizerOptions;
formatOptions: DialectFormatOptions;
}Parameter placeholder replacement for prepared statements with support for positional, named, and custom parameter types.
type ParamItems = { [k: string]: string };
interface ParamTypes {
positional?: boolean;
numbered?: ("?" | ":" | "$")[];
named?: (":" | "@" | "$")[];
quoted?: (":" | "@" | "$")[];
custom?: CustomParameter[];
}
interface CustomParameter {
regex: string;
key?: (text: string) => string;
}Helper functions for expanding SQL syntax patterns and handling configuration validation.
function expandPhrases(phrases: string[]): string[];
class ConfigError extends Error {}The package includes a command-line tool for formatting SQL files and integrating with development workflows.
npm install -g sql-formatter
sql-formatter --language mysql input.sql --output formatted.sql