happy-sql
v2.4.0
Published
sql to js ast parser and printer
Readme
Happy Sql

Write SQL as JavaScript function calls: parse, print, convert.
SQL written as JS lives in .js files it gets linted, formatted, and transformed by standard tooling. Happy Sql is the engine that converts between the two representations and exposes the underlying AST for tools that need it.
Install
npm i happy-sqlCLI
The CLI reads from stdin and auto-detects direction: SQL in → JS out, JS in → SQL out.
# SQL → JS
echo "SELECT * FROM users WHERE id = :id" | happy-sql
# [
# select('*', from(users), where(id === ':id')),
# ];
# JS → SQL (pipe the output back)
echo "[\n select('*', from(users), where(id === ':id')),\n];" | happy-sql
# SELECT *
# FROM users
# WHERE id = :idAPI
import {
convertSqlToJs,
convertJsToSql,
parseSql,
printSql,
} from 'happy-sql';convertSqlToJs(sql)
convertSqlToJs('SELECT COUNT(*)\nFROM users\n');
// [
// select(count('*'), from(users)),
// ];convertJsToSql(js)
convertJsToSql('select(count(\'*\'), from(users))');
// "SELECT COUNT(*)\nFROM users\n"parseSql(sql)
Returns a Babel-compatible AST node. Use when you need to inspect or transform the tree directly.
printSql(ast)
Prints a Babel AST node back to a SQL string.
Used by
@putout/processor-sql— lets 🐊Putout lint and transform.sqlfiles@putout/plugin-sql— SQL-specific rules for Putout (apply-count,postgres, SQLite↔Postgres conversions)putnik— code transformation engine that writes AST into SQLite and runs SQL-aware plugins against it
License
MIT
