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

tson-psql

v1.0.0

Published

Advanced TSON to PostgreSQL Query Generator & Type Caster. Converts TSON AI responses into parameterized PostgreSQL INSERT, ON CONFLICT Upsert, UPDATE, and WHERE queries with native JSONB, UUID, and TIMESTAMPTZ support.

Readme

tson-psql 🚀

npm version license bundle size TypeScript

Advanced TSON to PostgreSQL Query Generator & Type Caster.

Convert TSON (Token-Structured Object Notation) responses from LLMs like Gemini, OpenAI GPT-4o, Claude, and Llama directly into Parameterized PostgreSQL Queries (INSERT, ON CONFLICT DO UPDATE, UPDATE, WHERE) with RETURNING * and native PostgreSQL type safety.

Cuts AI output generation costs & latency by 30% to 50%!


💡 Why tson-psql?

When requesting database writes or PostgreSQL queries from an LLM:

  • High Output Costs: LLM output tokens cost 3x to 4x more than prompt input tokens.
  • Slow Output Speed: Generating full JSON arrays or raw SQL strings with repeating column names doubles generation latency.
  • SQL Injection Risk: Direct raw SQL string generation from LLMs can expose your database to injection vulnerabilities.

tson-psql instructs your AI model to reply in compact TSON format, and safely generates parameterized PostgreSQL queries ($1, $2) with native support for RETURNING * and ON CONFLICT upserts.


📊 Token Savings & Speed Benchmark

| LLM Output Target | Standard JSON Output | TSON Output | Output Tokens Saved | Speedup | | :--- | :--- | :--- | :--- | :--- | | Extract 10 Rows | ~1,200 tokens | ~610 tokens | 590 tokens | ~2.0x Faster ⚡ | | Extract 50 Rows | ~5,800 tokens | ~2,350 tokens | 3,450 tokens | ~2.2x Faster ⚡ | | PostgreSQL Upsert | ~300 tokens | ~140 tokens | 160 tokens | ~2.1x Faster ⚡ |


📦 Installation

npm install tson-psql
# or
yarn add tson-psql
# or
pnpm add tson-psql

⚡ Quick Start

1. Generate Parameterized PostgreSQL INSERT Query

import { tsonToPsqlInsert } from 'tson-psql';
import { Pool } from 'pg';

const pool = new Pool();

// TSON response received from LLM
const llmTsonOutput = `
first_name: Abhi, Sarah
last_name: Asok, Chen
email: [email protected], [email protected]
role: Architect, AI Engineer
`;

// Generate Parameterized PostgreSQL Query
const query = tsonToPsqlInsert(llmTsonOutput, {
  tableName: 'users',
  returning: true
});

console.log(query.text);
// Output: INSERT INTO "users" ("first_name", "last_name", "email", "role") VALUES ($1, $2, $3, $4), ($5, $6, $7, $8) RETURNING *;

console.log(query.values);
// Output: ['Abhi', 'Asok', '[email protected]', 'Architect', 'Sarah', 'Chen', '[email protected]', 'AI Engineer']

// Execute safely with pg pool!
const result = await pool.query(query.text, query.values);

2. Generate PostgreSQL ON CONFLICT DO UPDATE (Upsert) Query

import { tsonToPsqlUpsert } from 'tson-psql';

const upsertQuery = tsonToPsqlUpsert(llmTsonOutput, {
  tableName: 'users',
  conflictColumns: ['email']
});

console.log(upsertQuery.text);
// INSERT INTO "users" (...) VALUES (...) RETURNING * ON CONFLICT ("email") DO UPDATE SET "first_name" = EXCLUDED."first_name", "last_name" = EXCLUDED."last_name", "role" = EXCLUDED."role";

3. Generate Parameterized PostgreSQL UPDATE Query

import { tsonToPsqlUpdate } from 'tson-psql';

const tsonUpdate = `
id: 42
status: completed
updatedAt: 2026-08-27T17:05:00.000Z
`;

const updateQuery = tsonToPsqlUpdate(tsonUpdate, {
  tableName: 'orders',
  whereColumns: ['id']
});

console.log(updateQuery.text);
// Output: UPDATE "orders" SET "status" = $1, "updatedAt" = $2 WHERE "id" = $3;

4. Interactive Terminal Demo

Run the interactive CLI demonstration:

npm run demo

Check cumulative token savings logged automatically in SAVINGS_TRACKER.json:

import { getGlobalSavingsTracker } from 'tson-psql';

const tracker = getGlobalSavingsTracker();
console.log(tracker.getStats());

🛠️ API Reference

| Function | Output | Description | | :--- | :--- | :--- | | tsonToPsql(tsonStr, options?) | Record<string, any>[] | Parses TSON string into JavaScript PostgreSQL row objects. | | tsonToPsqlInsert(tsonStr, options?) | { text, values, columns } | Generates parameterized PostgreSQL INSERT statements ($1, $2) with RETURNING *. | | tsonToPsqlUpsert(tsonStr, options) | { text, values } | Generates PostgreSQL ON CONFLICT DO UPDATE queries. | | tsonToPsqlUpdate(tsonStr, options?) | { text, values } | Generates parameterized PostgreSQL UPDATE statements. | | tsonToPsqlWhere(tsonStr, options?) | { text, values } | Generates PostgreSQL WHERE filter clauses. |


👤 Author & Contact

Developed with ❤️ by Abhi Asok.

For business inquiries, collaboration, or support:


📄 License

MIT © Abhi Asok