$ show go-gorm/gorm
GORM is the dominant Object-Relational Mapping library for Go, providing a full-featured, database-agnostic abstraction layer. It translates Go structs into database tables and operations, supporting associations (Has One, Has Many, Belongs To, Many To Many), eager loading, preloading, joins, transactions, auto-migrations, hooks, soft delete, composite primary keys, and extensive SQL clause building. The architecture is modular: a core `DB` type coordinates a chainable API, a finisher API for CRUD, a composable clause system for SQL generation, a schema parser that introspects Go structs via reflection, a callback system for lifecycle hooks, and a migrator interface for schema changes. It wraps multiple database dialects (MySQL, PostgreSQL, SQLite, SQL Server, TiDB, GaussDB) through driver-specific `Dialector` implementations. The codebase is production-grade, heavily tested, and widely adopted. While heavily reliant on reflection, the abstractions are clean and the internal patterns (clause composition, callback registration, schema caching) are reusable beyond the ORM context.
The LLM identified 5 files worth highlighting for reuse:
Implements a thread-safe, generic LRU cache with configurable size, TTL expiration, and eviction callback. Uses bucket-based time slices for efficient cleanup and supports negative TTL. Demonstrates careful lock management and can be extracted for caching in any Go application.
Core of the SQL clause composition system. Each clause (Where, Select, Insert, etc.) implements the `Interface` with `Name()`, `Build(Builder)`, and `MergeClause(Clause)` methods. Clauses are merged in a `Clause` struct and built in order. This pattern allows flexible, extensible SQL generation and is reusable for any SQL query builder.
Parses Go structs into a `Schema` object containing fields, relationships, primary keys, indexes, and constraints using reflection and struct tags. Includes a caching mechanism with sync.Map and channel-based initialization to avoid duplicate parsing. The pattern is reusable for any framework that needs to introspect Go types at runtime.
Registers and executes ordered callbacks (before/after create, update, query, delete, etc.). Supports `Before`/`After`/`Replace`/`Remove` operations and compiles callbacks into a flat function list. This decoupled, ordered lifecycle pattern is reusable for any event-driven processing pipeline.
Implements soft deletion by injecting `WHERE deleted_at IS NULL` into queries and converting `DELETE` to `UPDATE`. Uses clause modifiers (`ClauseModifier`) added to the statement. This non-invasive, composable approach is reusable in any ORM that supports extensible query building.