xsql

SQL Query Builder

  • xsql

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
xsql
811.0.110 years ago10 years agoMinified + gzip package size for xsql in KB

Readme

XSQL
var Xsql = require('xsql');
var x = new Xsql({dialect:'mysql'});

var query = [
  x.select(x.names(['col1','col2'],'tbl')),
  x.from(x.name('tbl')),
  x.where(x.eq(x.name('id','tbl'),2)),
  ';'
].join(' ');

select `tbl`.`col1`,`tbl`.`col2` from `tbl` where `tbl`.`id`=2 ;

Primitives



Examples

Functions

var x = new xsql({dialect:'mysql'});

var concat = x.func('concat_ws',[
  x.wrap(' '),
  x.name('col1','tbl'),
  x.name('col2','tbl')
]);

var group = x.func('group_concat',['distinct', concat],' ');

var partial = x.as(group,x.name('alias'));

group_concat(distinct concat_ws(' ',`tbl`.`col1`,`tbl`.`col2`)) as `alias`

Join Tables

var query = [
  x.select([x.name('col','tbl1'),x.name('col','tbl2')]),
  x.from(x.name('tbl1')),
  x.join(x.name('tbl2'),x.eq(x.name('id','tbl1'),x.name('id','tbl2')),'left'),
  ';'
].join(' ');

select `tbl1`.`col`,`tbl2`.`col` from `tbl1`
  left join `tbl2` on `tbl1`.`id`=`tbl2`.`id` ;

PostgreSQL Schema

public by default
var x = new xsql({dialect:'pg'});
var query = [
  x.select(x.name('col','tbl',x.schema())),
  x.from(x.name('tbl',x.schema())),
  ';'
].join(' ');

select "public"."tbl"."col" from "public"."tbl" ;
globally defined
var x = new xsql({dialect:'pg', schema:'y'});
var query = [
  x.select(x.name('col','tbl',x.schema())),
  x.from(x.name('tbl',x.schema())),
  ';'
].join(' ');

select "y"."tbl"."col" from "y"."tbl" ;
specify schema
var x = new xsql({dialect:'pg', schema:'y'});
var query = [
  x.select(x.name('col','tbl',x.schema('z'))),
  x.from(x.name('tbl',x.schema())),
  ';'
].join(' ');

select "z"."tbl"."col" from "y"."tbl" ;
skip on other dialects
var x = new xsql({dialect:'sqlite'});
var query = [
  x.select(x.name('col','tbl',x.schema('z'))),
  x.from(x.name('tbl',x.schema())),
  ';'
].join(' ');

select "tbl"."col" from "tbl" ;

Wrapping it up

concat

function concat(columns, separator) {
  return (/mysql|pg/.test(x.dialect))
    ? x.func('concat_ws',[x.wrap(separator),columns])
    // sqlite
    : columns.join("||'"+separator+"'||");
}
mysql
var x = new xsql({dialect:'mysql'});
var partial = concat([x.name('col1','tbl'), x.name('col2','tbl')],',');
concat_ws(',',`tbl`.`col1`,`tbl`.`col2`)
sqlite
var x = new xsql({dialect:'sqlite'});
var partial = concat([x.name('col1','tbl'), x.name('col2','tbl')],',');
"tbl"."col1"||','||"tbl"."col2"

group

function group (columns) {
  return (/mysql|sqlite/.test(x.dialect))
    ? x.func('group_concat',['distinct',columns],' ')
    // pg
    : x.func('string_agg',['distinct',[columns,x.wrap(',')].join()],' ')
}
mysql
var x = new xsql({dialect:'mysql'});
var partial = group([x.name('col1','tbl'), x.name('col2','tbl')],',');
group_concat(distinct `tbl`.`col1`,`tbl`.`col2`)
pg
var x = new xsql({dialect:'pg'});
var partial = group([x.name('col1','tbl'), x.name('col2','tbl')],',');
string_agg(distinct "tbl"."col1","tbl"."col2",',')

License

MIT