Show HN: PgTyped – Typesafe SQL in TypeScript and Postgres

  • There are some similar projects, like sqlx [1] for Rust. My problem with these is that they don't help to solve the actually hard problems.

    While nice to have, preventing bugs with static SQL is usually easy to do by writing a few tests. Most of the SQL related bugs I have encountered were due to queries with dynamic/conditional joins, filters and sorting - and almost every project using a database needs those.

    Approaches like this don't help there. That requires heavy-weight solutions that are more cumbersome to use and need a strong type system, like diesel [2] (Rust), Slick [3] (Scala) and some similar Haskell projects.

    [1] https://github.com/launchbadge/sqlx

    [2] https://github.com/diesel-rs/diesel

    [3] https://scala-slick.org/

  • I really like the unique approach of the annotated SQL files and can definitely see some use cases where it would be good to declutter the SQL from the code. For me personally, I'd be hesitant to add another build tool to my already bloated toolchain. Could create a special Babel-style "import" type that automatically transforms your code (JIT)? It could remove some of the friction in adoption (for Babel users at least).

    Another one in a similar vein with strict typing and really nice SQL interpolation for Postgres: https://github.com/gajus/slonik

  • I came here to mention a similar approach, which last time I looked was a very compelling experiment[1], but its original author has actually built out a real library, Zapatos[2] which looks very very good.

    [1]: https://github.com/jawj/mostly-ormless

    [2]: https://jawj.github.io/zapatos/

  • As a maintainer of a similar project[0], it's great to see another entry in this space.

    sqlc currently has great support for Go and experimental support for Kotlin. I'm planning on adding TypeScript support in the future, so it's great to see that others in the TypeScript community find this workflow useful.

    [0] https://github.com/kyleconroy/sqlc

  • Looks pretty cool. What I really want though is a library that let's me write plain SQL queries which are then mapped into nested objects in a smart way without too much manual work (I know Postgres can do JSON stuff, but the queries look pretty complicated for what little they actually do).

    Say `SELECT * FROM user LEFT JOIN post ON user.id = post.id` would be mapped to `[{userId: 1, name: renke1, posts: [{postId: 2, title: "foo"]]`.

    You probably need some kind of meta data to figure out how tables and thus objects relate to each other though.

    Basically, I want to be able to leverage the full power of modern databases without being constrainted by typical ORM limitations. Also, I don't need features like lazy loading, sessions, caches and things like that.

    A great advantage is that you can (provided you have some test data) easily test your queries while you develop a new feature (think IntelliJ IDEA where you can simply execute an SQL query on the fly).

  • Is there anything like this for Rust or C++?

    I like the idea of code generation instead of doing the work at runtime (like in ORMs). This is like making your database schema the IDL spec.

  • Lots of comments here about similar projects in a different language, but the fact that this targets TypeScript is explicitly what makes it interesting to me. Using regular Javascript database libraries, even ones that have type definitions, require a lot of double typing.

    I've been relatively satisfied with TypeORM, but one thing that's been a hurdle for me to some extent is its reliance on experimental decorators, and the resulting incompatibility with Babel - which in turn makes it harder to integrate with the wider ecosystem, e.g. Next.js.

    As far as I can see on first glance, there's nothing here yet that makes it incompatible with Babel, so my tip would be to make it an explicit goal to keep it that way :)

  • My favorite SQL library has been Go-Jet in Go: https://github.com/go-jet/jet

    It has a different approach from PgTyped, which generates type-safe TypeScript code from SQL, whereas Go-Jet generates type-safe SQL from Go code

    I'd love to try something along the lines of PgTyped and see how the two solutions compare though

  • Almost every top-level comment is someone shilling another project, usually in Golang as if that's even related.

    Let's have some Show HN etiquette.

  • This is similar to sqlc for Golang: https://github.com/kyleconroy/sqlc

    If you're looking for the ability to generate type-safe SQL – given you write SQL correctly – this project is pretty good.

    Aalso a fan of SQLBoiler (https://github.com/volatiletech/sqlboiler) for Golang, for simple type safety:

    `models.Accounts(models.AccountWhere.ID.EQ(id)).One(ctx, db)`.

    Though SQLBoiler breaks with left joins, as it auto-generates your structs and maps results 1-1 with table definitions. In this case you have to custom type something, either using sqlc or squirrel.

  • Looks cool! And the header image looks awesome! Did you use any tool to do it?

  • You might also want to check Kanel out! https://github.com/kristiandupont/kanel

  • Looks a little like the Typescript equivalent of Xo (https://github.com/xo/xo) for Go. Especially with Go, getting help with some initial scaffolding can be a huge timesaver. I'm assuming it's a similar gain for Typescript.

  • So basically Dapper TS? I’m very interested!

  • I sort of have something like this for PHP and MySQL. https://github.com/ellisgl/GeekLab-GLPDO2

  • How is it different from Slonik? https://github.com/gajus/slonik

  • It’s cool, and I don’t fault the author for working on something that obviously gives him joy, but save yourself a bunch of trouble and avoid this kind of thing. The queries showcased are the least interesting of the set of queries you’ll ultimately end up with I’m a mature project.

  • an other lib with light ORM: https://www.npmjs.com/package/pogi

  • lol. This reminds me Hibernate xml mappings :D