# 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

```bash
$ npm install snack-query-builder
```

## Use of the NPM

```typescript

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

.....

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

```

**Result query**

```sql
select
        *
from    table_name
```

### Sample using select with projection and where clauses

```typescript
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**

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

### Sample using group and having

```typescript
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**

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

### Sample adding top of rows

```typescript
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**

```sql
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

```typescript
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**

```sql
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

- Author - Luis Arias <ariassd@gmail.com>
  [GitHub profile](https://github.com/ariassd)

## License

This is [MIT licensed](LICENSE)
