Skip to content
logo

ts-sql-query

Type-safe SQL query builder for TypeScript

What is ts-sql-query?

ts-sql-query is a type-safe SQL query builder for TypeScript. It allows you to write dynamic queries with full type safety โ€” meaning that the TypeScript compiler can validate your queries at compile time.

Type-safe SQL means that mistakes in your queries are caught at compile time. With ts-sql-query, you can safely refactor your database schema: any issues will be surfaced immediately by the compiler.

ts-sql-query is not an ORM โ€” it focuses on full SQL control with type safety.

Why?

There are many libraries available in JavaScript/TypeScript that allows querying a SQL database, but they are typically:

  • ORMs often limit direct control over SQL and can obscure database capabilities.
  • Many query builders rely on fragile string concatenation.
  • Some libraries lack proper type-safety integration.
  • Most tools make it hard to write truly dynamic SQL in a safe and expressive way.

ts-sql-query addresses these issues by providing a type-safe, SQL-centric API, with rich support for building dynamic queries in a declarative style.

For more details on the principles behind the library, see Philosophy and Design Goals.

Supported Databases

ts-sql-query uses a unified dialect inspired by SQLite and PostgreSQL, with naming conventions adapted to JavaScript. It automatically generates the correct SQL for your target database.

Install

Install with npm:

$ npm install --save ts-sql-query

ts-sql-query does not expose a global export. Instead, you should import specific modules as described in the documentation, depending on the features you need. Only the modules listed below are considered part of the public API โ€” avoid importing from any other internal paths, as they may change without prior notice:

  • ts-sql-query/Connection
  • ts-sql-query/Table
  • ts-sql-query/TypeAdapter
  • ts-sql-query/View
  • ts-sql-query/connections/*
  • ts-sql-query/extras/*
  • ts-sql-query/queryRunners/*
  • ts-sql-query/dynamicCondition

Any reference to a file outside of the previous list can change at any moment.

  • ts-sql-codegen: Utility that generates table mapper classes for ts-sql-query by inspecting a database through tbls.

License

MIT