eslint-plugin-format-sql
v1.1.1
Published
ESLint plugin to format SQL query in template literals
Downloads
436
Maintainers
Readme
eslint-plugin-format-sql
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();
// ...
}
}