A simple and lightweight library for highlighting SQL queries written in pure
JavaScript
[!IMPORTANT]
Due to some recent issues with the release flow, the 5.0.0 release was somewhat prematurely released. With NPM not allowing unpublishing the current release is 6.0.0.
What's it all about?
sql-highlight is a small package that highlights SQL queries. It can output to
both the terminal with Unicode escape sequences, as well as to normal HTML. Oh,
and there are no external dependencies 😉
Installation
sql-highlight is tested to work with Node.js 16, 18, 20 and 22.
Install with Yarn:
yarn add sql-highlight
Install with NPM:
npm install sql-highlight
Usage
In its most basic form:
const { highlight } = require('sql-highlight')const sqlString = "SELECT `id`, `username` FROM `users` WHERE `email` = 'test@example.com'"const highlighted = highlight(sqlString)console.log(highlighted)
The following options may be passed to the highlight function.
Option
Value
Default
Description
html
boolean
false
Set to true to render HTML instead of Unicode.
htmlEscaper
(str: string) => string
Basic escaper
Function to escape HTML entities. Uses a basic escaper by default. If HTML mode is used in a browser environment this could be useful to escape strings using the DOM.
classPrefix
string
'sql-hl-'
Prefix to prepend to classes for HTML span-tags. Is appended with entity name.
colors
Object
See below*
What color codes to use for Unicode rendering. A list of basic color codes can be found here.
In case you want to do the highlighting yourself you can use getSegments to only let sql-highlight parse the SQL string for you. You can then use the segments to highlight it yourself.
const { getSegments } = require('sql-highlight')const sqlString = "SELECT `id`, `username` FROM `users` WHERE `email` = 'test@example.com'"const segments = getSegments(sqlString)console.log(segments)
We use Jest for running our tests. The test suite can be run by running npm run test. This will run both Jest and Biome.
Code style
We use Biome for making sure that our code remains pretty and consistent throughout the project. If your editor doesn't automatically pick up our config you can lint the code using npm run lint.
A note on Dependabot
Dependabot Auto
Merge is
installed in this repository to automatically merge dependabot PRs for minor
version updates. Only PRs that pass the tests get merged. No new releases will
be created for dependency updates as there are no production dependencies and a
release would therefore be completely unnecessary.
This was initially a fork from https://github.com/pomahtuk/sequilize-highlight.
The repo wasn't being updated, NPM wasn't serving the latest version and there
was a severe memory leak. Though the latest version now exists on NPM, issues
still persist. This repo serves to address those problems, as well as providing
a cleaner interface that's not bound to Sequelize.
With version 3.0.0 the library was almost completely rewritten, which leaves
very little similarity with the original repo.