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

v1.0.0

Published

High-performance SQL to TSON (Token-Structured Object Notation) converter & parser for LLMs. Reduces prompt input tokens and output generation tokens by 50% for PostgreSQL, MySQL, SQLite, Knex, Prisma & Drizzle.

Readme

sql-tson 🚀

npm version license bundle size TypeScript

High-Performance SQL to Token-Structured Object Notation (TSON) Converter & Parser for LLMs.

Reduce your LLM token consumption by 30% to 60% when feeding SQL query results (PostgreSQL, MySQL, SQLite, Knex, Prisma, Drizzle) into AI models like Gemini, OpenAI GPT-4o, Claude, and Llama.


💡 Why sql-tson?

When querying relational SQL databases for Retrieval-Augmented Generation (RAG) or AI Agents:

  • Repeated Column Headers: Standard JSON duplicates column names ("user_id", "email", "created_at") on EVERY single row.
  • SQL Type Overhead: BigInts, Decimals, Timestamptz, and UUIDs introduce extra formatting noise.
  • High Costs: Wasting 50% of your context window on column key names inflates LLM API bills.

sql-tson eliminates key repetition by formatting SQL row arrays into TSON columnar layout, cutting input prompt tokens by half!


📊 Token Savings Benchmark

| SQL Query Output | Standard JSON Tokens | TSON Format Tokens | Token Savings | Savings % | | :--- | :--- | :--- | :--- | :--- | | 10 SQL Rows (10 cols) | ~1,450 tokens | ~670 tokens | 780 tokens | 53.7% ⚡ | | 50 SQL Rows (10 cols) | ~7,200 tokens | ~2,950 tokens | 4,250 tokens | 59.0% ⚡ | | 200 SQL Rows (10 cols) | ~28,500 tokens | ~11,200 tokens | 17,300 tokens | 60.7% ⚡ |


📦 Installation

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

⚡ Quick Start

1. SQL Query Results $\rightarrow$ TSON (Prompt Context)

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

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

// Convert to TSON (and log to SAVINGS_TRACKER.json)
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
created_at: 2026-08-27T15:45:00.000Z, 2026-08-27T15:46:12.000Z
*/

2. TSON AI Response $\rightarrow$ SQL Row Objects

import { tsonToSql } from 'sql-tson';

const tsonLlmOutput = `
first_name: Elena, David
last_name: Rostova, Kim
email: [email protected], [email protected]
role: Security Lead, DevOps Lead
`;

const rows = tsonToSql(tsonLlmOutput);
console.log(rows);
// [ { first_name: 'Elena', last_name: 'Rostova', ... }, { first_name: 'David', ... } ]

3. Generate Parameterized SQL INSERT Queries

import { tsonToSqlInsert } from 'sql-tson';

const tsonLlmOutput = `
first_name: Elena, David
email: [email protected], [email protected]
`;

// Generates parameterized query for PostgreSQL, MySQL, or SQLite
const query = tsonToSqlInsert(tsonLlmOutput, {
  tableName: 'users',
  dialect: 'postgres'
});

console.log(query.text);
// INSERT INTO "users" ("first_name", "email") VALUES ($1, $2), ($3, $4);

console.log(query.values);
// ['Elena', '[email protected]', 'David', '[email protected]']

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

4. Live Terminal Test & Demo

Run the interactive CLI test suite:

npm run demo

🛠️ API Reference

sqlToTson(sqlRows, options?)

Converts SQL query results into TSON.

Options (SqlTsonOptions)

| Option | Type | Default | Description | | :--- | :--- | :--- | :--- | | style | 'tabular' \| 'compact' | 'tabular' | 'tabular' for indented headers; 'compact' for pipe-delimited single lines. | | omitColumns | string[] | [] | Columns to exclude (e.g. ['password_hash']). | | includeColumns | string[] | undefined | Exclusive columns to include. | | formatDates | 'iso' \| 'timestamp' \| 'raw' | 'iso' | Date/Timestamptz formatting style. | | bigIntFormat | 'number' \| 'string' | 'number' | Casts BigInt/Decimal values. | | trackSavings | boolean | false | Automatically updates SAVINGS_TRACKER.json. |


tsonToSqlInsert(tsonString, options?)

Generates a parameterized SQL INSERT statement ($1, $2 for Postgres, ? for MySQL/SQLite).


👤 Author & Contact

Developed with ❤️ by Abhi Asok.

For business inquiries, collaboration, or support:


📄 License

MIT © Abhi Asok