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 🙏

© 2025 – Pkg Stats / Ryan Hefner

sql-typed

v0.1.11

Published

Typesafe query builder written in TypeScript

Readme

SQLTyped

CircleCI

SQLTyped is a simple, typesafe SQL DSL implementation in TypeScript that aims to provide a natural SQL interface for TypeScript applications. Goals of SQLTyped:

  • No magic. SQLTyped is not an ORM, you need to know SQL to use SQLTyped.
  • No performance surprises. The DSL looks like SQL and compiles down to predictable queries.
  • Best-in-class type safety. The DSL should avoid as many invalid queries as possible.
  • As few dependencies as possible. As of now, SQLTyped only depends on the pg module for querying PostgreSQL.
npm install sql-typed

SQLTyped is in active development and the API is not guaranteed to be stable.

Basic Example

import { createPool, createTable, TableAttributes, ColumnType } from 'sql-typed';

interface User extends TableAttributes {
  id: number;
  name: string;
}

// Type safe columns are created from the table definition.
const userTable = createTable<User>("users", {
  id: { type: ColumnType.PrimaryKey },
  name: { type: ColumnType.String },
  age: { type: ColumnType.Number, nullable: true }
});

const pool = createPool({
  host: 'localhost',
  user: 'postgres',
  password: 'postgres',
  database: 'postgres',
});

pool.transaction(async transaction => {
  // Create a user
  const [insertedUser] = await userTable
    .insert()
    .values([{ name: 'Josh' }])
    .execute(transaction);

  // Simple query
  const usersNamedJosh = await userTable
    .select()
    .where(userTable.columns.name.eql('Josh'))
    .execute(transaction);
  
  // Complex queries
  const millennialsNamedMia = await userTable
    .select()
    // Multiple `where` is equivalent to `and` chaining for conjunction logic
    .where(
      userTable.columns.name.like('Mia%')
    ).where(
      userTable.columns.age.gte(25).and(userTable.columns.age.lte(35))
    ).execute(
      transaction
    );

  // Count queries
  const countOver50 = await userTable
    // `where` also accepts a function to build predicates
    .where(({ age }) => age.gte(50))
    .count()
    .execute(transaction);
});

Developing

To run tests:

docker run --rm -d -p 5432:5432 postgres
npm test

Type Safety

There are some cases where TypeScript's type system cannot express a needed type (or I haven't figured out how). Known cases:

  • Joins with 3 or more tables. While joining two tables preserves type information, querying across 3 or more does not. Example:
    const rows = await articleTable
    .select()
    /**
     * This is typesafe. The args to the predication function are table-specific
     * and the return type of the query is table-specific.
     */
    .join(userTable, ([a, u]) => a.userId.eqls(u.id), JoinType.Left)
    /**
     * Adding a 3rd table drops down to very wide vague types. This could be
     * solved if TypesScript supported variadic types parameters.
     */
    .join(
      commentTable,
      ([a, _u, c]) => a.id.eqls(c.articleId),
      JoinType.Left
    )
    .execute(transaction);