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-tson-sdk

v1.0.0

Published

Unified bi-directional SQL <-> TSON (Token-Structured Object Notation) converter & query generator SDK for LLMs & RAG applications. Cuts prompt input tokens and AI output tokens by 50% for PostgreSQL, MySQL, SQLite, MSSQL, Knex, Prisma & Drizzle.

Readme

sql-tson-sdk 🚀

npm version license bundle size TypeScript

Unified Bi-directional SQL $\leftrightarrow$ TSON (Token-Structured Object Notation) Converter & Query Generator SDK for LLMs & RAG Applications.

Cuts 50% of prompt input tokens and 50% of AI output generation tokens when communicating between SQL databases (PostgreSQL, MySQL, SQLite, MSSQL, Knex, Prisma, Drizzle) and AI models like Gemini, OpenAI GPT-4o, Claude, and Llama.


💡 Why sql-tson-sdk?

When building SQL-driven AI Agents or RAG pipelines:

  1. Read Path Overhead: Sending raw SQL query rows formatted with JSON.stringify() duplicates column header strings across every record.
  2. Write Path Cost & Latency: Forcing LLMs to reply in verbose JSON or raw SQL strings inflates output costs by 3x to 4x and doubles generation latency.

sql-tson-sdk provides a single unified SDK to handle both directions:

  • sqlToTson(): Serializes SQL query rows into columnar TSON for prompt inputs.
  • tsonToSqlInsert(): Converts LLM TSON responses into Parameterized SQL INSERT statements ($1, $2 for Postgres, ? for MySQL/SQLite).
  • tsonToSqlUpdate(): Converts LLM TSON responses into Parameterized UPDATE statements.
  • tsonToSqlWhere(): Converts LLM TSON filter responses into SQL WHERE clauses.

📊 Bi-Directional Token Savings Benchmark

| Operation | Standard JSON Format | TSON Format | Token Savings | Speedup | | :--- | :--- | :--- | :--- | :--- | | Read Path (Prompt Context) | ~14,500 tokens | ~6,700 tokens | 7,800 tokens saved | 53.7% Less Input Cost ⚡ | | Write Path (LLM Output) | ~5,800 tokens | ~2,350 tokens | 3,450 tokens saved | ~2.2x Faster Generation ⚡ |


📦 Installation

npm install sql-tson-sdk
# or
yarn add sql-tson-sdk
# or
pnpm add sql-tson-sdk

⚡ Quick Start

1. Read Path: SQL Query Rows $\rightarrow$ TSON (Prompt Context)

import { sqlToTson } from 'sql-tson-sdk';
import { Pool } from 'pg';

const pool = new Pool();
const { rows } = await pool.query('SELECT user_id, first_name, email, role FROM users WHERE status = $1', ['active']);

// Convert to TSON prompt context
const tsonPrompt = sqlToTson(rows, { trackSavings: true });

console.log(tsonPrompt);
/*
user_id: 101, 102
first_name: Abhi, Sarah
email: [email protected], [email protected]
role: Architect, AI Engineer
*/

2. Write Path: LLM TSON Response $\rightarrow$ Parameterized SQL INSERT

import { tsonToSqlInsert } from 'sql-tson-sdk';

const llmTsonOutput = `
first_name: Abhi, Sarah
last_name: Asok, Chen
email: [email protected], [email protected]
role: Architect, AI Engineer
`;

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

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

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

// Execute directly with pg pool!
await pool.query(query.text, query.values);

3. Generate Parameterized UPDATE Query

import { tsonToSqlUpdate } from 'sql-tson-sdk';

const updateTson = `
id: 101
status: completed
updated_at: 2026-08-27T16:55:00.000Z
`;

const updateQuery = tsonToSqlUpdate(updateTson, {
  tableName: 'users',
  whereColumns: ['id'],
  dialect: 'postgres'
});

console.log(updateQuery.text);
// UPDATE "users" SET "status" = $1, "updated_at" = $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 'sql-tson-sdk';

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

🛠️ API Reference

| Function | Direction | Description | | :--- | :--- | :--- | | sqlToTson(sqlRows, options?) | SQL -> TSON | Converts SQL query rows/arrays to TSON prompt strings. | | tsonToSql(tsonStr, options?) | TSON -> SQL Rows | Parses TSON strings back into JS SQL row objects. | | tsonToSqlInsert(tsonStr, options?) | TSON -> INSERT | Generates parameterized SQL INSERT statements ($1, ?). | | tsonToSqlUpdate(tsonStr, options?) | TSON -> UPDATE | Generates parameterized SQL UPDATE statements. | | tsonToSqlWhere(tsonStr, options?) | TSON -> WHERE | Generates parameterized SQL WHERE filter clauses. |


👤 Author & Contact

Developed with ❤️ by Abhi Asok.

For business inquiries, collaboration, or support:


📄 License

MIT © Abhi Asok