prettier-plugin-sql-cst

raw JSON →
0.19.3 verified Sat Apr 25 auth: no javascript

A Prettier plugin for SQL that uses sql-parser-cst and the actual Prettier formatting algorithm (Wadler-Leijen). It adapts formatting based on expression length, sticking to one style with minimal configuration options. The plugin supports multiple SQL dialects: SQLite, BigQuery, PostgreSQL, MySQL, and MariaDB (some experimental). It parses and formats SQL code inside CREATE FUNCTION/PROCEDURE bodies (PostgreSQL). Current stable version is 0.19.3, with semi-regular releases (every few weeks). TypeScript types are shipped. Key differentiators vs other SQL formatters: uses Prettier's algorithmic line splitting for consistency, preserves syntax elements, and formats embedded languages.

error Error: Cannot find module 'prettier-plugin-sql-cst' require() of ES Module
cause The plugin is ESM-only since v0.19.3, but CommonJS require() is used.
fix
Use import() or ensure your project is configured as ESM. Alternatively, downgrade to v0.19.2 or earlier. Or use Prettier's CLI which handles plugins internally.
error TypeError: prettier-plugin-sql-cst.default is not a function
cause Direct import of the module and calling it as a function.
fix
Do not import or call directly. Use the plugin via Prettier's plugin configuration in .prettierrc or CLI --plugin option.
error Error: No parser could be inferred for file: myfile.sql
cause Prettier cannot auto-detect SQL dialect; no 'parser' option set.
fix
Set parser in .prettierrc overrides or use --parser flag: prettier --plugin prettier-plugin-sql-cst --parser sqlite myfile.sql
breaking v0.19.0+ - Upgraded sql-parser-cst to 0.39.0, possibly changing AST for some SQL constructs. Formatting may change.
fix Review formatted output after upgrade. No manual fix available.
breaking v0.17.0+ - PostgreSQL builtin data type names now formatted in upper case by default. sqlTypeCase, sqlIdentifierCase, sqlFunctionCase options added.
fix Add sqlTypeCase: 'lower' and sqlIdentifierCase: 'lower' to config for PostgreSQL if lowercase types are desired.
breaking v0.19.3 - Package compiled into a single ESM .js file. Breaking for CommonJS consumers.
fix Ensure project is ESM, or use a dynamic import: const prettierPluginSqlCst = await import('prettier-plugin-sql-cst'). Then use via Prettier's plugin system, not direct import.
gotcha `preserve` case option is incompatible with `sqlCanonicalSyntax: true` – added keywords like AS will always be uppercase.
fix Set sqlCanonicalSyntax: false if preserving case is important, or accept uppercase keywords.
gotcha PostgreSQL, MySQL, MariaDB parsers are experimental – expect crashes on complex queries.
fix Use sqlite or bigquery parsers for production; test with your SQL dialect thoroughly.
npm install prettier-plugin-sql-cst
yarn add prettier-plugin-sql-cst
pnpm add prettier-plugin-sql-cst

Shows how to install the plugin, configure it in .prettierrc.json, and format a basic SQL query.

// Install dependencies
// npm install --save-dev prettier prettier-plugin-sql-cst

// Create a .prettierrc.json file:
{
  "plugins": ["prettier-plugin-sql-cst"],
  "overrides": [
    {
      "files": ["*.sql"],
      "options": { "parser": "sqlite" }
    }
  ]
}

// Then create a SQL file (e.g., test.sql):
SELECT a, b, c FROM tbl WHERE x > 10;

// Run prettier:
// npx prettier --write test.sql

// Output:
SELECT a, b, c
FROM tbl
WHERE x > 10;