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

@triptease/sql-template

v0.37.8

Published

This is yet another SQL tagged template for Typescript/Javascript

Readme

Sql Template

This is yet another SQL tagged template for Typescript/Javascript

Why another library?

  • Typescript first
  • Functional/Immutable
  • Super simple implementation (see SQL.ts)
  • Full escaping of identifiers and values
  • Plugable to any DB (currently Postgres via @triptease/sql-template-postgres with pg)
  • Automatic support for prepareStatement naming (Postgres)

Installation

npm install @triptease/sql-template @triptease/sql-template-postgres
# or
bun add @triptease/sql-template @triptease/sql-template-postgres

Both packages are plain ESM JavaScript with type declarations, so they work in Node and Bun. @triptease/sql-template has no dependencies and @triptease/sql-template-postgres depends only on it (it does not depend on pg; bring your own driver). In Node they can also be loaded with require() (Node 20.19+/22.12+, which can require() ES modules).

Usage

Node with pg

import pg from "pg";
import {SQL, id, values} from "@triptease/sql-template";
import {statement, prepareStatement} from "@triptease/sql-template-postgres";

const pool = new pg.Pool();
const table = 'users', name = "O'Brien";

await pool.query(statement(SQL`select * from ${id(table)} where name = ${name}`));
// { text: 'select * from "users" where name = $1', values: ["O'Brien"] }

// named prepared statement (the name defaults to a hash of the SQL)
await pool.query(prepareStatement(SQL`select * from users where id in (${values([1, 2, 3])})`));
// { name: 'feb13b21…', text: 'select * from users where id in ($1, $2, $3)', values: [1, 2, 3] }

Bun

If you only target Bun, its built-in sql tagged template already binds values and escapes identifiers, so you probably don't need this library. Use it in Bun when your query code also has to run on Node with pg, or when you want queries as plain immutable values you can build, test and log (debugQuery) without a connection.

Composing

import {SQL, id, ids, text, values} from "@triptease/sql-template";

// templates nest, and values() can join them with an explicit (raw) separator
const filters = [SQL`${id('name')} = ${name}`, SQL`${id('age')} > ${30}`];
const query = SQL`select ${ids(['id', 'name'])} from users where ${values(filters, text(' and '))}`;
// select "id", "name" from users where "name" = $1 and "age" > $2

Templates are immutable (frozen) and flat. Expressions are identified by a Symbol.for brand rather than instanceof, so templates built by a different copy of @triptease/sql-template (e.g. two installed versions) still work, while plain data (such as parsed JSON) can never be mistaken for SQL.

Cheatsheet

Core (@triptease/sql-template)

| function | Description | |------------------------------------------------|---------------------------------------------------------------------------------------------| | SQL | The main function to create tagged templates for SQL (DB agnostic) | | text (alias raw) | Input raw SQL without any escaping (use with care) | | id / ids(names, separator?) | Input dynamic identifiers into SQL (escaped as needed) | | value (optional) / values(list, separator?) (alias spread) | Input one or more values into SQL (bound as parameters) | | template | Combine expressions into a (flattened, frozen) template | | isExpression / isText / isIdentifier / isValue / isTemplate / kindOf | Type guards based on the expression brand (for adapters) |

separator is an Expression and defaults to text(', ').

Postgres (@triptease/sql-template-postgres)

| function | Description | |------------------------------------------------|----------------------------------------------------------------------| | statement | Converts DB agnostic SQL template into a postgres statement (pg) | | prepareStatement | Converts DB agnostic SQL template into a named prepared statement (pg) | | escapeIdentifier / escapeLiteral | Postgres identifier and string literal escaping (ported from pg) | | debugQuery | Renders a template with values inlined, for debugging (see below) |

use with care -> Used incorrectly you can open yourself up to SQL injection

debugQuery escapes every value (strings, numbers, booleans, null, Dates, Buffers, arrays and objects), but it is meant for logging: execute queries with statement/prepareStatement so values are bound as parameters. Inlined literals are not always typed the way parameters are (e.g. arrays become array literal strings and objects JSON strings, which need a cast in some contexts). Dates are inlined as UTC ISO strings, so for timestamp (without time zone) or text targets the output only matches what pg executes when the process runs with TZ=UTC.

Breaking changes (since the pg-only releases)

  • The separator of values/spread/ids is now an Expression: replace values(xs, ' or ') with values(xs, text(' or ')). Passing a string throws a TypeError (it used to be inserted as raw SQL).
  • @triptease/sql-template-postgres no longer depends on pg; its QueryConfig is a local type that is structurally compatible with pg's.
  • Adapters throw on expressions they do not understand instead of silently dropping them, and SQL with an invalid escape sequence (e.g. SQL`\u` ) throws instead of producing the text undefined.
  • Public types use unknown instead of any; ids takes string[].
  • The packages are ESM only (they used to be CommonJS):
    • require() needs Node 20.19+/22.12+ (older Node versions can only import them).
    • TypeScript consumers need "module"/"moduleResolution" set to nodenext (or node20) or bundler. A CommonJS project using node16 (or nodenext before TypeScript 5.8) gets error TS1479; switch to one of those settings or load the packages with import().
    • Only the package roots are exported: deep imports such as @triptease/sql-template/Text fail with ERR_PACKAGE_PATH_NOT_EXPORTED. Import everything from @triptease/sql-template / @triptease/sql-template-postgres.
  • Expression is abstract and subclassing it is not supported: an instance of your own subclass is rejected with a TypeError (compose SQL/text/id/value/template instead).
  • debugQuery output changed: null/undefined render as NULL, booleans as TRUE/FALSE, negative numbers are parenthesised, NaN/Infinity are quoted, and Dates, Buffers, arrays and objects are rendered as escaped literals. Do not parse or compare its output.
  • A Template built directly with new Template([...]) now keeps the values of nested templates (they used to be dropped).

Extending

It is simple to extend to other DBs: switch on kindOf(expression) ('text', 'identifier', 'value', 'template') and throw on anything else. Have a look at the postgres implementation.

Development

Tool versions (node, bun) are pinned in mise.toml. Install mise, then:

mise install   # installs the pinned node and bun
./run          # install deps, clean, build (tsc --build), typecheck and test (bun test)

The repo is a bun workspace: each package has a published src/package.json (runtime dependencies only) and a private test/package.json (test dependencies). Tests import the packages by name.

The postgres integration tests (sql-template-postgres/test/integration.test.ts) run real queries through pg. They need a database:

DATABASE_URL=postgres://postgres:postgres@localhost:5432/postgres ./run

Without DATABASE_URL they are skipped locally (with a warning) but fail when CI=true; the GitHub Actions test job provides a postgres:18 service.

Releasing

Every push to triptease/sql-template that passes the test job runs the publish job in .github/workflows/build.yml, which runs scripts/release.ts:

  • Version: 0.<git rev-list --count HEAD>.<GITHUB_RUN_NUMBER>; dist-tag latest on master, dev on other branches.
  • It does a clean tsc --build (without declaration maps, as the .ts sources are not published), then writes <pkg>/dist/package.json from <pkg>/src/package.json (version stamped, workspace:* replaced by that version, main/types/exports pointing at the .js/.d.ts files, explicit files) and copies this README next to it. The dist directory is what gets published, so import paths never contain src/ or dist/. It fails if any workspace: range is left.
  • It publishes core first, then postgres, with npm publish <pkg>/dist:
    • to GitHub Packages (npm.pkg.github.com) on every run, using the workflow's GITHUB_TOKEN;
    • to npmjs only when the repo variable NPM_PUBLISH is true, with --access public --provenance, authenticated by npm Trusted Publishing (OIDC).
  • Versions that are already on a registry are skipped, so a failed run can simply be re-run.
  • The staged dist directories are kept as the dist artifact of the workflow run.

Check what would be published without uploading anything (npm publish --dry-run; add NPM_PUBLISH=true to include npmjs):

./run release --dry-run

./run ci runs the full build and tests, then the release script.

One-off setup (needs an npm org admin and a GitHub repo admin)

  1. On npmjs.com, for each of @triptease/sql-template and @triptease/sql-template-postgres: Settings → Trusted Publisher → GitHub Actions, organization triptease, repository sql-template, workflow filename build.yml, environment empty, allowed actions: npm publish only. The workflow filename is part of the trust relationship, so do not rename build.yml. Publishing access is set to "require two-factor authentication and disallow tokens", so only this workflow can publish.
  2. In the GitHub repo, set the Actions variable NPM_PUBLISH to true (Settings → Secrets and variables → Actions → Variables). Until then npmjs publishing is skipped and only GitHub Packages is published.
  3. GitHub Packages: after the first publish, check both packages under the org's Packages page, make sure they are linked to this repository and set their visibility to public if they should be visible outside the org.

Installing from GitHub Packages

npmjs is the main channel. Installing from GitHub Packages needs authentication even for public packages: a token with read:packages and an .npmrc like

@triptease:registry=https://npm.pkg.github.com
//npm.pkg.github.com/:_authToken=${GITHUB_TOKEN}