JSPM

  • Created
  • Published
  • Downloads 6
  • Score
    100M100P100Q30249F
  • License MIT

Convert informal SQL SELECT to formal SQL.

Package Exports

  • pg-slang

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (pg-slang) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

pg-slang

Convert informal SQL SELECT to formal SQL. ex:

-- SLANG
SELECT "food name", "calcium" FROM "apples"
-- SQL
SELECT "name", "ca", "ca_e" FROM "compositions"
WHERE TRUE AND (FALSE OR ("tsvector" @@ 'apples'))

examples

const slang = require('pg-slang');
// slang(<informal sql>, <entity function>, [this], [options])
// - <entity function>(<text>, <type>, <hint>)
// - [options]: {
//     from: default table,
//     limit: global max limit,
//     limits: {table specific limits}
//   }
// -> Promise <formal sql>

var txt = `SELECT "food name", "calcium" FROM "apples";`;
slang(txt, () => ['sample']).then(console.log);
// SELECT "sample", "sample" FROM "sample" WHERE TRUE AND TRUE


function fnA(text, type, hint) {
  if(type==='column') return ['name'];
  return ['compositions'];
};
slang(txt, fnA).then(console.log);
// SELECT "name", "name" FROM "compositions" WHERE TRUE AND TRUE


function fnB(text, type, hint) {
  if(type==='column') return ['ca', 'ca_e'];
  return ['compositions'];
};
slang(txt, fnB).then(console.log);
// SELECT "ca", "ca_e", "ca", "ca_e" FROM "compositions" WHERE TRUE AND TRUE


var columns = {
  'food code': ['code'],
  'food name': ['name'],
  'calcium': ['ca', 'ca_e'],
  'magnesium': ['mg', 'mg_e']
};
var tables = ['food', 'compositions'];
function fnC(text, type, hint) {
  if(type==='column') return columns[text];
  return tables.includes(text)? ['compositions']:[];
};
slang(txt, fnC).then(console.log);
// SELECT "name", "ca", "ca_e" FROM "apples" WHERE TRUE AND TRUE


function fnD(text, type, hint) {
  if(type==='column') return columns[text];
  return tables.includes(text)? ['compositions']:[`"tsvector" @@ '${text}'`];
};
slang(txt, fnD).then(console.log);
// SELECT "name", "ca", "ca_e" FROM "notable" WHERE TRUE AND (FALSE OR ("tsvector" @@ 'apples'))