frontpage.
newsnewestaskshowjobs

Open Source @Github

fp.

Open in hackernews

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

https://github.com/ilbertt/bun-sqlgen
25•ilbert•1h ago

Comments

ilbert•1h 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.

trollbridge•32m ago
Can you describe how it's the same and how it's different than SQLx (a Rust thing)?
ilbert•25m 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

psc007•1h ago
Can you make it work/ does it work with Porsager-Postgres in modnes which buns Postgres client is «based on»?
ilbert•1h 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?
psc007•1h ago
Support for postgis?
ilbert•1h ago
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

sHooKDT•1h 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?

ilbert•58m 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
tomComb•8m 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.
danr4•1h ago
pretty cool

Show HN: TikZ Editor – WYSIWYG editor for figures in LaTeX

https://tikz.dev/editor/
122•DominikPeters•1h ago•24 comments

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

https://github.com/ilbertt/bun-sqlgen
25•ilbert•1h ago•11 comments

Show HN: Treedocs: Documentation that automatically checks for staleness

https://dandylyons.github.io/treedocs/
9•DandyLyons•1h ago•4 comments

Show HN: Neural Particle Automata

https://selforg-npa.github.io/
63•esychology•7h ago•14 comments

Show HN: Shumai – open-source Frame.io alternative for creative work

https://github.com/shumaiOne/shumai
37•Yiling-J•6h ago•2 comments

Show HN: Oak – Git alternative designed for agents

https://oak.space/oak/oak
204•zdgeier•1d ago•177 comments

Show HN: A pure ARM64 Assembly web server, now on Linux with CGI for no reason

https://github.com/imtomt/ymawky/tree/linux
46•imtomt•11h ago•17 comments

Show HN: Transformer Primitives – A visual explainer you can send to anyone

https://understandgpt.xyz/
4•arzt•2h ago•0 comments

Show HN: Got sick of ads, so I made my own logic puzzle site

https://puzzlelair.com/
232•HaxleRose•1d ago•142 comments

Show HN: AGI will not come from better models, it will come from meta harness

https://github.com/the-open-engine/zeroshot
2•covibes•2h ago•2 comments

Show HN: FuckUI - AI hates your UI. Feed it the web text first

https://fuckui.com
3•keepamovin•2h ago•1 comments

Show HN: perl-lsp – annotation free static analysis for Perl

https://github.com/tree-sitter-perl/perl-lsp
3•rabbiveesh•2h ago•0 comments

Show HN: Parlel – run Redis, Postgres, S3 and SaaS APIs in ~1 MB each

https://github.com/dksingh1997/parlel
3•Dheerajiitr•3h ago•0 comments

Show HN: I built an online patch backup tool for vintage 80s synths

https://knob.monster/
4•halfradaition•3h ago•0 comments

Show HN: CleverCrow: give tokens to your favorite projects

https://clevercrow.io
56•zhubert•1d ago•78 comments

Show HN: Cachet – A drop-in semantic cache for LLM APIs, 100% local, in Rust

https://github.com/abhix2112/Cachet
4•Abhi_2112•3h ago•0 comments

Show HN: Pagecast – Publish Markdown/HTML Reports to Cloudflare Pages

https://github.com/Amal-David/pagecast
51•amaldavid•4d ago•14 comments

Show HN: Criterion Closet as a website – pull any of 1,247 films off the shelf

https://the-criterion-closet.vercel.app
186•olievans•3d ago•54 comments

Show HN: Aharness – Enforce coding-agent workflows as state machines on Codex

https://github.com/Alfredvc/aharness
4•alfredvc•5h ago•2 comments

Show HN: Recall – Local project memory for Claude Code

https://github.com/raiyanyahya/recall
134•mateenah•1d ago•83 comments

Show HN: OpenTerm – A tiny, fast SSH client (no login, no cloud, free)

https://github.com/zhangshuo1991/openterm
3•zhangshuo1991•6h ago•0 comments

SHOW HN: Huskmail - A disposable email with private inboxes and trial library

https://huskmail.xyz
4•draggodra•7h ago•3 comments

Show HN: Selector Forge – browser extension for AI-generated resilient selectors

https://github.com/Intuned/selector-forge
34•ahmadilaiwi•1d ago•2 comments

Show HN: HN Game Stories – mini-documentary of games that hit the front page

https://video.intellios.ai
15•coolwulf•2d ago•2 comments

Show HN: Teach your kids perfect pitch

https://github.com/paytonjjones/bsharp
207•paytonjjones•2d ago•145 comments

Show HN: Exeria Charts – A native, high-performance financial charting library

https://github.com/efixdata/exeria-charts
3•katomek•8h ago•0 comments

Show HN: StartupWiki – A Free Alternative to Crunchbase

https://startupwiki.tech/
231•shpran•3d ago•69 comments

Show HN: TownSquare, a tiny presence layer for websites

https://townsquare.cauenapier.com/
268•cauenapier•3d ago•165 comments

Show HN: Make PDFs look scanned (CLI or in the browser via WASM)

https://github.com/overflowy/make-look-scanned
152•overflowy•2d ago•65 comments

Show HN: WSL Dashboard – an open-source,low-memory, high-performance GUI manager

https://github.com/owu/wsl-dashboard
7•GuardCalf•11h ago•0 comments