SQLTyped
SQLTyped is a simple, typesafe SQL DSL implementation in TypeScript that aims to provide a natural SQL interface for TypeScript applications. Goals of SQLTyped:
- No magic. SQLTyped is not an ORM, you need to know SQL to use SQLTyped.
- No performance surprises. The DSL looks like SQL and compiles down to predictable queries.
- Best-in-class type safety. The DSL should avoid as many invalid queries as possible.
- As few dependencies as possible. As of now, SQLTyped only depends on the
pg
module for querying PostgreSQL.
npm install sql-typed
SQLTyped is in active development and the API is not guaranteed to be stable.
Basic Example
; // Type safe columns are created from the table definition.; ; pool.transaction;
Developing
To run tests:
docker run --rm -d -p 5432:5432 postgres
npm test
Type Safety
There are some cases where TypeScript's type system cannot express a needed type (or I haven't figured out how). Known cases:
- Joins with 3 or more tables. While joining two tables preserves type information, querying across 3 or more does
not. Example:.select/*** This is typesafe. The args to the predication function are table-specific* and the return type of the query is table-specific.*/.joinuserTable,a.userId.eqlsu.id, JoinType.Left/*** Adding a 3rd table drops down to very wide vague types. This could be* solved if TypesScript supported variadic types parameters.*/.joincommentTable,a.id.eqlsc.articleId,JoinType.Left.executetransaction;