mrkeyoor.com_
Sat 08 Aug 20:58 UTC
npmDataupdated 08 Aug 2026

sql-formatter

sql-formatter is a JavaScript pretty-printer for SQL strings and files. Its library API and bundled CLI parse SQL using a selected dialect, then normalize indentation, line breaks, keyword casing, operator spacing, and spacing between statements. It understands more than twenty named dialect modes, including PostgreSQL, MySQL, SQLite, BigQuery, Snowflake, Spark, DuckDB, PL/SQL, T-SQL, and Trino. It can preserve disabled regions and replace recognized placeholders, but it does not validate a query against your database or make substituted values safe.

Verdict

A mature, flexible formatter and CLI for existing multi-dialect workflows, especially when configuration control matters. For a new opinionated formatting standard, start with prettier-plugin-sql-cst; for stored procedures or semantic SQL work, use a database-native formatter or parser.

API stability4/5The central format(sql, options) API and CLI configuration shape are established, while version 12 added formatDialect() as a compatible bundle-size path rather than removing format(). Most style options are explicit and documented. Two edges lower confidence: indentStyle is marked deprecated, and the custom DialectOptions API is labeled experimental with a warning that it can change in non-major releases.
Docs5/5The README lists supported and unsupported SQL, demonstrates the library and CLI, documents config discovery, links every option to a dedicated page, and answers common dialect, Webpack, editor, and template errors. The parameter pages clearly state that callers own escaping, while the language page explains that generic SQL is not detection and that runtime dialect selection increases browser bundles.
Maintenance3/5Version 15.8.2 was published on June 21, 2026, and the repository was pushed on July 23, 2026, so bug-fix work is current. The repo reports 80 open issues and pull requests. The limiting evidence comes from the maintainer: the README explicitly calls the formatter maintenance-mode software, says new features are unlikely, and directs ambitious formatting work to prettier-plugin-sql-cst.
Ecosystem4/5npm recorded 4,045,142 downloads for the measured week, and the project has 2,884 GitHub stars. It ships a CLI, JSON Schema for editor completion, ESM and CommonJS builds, and documented integrations for VS Code, Vim, Prettier, and eslint-plugin-sql. Dialect coverage is broad, but stored-procedure gaps and maintenance-mode status limit how far new integrations can grow.

Use it if

  • You need one formatter for SQL generated or stored by a JavaScript application across several named database dialects
  • You want a CLI that reads stdin or files, supports in-place fixes, and discovers a shared .sql-formatter.json configuration
  • You need configurable casing, indentation, expression width, boolean-operator placement, and spacing between multiple statements
  • You embed SQL in an editor or developer tool and need both ESM and CommonJS entry points with declarations included
Skip it if

Setup reality

npm install sql-formatter provides dual ESM and CommonJS entry points, adjacent declaration files, and a sql-formatter executable. There are no peer dependencies or native builds. The first important choice is the dialect: language: 'sql' is only a common subset, not automatic detection, and the FAQ says a wrong or omitted dialect is the usual cause of parse errors. For browser builds, format(sql, { language }) can pull every dialect because the selection happens at runtime; formatDialect(sql, { dialect: postgresql }) was added so bundlers can retain a specific imported dialect. The CLI reads stdin by default, accepts a file, can overwrite with --fix, and searches the current directory and parents for .sql-formatter.json. That discovery is convenient locally but can make CI output depend on an unexpected parent config, so pass --config when reproducibility matters. The parser does not support stored procedures or delimiter changes beyond semicolon, and template syntax may require custom paramTypes or formatter-disable comments. Placeholder replacement is text substitution, not database binding, and the docs put escaping entirely on you. Old Webpack and Babel setups can also choke on class-property syntax unless updated or configured with preset-env. Finally, the maintainer calls the project maintenance-only, so expect feasible bug fixes rather than new language architecture or major formatting features.

Patterns

Format a SQL string with an explicit dialectformat-sql

import { format } from 'sql-formatter';

const output = format('select id,name from users where active=true', {
  language: 'postgresql',
});

console.log(output);

Always choose the real dialect; the default sql mode is a common subset and does not detect syntax automatically.

Control casing and layoutconfigure-style

const output = format(sql, {
  language: 'mysql',
  tabWidth: 4,
  keywordCase: 'upper',
  dataTypeCase: 'upper',
  functionCase: 'lower',
  logicalOperatorNewline: 'before',
  expressionWidth: 60,
});

identifierCase is experimental, and indentStyle is deprecated, so avoid baking either into a new shared standard.

Import one dialect for a browser bundleimport-one-dialect

import { formatDialect, postgresql } from 'sql-formatter';

const output = formatDialect(sql, {
  dialect: postgresql,
  keywordCase: 'upper',
});

formatDialect enables static dialect selection; format() with language can cause a bundler to include every dialect.

Format piped SQL with the CLIformat-from-stdin

echo 'select * from users where id = 3' | npx sql-formatter -l postgresql

Without -l or a config file, the CLI uses the generic sql dialect rather than guessing PostgreSQL.

Rewrite a file in placefix-sql-file

npx sql-formatter --fix -l snowflake queries/report.sql

--fix overwrites the input file; run it through version control or CI checks so unintended formatting is recoverable.

Create a project CLI configurationshare-cli-config

{
  "language": "bigquery",
  "tabWidth": 2,
  "keywordCase": "upper",
  "linesBetweenQueries": 2
}

Save this as .sql-formatter.json; the CLI searches parent directories, so use --config in CI if parent files could vary.

Display positional parameter valuesreplace-positional-params

const output = format(
  'SELECT * FROM users WHERE email = ? AND age >= ?',
  {
    language: 'mysql',
    params: ["'ada@example.com'", '21'],
  },
);

Values are inserted as SQL text and are not escaped; never use params as a substitute for your database driver's parameter binding.

Display named T-SQL parameter valuesreplace-named-params

const output = format(
  'SELECT * FROM users WHERE name = @name AND age = @age',
  {
    language: 'transactsql',
    params: { name: "'Ada'", age: '37' },
  },
);

Recognized placeholder forms differ by dialect; @name works by default for Transact-SQL but not every language.

Display PostgreSQL numbered parametersreplace-numbered-params

const output = format(
  'SELECT * FROM events WHERE owner_id = $1 AND created_at >= $2',
  {
    language: 'postgresql',
    params: { 1: '42', 2: "'2026-01-01'" },
  },
);

Object keys map to the numeric placeholder labels; the replacement strings still need correct SQL quoting.

Teach the parser a custom template placeholderpreserve-template-placeholders

const output = format(
  'SELECT {columns} FROM {table} WHERE id = {id}',
  {
    language: 'sql',
    paramTypes: {
      custom: [{ regex: String.raw`\{[a-zA-Z0-9_]+\}` }],
    },
  },
);

Custom placeholders help parsing common templates but cannot make arbitrary template control flow valid SQL.

Leave a troublesome region untoucheddisable-formatting-region

const sql = `
/* sql-formatter-disable */
SELECT {{ dynamic_columns }} FROM {{ dynamic_table }};
/* sql-formatter-enable */
SELECT id, name FROM users;
`;

const output = format(sql, { language: 'sql' });

Disabled text is not parsed at all, which is useful for templates but can also hide malformed SQL from formatter checks.

Space multiple SQL statements consistentlyseparate-statements

const output = format(
  'SELECT 1; SELECT 2; SELECT 3;',
  {
    language: 'sql',
    linesBetweenQueries: 2,
    newlineBeforeSemicolon: false,
  },
);

Statement splitting assumes semicolons; changing the delimiter to forms such as MySQL DELIMITER is not supported.

Alternatives

PackageRegistryPick it when
prettier-plugin-sql-cstnpmUse it for new opinionated SQL formatting built on Prettier's layout algorithm and the current maintainer's newer parser architecture.
node-sql-parsernpmUse it when you need a SQL AST, table and column analysis, or query rewriting rather than only pretty-printing.
@sqltools/formatternpmUse it when you are already in the SQLTools ecosystem and its smaller set of formatting conventions and dialects is sufficient.