JSPM

eslint-plugin-format-sql

1.1.1
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 37
  • Score
    100M100P100Q68918F
  • License ISC

ESLint plugin to format SQL query in template literals

Package Exports

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

Readme

eslint-plugin-format-sql

NPM

ESLint plugin to format SQL queries inside any SQL template tag. It works on top of the sql-formatter with some improvements and post-formations.

Installation

Install the plugin first:

npm install --save-dev eslint-plugin-format-sql

Then add this plugin to eslint config in extends section and configure rule for your need:

// .eslintrc
{
  "extends": [
    // ...
    "plugin:format-sql/format"
  ],
  "rules": {
    "format-sql/format": ["warn", {
      "tags": ["SQL", "sql"], // names of SQL template tags to parse their literals
      "language": "postgresql", // language name
      "startSpaces": 0, // extra spaces to indent for each query line in first column of a query
      "spaces": 2, // amount of indentation to use in statements
    }],
    // ...
  },
}

Available options for language: sql, postgresql, db2, hive, mariadb, mysql, n1ql, plsql, bigquery, redshift, singlestoredb, spark, sqlite, snowflake, transactsql, trino,

In case no options is provided for the rule then default params will be used:

{
  language: 'postgresql',
  tags: ['SQL', 'sql'],
  startSpaces: 2,
  spaces: 2,
}

Example

Example of formation with default options.

Before:

import { SQLFactory } from 'pg-sql-template'; // or another sql template library
import { client } from './client';

const SQL = SQLFactory({ client });

class PostsController {
  async getPosts(userId: number) {
    const posts = await SQL`
      SELECT posts.id, posts.text, posts.created_at AS created, users.name AS author 
      FROM posts LEFT JOIN users ON users.id = posts.author_id
      WHERE posts.author_id = ${userId}
      ORDER BY posts.created_at`.many();
    
    // ...
  }
}

After --fix:

import { SQLFactory } from 'pg-sql-template'; //  or another sql template library
import { client } from './client';

const SQL = SQLFactory({ client });

class PostsController {
  async getPosts(userId: number) {
    const posts = await SQL`
      SELECT posts.id,
        posts.text,
        posts.created_at AS created,
        users.name AS author
      FROM posts
        LEFT JOIN users ON users.id = posts.author_id
      WHERE posts.author_id = ${userId}
      ORDER BY posts.created_at
    `.many();
    
    // ...
  }
}