Relica

Relica is a lightweight, type-safe database query builder for Go with zero production dependencies.
🤖 AI Agents: Before generating code, read AGENTS.md for correct API patterns. Use Model() API for CRUD, Expression API for WHERE conditions. Avoid map[string]interface{}.
Agent skill: skills/relica/ (Agent Skills format) — version-exact docs, an audit
script for a project's Relica code, the upgrade procedure. In Claude Code (v2.1.275+):
/plugin install relica --marketplace coregx/relica (or /plugin marketplace add coregx/relica, then
/plugin install relica@relica; the shell form claude plugin install needs v2.1.292+). Other agents: read <module Dir>/skills/relica/SKILL.md at your version
(go list -m -f '{{.Dir}}' github.com/coregx/relica).
✨ Features
- Zero Dependencies - Truly empty go.mod, uses only Go standard library
- High Performance - LRU statement cache, batch operations (3.3x faster)
- Type-Safe - Generic
One[T]/All[T]/Scalar[T] + reflection-based struct scanning
- Model() API - ORM-style CRUD with auto-populated IDs (int + UUID via
autoincrement tag), composite PKs
- AutoID - Stripe-like prefixed IDs (
usr_, ord_) with dual-key pattern, FindByPublicID(), prefix validation
- NullStringMap - Dynamic scanning without predefined structs
- Named Placeholders -
{:name} syntax with relica.Params (Where, Having, NewQuery(...).BindParams) for readable queries
- Functional Expressions - CASE, COALESCE, NULLIF, GREATEST, LEAST, CONCAT
- Transactional() - Auto commit/rollback helper with panic recovery
- Dynamic WHERE -
AndWhere() / OrWhere() for conditional query building
- Row() / Column() - Convenient scalar and single-column queries
- Prepare() / Close() - Manual statement control for batch operations
- Transaction Support - Full ACID with all isolation levels
- Security - Parameterized queries prevent SQL injection by design
- Batch Operations - Efficient multi-row INSERT and UPDATE
- JOIN Operations - INNER, LEFT, RIGHT, FULL, CROSS JOIN support
- Sorting & Pagination - ORDER BY, LIMIT, OFFSET, DISTINCT, FOR UPDATE/FOR SHARE
- Aggregate Functions - COUNT, SUM, AVG, MIN, MAX, GROUP BY, HAVING
- Subqueries - IN, EXISTS, FROM subqueries, scalar subqueries
- Set Operations - UNION, UNION ALL, INTERSECT, EXCEPT
- Common Table Expressions - WITH clause, recursive CTEs
- Multi-Database - PostgreSQL, MySQL 8.0+, SQLite 3.25+ support
- Well-Tested - 85%+ coverage, stdlib testing only
- Clean API - Fluent builder pattern with context support
Latest Release: See CHANGELOG.md for version history and GitHub Releases for release notes.
📌 API Usage Priority
| Priority | API | When to Use |
|---|
| PREFERRED | db.Model(&struct).Insert/Update/Delete/Upsert() | All CRUD operations with structs |
| PREFERRED | relica.Eq(), relica.And(), relica.In(), etc. | WHERE conditions |
| PREFERRED | relica.HashExp{"col": val} | Simple equality conditions |
| ACCEPTABLE | Where("col = {:col}", relica.Params{"col": val}) | Named placeholders |
| ACCEPTABLE | Where("col = ?", val) | Positional placeholders |
| AVOID | map[string]interface{} | Only for dynamic/unknown schemas |
For AI Agents: See AGENTS.md for complete patterns and examples.
🚀 Quick Start
Installation
go get github.com/coregx/relica
Note: Always import only the main relica package. Internal packages are protected and not part of the public API.
Basic Usage
package main
import (
"context"
"fmt"
"log"
"github.com/coregx/relica"
_ "github.com/lib/pq" // PostgreSQL driver
)
type User struct {
ID int `db:"id"`
Name string `db:"name"`
Email string `db:"email"`
}