Live data from Hacker News

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

github.com

11–20 of 44 posts

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

#11
post #2

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…

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

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

#12
post #2

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…

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

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

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

#13
post #6

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?

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.

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

#15
post #2

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…

> silently drifts

> genuinely

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

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

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

#17
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/

Post reply on HN