This is just a little experiment in building a type-safe SQL library in TypeScript. It's somewhat inspired by Slick for Scala.
- Create an expressive, type-safe, SQL-like syntax.
- Require as little boilerplate as possible.
let q2 = Query
.find(User, Avatar)
.select((u, a) => [ u.firstName, a.url ])
.where((u, a) => a.userId.eq(u.id))
console.log(q2.toSql())
// SELECT u.firstName, a.url FROM User AS u WHERE a.userId = u.id
The basic proof of concept is here but there are a few things I don't like about it:
- Building the tables as classes requires a lot of boilerplate code. This was also true of Slick.
- The reliance of TypeScript's mapped types to build the column expressions makes the syntax nice and expressive but adds a lot of complexity.
- Generalizing between a database and a subquery is difficult because of the use of mapped types.
- A stronger type system in TypeScript would probably make this much easier. Variadic types could help substantially but that feature is not being actively developed yet. Even a way to build strongly typed tuples in some kind of dynamic way would be very helpful.
- Could we build the tables via a command line tool?
- Use
tsc -w
from the root directory. TypeScript code will be compiled to./build
- Run with
node build/test.js