NewsLab
Jun 28 20:16 UTC

Show HN: Bun-sqlgen – Type-safe raw SQL for Bun, no ORM (github.com)

55 points|by ilbert||42 comments|Read full story on github.com

Comments (42)

42 shown
  1. 1. ilbert||context
    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.

  2. 2. trollbridge||context
    Can you describe how it's the same and how it's different than SQLx (a Rust thing)?
  3. 3. ilbert||context
    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

  4. 4. trollbridge||context
    Very interesting! I spent a long session in a harness the other day getting pglite to the point it would work reliably in a browser for a specific use case I had. The key was NOT using that environment to run the SQLx validation, but just having a real long running Postgres for local builds + CI, and simply creating and dropping a database for that run.

    Used pglite-oxide for the Electron build and then we went off into the weeds and ported pglite so it could run in iOS for iOS apps. The easiest way to do this is to just do it as a plain old Mac app first and get it working there first.

    I’ll open source this if I figure out if we can maintain it and document it.

  5. 5. Retr0id||context
    > silently drifts

    > genuinely

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

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

  6. 6. psc007||context
    Can you make it work/ does it work with Porsager-Postgres in modnes which buns Postgres client is «based on»?
  7. 7. ilbert||context
    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?
  8. 8. psc007||context
    Not solved as far as I know. Most libraries don’t work well with the template syntax in porsagers Postgres
  9. 9. psc007||context
    Support for postgis?
  10. 10. ilbert||context
    The cli config supports specifying the PGLite extensions (the codegen uses PGLite as an in-memory light postgres), see this example: https://github.com/ilbertt/bun-sqlgen/tree/main/examples/wit....

    You'll still probably have to manually override some column types using PG comments like: https://github.com/ilbertt/bun-sqlgen/blob/dee757ebc9c38aec7...

    PGLite extensions: https://pglite.dev/extensions/#postgis

  11. 11. sHooKDT||context
    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?

  12. 12. ilbert||context
    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
  13. 13. tomComb||context
    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.
  14. 14. MegagramEnjoyer||context
    bun truly made itself unappealing when it did the vibe coded port to rust
  15. 15. mmkal||context
    have a look at https://sqlfu.dev - if you're using sqlite especially this likely has what you're looking for

    (disclaimer: i made sqlfu! and it's pretty early so use with appropriate caution. there's very little to the runtime part of it though, just very thin adapters around battle-tested clients)

  16. 16. danr4||context
    pretty cool
  17. 17. rankdiff||context
    sqlc is worth a mention.

    https://sqlc.dev

  18. 18. genshii||context
    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.
  19. 19. lacy_tinpot||context
    LLM generated docs/readmes are at this point old news.
  20. 20. ilbert||context
    Yeah, I'm still iterating on the docs
  21. 21. MegagramEnjoyer||context
    you can review the code and decide if the quality is up to par?
  22. 22. nozzlegear||context
    They reviewed the readme and decided the quality wasn't up to par. Perfectly valid IMO, given reviewing code you didn't write is an investment.
  23. 23. ilbert||context
    I've updated the READMEs by the way
  24. 24. giovannibonetti||context
    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/

  25. 25. zareith||context
    It may be a good choice for golang, but support for other languages is not as great.

    I remember creating a PR [1] for some very obvious issues with Typescript generator for better-sqlite3 some two years ago which never received any attention from maintainers. There are bunch of such PRs languishing in obscurity in their repos.

    [1] https://github.com/sqlc-dev/sqlc-gen-typescript/pull/45

  26. 26. allthetime||context
    Kysely is best choice for TS imo.
  27. 27. satvikpendem||context
    And sqlx on the Rust side just for completeness
  28. 28. ilbert||context
    That's actually the library that inspired me the most for building bun-sqlgen
  29. 29. EddieRingle||context
    Also SQLDelight for Kotlin: https://sqldelight.github.io/sqldelight/
  30. 30. atombender||context
    I love sqlc, but I think it's more fair to say it's an SQL code generator for Postgres and Go. Everything else is much less well-supported. You can pretty much forget about trying to use it with ClickHouse or SQLite, for example.

    It's also a project that seems to be suffering a bit from maintainer burnout. Important bugs aren't being fixed and contributor PRs aren't being merged. It took months to merge support for the new "\restrict" syntax introduced in Postgres 17's SQL schema dump format, for example.

  31. 31. allthetime||context
    Kysely rules
  32. 32. ilbert||context
    Kysely is really cool, but I don't like that you are not writing SQL directly
  33. 33. allthetime||context
    You can use sql<type>`query` anywhere you like combined with query builder syntax (or not) when you want to write raw sql and maintain type inference.
  34. 34. ilbert||context
    sql<type>`query` is the reason why I started bun-sqlgen, to not have to write types manually
  35. 35. zareith||context
    This looks great. I love using pgtyped, but have missed a solution that works well for sqlite.
  36. 36. inline_always||context
    pretty neat, what's the technical reason it has to be Bun only?
  37. 37. ilbert||context
    No real reason, I was working with Bun's SQL module for a project and felt the need for such codegen. I'm thinking of generalizing it to Node.js
  38. 38. schultzer||context
    I don’t understand why no one is making SQL a first class citizen in a language, there are tons of languages out there that are extendable, how hard can it be, enough of DSL and generated type-safe application code which is a DSL in reverse order.
  39. 39. fabianlindfors||context
    Microsoft has been doing this for a long time: https://learn.microsoft.com/en-us/dotnet/csharp/linq/
  40. 40. schultzer||context
    We can do way better then LINQ in an extendable language.
  41. 41. mmkal||context
    We built something quite similar - a full SQL framework with migrations, schema diffing, type-generation (this is the similar part I think), and named queries along with observability and more: https://sqlfu.dev

    It also ships adapters for pretty much all platforms, for sqlite at least: bun, node, libsql, better-sqlite3, expo-sqlite, sqlite-wasm, etc. etc.

    github: https://github.com/iterate/sqlfu

  42. 42. ilbert||context
    Really cool! I see you don't discover the SQL in the code right?