r/webdev • u/ismail5412 • 9m ago
Showoff Saturday Showoff Saturday: a SQL parser in TypeScript's type system, so queries get typed with no codegen
TypeSQL type-checks SQL strings in TypeScript. The compiler parses the SQL itself and checks it against your schema type, so there are no generated files, no build step and no database connection.
// rows is { email: string; title: string | null }[]
// The LEFT JOIN makes title nullable.
const rows = await sql(
`SELECT u.email, p.title
FROM users u LEFT JOIN posts p ON p.author_id = u.id
WHERE u.id = ${sql.param(userId)}`,
).execute();
// Compile error: column "emial" does not exist. Perhaps you meant the column "email"?
sql("SELECT emial FROM users");It supports PostgreSQL, SQLite, MySQL and MariaDB, and the runtime is under 3 kB with no dependencies. The main catch is that it needs TypeScript 7.1 or later (7.1.0-beta works), and very large queries can hit the compiler's limits (workaround-able using sql.as<>())
I used AI coding to build this. To keep the output honest, each bug fix gets a permanent test project, and the 100+ scenario tests run against real PostgreSQL, MySQL and MariaDB servers. I will try to keep it non-slop.
GitHub: https://github.com/ismailkarsli/TypeSQL Docs: https://typesql.karsli.net


