Igniculus
SQL Syntax Highlighter and Logger. Unadorned and customizable.
Install
$ npm install igniculus
Usage
const igniculus = require('igniculus')();
igniculus('SELECT [port] AS Printer, \'on fire\' AS Status FROM [Printers] P WHERE P."online" AND P."check"');
Options
A default color scheme is provided. However, you can define the highlight style for each rule, passing them along when instantiating the logger function:
const igniculus = require('igniculus');
const options = {
constants: { mode: 'inverse', fg: 'red', bg: 'white' },
standardKeywords: { mode: 'bold', fg: 'black' },
lesserKeywords: { mode: 'bold', fg: 'black' },
prefix: { mode: 'bold', fg: 'white', text: '(query) '}
};
const illumine = igniculus(options);
illumine('SELECT * FROM Student s WHERE s.programme = \'IT\' AND EXISTS (SELECT * FROM Enrolled e JOIN Class c ON c.code = e.code JOIN Tutor t ON t.tid = c.tid WHERE e.sid = s.sid AND t.name LIKE \'%Hoffman\')');
The options argument is optional and each property should be one of the following.
Rules
- options.constants - Values surrounded by single quotes. E.g:
'static'
- options.numbers - Numeric values. E.g:
2.5
- options.operators - Arithmetic, Bitwise and Comparison operators. E.g:
+
or >=
- options.delimitedIdentifiers - Text between brackets or double quotes. E.g:
[Employee]
or "salary"
- options.dataTypes - One of the included data types. For now it contains those defined in T-SQL. E.g:
INTEGER
or VARCHAR
- options.standardKeywords - One the included keywords. Contains the most widely used T-SQL and SQL-92 Standard keywords. E.g:
SELECT
or CONSTRAINT
- options.lesserKeywords - One of the included subset of keywords. E.g:
ANY
, AVG
or DESC
- options.prefix
- prefix.text - A prefix can be appended to every log through this option. This prefix can be styled like any previous options.
- prefix.replace - Also, a string or regular expression can be provided and it will replace (if a prefix.text was given) or remove a prefix that matches such parameter. E.g: Sequelize prefixes every SQL statement with
Executing (default):
This is removed by default by the option prefix: { replace: /.*?: / }
- options.postfix
- postfix.text - A postfix can be appended to every log through this option. This postfix can be styled like any previous options.
If defined, the options argument takes precedence over default options. If a rule or it´s style is missing it won't be applied. This allows to "enable" or "disable" certain syntax highlighting as you see fit. (Examples below)
Styles
All of the previous rule styles can be defined like this:
const options = {
constants: {
mode: 'inverse',
fg: 'red',
bg: 'white'
},
...
};
Each style having an optional:
- style.mode - Modifier. E.g:
'bold'
- style.fg - Foreground text color. E.g:
'red'
- style.bg - Background color. E.g:
'black'
These can be one of the following.
Modifiers
reset
bold
dim
italic
underline
blink
inverse
hidden
strikethrough
Colors (Foreground and Background)
black
red
green
yellow
blue
magenta
cyan
white
Examples
const defaults = {
constants: { mode: 'dim', fg: 'red' },
delimitedIdentifiers: { mode: 'dim', fg: 'yellow' },
dataTypes: { mode: 'dim', fg: 'green' },
standardKeywords: { mode: 'dim', fg: 'cyan' },
lesserKeywords: { mode: 'bold', fg: 'black' },
prefix: { replace: /.*?: / }
};
const igniculus = require('igniculus')(
{
constants: { mode: 'bold', fg: 'yellow' },
numbers: { mode: 'bold', fg: 'magenta' },
delimitedIdentifiers: { mode: 'bold', fg: 'red' },
standardKeywords: { mode: 'bold', fg: 'blue' }
}
);
igniculus("INSERT INTO [Printers] ([port], [name], [ready], [online], [check]) VALUES ('lp0', 'Bob Marley', 0, 1, 1)");
Integration
Igniculus' logger is a drop in replacement on any tool that passes the logging function either a string
or Object
paramater. In the latest case the toString()
method will be called to obtain a string
primitive.
Sequelize
Using igniculus with sequelize is straightforward.
const Sequelize = require('sequelize');
const igniculus = require('igniculus')();
const sequelize = new Sequelize('database', 'username', 'password', {
logging: igniculus
});
const Sequelize = require('sequelize');
const igniculus = require('igniculus')(
{
constants: { fg: 'red' },
delimitedIdentifiers: { fg: 'yellow' },
dataTypes: { fg: 'red' },
standardKeywords: { fg: 'magenta' },
lesserKeywords: { mode: 'bold', fg: 'black' },
prefix: {
mode: 'bold',
fg: 'white',
replace: /.*?:/,
text: '(Sequelize)'
},
postfix: { text:'\r\n' }
}
);
const sequelize = new Sequelize('database', 'username', 'password',
{
logging: igniculus
});
...
sequelize.sync({ logging: igniculus});
Before
After
Future Upgrades
- Custom keywords
- Custom rules
- Selecting log stream E.g:
process.stdout
Maintainers
License
MIT