snack-query-builder

Query generator for SQL

Usage no npm install needed!

<script type="module">
  import snackQueryBuilder from 'https://cdn.skypack.dev/snack-query-builder';
</script>

README

Snack Query Builder NPM

Description

This NPM is not an ORM!. Snack Query Builder NPM is just a helper useful to build SQL Queries directly from typescript using clear and easy to understand syntax.

Installation

$ npm install snack-query-builder

Use of the NPM


import { Aggregation, SnackQueryBuilder } from 'snack-query-builder/dist';

.....

const query = new SnackQueryBuilder();
query.from('table_name').end()
console.log(query.build())

Result query

select
        *
from    table_name

Sample using select with projection and where clauses

const query = new SnackQueryBuilder();
query
  .from('table_name')
  .select('column1', 'column3 as newcolname')
  .where()
  .greaterOrEqualThan(Aggregation.and, 'column1', 100)
  .end();

console.log(query.build(Colorful.Terminal));

Result query

select
        column1,
        column3 as newcolname
from    table_name
where   column1 >= '100'

Sample using group and having

const query = new SnackQueryBuilder();
query
  .from('table_name')
  .select('column1', 'column3 as newcolname')
  .where()
  .greaterOrEqualThan(Aggregation.and, 'column1', 100)
  .groupBy('column1', 'column3')
  .having()
  .greaterThan(Aggregation.and, 'colum3', 700)
  .end();

console.log(query.build(Colorful.Terminal));

Result query

select
        column1,
        column3 as newcolname
from    table_name
where   column1 >= '100'
group by column1,
        column3
having colum3 > '700'

Sample adding top of rows

const query = new SnackQueryBuilder();
query
  .from('table_name')
  .select('column1', 'column3 as newcolname')
  .top(100)
  .where()
  .greaterOrEqualThan(Aggregation.and, 'column1', 100)
  .groupBy('column1', 'column3')
  .having()
  .greaterThan(Aggregation.and, 'colum3', 700)
  .end();

console.log(query.build(Colorful.Terminal));

Result query

select  top 100
        column1,
        column3 as newcolname
from    table_name
where   column1 >= '100'
group by column1,
        column3
having colum3 > '700'

Sample using a sub query

const subQuery = new SnackQueryBuilder();
subQuery
  .from('table_name')
  .select('column1', 'column3')
  .where()
  .greaterOrEqualThan(Aggregation.and, 'column1', 100)
  .groupBy('column1', 'column3')
  .having()
  .greaterThan(Aggregation.and, 'colum3', 700)
  .end();

const supQuery = new SnackQueryBuilder();
supQuery
  .fromSubQuery(subQuery)
  .select('column1')
  .where()
  .in(Aggregation.and, 'column1', 1, 2, 3)
  .end();

Result query

select
        column1
from    (
        select
                column1,
                column3
        from    table_name
        where   column1 >= '100'
        group by column1,
                column3
        having colum3 > '700'
       ) as sub_query
where   column1 in ('1', '2', '3')

Support

This is an open source project. It can grow thanks to the sponsors and support by the amazing backers.

Stay in touch

License

This is MIT licensed