{"id":16538,"library":"sql-select-ts","title":"SQL Select TS Query Builder","description":"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.","status":"active","version":"2.0.18","language":"javascript","source_language":"en","source_url":"https://github.com/lucasavila00/sql-select-ts","tags":["javascript","typescript"],"install":[{"cmd":"npm install sql-select-ts","lang":"bash","label":"npm"},{"cmd":"yarn add sql-select-ts","lang":"bash","label":"yarn"},{"cmd":"pnpm add sql-select-ts","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"The primary function to start building a SELECT query; it is a named export.","wrong":"import select from 'sql-select-ts'","symbol":"select","correct":"import { select } from 'sql-select-ts'"},{"note":"Used to reference database columns with type safety. This library primarily uses ES Module exports.","wrong":"const column = require('sql-select-ts').column","symbol":"column","correct":"import { column } from 'sql-select-ts'"},{"note":"Used to define and type-associate database tables within the query builder.","symbol":"table","correct":"import { table } from 'sql-select-ts'"},{"note":"For injecting raw SQL strings, to be used cautiously for security reasons.","symbol":"raw","correct":"import { raw } from 'sql-select-ts'"}],"quickstart":{"code":"import { select, column, table, raw } from 'sql-select-ts';\n\ninterface User {\n  id: number;\n  name: string;\n  email: string;\n  createdAt: Date;\n}\n\ninterface Post {\n  id: number;\n  userId: number;\n  title: string;\n}\n\nconst users = table<User>('users');\nconst posts = table<Post>('posts');\n\n// Build a complex SELECT query with joins, conditions, grouping, and ordering\nconst query = select(\n  column(users.id),\n  column(users.name).as('userName'),\n  column(users.email),\n  raw('COUNT(posts.id)').as('postCount')\n)\n  .from(users)\n  .leftJoin(posts)\n  .on(column(users.id).eq(column(posts.userId)))\n  .where(column(users.createdAt).gt(raw(\"NOW() - INTERVAL '1 year'\")))\n  .groupBy(column(users.id), column(users.name), column(users.email))\n  .orderBy(column(users.name).asc())\n  .limit(10)\n  .offset(0);\n\nconst queryString = query.toSQL();\nconsole.log(queryString);\n/*\nExpected output:\nSELECT users.id, users.name AS userName, users.email, COUNT(posts.id) AS postCount\nFROM users\nLEFT JOIN posts ON users.id = posts.userId\nWHERE users.createdAt > NOW() - INTERVAL '1 year'\nGROUP BY users.id, users.name, users.email\nORDER BY users.name ASC\nLIMIT 10 OFFSET 0\n*/","lang":"typescript","description":"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."},"warnings":[{"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.","message":"Using 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.","severity":"gotcha","affected_versions":">=2.0.0"},{"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.","message":"While `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.","severity":"gotcha","affected_versions":">=2.0.0"},{"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.","message":"When 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.","severity":"gotcha","affected_versions":">=2.0.0"}],"env_vars":null,"last_verified":"2026-04-22T00:00:00.000Z","next_check":"2026-07-21T00:00:00.000Z","problems":[{"fix":"Ensure the column name exactly matches a property in the interface provided to `table<T>`, or add the missing property to the interface.","cause":"Attempting to reference a column name that is not defined in the TypeScript interface associated with the table.","error":"TS2339: Property 'nonExistentColumn' does not exist on type 'Table<User>'"},{"fix":"Use ES Module `import` syntax: `import { select, column } from 'sql-select-ts';`","cause":"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).","error":"SyntaxError: require is not defined in ES module scope"},{"fix":"Ensure `select` is imported as a named export: `import { select } from 'sql-select-ts';` (not `import select from 'sql-select-ts'`).","cause":"Incorrectly attempting a default import of `select` or a CommonJS `require` of a named export.","error":"TypeError: select is not a function"}],"ecosystem":"npm"}