happy-sql 1.7.1 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/ChangeLog +5 -0
- package/README.md +56 -6
- package/lib/happy-sql.js +3 -3
- package/lib/parser/parser.js +1 -1
- package/package.json +1 -1
package/ChangeLog
CHANGED
package/README.md
CHANGED
|
@@ -9,24 +9,74 @@
|
|
|
9
9
|
|
|
10
10
|
<img width="600" height="428" alt="image" src="https://github.com/user-attachments/assets/aefbd055-957e-4de3-aca5-c30e5e3ee3f9" />
|
|
11
11
|
|
|
12
|
-
SQL
|
|
12
|
+
Write SQL as JavaScript function calls: parse, print, convert.
|
|
13
|
+
|
|
14
|
+
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.
|
|
13
15
|
|
|
14
16
|
## Install
|
|
15
17
|
|
|
16
18
|
```
|
|
17
|
-
npm i happy-sql
|
|
19
|
+
npm i happy-sql
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## CLI
|
|
23
|
+
|
|
24
|
+
The CLI reads from stdin and auto-detects direction: SQL in → JS out, JS in → SQL out.
|
|
25
|
+
|
|
26
|
+
```sh
|
|
27
|
+
# SQL → JS
|
|
28
|
+
echo "SELECT * FROM users WHERE id = :id" | happy-sql
|
|
29
|
+
# [
|
|
30
|
+
# select('*', from(users), where(id === ':id')),
|
|
31
|
+
# ];
|
|
32
|
+
# JS → SQL (pipe the output back)
|
|
33
|
+
echo "[\n select('*', from(users), where(id === ':id')),\n];" | happy-sql
|
|
34
|
+
# SELECT *
|
|
35
|
+
# FROM users
|
|
36
|
+
# WHERE id = :id
|
|
18
37
|
```
|
|
19
38
|
|
|
20
39
|
## API
|
|
21
40
|
|
|
22
41
|
```js
|
|
23
|
-
|
|
42
|
+
import {
|
|
43
|
+
convertSqlToJs,
|
|
44
|
+
convertJsToSql,
|
|
45
|
+
parseSql,
|
|
46
|
+
printSql,
|
|
47
|
+
} from 'happy-sql';
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
### `convertSqlToJs(sql)`
|
|
51
|
+
|
|
52
|
+
```js
|
|
53
|
+
convertSqlToJs('SELECT COUNT(*)\nFROM users\n');
|
|
54
|
+
// [
|
|
55
|
+
// select(count('*'), from(users)),
|
|
56
|
+
// ];
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
### `convertJsToSql(js)`
|
|
24
60
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
61
|
+
```js
|
|
62
|
+
convertJsToSql('select(count(\'*\'), from(users))');
|
|
63
|
+
// "SELECT COUNT(*)\nFROM users\n"
|
|
28
64
|
```
|
|
29
65
|
|
|
66
|
+
### `parseSql(sql)`
|
|
67
|
+
|
|
68
|
+
Returns a Babel-compatible AST node. Use when you need to inspect or transform the tree directly.
|
|
69
|
+
|
|
70
|
+
### `printSql(ast)`
|
|
71
|
+
|
|
72
|
+
Prints a Babel AST node back to a SQL string.
|
|
73
|
+
|
|
74
|
+
## Used by
|
|
75
|
+
|
|
76
|
+
- [`@putout/processor-sql`](https://github.com/coderaiser/putout/tree/master/packages/processor-sql) — lets 🐊[Putout](https://github.com/coderaiser/putout) lint and transform `.sql` files
|
|
77
|
+
- [`@putout/plugin-sql`](https://github.com/coderaiser/putout/tree/master/packages/plugin-sql) — SQL-specific rules for Putout (`apply-count`, `postgres`, SQLite↔Postgres conversions)
|
|
78
|
+
- [`putnik`](https://github.com/coderaiser/putnik) — code transformation engine that writes AST into SQLite and runs SQL-aware plugins against it
|
|
79
|
+
|
|
30
80
|
## License
|
|
31
81
|
|
|
32
82
|
MIT
|
package/lib/happy-sql.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import {parse} from '@putout/babel';
|
|
2
2
|
import {print} from '@putout/printer';
|
|
3
|
-
import {
|
|
3
|
+
import {parseSql} from '#parser';
|
|
4
4
|
import {printSql} from '#printer';
|
|
5
5
|
|
|
6
6
|
export {printSql} from '#printer';
|
|
7
|
-
export {
|
|
7
|
+
export {parseSql} from '#parser';
|
|
8
8
|
|
|
9
|
-
export const convertSqlToJs = (sql) => print(
|
|
9
|
+
export const convertSqlToJs = (sql) => print(parseSql(sql));
|
|
10
10
|
|
|
11
11
|
export const convertJsToSql = (js) => printSql(parse(js));
|
package/lib/parser/parser.js
CHANGED
|
@@ -25,7 +25,7 @@ const parseCstDialect = (source, dialect) => parseCst(source, {
|
|
|
25
25
|
includeComments: true,
|
|
26
26
|
});
|
|
27
27
|
|
|
28
|
-
export const
|
|
28
|
+
export const parseSql = (source) => {
|
|
29
29
|
for (const dialect of DIALECTS) {
|
|
30
30
|
const [, cst] = tryCatch(parseCstDialect, source, dialect);
|
|
31
31
|
|