tson-sql
v1.0.0
Published
Advanced TSON to SQL Query Generator & ORM Payload Parser. Converts TSON responses from LLMs directly into parameterized INSERT statements, UPDATE queries, WHERE filters, and ORM objects (Knex, Prisma, Drizzle).
Maintainers
Readme
tson-sql 🚀
Advanced TSON to SQL Query Generator & ORM Payload Parser.
Convert TSON (Token-Structured Object Notation) responses from LLMs like Gemini, OpenAI GPT-4o, Claude, and Llama directly into Parameterized SQL Queries (INSERT, UPDATE, WHERE) and ORM payloads (Knex, Prisma, Drizzle).
Cut AI output generation costs & latency by 30% to 50%!
💡 Why tson-sql?
When forcing LLMs to output database payloads or SQL inserts:
- ❌ Expensive Output Tokens: LLM output tokens cost 3x to 4x more than input prompt 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 output from LLMs can expose your database to injection vulnerabilities.
tson-sql instructs your AI model to reply in compact TSON format, and safely generates parameterized SQL queries ($1, $2, ?) for PostgreSQL, MySQL, SQLite, and MSSQL.
📊 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 ⚡ | | SQL Update Payload | ~250 tokens | ~120 tokens | 130 tokens | ~1.9x Faster ⚡ |
📦 Installation
npm install tson-sql
# or
yarn add tson-sql
# or
pnpm add tson-sql⚡ Quick Start
1. Generate Parameterized PostgreSQL INSERT Query
import { tsonToSqlInsert } from 'tson-sql';
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 Query
const query = tsonToSqlInsert(llmTsonOutput, {
tableName: 'users',
dialect: 'postgres',
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 Parameterized MySQL INSERT Query
import { tsonToSqlInsert } from 'tson-sql';
const query = tsonToSqlInsert(llmTsonOutput, {
tableName: 'users',
dialect: 'mysql'
});
console.log(query.text);
// Output: INSERT INTO `users` (`first_name`, `last_name`, `email`, `role`) VALUES (?, ?, ?, ?), (?, ?, ?, ?);3. Generate Parameterized UPDATE Query
import { tsonToSqlUpdate } from 'tson-sql';
const tsonUpdate = `
id: 42
status: completed
paidAt: 2026-08-27T16:50:00.000Z
`;
const updateQuery = tsonToSqlUpdate(tsonUpdate, {
tableName: 'orders',
whereColumns: ['id'],
dialect: 'postgres'
});
console.log(updateQuery.text);
// Output: UPDATE "orders" SET "status" = $1, "paidAt" = $2 WHERE "id" = $3;4. Generate SQL WHERE Clauses from LLMs
import { tsonToSqlWhere } from 'tson-sql';
const tsonWhere = `
status: active, pending
role: Architect
`;
const where = tsonToSqlWhere(tsonWhere, { dialect: 'postgres' });
console.log(where.text);
// Output: WHERE "status" IN ($1, $2) AND "role" = $35. Interactive Terminal Demo
Run the interactive CLI demonstration:
npm run demo🛠️ API Reference
| Function | Output | Description |
| :--- | :--- | :--- |
| tsonToSql(tsonStr, options?) | Record<string, any>[] | Parses TSON string into JavaScript SQL row objects. |
| tsonToSqlInsert(tsonStr, options?) | { text, values, columns } | Generates parameterized SQL INSERT statements ($1, ?, @p1). |
| tsonToSqlUpdate(tsonStr, options?) | { text, values } | Generates parameterized SQL UPDATE statements. |
| tsonToSqlWhere(tsonStr, options?) | { text, values } | Generates parameterized SQL WHERE filter clauses. |
👤 Author & Contact
Developed with ❤️ by Abhi Asok.
For business inquiries, collaboration, or support:
- 📧 Email: [email protected]
- 📞 Phone / WhatsApp: +91 9142125724
- 💼 LinkedIn: linkedin.com/in/abhi-asok-09439788
- 🐙 GitHub: github.com/AbhiArvension
📄 License
MIT © Abhi Asok
