A T-SQL formatting library in JS, transpiled from the C# library of the same name. This version includes some tweaks by me, but all credit goes to the original author, Tao Klerks
npm install @kyl12/poor-mans-t-sql-formatternpm install poor-mans-t-sql-formatter
var formatterLib = require('poor-mans-t-sql-formatter');
var queryToFormat = "select 1 from somewhere WHERE someoneforgot = punctuation and [we want better] = 1";
var result = formatterLib.formatSql(queryToFormat);
console.log(result.text);
`
Options are provided in a second argument.
Example:
`
var formatterLib = require('poor-mans-t-sql-formatter');
var result = formatterLib.formatSql("select 1, 2, 3, 4 from counter", { trailingCommas: false, spaceAfterExpandedComma: true });
console.log(result.text);
`
General options:
| Option | Description | Type | Default |
| --- | --- | --- | --- |
| formattingType | `standard`, `identity` or `obfuscation` - what you'd like to do | enum | standard |
| errorOutputPrefix | Text to be included (in a comment at the top) if parsing failed and result is therefore suspect | string | (something like "--WARNING: errors during parsing") |
| includeText | Specify that the formatted SQL should be included in the result as a Text value/property | bool | true |
| includeHtml | Specify that the formatted SQL should be included in the result as HTML | bool | false |
| includeHtmlPage | Specify that the formatted SQL should be included in the result as a full HTML document | bool | false |
| includeTokenList | Specify that the Token List produced from the Tokenization phase should be included | bool | false |
| includeParseTree | Specify that the Parse Tree produced from the Parsing phase should be included | bool | false |
Standard formatter options:
| Option | Description | Type | Default |
| --- | --- | --- | --- |
| indent | The unit of indentation - typically a tab (\t) or a number of spaces | string | \t |
| maxLineWidth | Request that the formatter wrap long lines to avoid exceeding this line length | int | 999 |
| spacesPerTab | This is used to measure line length, and only applies if you use tabs | int | 4 |
| statementBreaks | How many linebreaks should be added when starting a new statement? | int | 2 |
| clauseBreaks | How many linebreaks should be added when starting a new clause within a statement? | int | 1 |
| expandCommaLists | Should comma-delimited lists (columns, group by args, etc) be broken out onto new lines? | bool | true |
| trailingCommas | When starting a new line because of a comma, should the comma be at the end of line (VS the start of the next)? | bool | true |
| spaceAfterExpandedComma | Should a space be added after the comma? (typically not if they are "trailing") | bool | false |
| expandBooleanExpressions | Should boolean operators (AND, OR) cause a linebreak? | bool | true |
| expandCaseStatements | Should CASE expressions have their WHEN and THEN expressions be broken out on new lines? | bool | true |
| expandBetweenConditions | Should BETWEEN expressions have the max argument broken out on a new line? | bool | true |
| expandInLists | Should IN() lists have each argument on a new line? | bool | false |
| breakJoinOnSections | Should the ON section of a JOIN clause be broken out onto its own line? | bool | false |
| uppercaseKeywords | Should T-SQL keywords (like SELECT, FROM) be automatically uppercased? | bool | true |
| coloring | (In HTML output, if enabled) should keywords, comments etc have distinct color classes? | bool | true |
| keywordStandardization | Should less-common T-SQL keywords be replaced with their standard counterparts? (NOTE: only safe for T-SQL!) | bool | false |
Obfuscating formatter options:
| Option | Description | Type | Default |
| --- | --- | --- | --- |
| randomizeKeywordCase | Should the case of keywords be randomized, to minimize legibility? | bool | false |
| randomizeColor | (In HTML output, if enabled) should the color of the SQL text be randomly varied? | bool | false |
| randomizeLineLengths | Should the SQL be wrapped at arbitrary intervals, to minimize legibility? | bool | false |
| preserveComments | Should comments in the code be retained (vs being stripped out)? | bool | true |
| enableKeywordSubstitution | Should keywords with synonyms use less common forms? (NOTE: only safe for T-SQL!) | bool | false |
Tests
npm test`