Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@kyl12/poor-mans-t-sql-formatter

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@kyl12/poor-mans-t-sql-formatter

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

  • 1.6.11
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
4
Maintainers
1
Weekly downloads
 
Created
Source

Poor Man's T-SQL Formatter - NPM Package

An NPM package for the Poor Man's T-SQL Formatter JS Library.

This library is transpiled from the C# original (it's not a port in that the JS code is never maintained independently) - so for history of changes, issues with the library itself, etc please see https://github.com/TaoK/PoorMansTSqlFormatter.

This library exists to make it easy to include the formatter library in websites, in random node projects or in command-line usage in node-friendly environments (which may not be as C#-friendly / mono-friendly).

This library also has a much better (in my opionion) API than the original C# library, hopefully I'll get around to changing the source at some point.

There is one serious consideration that might drive you to the C#/mono library or command-line utility, however: This JS library is about 8x slower than the C# original. I haven't investigated exactly why yet, but I don't hold high hopes of this changing anytime soon. If you need to deal with large sets of files and performance matters, you will likely want to look at the C#/mono library/project.

Please note, there is a command-line utility for node/npm also, but it is in a separate npm package, poor-mans-t-sql-formatter-cli (github: https://github.com/TaoK/poor-mans-t-sql-formatter-npm-cli)

Installation

npm install poor-mans-t-sql-formatter

Usage

This JS library exposes a very simple API to the formatter: one method, with options, returning one datastructure (with optional bits).

Example:

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:

OptionDescriptionTypeDefault
formattingTypestandard, identity or obfuscation - what you'd like to doenumstandard
errorOutputPrefixText to be included (in a comment at the top) if parsing failed and result is therefore suspectstring(something like "--WARNING: errors during parsing")
includeTextSpecify that the formatted SQL should be included in the result as a Text value/propertybooltrue
includeHtmlSpecify that the formatted SQL should be included in the result as HTMLboolfalse
includeHtmlPageSpecify that the formatted SQL should be included in the result as a full HTML documentboolfalse
includeTokenListSpecify that the Token List produced from the Tokenization phase should be includedboolfalse
includeParseTreeSpecify that the Parse Tree produced from the Parsing phase should be includedboolfalse

Standard formatter options:

OptionDescriptionTypeDefault
indentThe unit of indentation - typically a tab (\t) or a number of spacesstring\t
maxLineWidthRequest that the formatter wrap long lines to avoid exceeding this line lengthint999
spacesPerTabThis is used to measure line length, and only applies if you use tabsint4
statementBreaksHow many linebreaks should be added when starting a new statement?int2
clauseBreaksHow many linebreaks should be added when starting a new clause within a statement?int1
expandCommaListsShould comma-delimited lists (columns, group by args, etc) be broken out onto new lines?booltrue
trailingCommasWhen starting a new line because of a comma, should the comma be at the end of line (VS the start of the next)?booltrue
spaceAfterExpandedCommaShould a space be added after the comma? (typically not if they are "trailing")boolfalse
expandBooleanExpressionsShould boolean operators (AND, OR) cause a linebreak?booltrue
expandCaseStatementsShould CASE expressions have their WHEN and THEN expressions be broken out on new lines?booltrue
expandBetweenConditionsShould BETWEEN expressions have the max argument broken out on a new line?booltrue
expandInListsShould IN() lists have each argument on a new line?boolfalse
breakJoinOnSectionsShould the ON section of a JOIN clause be broken out onto its own line?boolfalse
uppercaseKeywordsShould T-SQL keywords (like SELECT, FROM) be automatically uppercased?booltrue
coloring(In HTML output, if enabled) should keywords, comments etc have distinct color classes?booltrue
keywordStandardizationShould less-common T-SQL keywords be replaced with their standard counterparts? (NOTE: only safe for T-SQL!)boolfalse

Obfuscating formatter options:

OptionDescriptionTypeDefault
randomizeKeywordCaseShould the case of keywords be randomized, to minimize legibility?boolfalse
randomizeColor(In HTML output, if enabled) should the color of the SQL text be randomly varied?boolfalse
randomizeLineLengthsShould the SQL be wrapped at arbitrary intervals, to minimize legibility?boolfalse
preserveCommentsShould comments in the code be retained (vs being stripped out)?booltrue
enableKeywordSubstitutionShould keywords with synonyms use less common forms? (NOTE: only safe for T-SQL!)boolfalse

Tests

npm test

(please note, coverage is not yet up to scratch; the general goal is to address the same test cases as the C# library)

License

This library, like the original at https://github.com/TaoK/PoorMansTSqlFormatter, is made freely available under the AGPL 3.0 license.

Contributing

Given that this module is mostly wrapping over the original code at https://github.com/TaoK/PoorMansTSqlFormatter, there is little opportunity for contributing here - but any input is of course welcome!

To produce/refresh the npm library file (index.js) just run the build.sh script. When executed from the root of the project, it will simply collect the files from the parent project in github and add the wrapper code defined in prefix and suffix files.

STATUS

When it is published, I'll see if I can take care of all those trendy things like:

  • CI & test tracking
  • coverage tracking
  • a suitable "try it out" link
  • Think about restructuring the library into a "legacylib" file (using prefix/suffix build process) and a new stuff file.
  • (anything else?)

Eventually, probably once the library has been published and shows some sign of interest/activity, I'll go for the bigger stuff

  • look for perf opportunities. 8s is a LONG time for some simple tests!! (C# equivalent is 1s) - otherwise DOCUMENT the terrible performance...

Keywords

FAQs

Package last updated on 01 Feb 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc