SQG - Compile SQL to Type-Safe Code

2 min read Original article ↗

Write your queries in plain SQL with simple annotations. SQG introspects your database at build time and generates fully typed code.

-- QUERY find_user :one

@set email = '[email protected]'

SELECT * FROM users WHERE email = ${email};

becomes

// TypeScript

findUser(email: string): User | undefined

// Java

public UserRow findUser(String email) throws SQLException

No ORMs. No query builders. Just SQL and types.

Real SQL

Write standard SQL that runs directly against your database. No proprietary query language to learn.

True Type Safety

Types are introspected from your actual schema at build time. Catch errors before production.

Multiple Databases

SQLite, DuckDB, and PostgreSQL. Same annotations work across all engines.

TypeScript & Java

Generate code for multiple languages from the same SQL definitions.

Diagram

Most tools try to parse SQL to figure out types. SQG takes a simpler approach: run the query.

Your migrations set up the schema, SQG executes your queries against a real database, and captures the exact column names and types from the result. No parser limitations, no guessing—100% accurate types.

ORMsSQG
Learn a query builder APIWrite standard SQL
Types generated from modelsTypes introspected from queries
Hidden SQL, hard to optimizeTransparent, debuggable
Runtime query translationNo runtime overhead

SQG is for developers who want the safety of typed code without giving up control over their SQL.

GeneratorDatabaseLanguage
typescript/sqliteSQLiteTypeScript (sync)
typescript/duckdbDuckDBTypeScript (async)
java/sqlite, java/duckdb, java/postgresSQLite, DuckDB, PostgreSQLJava (JDBC)
java/duckdb/arrowDuckDBJava (Arrow API)

Quick Start Install SQG and generate your first type-safe queries in minutes.

Playground Try SQG in your browser—no installation required.

SQL Syntax Learn the annotation syntax for queries, migrations, and parameters.

Build with AI Use SQG with Claude and Cursor to generate code through MCP.

FAQ Common questions and troubleshooting.

SQG’s @set syntax is compatible with DBeaver, the popular open-source database IDE. Develop and test your queries interactively, then run SQG to generate code.

SQG is open source under the Apache 2.0 license.

GitHub · Issues