Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

prettier-plugin-sql-cst

Package Overview
Dependencies
Maintainers
1
Versions
30
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

prettier-plugin-sql-cst

Prettier plugin for SQL

  • 0.1.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
4.1K
increased by15.29%
Maintainers
1
Weekly downloads
 
Created
Source

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

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.

Configuration

The standard Prettier options apply. There's also one SQL-specific option:

API OptionDefaultDescription
printWidth80Same as in Prettier
tabWidth2Same as in Prettier
useTabsfalseSame as in Prettier
sqlKeywordCasepreserveConverts SQL keywords to upper or lower case.
sqlAliasAspreserveUses AS keyword in aliases either always or never.

Limitations and development status

Currently this plugin only supports subset of SQLite syntax. Formatting of the following SQL statements is fully implemented:

  • SELECT
  • UPDATE
  • INSERT
  • DELETE
  • CREATE TABLE

Unlike Prettier for JavaScript, this plugin currently preserves all the syntax elements. For example it does not add/remove paratheses or modify the case of keywords (by default). It only manipulates whitespace.

This will likely change in the future, with the goal of being more opinionated. Some possibilities:

  • capitalize all keywords
  • ensure all alias definitions use the AS keyword.
  • ensure consistent use of quotes (e.g. always quote keywords)
  • eliminate redundant parenthesis.

The exact formatting style is also very much still in flux. The overall goal is to have no options to configure different styles, but rather to develop one opinionated style of formatting SQL.

Keywords

FAQs

Package last updated on 24 Jan 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc