Registry / database / sql-select-ts

sql-select-ts

JSON →
library2.0.18jsnpmunverified

sql-select-ts is a modern, database-agnostic SELECT query builder for JavaScript and TypeScript, currently stable at version 2.0.18. It distinguishes itself through its composable API and strong static type checking, making it an excellent choice for projects prioritizing type safety and maintainable SQL generation. The package exhibits a frequent release cadence, with numerous minor updates and bug fixes being published regularly, indicating active development and responsiveness to community feedback. Its core value proposition lies in enabling developers to construct complex SELECT queries programmatically without sacrificing type safety or database portability, moving beyond raw string concatenation or less type-safe alternatives. It focuses specifically on SELECT statements, offering a streamlined API for this common database operation, and is suitable for both Node.js and browser environments.

npm install sql-select-ts
INSTALL
IMPORT
SIG · SQL-SELECT-TS
S
sql-select-ts
databasejavascriptv2.0.18
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

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

select
import { select } from 'sql-select-ts'
import select from 'sql-select-ts'
The primary function to start building a SELECT query; it is a named export.
column
import { column } from 'sql-select-ts'
const column = require('sql-select-ts').column
Used to reference database columns with type safety. This library primarily uses ES Module exports.
table
import { table } from 'sql-select-ts'
Used to define and type-associate database tables within the query builder.
raw
import { raw } from 'sql-select-ts'
For injecting raw SQL strings, to be used cautiously for security reasons.

Demonstrates building a multi-table SELECT query with aliased columns, aggregates, joins, complex WHERE clauses, grouping, ordering, and pagination, then converts it to a raw SQL string.

import { select, column, table, raw } from 'sql-select-ts'; interface User { id: number; name: string; email: string; createdAt: Date; } interface Post { id: number; userId: number; title: string; } const users = table<User>('users'); const posts = table<Post>('posts'); // Build a complex SELECT query with joins, conditions, grouping, and ordering const query = select( column(users.id), column(users.name).as('userName'), column(users.email), raw('COUNT(posts.id)').as('postCount') ) .from(users) .leftJoin(posts) .on(column(users.id).eq(column(posts.userId))) .where(column(users.createdAt).gt(raw("NOW() - INTERVAL '1 year'"))) .groupBy(column(users.id), column(users.name), column(users.email)) .orderBy(column(users.name).asc()) .limit(10) .offset(0); const queryString = query.toSQL(); console.log(queryString); /* Expected output: SELECT users.id, users.name AS userName, users.email, COUNT(posts.id) AS postCount FROM users LEFT JOIN posts ON users.id = posts.userId WHERE users.createdAt > NOW() - INTERVAL '1 year' GROUP BY users.id, users.name, users.email ORDER BY users.name ASC LIMIT 10 OFFSET 0 */
Debug
Known issues
gotchaUsing the `raw()` function directly allows for arbitrary SQL injection if user-supplied input is not properly sanitized, bypassing the type safety and protective mechanisms of the query builder.
fix
Always ensure that any data passed into `raw()` functions is either hardcoded or strictly sanitized/parameterized if derived from user input. Prefer using the type-safe builder methods where possible.
affects: >=2.0.0
gotchaWhile `sql-select-ts` is database-agnostic, it generates standard SQL. Specific database dialects (e.g., PostgreSQL, MySQL, SQL Server) may have unique functions or syntax not directly supported by the builder's fluent API. These will require the use of `raw()` expressions.
fix
Consult your specific database's documentation for non-standard features. Employ `raw()` for such constructs, carefully testing the generated SQL against your target database.
affects: >=2.0.0
gotchaWhen using `.as()` for column aliases, especially in conjunction with `raw()` expressions, the inferred TypeScript type of the resulting query's output may become less specific or require explicit type assertions downstream.
fix
For complex `raw()` expressions with aliases, consider defining explicit interfaces or types for the result set, or use type assertions (`as MyResultType`) where the type inference might fall short to maintain strict type safety.
affects: >=2.0.0
Errors
Common errors & fixes
TS2339: Property 'nonExistentColumn' does not exist on type 'Table<User>'
Attempting to reference a column name that is not defined in the TypeScript interface associated with the table.
fix
Ensure the column name exactly matches a property in the interface provided to `table<T>`, or add the missing property to the interface.
SyntaxError: require is not defined in ES module scope
Attempting to use CommonJS `require()` syntax to import modules from `sql-select-ts` in an ES Module environment (or when Node.js is configured for ESM).
fix
Use ES Module `import` syntax: `import { select, column } from 'sql-select-ts';`
TypeError: select is not a function
Incorrectly attempting a default import of `select` or a CommonJS `require` of a named export.
fix
Ensure `select` is imported as a named export: `import { select } from 'sql-select-ts';` (not `import select from 'sql-select-ts'`).
Upgrade
Version history
2.0.18latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
19 hits · last 30 days
node
16
Meta
1
OpenAI (training)
1
Resources
sql-select-ts — npm install sql-select-ts · libregistry