JSPM

  • Created
  • Published
  • Downloads 13123
  • Score
    100M100P100Q151355F
  • License GPL-3.0-or-later

Prettier plugin for SQL

Package Exports

  • prettier-plugin-sql-cst
  • prettier-plugin-sql-cst/dist/index.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 (prettier-plugin-sql-cst) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Prettier plugin SQL-CST npm version build status

A Prettier plugin for SQL that uses sql-parser-cst and the actual Prettier formatting algorithm.

Like Prettier for JavaScript, this plugin formats SQL expressions differently depending on their length. A short SQL query will be formatted on a single line:

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

A longer query, will get each clause printed on a separate line:

SELECT id, client.name, client.priority
FROM client
WHERE client.id IN (12, 18, 121);

An even longer one gets the contents of each clause indented:

SELECT
  client.id,
  client.name AS client_name,
  organization.name AS org_name,
  count(order.id) AS nr_of_orders
FROM
  client
  LEFT JOIN organization ON client.organization_id = organization.id
  LEFT JOIN order ON order.client_id = client.id
WHERE
  client.status = 'active'
  AND client.id IN (28, 214, 457)
  AND order.status IN ('active', 'pending', 'processing')
GROUP BY client.id
ORDER BY client.name
LIMIT 100;

Formatting philosophy

  • Adapt formatting based on expression length.
  • Stick to one style and avoid configuration options.
  • Format embedded languages (like JSON data and JavaScript programs).
  • When unsure, preserve existing syntax.

Currently this plugin preserves most of the syntax elements and concentrates mainly on the layout of whitespace. For example it does not add/remove paratheses.

See STYLE_GUIDE for overview of the SQL formatting style used.

Getting started

Install it as any other Prettier plugin:

npm install --save-dev prettier prettier-plugin-sql-cst

Then use it on SQL files though Prettier command line tool or Prettier extension for your editor of choice.

Choosing an SQL dialect

By default the plugin will determine SQL dialect based on file extension:

  • .sql or .sqlite - SQLite
  • .bigquery - BigQuery

You can override this behavior with a prettier configuration:

{
  "overrides": [
    {
      "files": ["*.sql"],
      "options": { "parser": "bigquery" }
    }
  ]
}

The plugin provides the following parsers:

  • sqlite
  • bigquery

Configuration

The standard Prettier options printWidth, tabWidth, useTabs apply. There are also some SQL-specific options:

API Option Default Description
sqlKeywordCase upper Converts SQL keywords to upper or lower case, or preserve existing.

Limitations and development status

Currently this plugin supports the most common everyday SQLite and BigQuery syntax. Formatting of the following SQL statements is fully implemented:

  • SELECT
  • UPDATE
  • INSERT
  • DELETE
  • TRUNCATE
  • CREATE / DROP / ALTER TABLE
  • CREATE / DROP VIEW
  • CREATE / DROP INDEX