better-sql
better-sql copied to clipboard
Generate sql query from a concise query syntax inspired from EdgeDB and GraphQL
better-sql.ts
Generate sql query from a concise query syntax inspired from EdgeDB and GraphQL.
Online Playground: https://better-sql.surge.sh
Supported Features
- [x] output typical sql query, compatible with mysql, postgres, sqlite, e.t.c.
- [x] automatically add table name on columns if not specified
- [x] inner join with nested
table {fields} - [x] left join with nested
table [fields] - [x] nested
selectsub-query - [x]
wherestatement - [x]
havingstatement - [x]
group bystatement - [x] aggregate function, e.g.
sum(score) - [x]
order bystatement - [x]
limitandoffsetstatement
TypeScript Signature
export function queryToSQL(query: string): string
Usage
import { queryToSQL } from 'better-sql.ts'
import { db } from './db'
let keyword = '%script%'
let query = 'select post [...] where title like :keyword'
let sql = queryToSQL(query)
let result = db.query(sql, { keyword })
Example
A query in better-sql:
|
is converted into formatted sql as below:
|
Details refers to sample.ts and lang.spec.ts
License
This project is licensed with BSD-2-Clause
This is free, libre, and open-source software. It comes down to four essential freedoms [ref]:
- The freedom to run the program as you wish, for any purpose
- The freedom to study how the program works, and change it so it does your computing as you wish
- The freedom to redistribute copies so you can help others
- The freedom to distribute copies of your modified versions to others