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

mongo-tson-sdk

v1.0.0

Published

Unified bi-directional MongoDB <-> TSON (Token-Structured Object Notation) converter & parser for LLMs & RAG applications. Reduces prompt input tokens and AI output tokens by 50%.

Readme

mongo-tson-sdk 🚀

npm version license bundle size TypeScript

Unified Bi-directional MongoDB $\leftrightarrow$ TSON (Token-Structured Object Notation) Converter & Parser SDK for LLMs & RAG Pipelines.

Cuts 50% of prompt input tokens and 50% of AI output generation tokens when communicating between MongoDB and AI models like Gemini, OpenAI GPT-4o, Claude, and Llama.


💡 Why mongo-tson-sdk?

When building AI Agents or RAG pipelines with MongoDB:

  1. Read Path Bloat: Sending raw JSON.stringify() MongoDB outputs into prompts duplicates key names across every record.
  2. Write Path Latency & Cost: Forcing LLMs to reply in verbose JSON increases output costs by 3x to 4x and doubles generation latency.

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

  • mongoToTson(): Serializes MongoDB records into columnar TSON for prompt inputs.
  • tsonToMongo(): Parses LLM TSON responses into MongoDB write payloads, filters, and updates.

📊 Bi-Directional Token Savings Benchmark

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


📦 Installation

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

⚡ Quick Start

1. Read Path: MongoDB $\rightarrow$ TSON (Prompt Context)

import { mongoToTson } from 'mongo-tson-sdk';

// Fetch MongoDB query results
const users = await User.find({ active: true }).lean();

// Convert to compact TSON prompt context
const tsonPrompt = mongoToTson(users, { trackSavings: true });

console.log(tsonPrompt);
/*
_id: 64f123456789, 64f12345678a
name: Abhi Asok, Sarah Chen
email: [email protected], [email protected]
role: Architect, AI Engineer
*/

2. Write Path: LLM TSON Response $\rightarrow$ MongoDB Writes

import { tsonToMongo } from 'mongo-tson-sdk';

// Prompt LLM to output in TSON
const prompt = "Extract user records and reply strictly in TSON format with headers: name, email, role, status.";
const response = await model.generateContent(prompt);

// Convert AI TSON output directly into MongoDB documents
const mongoDocs = tsonToMongo(response.response.text(), {
  restoreDates: true,
  trackSavings: true
});

// Insert straight to MongoDB!
await db.collection('users').insertMany(mongoDocs);

3. Generate Query Filters & Update Pipelines

import { tsonToMongoFilter, tsonToMongoUpdate } from 'mongo-tson-sdk';

// Generate Query Filter
const filterTson = "status: active, pending\nrole: Architect";
const filter = tsonToMongoFilter(filterTson);
// Output: { status: { $in: ['active', 'pending'] }, role: 'Architect' }

// Generate $set Update Payload
const updateTson = "status: completed\npaidAt: 2026-08-27T15:45:00.000Z";
const update = tsonToMongoUpdate(updateTson);
// Output: { $set: { status: 'completed', paidAt: Date(...) } }

4. Interactive Terminal Demo & Token Tracker

Run the terminal demonstration to verify both directions:

npm run demo

Check cumulative token savings logged automatically in SAVINGS_TRACKER.json:

import { getGlobalSavingsTracker } from 'mongo-tson-sdk';

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

🛠️ API Reference

| Function | Direction | Description | | :--- | :--- | :--- | | mongoToTson(data, options?) | MongoDB -> TSON | Converts documents/arrays/cursors to TSON prompt strings. | | tsonToMongo(tsonStr, options?) | TSON -> MongoDB | Parses TSON strings back into MongoDB objects / document arrays. | | tsonToMongoFilter(tsonStr, options?) | TSON -> Filter | Converts TSON strings into MongoDB $in filter query objects. | | tsonToMongoUpdate(tsonStr, options?) | TSON -> Update | Converts TSON strings into MongoDB $set update pipeline objects. | | streamMongoToTson(cursor, options?) | Stream -> TSON | Transforms MongoDB node streams / AsyncIterables into TSON chunks. |


👤 Author & Contact

Developed with ❤️ by Abhi Asok.

For business inquiries, collaboration, or support:


📄 License

MIT © Abhi Asok