7 releases

0.3.0 Apr 30, 2023
0.2.3 Apr 9, 2023
0.1.2 Apr 7, 2023

#919 in Development tools

Download history 5/week @ 2024-06-17 7/week @ 2024-06-24 35/week @ 2024-07-08 18/week @ 2024-07-22 16/week @ 2024-07-29 16/week @ 2024-08-05 4/week @ 2024-08-12 8/week @ 2024-08-19 2/week @ 2024-09-02 7/week @ 2024-09-09 2/week @ 2024-09-16 13/week @ 2024-09-23 36/week @ 2024-09-30

58 downloads per month

MIT license

9KB
89 lines

Sleek: SQL Formatter ✨

Crates.io GitHub Actions GitHub Releases

Sleek is a CLI tool for formatting SQL. It helps you maintain a consistent style across your SQL code, enhancing readability and productivity.

The heavy lifting is done by the sqlformat crate.

Before and After

Here's an example of a SQL query before and after being formatted by Sleek:

Before

select id, name, email from users where id in (select user_id from orders where total > 100) and status = 'active'

After

SELECT
    id,
    name,
    email
FROM
    users
WHERE
    id IN (
        SELECT
            user_id
        FROM
            orders
        WHERE
            total > 100
    )
    AND STATUS = 'active'

Features

  • Format SQL files using customizable indentation and character case options
  • Supports glob patterns, allowing you to format multiple files and patterns
  • Check whether your SQL files are already formatted without altering them with the --check flag

Installation

Download Compiled Binaries

You can download the compiled binaries for Sleek from the GitHub Releases page. Choose the binary that corresponds to your operating system and architecture, and place it in a directory included in your system's PATH environment variable.

Install with Cargo

To install Sleek using Cargo, you'll need to have Rust installed on your system. Once Rust is installed, you can install Sleek with Cargo:

cargo install sleek

Usage

sleek [FLAGS] [OPTIONS] <file_paths>...

Arguments

  • <file_paths>...: File path(s) to format, supports glob patterns. If no file paths are provided, reads from stdin.

Flags

  • -c, --check: Check if the code is already formatted. If not, it will exit with an error message.
  • -h, --help: Prints help information.
  • -V, --version: Prints version information.

Options

  • -i, --indent_spaces <indent_spaces>: Set the number of spaces to use for indentation (default: 4).
  • -U, --uppercase <uppercase>: Change reserved keywords to ALL CAPS (default: true).
  • -l, --lines_between_queries <lines_between_queries>: Set the number of line breaks after a query (default: 2).

Examples

Format a query from stdin:

> echo "select * from users" | sleek --uppercase
SELECT
    *
FROM
    user

To check if a query is formatted correctly from stdin:

> echo "select * from users" | sleek --check
Input is not formatted correctly. Run without --check to format the input.

To format a single file with the default options:

sleek my_query.sql

To format multiple files using a glob pattern:

sleek "queries/*.sql"

To format files with custom options:

sleek --indent_spaces 2 --uppercase false "queries/*.sql"

To check if files are already formatted:

sleek --check "queries/*.sql"

License

This project is available under the MIT License.

Dependencies

~3.5MB
~60K SLoC