🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Sign inDemoInstall
Socket

eslint-plugin-format-sql

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

eslint-plugin-format-sql

ESLint plugin to format SQL query in template literals

1.1.1
latest
Source
npm
Version published
Weekly downloads
152
-24.38%
Maintainers
1
Weekly downloads
 
Created
Source

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();
    
    // ...
  }
}

Keywords

sql

FAQs

Package last updated on 31 Mar 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