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

sql-typechecker

v0.0.134

Published

SQL-Typechecker is a CLI tool written in TypeScript, typechecking PostgreSQL files and generating TypeScript type definitions for them.

Downloads

451

Readme

SQL-Typechecker

SQL-Typechecker is a CLI tool written in TypeScript, typechecking PostgreSQL files and generating TypeScript type definitions for them.

It reads your SQL DDL statements (CREATE TABLE, etc) on one hand and your SQL functions (CREATE FUNCTION ...) on the other. It then typechecks your SQL functions and generates TypeScript files for them.

Design goals:

  • Don't rely on PostgreSQL for types. Postgres itself doesn't perform rigorous enough type-checking for our purposes.
  • Don't just generate TypeScript types; also type-check SQL statements.
  • Support branded types (aka Haskell/Rust newtypes).
  • Support JSON output with nested data type parsing.
  • Support as much useful PostgreSQL syntax as possible.

Small example

Consider the following DDL file:

-- sql/datamodel.sql
CREATE TABLE my_table (
  id int8 NOT NULL PRIMARY KEY,
  name text
);

And the following functions file:

-- sql/functions.sql
CREATE FUNCTION my_function() RETURNS SETOF record AS $$
  SELECT id, name 
    FROM my_table
$$ LANGUAGE sql;

Running SQL-Typechecker as follows:

> sql-typechecker --dir ./sql --out ./sql

Will generate a TypeScript function with the following type:

async function my_function(pool: Pool, args: {}): Promise<{ id: number; name: string | null }[]>

The following functions file will fail to typecheck:

-- functions.sql
CREATE FUNCTION my_function() RETURNS SETOF record AS $$
  SELECT id, name 
    FROM my_table
   WHERE name = 2 --> type error
$$ LANGUAGE sql;

Extended example

Consider the following DDL file:

-- sql/datamodel.sql
CREATE DOMAIN customer_id AS int8;
CREATE DOMAIN order_id AS int8;

CREATE TABLE customers (
  id customer_id NOT NULL PRIMARY KEY,
  name text
);
CREATE TABLE orders (
  id order_id NOT NULL PRIMARY KEY,
  customer_id customer_id NOT NULL REFERENCES customers(id) ON DELETE CASCADE,
  description text,
);

And the following functions file:

-- sql/functions.sql
CREATE FUNCTION get_customers(customer_ids customer_id[]) RETURNS SETOF record AS $$
  SELECT id, name, grouped_orders.grouped_orders
    FROM customers
    LEFT OUTER JOIN (SELECT customer_id, 
                            ARRAY_AGG(JSONB_BUILD_OBJECT(
                              'id', id,
                              'description', description
                            )) AS grouped_orders
                       FROM orders
                      GROUP BY customer_id
                    ) AS grouped_orders
                       ON grouped_orders.customer_id = customers.id
  WHERE customers.id = ANY(customer_ids)
$$ LANGUAGE sql;

Running SQL-Typechecker as follows:

> sql-typechecker --dir ./extendedexample --out ./extendedexample/out

Will generate a TypeScript function with the following type:

async function get_customers(
  pool: Pool,
  args: { customer_ids: types.customer_id[] }
): Promise<
  {
    id: types.customer_id;
    name: string | null;
    grouped_orders:
      | {
          id: types.order_id;
          description: string | null;
        }[]
      | null;
  }[]
>

Note types.customer_id, declared as branded type:

// types.ts
export type customer_id = number & { readonly __tag: "customer_id" };
```;

## Project status
Is this project finished? No, quite a few functions, syntax elements, etc., are not implemented yet. It has some rough edges, and performance can also be significantly improved.

That said, I've been using this library for years in multiple commercial projects, and it works very well within its current limitations.

In the future, I would love to add support for `plpgsql`. The biggest blocking factor is the lack of support in the parsing library SQL-Typechecker is built upon.

PRs, questions, remarks, and advice are all very welcome!