knex-case
⚙️ A Knex.js plugin to add case statement support
Installation
$ yarn add knex-case
or
$ npm i knex-case
Usage
Simple
(CASE WHEN column=1 THEN 1 ELSE 0 END)
;const Knex = client: 'mssql' ; const q = Knex ; console; // (CASE WHEN column=1 THEN 1 ELSE 0 END);
Conditional Simple
(CASE WHEN column=1 OR column=2 THEN 1 ELSE 0 END)
;const Knex = client: 'mssql' ; const q = Knex ; console; // (CASE WHEN column=1 OR column=2 THEN 1 ELSE 0 END);
Nested Simple
(CASE WHEN column=1 THEN (CASE WHEN column='' THEN 2 ELSE 0 END) ELSE 0 END)
;const Knex = client: 'mssql' ; const q = Knex ; console; // (CASE WHEN column=1 THEN (CASE WHEN column='' THEN 2 ELSE 0 END) ELSE 0 END);
Caveats
String Escaping
Currently if you want to output correct SQL that includes string comparisons (ie. something='some-string'
) you will have to escape the internal value.
Example:
// String literalsKnex; // Escape sequencesKnex; // Alternating quotationsKnex;