Show HN: Bun-sqlgen – Type-safe raw SQL for Bun, no ORM

(github.com)

37 points | by ilbert 3 hours ago ago

17 comments

  • giovannibonetti 33 minutes ago

    Those looking for a more mature solution in this space will probably enjoy SQLc [1]. It was initially developed for Go applications, but over the years it got pluggins for many other languages, including JavaScript/Typescript.

    [1] https://sqlc.dev/

  • genshii 35 minutes ago

    This is cool, but when the very first paragraph of the readme is clearly LLM generated, it makes me doubt the quality of the project.

    • lacy_tinpot 31 minutes ago

      LLM generated docs/readmes are at this point old news.

  • allthetime 15 minutes ago

    Kysely rules

  • rankdiff an hour ago

    sqlc is worth a mention.

    https://sqlc.dev

  • psc007 2 hours ago

    Can you make it work/ does it work with Porsager-Postgres in modnes which buns Postgres client is «based on»?

    • ilbert 2 hours ago

      I think with some tweaks to the TS parser that goes and looks for the sql statements it's doable. How are you solving the problem right now?

  • sHooKDT 2 hours ago

    Nice project, thanks! I was looking for something like that for quite a while.

    Any chance to get it to work with Node?

    Unfortunately in my opinion and experience Bun is not really suitable for production. Does it have anything special which makes this possible?

    • tomComb an hour ago

      Yes, bun is good locally, but need node support for deployment. So while the bun specific stuff sounds great I feel I need to avoid it.

    • ilbert 2 hours ago

      I was targeting Bun because I really like its built-in SQL module. I can tweak the TS parser to look for e.g. postgres.js tagged template functions and make it work for that as well. I don't really see any blockers

  • psc007 2 hours ago

    Support for postgis?

  • danr4 2 hours ago

    pretty cool

  • ilbert 3 hours ago

    I write Bun.sql with raw SQL and no ORM, and the one thing I kept missing was types. You write a query, get back `any[]`, and hand-write a row type that silently drifts from the actual columns. Drizzle/Kysely fix this by moving the query into TypeScript, but then you're not really writing SQL anymore.

    bun-sqlgen goes the other way. You keep writing raw SQL queries, just give each one a name.

    A codegen step reads your migration `.sql` files, stands up a throwaway Postgres via PGlite (so no Docker) or SQLite, prepares every tagged query against it, and writes a `.d.ts` that maps each query name to its real result type. After that, plain `tsc` does the rest: `user.notExistingField` won't compile, and `display_name.length` gets flagged because the column is nullable.

    Nullability was the annoying part. Postgres's describe doesn't hand you per-column nullability, so I infer it from the query plan plus the catalog, with manual overrides for the cases that genuinely can't be inferred. SQLite works too.

    The runtime stays 100% Bun.sql, the generated file is the only artifact (commit it), and codegen is fast enough to rerun on save.

    It's early (v0.1, built it for my own projects) so I'd mostly like to hear where it falls over.

    • Retr0id an hour ago

      > silently drifts

      > genuinely

      > I'd mostly like to hear where it falls over.

      https://news.ycombinator.com/newsguidelines.html#generated

    • trollbridge 2 hours ago

      Can you describe how it's the same and how it's different than SQLx (a Rust thing)?

      • ilbert an hour ago

        I actually took a lot of inspiration from sqlx, which is really nice. The main differences are:

        - in JS/TS you don't have compile-time scripts that you can run like with Rust's macros, so you need to run a codegen command before running the type checks (disadvantage)

        - I had to create a TS parser that goes and finds the tagged template functions with the sql statements, while sqlx has them "for free" because sql statements are the input to the macro itself (disadvantage)

        - I use an in-memory Postgres (PGLite) to describe the queries, instead of requiring a running pg instance (advantage)

        - I don't cache the statements and codegen for now like sqlx does, something that can be added later

        I think they are similar in that they both substitute the dynamic params with no-ops like $1, $2, etc. before handing the sql statement to the pg's DESCRIBE function