prettifySql() function

Format a one-line SQL string so it is readable in a log.

Signature:

export declare function prettifySql(sql: string): string;

Parameters

Parameter

Type

Description

sql

string

A SQL statement, typically as captured by a query log.

Returns:

string

The same statement across multiple indented lines.

Remarks

Three passes: runs of whitespace collapse to single spaces, every major keyword is upper-cased and moved onto a new line, and continuation clauses (AND, OR and the JOIN family) are indented one level under the statement they belong to. Longer keywords are matched before their prefixes, so WITH RECURSIVE and LEFT JOIN survive intact rather than breaking at WITH and JOIN.

Purely cosmetic and deliberately not a SQL parser. Matching is a word-boundary regex over a fixed keyword list, which is why a keyword inside an identifier is left alone but a keyword inside a string literal is not — it will be upper-cased and broken like any other. Use it for logs, never to rewrite SQL you intend to execute.

Example

prettifySql('select id from "User" where active = $1');
// SELECT id
// FROM "User"
// WHERE active = $1

Read this page as plain markdown — no HTML, no navigation. For pasting into an LLM, or for an agent to fetch.