$ show uptrace/bun
Bun is a SQL-first Go ORM library that provides a fluent query builder, schema introspection, and migration tooling for PostgreSQL, MySQL, MariaDB, SQLite, MSSQL, and Oracle. It wraps the standard database/sql interface and offers a rich set of features including model-based CRUD with struct tags, relation loading (belongs-to, has-one, has-many, many-to-many), soft deletes, CTEs, upserts, and bulk operations. The library is designed for production use with built-in support for OpenTelemetry, structured logging, and New Relic instrumentation via optional extension packages. Architecturally, Bun separates SQL generation into dialect-specific packages (pgdialect, mysqldialect, etc.) and provides its own PostgreSQL wire protocol driver (pgdriver) for low-latency connections. The core query builders (SelectQuery, InsertQuery, etc.) compose reusable query parts (WHERE, ORDER, RETURNING) and use a feature flag bitmask to conditionally emit dialect-specific SQL. The migration system supports both SQL-file migrations and automatic schema diffing, comparing model definitions against live database catalogs to generate ALTER TABLE statements. Key entry points include the DB type (NewDB, NewSelect, etc.), the schema package for table/field metadata, and the migrate package for versioned migrations. The library is extensively tested across multiple database backends using Docker containers and snapshot-based golden file testing. While the codebase is large and dialect-specific branches add complexity, the overall architecture is clean, well-factored, and follows Go idioms consistently.
The LLM identified 2 files worth highlighting for reuse:
A generic ordered map implementation using a doubly linked list and a hash map. It maintains insertion order for iteration while providing O(1) lookups. The implementation is self-contained, uses Go generics, and can be extracted into any Go project that needs deterministic key-value iteration (e.g., for caching, configuration, or serialization). It demonstrates clean separation of concerns and efficient pointer-based list management.
A recursive descent parser for PostgreSQL array literal strings (e.g., '{1,2,{3,4}}'). It handles nested arrays, quoted elements, and NULL values. The parser is self-contained, has no external dependencies beyond the standard library, and can be reused in any Go project that needs to parse PostgreSQL array output. It demonstrates careful state management and correct handling of PostgreSQL's array escaping rules.