npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

pg-slang

v0.2.6

Published

Convert informal SQL SELECT to formal SQL.

Downloads

132

Readme

pg-slang

NPM

Convert informal SQL SELECT to formal SQL.

NOTE: pg-english can convert english query to informal SQL.

-- ex: SLANG
SELECT "food name", "calcium" FROM "apples"
-- ex: SQL
SELECT "name", "ca", "ca_e" FROM "compositions"
WHERE TRUE AND (FALSE OR ("tsvector" @@ 'apples'))
const slang = require('pg-slang');
// slang(<informal sql>, <map fn>, [this], [options])
// -> Promise (formal sql)

// <informal sql>:
// SELECT "food name", "trans fat" FROM "food" ORDER BY "trans fat" DESC
// SELECT "protein", "vitamin d" FROM "poultry ORDER BY "vitamin d" DESC
// SELECT "sum: essential amino acids" AS "amino" FROM "meat" ORDER BY "amino"
// ...

// <map fn>(<text>, <type>, [hint], [from]):
// - text: field name, like "food name", "trans fat", "food", ...
// - type: field type, can be "from","columns", "where", "having", "orderBy", or "groupBy"
// - hint: field hint, can be null, "all", "sum", or "avg"
// - from: field from, will be null for type=table
// -> Promise [<value>]
// - value: expression string

// [options]:
// - from: default table
// - limit: default maximum limit
// - limits: table specific maximum limts
// 1. 
function fnA(text, type, hint, from) {
  return ['sample1', 'sample1'];
};
slang(`SELECT "food name", "calcium" FROM "apples"`, fnA).then(console.log);
// SELECT "sample", "sample" FROM "sample" WHERE TRUE AND TRUE


function fnB(text, type, hint, from) {
  if(type==='column') return ['name'];
  return ['compositions'];
};
slang(`SELECT "food name", "calcium" FROM "apples"`, fnB).then(console.log);
// SELECT "name", "name" FROM "compositions" WHERE TRUE AND TRUE


function fnC(text, type, hint, from) {
  if(type==='column') return ['ca', 'ca_e'];
  return ['compositions'];
};
slang(`SELECT "food name", "calcium" FROM "apples"`, fnC).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 fnD(text, type, hint, from) {
  if(type==='column') return columns[text];
  return tables.includes(text)? ['compositions']:[];
};
slang(`SELECT "food name", "calcium" FROM "apples"`, fnD).then(console.log);
// SELECT "name", "ca", "ca_e" FROM "null" WHERE TRUE AND TRUE


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


var options = {from: 'compositions'};
slang(`SELECT "food name", "calcium" FROM "apples"`, fnE, null, options).then(console.log);
// SELECT "name", "ca", "ca_e" FROM "compositions" WHERE TRUE AND (FALSE OR ("tsvector" @@ 'apples'))


// PRIMARY USECASE
// ---------------
function fnDB(text, type, hint, from) {
  return new Promise((resolve, reject) => {
    // ...
    // <do some database lookup>
    // ...
  });
};
slang(/*  */, fnDB, null, options).then(console.log);
// SELECT "name", "ca", "ca_e" FROM "compositions" WHERE TRUE AND (FALSE OR ("tsvector" @@ 'apples'))


// NOTES
// -----
// 1. Map function return value can be an expression array
// 2. For column, return multiple values to select multiple columns
// 3. But in expressions, only first return value is considered
// 4. Hints perform an operation on matching columns
// 5. Use hint to decide which columns to return
// 6. For table, returning expression will append to where
// 7. Return expression and table name for full association
// 8. Hint can be used in column text as "<hint>: <column text>"
// 9. Hint "all": all|each|every
// 10. Hint "sum": sum|gross|total|whole|aggregate
// 11. Hint "avg": avg|mid|par|mean|norm|center|centre|average|midpoint
// 12. Experiment! And mail me for issues / pull requests!