snack-query-builder
Version:
Query generator for SQL
174 lines (135 loc) • 3.22 kB
Markdown
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.
```bash
$ npm install snack-query-builder
```
```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
```
```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'
```
```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'
```
```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'
```
```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)