npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@sqg/sqg

v0.5.0

Published

SQG - SQL Query Generator - Type-safe code generation from SQL (https://sqg.dev)

Readme

SQG - SQL Query Generator

Type-safe code generation from SQL. Write SQL, get fully-typed database access code.

What it does

SQG reads annotated .sql files, executes queries against real databases to introspect column types, and generates type-safe code to execute the SQL queries.

The syntax of the .sql file is compatible with DBeaver, this allows to develop the SQL queries with it and then generate the code from the same file.

Features

  • Type-safe by design - Generates fully-typed code with accurate column types inferred from your database
  • Multiple database engines - Supports SQLite, DuckDB, and (soon) PostgreSQL
  • Multiple language targets - Generate TypeScript or Java code from the same SQL files
  • Arrow API support - Can generate Apache Arrow API bindings for DuckDB (Java)
  • DBeaver compatible - Works seamlessly with DBeaver for database development and testing
  • Complex type support - DuckDB: Handles structs, lists, and maps
  • Migration management - Built-in support for schema migrations and test data

Installation

pnpm add -g @sqg/sqg
pnpm approve-builds -g  # needed for sqlite dependency

Check if the install was successful:

sqg --help

Quick Start

Option 1: Use sqg init (Recommended)

# Initialize a new project (creates sqg.yaml and queries.sql)
sqg init

# Or with a specific database engine
sqg init --engine duckdb

# Generate code
sqg sqg.yaml

Option 2: Manual Setup

  1. Create sqg.yaml in your project root:
version: 1
name: my-project
sql:
  - engine: sqlite    # sqlite, duckdb, or postgres
    files:
      - queries.sql
    gen:
      - generator: typescript/better-sqlite3
        output: src/db.ts
  1. Write your SQL file with annotations

For example queries.sql:

-- MIGRATE createUsersTable
CREATE TABLE users (id INTEGER PRIMARY KEY,
                    name TEXT NOT NULL,
                    email TEXT);

-- QUERY getUserById :one
@set id = 1
SELECT id, name, email FROM users WHERE id = ${id};

-- QUERY getUsers
SELECT id, name, email FROM users;

-- EXEC insertUser
@set name = 'John'
@set email = '[email protected]'
INSERT INTO users (name, email) VALUES (${name}, ${email});
  1. Run SQG to generate code:
sqg sqg.yaml
  1. Use the generated code:
import Database from 'better-sqlite3';
import { Queries } from './db';

const db = new Database(':memory:');
const queries = new Queries(db);

// Run migrations
for (const sql of Queries.getMigrations()) {
  db.exec(sql);
}

// Type-safe queries
queries.insertUser('Alice', '[email protected]');
const user = queries.getUserById(1);
console.log(user?.name);

SQL Annotations

| Annotation | Description | |------------|-------------| | -- MIGRATE name | Schema migration (CREATE TABLE, etc.) | | -- QUERY name | SELECT query returning rows | | -- QUERY name :one | Query returning single row or undefined | | -- QUERY name :pluck | Return single (first) column value | | -- EXEC name | INSERT/UPDATE/DELETE (no result rows) | | -- TESTDATA name | Test data, runs after migrations | | @set var = value | Define parameter with sample value | | ${var} | Reference parameter in query |

Supported Databases & Generators

| Language | Database | API | Generator | Status | |----------|----------|-----|-----------|--------| | TypeScript | SQLite | better-sqlite3 | typescript/better-sqlite3 | Tested | | TypeScript | DuckDB | @duckdb/node-api | typescript/duckdb | Tested | | Java | Any (JDBC) | JDBC | java/jdbc | Tested | | Java | DuckDB | Apache Arrow | java/duckdb-arrow | Tested | | TypeScript | PostgreSQL | pg (node-postgres) | typescript/pg | under development |

CLI Commands

sqg <config>              # Generate code from config file
sqg init                  # Initialize new project with example files
sqg init --engine duckdb  # Initialize with specific database engine
sqg --validate <config>   # Validate config without generating code
sqg --format json <config> # Output as JSON (for tooling integration)
sqg syntax                # Show SQL annotation syntax reference
sqg --help                # Show all options

Documentation

Full documentation at sqg.dev

License

Apache-2.0