@sqg/sqg 0.2.0 → 0.2.1
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/README.md +156 -0
- package/dist/sqg.mjs +1 -1
- package/package.json +16 -2
package/README.md
ADDED
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
# SQG - SQL Query Generator
|
|
2
|
+
|
|
3
|
+
Type-safe code generation from SQL. Write SQL, get fully-typed database access code.
|
|
4
|
+
|
|
5
|
+
## What it does
|
|
6
|
+
|
|
7
|
+
SQG reads annotated `.sql` files, executes queries against real databases to introspect column types, and generates type-safe code to execute the SQL queries.
|
|
8
|
+
|
|
9
|
+
The syntax of the `.sql` file is compatible with [DBeaver](https://dbeaver.io/), this allows to develop the SQL
|
|
10
|
+
queries with it and then generate the code from the same file.
|
|
11
|
+
|
|
12
|
+
## Features
|
|
13
|
+
|
|
14
|
+
- **Type-safe by design** - Generates fully-typed code with accurate column types inferred from your database
|
|
15
|
+
- **Multiple database engines** - Supports SQLite, DuckDB, and (soon) PostgreSQL
|
|
16
|
+
- **Multiple language targets** - Generate TypeScript or Java code from the same SQL files
|
|
17
|
+
- **Arrow API support** - Can generate Apache Arrow API bindings for DuckDB (Java)
|
|
18
|
+
- **DBeaver compatible** - Works seamlessly with DBeaver for database development and testing
|
|
19
|
+
- **Complex type support** - DuckDB: Handles structs, lists, and maps
|
|
20
|
+
- **Migration management** - Built-in support for schema migrations and test data
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
## Installation
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
pnpm add -g @sqg/sqg
|
|
27
|
+
pnpm approve-builds -g # needed for sqlite dependency
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
Check if the install was successful:
|
|
31
|
+
```bash
|
|
32
|
+
sqg --help
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## Quick Start
|
|
36
|
+
|
|
37
|
+
### Option 1: Use `sqg init` (Recommended)
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
# Initialize a new project (creates sqg.yaml and queries.sql)
|
|
41
|
+
sqg init
|
|
42
|
+
|
|
43
|
+
# Or with a specific database engine
|
|
44
|
+
sqg init --engine duckdb
|
|
45
|
+
|
|
46
|
+
# Generate code
|
|
47
|
+
sqg sqg.yaml
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
### Option 2: Manual Setup
|
|
51
|
+
|
|
52
|
+
1. Create `sqg.yaml` in your project root:
|
|
53
|
+
|
|
54
|
+
```yaml
|
|
55
|
+
version: 1
|
|
56
|
+
name: my-project
|
|
57
|
+
sql:
|
|
58
|
+
- engine: sqlite # sqlite, duckdb, or postgres
|
|
59
|
+
files:
|
|
60
|
+
- queries.sql
|
|
61
|
+
gen:
|
|
62
|
+
- generator: typescript/better-sqlite3
|
|
63
|
+
output: src/db.ts
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
2. Write your SQL file with annotations
|
|
67
|
+
|
|
68
|
+
For example `queries.sql`:
|
|
69
|
+
|
|
70
|
+
```sql
|
|
71
|
+
-- MIGRATE createUsersTable
|
|
72
|
+
CREATE TABLE users (id INTEGER PRIMARY KEY,
|
|
73
|
+
name TEXT NOT NULL,
|
|
74
|
+
email TEXT);
|
|
75
|
+
|
|
76
|
+
-- QUERY getUserById :one
|
|
77
|
+
@set id = 1
|
|
78
|
+
SELECT id, name, email FROM users WHERE id = ${id};
|
|
79
|
+
|
|
80
|
+
-- QUERY getUsers
|
|
81
|
+
SELECT id, name, email FROM users;
|
|
82
|
+
|
|
83
|
+
-- EXEC insertUser
|
|
84
|
+
@set name = 'John'
|
|
85
|
+
@set email = 'john@example.com'
|
|
86
|
+
INSERT INTO users (name, email) VALUES (${name}, ${email});
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
3. Run SQG to generate code:
|
|
90
|
+
|
|
91
|
+
```bash
|
|
92
|
+
sqg sqg.yaml
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
4. Use the generated code:
|
|
96
|
+
|
|
97
|
+
```typescript
|
|
98
|
+
import Database from 'better-sqlite3';
|
|
99
|
+
import { Queries } from './db';
|
|
100
|
+
|
|
101
|
+
const db = new Database(':memory:');
|
|
102
|
+
const queries = new Queries(db);
|
|
103
|
+
|
|
104
|
+
// Run migrations
|
|
105
|
+
for (const sql of Queries.getMigrations()) {
|
|
106
|
+
db.exec(sql);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// Type-safe queries
|
|
110
|
+
queries.insertUser('Alice', 'alice@example.com');
|
|
111
|
+
const user = queries.getUserById(1);
|
|
112
|
+
console.log(user?.name);
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
## SQL Annotations
|
|
116
|
+
|
|
117
|
+
| Annotation | Description |
|
|
118
|
+
|------------|-------------|
|
|
119
|
+
| `-- MIGRATE name` | Schema migration (CREATE TABLE, etc.) |
|
|
120
|
+
| `-- QUERY name` | SELECT query returning rows |
|
|
121
|
+
| `-- QUERY name :one` | Query returning single row or undefined |
|
|
122
|
+
| `-- QUERY name :pluck` | Return single (first) column value |
|
|
123
|
+
| `-- EXEC name` | INSERT/UPDATE/DELETE (no result rows) |
|
|
124
|
+
| `-- TESTDATA name` | Test data, runs after migrations |
|
|
125
|
+
| `@set var = value` | Define parameter with sample value |
|
|
126
|
+
| `${var}` | Reference parameter in query |
|
|
127
|
+
|
|
128
|
+
## Supported Databases & Generators
|
|
129
|
+
|
|
130
|
+
| Language | Database | API | Generator | Status |
|
|
131
|
+
|----------|----------|-----|-----------|--------|
|
|
132
|
+
| TypeScript | SQLite | better-sqlite3 | `typescript/better-sqlite3` | Tested |
|
|
133
|
+
| TypeScript | DuckDB | @duckdb/node-api | `typescript/duckdb` | Tested |
|
|
134
|
+
| Java | Any (JDBC) | JDBC | `java/jdbc` | Tested |
|
|
135
|
+
| Java | DuckDB | Apache Arrow | `java/duckdb-arrow` | Tested |
|
|
136
|
+
| TypeScript | PostgreSQL | pg (node-postgres) | `typescript/pg` | under development |
|
|
137
|
+
|
|
138
|
+
## CLI Commands
|
|
139
|
+
|
|
140
|
+
```bash
|
|
141
|
+
sqg <config> # Generate code from config file
|
|
142
|
+
sqg init # Initialize new project with example files
|
|
143
|
+
sqg init --engine duckdb # Initialize with specific database engine
|
|
144
|
+
sqg --validate <config> # Validate config without generating code
|
|
145
|
+
sqg --format json <config> # Output as JSON (for tooling integration)
|
|
146
|
+
sqg syntax # Show SQL annotation syntax reference
|
|
147
|
+
sqg --help # Show all options
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
## Documentation
|
|
151
|
+
|
|
152
|
+
Full documentation at [sqg.dev](https://sqg.dev)
|
|
153
|
+
|
|
154
|
+
## License
|
|
155
|
+
|
|
156
|
+
Apache-2.0
|
package/dist/sqg.mjs
CHANGED
|
@@ -2237,7 +2237,7 @@ Documentation: https://sqg.dev
|
|
|
2237
2237
|
|
|
2238
2238
|
//#endregion
|
|
2239
2239
|
//#region src/sqg.ts
|
|
2240
|
-
const version = process.env.npm_package_version ?? "0.2.
|
|
2240
|
+
const version = process.env.npm_package_version ?? "0.2.1";
|
|
2241
2241
|
const description = process.env.npm_package_description ?? "SQG - SQL Query Generator - Type-safe code generation from SQL (https://sqg.dev)";
|
|
2242
2242
|
consola.level = LogLevels.info;
|
|
2243
2243
|
const program = new Command().name("sqg").description(`${description}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sqg/sqg",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.1",
|
|
4
4
|
"description": "SQG - SQL Query Generator - Type-safe code generation from SQL (https://sqg.dev)",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
|
@@ -20,7 +20,21 @@
|
|
|
20
20
|
"bugs": {
|
|
21
21
|
"url": "https://github.com/sqg-dev/sqg/issues"
|
|
22
22
|
},
|
|
23
|
-
"keywords": [
|
|
23
|
+
"keywords": [
|
|
24
|
+
"sql",
|
|
25
|
+
"codegen",
|
|
26
|
+
"code-generation",
|
|
27
|
+
"typescript",
|
|
28
|
+
"java",
|
|
29
|
+
"sqlite",
|
|
30
|
+
"duckdb",
|
|
31
|
+
"postgres",
|
|
32
|
+
"postgresql",
|
|
33
|
+
"type-safe",
|
|
34
|
+
"database",
|
|
35
|
+
"orm-alternative",
|
|
36
|
+
"query-builder"
|
|
37
|
+
],
|
|
24
38
|
"author": "Uwe Maurer",
|
|
25
39
|
"license": "Apache-2.0",
|
|
26
40
|
"dependencies": {
|