Registry / database / ts-sql-lang

ts-sql-lang

JSON →
library1.0.18jsnpmunverified

ts-sql-lang (v1.0.18) is a TypeScript SQL query builder that compiles check at compile time, preventing SQL syntax errors, wrong column names, missing tables, and duplicate aliases. It generates raw SQL strings and works with any MySQL/MariaDB driver (mysql, mysql2, etc.). Unlike runtime query builders, it relies heavily on TypeScript's type system for validation. Release cadence is low; no recent updates. Key differentiator: compile-time errors via TypeScript generics, not runtime checks.

npm install ts-sql-lang
INSTALL
IMPORT
SIG · TS-SQL-LANG
T
ts-sql-lang
databasejavascriptv1.0.18
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

SQL
import { SQL } from 'ts-sql-lang'
const SQL = require('ts-sql-lang')
Default export not available; SQL is a named export.
tUser
import { tUser } from './tables' // where tUser is defined using library's table helpers
import { tUser } from 'ts-sql-lang'
Table objects must be defined using the library's helpers (e.g., createTable), not imported from the package.
SqlQuery
import { SqlQuery } from 'ts-sql-lang'
import { SqlQuery } from 'ts-sql-lang/types'
SqlQuery is exported from the main package, not a separate types module.

Basic SELECT query with COUNT, WHERE, GROUP BY, ORDER BY, and LIMIT using typed SQL builder.

import { SQL } from 'ts-sql-lang'; // Define a table (normally in a separate file) import { createTable, int, varchar } from 'ts-sql-lang'; const tUser = createTable('users', { id: int().primaryKey(), firstName: varchar(255), created: int(), isMan: int(), }); // Build a query const query = SQL .selectFrom(tUser) .columns( tUser.id, tUser.firstName, (SQL as any).COUNT(tUser.firstName).as('count') ) .where(tUser.isMan.eq(1)) .groupBy(tUser.firstName) .orderBy(tUser.firstName) .limit(20); // Execute (using your own execute function) function execute<T>(query: any): Promise<T[]> { return yourDb.query(query.toSqlString()); } const rows = await execute<{ id: number; firstName: string; count: number }>(query);
Debug
Known issues
gotchaThe library only builds SQL strings; it does NOT execute queries. You must pass the generated SQL to your own database driver.
fix
Call query.toSqlString() and pass the string to mysql/mysql2 or any other library.
affects: >=0.0.0
gotchaTables must be explicitly added to queries via .selectFrom(), .from(), .join(), etc. Using a column from a table not in the query causes a compile-time error.
fix
Always include the table in the FROM/JOIN clause before referencing its columns.
affects: >=0.0.0
gotchaDuplicate column names in SELECT cause a compile-time error. Use .as() to alias columns uniquely.
fix
Ensure all selected columns have unique names, or use .as() for duplicates.
affects: >=0.0.0
gotchaThe library uses complex TypeScript generics; error messages can be long and cryptic, especially for nested queries.
fix
Read the first line of the error carefully; it often points to the root cause. Simplify queries incrementally.
affects: >=0.0.0
Errors
Common errors & fixes
TypeError: Cannot read properties of undefined (reading 'toSqlString')
Forgetting to call .toSqlString() on a query before passing to a db driver.
fix
Call query.toSqlString() to convert to string.
Error: Table 'users' not added to query.
Attempting to use a column from a table that isn't included via FROM/JOIN.
fix
Add the table using .selectFrom(tUser) or .join(tUser) before using its columns.
Error: Duplicate alias 'count'
Two columns in SELECT have the same alias (including implicit ones from column names).
fix
Rename one column using .as('uniqueAlias').
Upgrade
Version history
1.0.18latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
20 hits · last 30 days
node
16
OpenAI (training)
2
Meta
1
Amazon
1
Resources
ts-sql-lang — npm install ts-sql-lang · libregistry