Package Exports
- eslint-plugin-format-sql
- eslint-plugin-format-sql/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
ESLint plugin to format SQL queries inside any SQL template tag using pg-formatter. One of the key features of this plugin is keeping code's indent after a formation.
Installation
Install the plugin first:
npm install --save-dev eslint-plugin-format-sqlThen 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
"startIndent": 2, // extra spaces to indent for each query line
"formatter": {
// pg-formatter options
},
}],
// ...
},
}You can check available options for formatter object here.
In case no options is provided for the rule then default ones will be used:
{
"tags": ["SQL"],
"startIndent": 2,
"formatter": {
"spaces": 2,
"keywordCase": "uppercase",
"functionCase": "lowercase",
},
}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();
// ...
}
}