ORM¶
The EERP ORM is a custom data access layer built on top of pgx v5. It provides type-safe CRUD operations, composable query builders, transactions with savepoints, and soft-delete — all without reflection at query time.
Purpose¶
Standard Go ORMs (GORM, ent) solve the general case. EERP's ORM solves a more specific case: an ERP system where:
- Every entity needs audit fields (created/updated/deleted timestamps, UUID primary key)
- Soft-delete is the norm, hard delete is the exception
- Transactions frequently need partial rollback via savepoints
- Startup cost is acceptable; per-query reflection is not
- The query layer must be composable but never hide what SQL it generates
See ADR-001 for the full rationale.
Responsibilities¶
- Map Go structs to database rows and back
- Build parameterized SQL from a fluent builder API
- Manage connection pool lifecycle
- Provide transactional boundaries (including savepoints)
- Log all queries (optionally)
- Cache all struct metadata at startup
Lifecycle¶
sequenceDiagram
participant Startup
participant Cache as MetadataCache
participant Repo as Repository[T]
participant Builder as QueryBuilder
participant DB as pgxpool
Startup->>Cache: orm.MustRepo[Order](db)
Cache->>Cache: reflect.VisibleFields(Order)
Cache->>Cache: Parse db tags, build StructMeta
Cache-->>Repo: Repository[Order]{meta: StructMeta}
note over Startup,DB: Zero reflection from here on
Startup->>Repo: repo.FindByID(ctx, id)
Repo->>Builder: SELECT * FROM orders WHERE id=$1 AND deleted_at IS NULL
Builder->>DB: Query(ctx, sql, args)
DB-->>Builder: pgx.Rows
Builder->>Builder: Scan rows using pre-built column index
Builder-->>Startup: Order{} Core Concepts¶
Entity and BaseModel¶
Every ORM-managed struct embeds model.BaseModel:
import "core/orm/model"
type Order struct {
model.BaseModel // (1)
CustomerID uuid.UUID `db:"customer_id"`
Status string `db:"status"`
TotalCents int64 `db:"total_cents"`
}
func (Order) TableName() string { return "orders" } // (2)
BaseModelcontributes five columns:id(UUID, PK),created_at,updated_at,deleted_at, andtenant_id(indexed — every entity that embeds it is tenant-owned).- If
TableName()is not implemented, the table name is derived assnake_case(TypeName).
BaseModel provides:
| Field | Type | DB Column | Behaviour |
|---|---|---|---|
ID | uuid.UUID | id | Auto-generated on INSERT |
CreatedAt | time.Time | created_at | Set on INSERT |
UpdatedAt | time.Time | updated_at | Set on INSERT and UPDATE |
DeletedAt | *time.Time | deleted_at | nil = active; non-nil = soft-deleted |
TenantID | uuid.UUID | tenant_id (indexed) | the owning tenant — the row isolation boundary, set server-side |
A genuinely tenant-less table (the global permission catalog, refresh tokens scoped by user_id, boot-time bookkeeping) does not embed BaseModel; it declares the other fields by hand. Go embedding has no per-field opt-out.
Struct Tags¶
| Tag | Meaning |
|---|---|
db:"col" | Explicit column name |
db:"col,pk" | Primary key |
db:"col,omitempty" | Skip field when value is the zero value |
db:"col,softdelete" | Marks the soft-delete timestamp column |
db:"col,index" | Secondary index on the column (btree) |
db:"col,index=hash" | Secondary index with an explicit access method |
db:"col,generated" | A Postgres generated column: selected, filtered and sorted, never written (see Generated columns) |
db:"-" | Exclude from all queries |
If no db tag is present, the column name is the snake_case conversion of the field name.
Indexes¶
Tag a column with index to have an index created during auto-migration. The optional index=<method> selects the PostgreSQL access method — valid methods are btree (default), hash, gist, spgist, gin, and brin. An unknown method fails fast at startup with a metadata error, so a typo never silently ships an unindexed column.
type Session struct {
model.BaseModel
Token string `db:"token,index=hash"` // exact-match lookups
UserID uuid.UUID `db:"user_id,index"` // foreign-key style lookups (btree)
Tags map[string]any `db:"tags,index=gin"` // jsonb / array containment
}
Indexes are created as idx_<table>_<column> with CREATE INDEX IF NOT EXISTS, so migration is idempotent: adding an index tag to an existing column creates the index on the next startup without touching the data. Removing the tag does not drop the index — drop it manually if it is no longer wanted (the migration path is additive by design; see Module System).
No unique constraints in tags; no hand-written search indexes
Tags only create plain, non-unique indexes. A unique constraint (needed by Upsert's conflict target) is hand-written SQL in the module's Migrate() hook — e.g. user_presence(tenant_id, user_id). Conversely, never hand-write a trigram index for search[]: automatic search indexes build them where the data pays for them. Geographic columns use index=gist (Geographic Fields).
All schema DDL — at boot and in tests — runs under a Postgres advisory lock, because CREATE TABLE IF NOT EXISTS races when several processes migrate one database.
The WASM/JSON migration path exposes the same capability via an operation: set "index": true (optionally "index_type") on an add_column op, or emit a standalone {"type": "create_index", "table": "...", "column": "...", "index_type": "gin"}. This keeps the Go-tag path and the WASM-ABI path at feature parity — a module declares an index the same way regardless of which loader path it uses.
MetadataCache¶
orm/internal/cache/cache.go holds a global sync.Map keyed by reflect.Type. The first call to MustRepo[T] computes a StructMeta for T and stores it. All subsequent calls return the cached value.
StructMeta contains: - Table name - Ordered list of FieldMeta (field pointer, column name, flags) - A columnIndex map for O(1) lookup during scanning
After startup, zero reflection occurs in the hot path.
Repository¶
orm/repo/repository.go
The Repository[T] is the primary interface for CRUD operations. Construct one per entity at application startup:
MustRepo is appropriate at startup because misconfigured repositories are programming errors, not runtime conditions.
Read Operations¶
All reads exclude soft-deleted rows (deleted_at IS NULL) automatically.
// Single row by primary key
order, err := orders.FindByID(ctx, id)
// First row matching a condition
open, err := orders.FindOne(ctx, orm.Cond("status = $1", "open"))
// All rows
all, err := orders.FindAll(ctx)
// All rows matching a condition
pending, err := orders.FindAll(ctx, orm.Cond("status = $1", "pending"))
// Count
n, err := orders.Count(ctx)
n, err := orders.Count(ctx, orm.Cond("status = $1", "open"))
Write Operations¶
// INSERT … RETURNING *
created, err := orders.Create(ctx, order)
// UPDATE … WHERE id=$1 RETURNING *
updated, err := orders.Update(ctx, order, id)
// Soft-delete: sets deleted_at = NOW()
n, err := orders.Delete(ctx, id)
// Hard delete: DELETE FROM orders WHERE id=$1
n, err := orders.HardDelete(ctx, id)
// Restore: clears deleted_at
err = orders.Restore(ctx, id)
Create / Upsert insert the primary key too when it is set explicitly (a DB-side DEFAULT gen_random_uuid() fills it otherwise), so deterministic-id seeding inserts the id as given.
Upsert on a natural key¶
Update replaces a whole row by primary key. When a row is identified by a natural key ((tenant_id, user_id)) and a write must touch only some columns, use Upsert:
row, err := presence.Upsert(ctx, p,
[]string{"tenant_id", "user_id"}, // conflict target
"connected = EXCLUDED.connected, updated_at = now()") // only these columns on conflict
// setFragment "" = ON CONFLICT DO NOTHING; the existing row is re-read and returned.
role, err := roles.Upsert(ctx, seededRole, []string{"id"}, "")
// Target backed by a PARTIAL unique index (e.g. "… WHERE deleted_at IS NULL"):
// Postgres needs the predicate repeated, or the upsert fails with 42P10.
row, err := templates.UpsertPartial(ctx, t, []string{"tenant_id", "key", "locale"}, "deleted_at IS NULL", "body_html = EXCLUDED.body_html")
The conflict target must be a real unique index — struct tags can't declare one, so it is hand-written in the module's Migrate() (see Indexes).
Conditional updates: UpdateQuery¶
The write-side counterpart of the SELECT builder, pre-bound to the repository's table:
n, err := orders.UpdateQuery().
Set("status", "shipped").
Where(orm.Cond("id = $1 AND status = $2", id, "paid")). // a guarded transition
Exec(ctx, db)
n == 0 tells the caller the guard didn't match — the basis of race-free counters such as event seat capture.
Row locking: SelectForUpdate¶
err := orm.Transact(ctx, db, func(tx *orm.Tx) error {
due, err := jobs.SelectForUpdate().SkipLocked().
Where(orm.Cond("status = $1", "pending")).Limit(20).All(ctx, tx)
// … rows stay locked until COMMIT …
})
SELECT … FOR UPDATE, excluding soft-deleted rows. It must run on a *Tx (ErrLockOutsideTx otherwise). SkipLocked() for work queues (the mail sender claims its batch this way), NoWait() to fail instead of waiting, Of(tables…) with joins.
Batch Operations¶
// Single INSERT with multiple rows, one round-trip
created, err := orders.CreateBatch(ctx, []Order{o1, o2, o3})
Scoping to a Transaction¶
err = orm.Transact(ctx, db, func(tx *orm.Tx) error {
txOrders := orders.WithTx(tx) // shallow copy, same meta, different executor
_, err := txOrders.Create(ctx, order)
return err
})
Tenant Isolation¶
EERP is multi-tenant: rows belong to a tenant, and a caller must never see or touch another tenant's rows. The generic CRUD layer enforces this automatically so that every auto-generated endpoint is safe by default.
Any registered table that has a tenant_id column is treated as tenant-owned. For those tables the generic crud.Repository (the map-based repository behind the auto-generated HTTP handlers):
- Reads (
FindAllincl. its count,FindByID) addWHERE tenant_id = $tenant. - Updates, soft/hard deletes and restore add the same predicate, so a cross-tenant id affects zero rows →
404. - Create forces
tenant_idto the caller's tenant, ignoring any value in the request body. - Update treats
tenant_idas immutable — a client cannot move a row into another tenant.
The tenant comes from the request context, where the JWT middleware stamps it via orm/access.WithTenant. The repository reads it with access.TenantFromContext.
Fail closed
If a tenant-scoped table is accessed with no tenant in the context, the repository returns crud.ErrTenantMissing rather than running an unscoped, all-tenants query. A wiring or authentication gap can never silently widen access.
Embedding model.BaseModel is what makes an entity tenant-owned — TenantID is one of its fields — so every business entity is isolated with no handler changes. Tables without a tenant_id column (the shared permission catalog) are global by design:
Only the generic CRUD layer is auto-scoped
The typed Repository[T] and the query builders below are not tenant-aware. Custom service code that uses them must apply WHERE tenant_id = $1 from the identity context itself (see Authentication → Multi-Tenancy).
tenant_id is treated as a server-generated field by request validation, so clients are never asked to supply it on create — it is injected from the token, exactly like id and the timestamps.
API Exposure vs. Registration¶
Registering a struct with the ORM does two independent things: it makes the schema migrate and, by default, mounts a full HTTP CRUD surface for it. These are decoupled:
| Option | Effect |
|---|---|
orm.Register[T]() | Registered, migrated, and exposed via generic CRUD |
orm.Register[T](orm.WithExcluded()) | Registered and migrated, but no HTTP routes |
orm.WithExcludeFields("col") | Column removed from the API entirely (input and output) |
orm.WithReadOnlyFields("col") | Column stripped from write bodies |
orm.WithTableName("existing_table") | Registers under another module's table instead of deriving one from the struct name — see Extending another module's table |
WithExcluded() is the fail-closed way to keep a table off the API while still letting the ORM own its schema. The auth tables (users, roles, permissions, refresh_tokens) use it: they must not be listable or mutable through unaudited generic CRUD, but their schemas must still migrate. Internally, registry.All() (the HTTP surface) omits excluded tables while registry.AllRegistered() (the migration set) includes them.
Security controls belong in code, not api.yaml
Exclusions can also be expressed in api.yaml, but that file loads at startup and only shapes the API surface — a missing or malformed file must never widen it. Security-load-bearing exclusions (hiding password_hash, withholding refresh_tokens) are therefore enforced in code via the options above, so the guarantee is fail-closed. api.yaml is reserved for cosmetic overrides (route prefixes, field renames), and a configured-but-broken file is a fatal startup error.
Extending Another Module's Table¶
orm.WithTableName("existing_table") lets one module register its own struct under a table another module already owns — the mechanism behind module isolation without patching. A module embeds the base module's struct, adds its own fields, and re-registers:
// core/modules/crminheritdemo/module.go
type CRM struct {
crm.CRM // embed the base module's struct
Date *time.Time `db:"date"`
Comment *string `db:"comment"`
}
func (m *crminheritdemoModule) Register() error {
return orm.Register[CRM](orm.WithTableName("crm"))
}
The crm table gains date/comment columns and they appear on the generic CRUD surface for /api/v1/crm — without a single line of core/modules/crm changing.
Extension columns must be nullable
An extension field must be a pointer type (*time.Time, *string, …), never a bare value type. A non-pointer field migrates as NOT NULL, which makes the extension column mandatory on every create against the base table — including from the base module's own form, which has no idea the extension exists. This shipped as a real bug once (a non-pointer Comment string 422'd every plain crm write) and is now pinned by a regression test asserting every column an extension adds reports Nullable: true. The WASM/JSON schema-extension path (orm.ExtendSchema) carries the identical rule.
For registration ordering (why the base module must load first) and the frontend's mirror of this same "extend, don't patch" pattern, see ADR-006: Frontend View Inheritance.
Composable extension: ExtendSchema¶
Re-registering a table is not composable: the last Register wins, so two modules can't both extend crm that way. orm.ExtendSchema adds columns without re-registering:
func (m *myModule) Register() error {
return orm.ExtendSchema("contact", []orm.SchemaField{
{Column: "loyalty_points", Type: reflect.TypeFor[int]()},
{Column: "loyalty_tier"}, // Type nil = TEXT
})
}
Several modules may extend one table; columns are always nullable, typed by Type, and kept when the owner registers the table again later. Trade-off: no typed Go access — use the embed pattern when your own Go code needs the fields as struct members.
Public read scope¶
orm.Register[T](orm.WithPublicFields("name", "price")) declares which fields may be read anonymously on /api/v1/public/{table} (orm.AllPublicFields = every API column except the PK, tenant_id, deleted_at and gated ones). Nothing is public until an admin publishes a subset. Enforced by the same checkColumn / BuildResponse choke points as group gating — see Website & Public Access.
Row scope¶
Tenant isolation is automatic; a per-caller restriction on top of it ("a project is visible to its members") is a row scope: a function returning, per request, the condition limiting the caller's rows (ADR-039).
orm.Register[Project](orm.WithRowScope(func(ctx context.Context) (orm.Condition, bool, error) {
user, ok := callerFromContext(ctx) // the module's own identity lookup
if !ok {
return orm.Condition{}, false, errors.New("no caller") // fail closed → 500
}
if user.IsAdmin {
return orm.Condition{}, false, nil // ok=false: no restriction
}
return orm.Cond("EXISTS (SELECT 1 FROM project_member m WHERE m.project_id = project.id AND m.user_id = $1)", user.ID), true, nil
}))
The condition is applied next to the tenant condition on every generic read and write: list and its total, ?distinct=, ?aggregate=, get, update, restore and delete. A row outside the scope is a 404. Row-scoped tables are never served from the Redis read cache, and geo references to them are refused. Services anchored on another table's record (chatter, notebook, attachments, pictures) check it with orm.CanSeeRecord(ctx, db, table, id), which is true for a table without a scope, and answer 404 on a hidden record.
Pitfalls
- Bind values as
$nparameters, never by string concatenation. - Typed
Repository[T]reads and raw SQL are not scoped; only the generic surface applies the scope. Use the generic handlers ororm.CanSeeRecordwhen a caller's visibility matters.
The Project app is the first user.
Generated columns¶
A value derived from other columns (seats left = capacity − seats taken) is best computed by Postgres: it can be filtered and sorted like any column and can never drift from its inputs. Tag the field db:"remaining_seats,generated": the ORM still selects, scans, filters and sorts it, but leaves it out of typed and generic INSERT, UPDATE and Upsert, the generic ensureColumns pass does not add it, and its API field is read-only. The owning module adds the column with its expression in Migrate():
ALTER TABLE event_session ADD COLUMN IF NOT EXISTS remaining_seats integer
GENERATED ALWAYS AS (capacity - seats_taken) STORED
After Create/Update the struct's value comes from RETURNING, never from what the caller put in it. A table that doesn't exist yet is created without the column until Migrate() runs, so add it there.
Relations¶
A relation field on the frontend descriptor (many-to-one, one-to-many, many-to-many — see View Engine → Descriptors) has no special representation in the ORM. Each kind maps to an ordinary registered entity:
| Kind | Go shape | Example |
|---|---|---|
| many-to-one (m2o) | A nullable FK column on the owning struct | ContactID *uuid.UUIDdb:"contact_id"` onCrm` |
| one-to-many (o2m) | The inverse side of an m2o — no extra column, just a query scoped by the FK | Every Crm row where contact_id = <this contact> |
| many-to-many (m2m) | A plain junction entity, registered like any other table | CrmTag{ CrmID uuid.UUID, TagID uuid.UUID } → crm_tag |
// core/modules/crm/internal/tags.go
type Tag struct {
model.BaseModel
Name string `db:"name"`
}
type CrmTag struct { // junction row — <own>_id / <related>_id by convention
model.BaseModel
CrmID uuid.UUID `db:"crm_id,index"`
TagID uuid.UUID `db:"tag_id"`
}
Both are registered with plain orm.Register[T]() and get their own full generic CRUD surface (/api/v1/tag, /api/v1/crm_tag). There is no dedicated m2m handler — the frontend's tags widget reads links with GET /api/v1/crm_tag?filter[crm_id]=<id> and writes/removes them as ordinary rows through the same generic routes. This is a deliberate consequence of the ORM having no relation concept to special-case: a junction table is exactly as auto-generated, tenant-isolated, and soft-deletable as any other entity.
Query Parameters (Generic CRUD)¶
The generic CRUD list endpoint (GET /api/v1/{table}) accepts bracketed query parameters in addition to page/page_size:
| Parameter | Matching | Example |
|---|---|---|
filter[<column>]=<value> | Exact match (column::text = $1) | ?filter[status]=won |
search[<column>]=<text> | Case-insensitive containment (column::text ILIKE '%'\|\|$1\|\|'%') | ?search[name]=mart |
in[<column>]=<v1>,<v2>,... | One of several values (column::text = ANY($1)) | ?in[id]=<id1>,<id2> |
gt[<column>]=, gte[<column>]=, lt[<column>]=, lte[<column>]= | Range comparison, type-aware cast (numeric or timestamptz, derived from FieldMeta.GoType — never a lexicographic ::text compare) | ?gte[created_at]=2026-01-01 |
empty[<column>]=1 | NULL or '' — what an empty filter value can't say (it means "no filter") | ?empty[status]=1 |
order=<column> / order=-<column> | Sort (descending with -), primary key as tiebreak. Pass one whenever you page — without it the order is undefined | ?order=-created_at |
distinct=<column> (combinable with the above) | Returns that column's distinct values plus per-value row counts (capped at 500), instead of a paginated page | ?distinct=status |
aggregate=sum\|avg\|mean\|median\|count&value=<col or formula>[&x=<date>&bucket=day\|week\|month][&group=<col>] | {groups:[{x, group, value, count}]} over every matching row — Graph tiles (ADR-024) | ?aggregate=sum&value=total&x=created_at&bucket=month |
near[], within[], covers[], inside[] | Geographic filters on orm.GeoPoint / orm.GeoShape columns | see Geographic Fields |
The envelope is {data, total}. total is exact up to 10 000 matching rows (orm.ExactCountLimit); beyond, it is the planner's estimate and the envelope adds total_estimated: true — never use it as a loop bound (List Read Performance). A unique violation on create/update answers 409. With redis_url set, these reads may be served from the Redis read cache.
Every one of these is validated against the table's registered columns before touching SQL — an unknown column is a 400 ("unknown filter column: …"), not a silently-ignored parameter or an injection risk, since only known-safe identifiers are ever interpolated. filter[col] is what the frontend uses to scope relation reads (an o2m's inverse list, an m2m's junction rows); search[col] backs the m2o relation widget's autocomplete; in[col] is what the built-in search/filter bar uses to batch-resolve many-to-many links in one round trip instead of one request per row (see ADR-018); distinct= backs the search bar's group-by section. See View Engine → Relation widgets and Search & Filter Bar for the client side.
Field-Level Group Gating¶
A column can be restricted to callers whose resolved role-group set intersects a declared list, independent of the filter/search/in/range/distinct parameters above:
FieldMeta.Groups (empty by default = ungated) is checked in exactly one place for response bodies — crud.BuildResponse omits a gated column's key entirely (never nulls it) when the caller's groups don't intersect — and in exactly one place for queries — Repository.checkColumn rejects a gated column used in filter/search/in/range/distinct with the same "unknown filter column" error a nonexistent column gets, so a caller without the group can't infer a gated value by filtering on it either. A field with no Groups declared is completely unaffected. The caller's groups arrive via core/orm/access.WithGroups/GroupsFromContext (mirrors WithTenant/TenantFromContext), resolved once at login/refresh from the user's roles plus every role they transitively "belong to." See ADR-014 and ADR-015.
A UX mirror on the frontend, not the security boundary
FieldDescriptor.groups on the client only avoids rendering an empty slot for a field the caller was never sent — the server-side omission above is what actually keeps the data from leaving the backend.
Query Builders¶
When Repository methods are not expressive enough, drop down to the builder API. Builders are immutable value types — safe to branch and reuse.
SELECT¶
results, err := orm.Select[Order](orders.Meta()).
Columns("id", "status", "total_cents"). // default: all mapped columns
Join("JOIN customers c ON c.id = o.customer_id").
Where(orm.Cond("c.region = $1", "EU")).
Where(orm.Cond("o.status != $1", "cancelled")).
OrderBy("created_at DESC").
Limit(50).
Offset(page * 50).
All(ctx, db)
// Single row
one, err := builder.One(ctx, db)
// COUNT(*) — strips ORDER BY, LIMIT, OFFSET automatically
n, err := builder.Count(ctx, db)
Multiple Where() calls are joined with AND. Placeholder numbers ($1, $2) in each condition are rebased automatically so they don't conflict.
INSERT¶
// Single row
created, err := orm.Insert[Order](orders.Meta(), order).
Returning("*").
One(ctx, db)
// Multiple rows (VALUES (…), (…), …) — one round-trip
all, err := orm.Insert[Order](orders.Meta(), o1, o2, o3).
Returning("id", "created_at").
Batch(ctx, db)
// Upsert: on conflict do nothing
err = orm.Insert[Order](orders.Meta(), order).
OnConflictDoNothing().
Exec(ctx, db)
// Upsert: on conflict update
updated, err := orm.Insert[Order](orders.Meta(), order).
OnConflict("id").DoUpdate("status = EXCLUDED.status").
Returning("*").
One(ctx, db)
UPDATE¶
// Explicit column assignment
tag, err := orm.Update[Order](orders.Meta()).
Set("status", "shipped").
Set("updated_at", time.Now()).
Where(orm.Cond("id = $1", id)).
Exec(ctx, db)
// From a full struct (PK excluded, zero-value fields with omitempty skipped)
updated, err := orm.Update[Order](orders.Meta()).
FromStruct(order).
Where(orm.Cond("id = $1", id)).
Returning("*").
One(ctx, db)
Safety check
Update.ToSQL() returns an error if no Where() has been called. Accidental full-table updates are refused at build time.
DELETE¶
// Hard delete with condition
n, err := orm.Delete[Order](orders.Meta()).
Where(orm.Cond("id = $1", id)).
Exec(ctx, db)
// With RETURNING for audit
deleted, err := orm.Delete[Order](orders.Meta()).
Where(orm.Cond("status = $1", "cancelled")).
Returning("id", "customer_id").
All(ctx, db)
Safety check
Delete.ToSQL() returns an error if no Where() has been called.
Transactions and Savepoints¶
orm/pool/tx/tx.go
err = orm.Transact(ctx, db, func(tx *orm.Tx) error {
txOrders := orders.WithTx(tx)
txLines := lines.WithTx(tx)
// Savepoint for partial rollback
if err := tx.Savepoint(ctx, "order_lines"); err != nil {
return err
}
if err := createLines(ctx, txLines, lineItems); err != nil {
// Roll back only the lines, keep the order
if rbErr := tx.RollbackTo(ctx, "order_lines"); rbErr != nil {
return rbErr
}
return applyFallback(ctx, txOrders, txLines)
}
return tx.Release(ctx, "order_lines")
})
Semantics: - fn returns nil → COMMIT - fn returns error → ROLLBACK, error is returned to caller - Savepoints enable partial rollback within a transaction
Logging¶
import "core/orm/log"
// Production: log only errors
db, err := orm.Open(cfg, log.NewNoopLogger())
// Development: log every query
db, err := orm.Open(cfg, log.NewZapLogger(zapLogger))
The Logger interface is minimal:
type Logger interface {
Log(ctx context.Context, entry LogEntry)
}
type LogEntry struct {
SQL string
Args []any
Duration time.Duration
Err error
}
Query-caller tracking
Every LogEntry also carries the call site (orm/log.Caller() walks the stack past the ORM's own frames) so a slow-query or error log line names the actual repository/service method that issued it, not just "somewhere in the ORM." The same change quieted orm.ErrNoRows logging (a normal, expected outcome for FindOne/FindByID misses) and trimmed stack traces attached to error logs to the caller-relevant frames.
Interactions¶
graph LR
Repo["Repository[T]"] -->|uses| Cache["MetadataCache"]
Repo -->|delegates to| Builder["QueryBuilder"]
Builder -->|executes via| Exec["Executor interface"]
Exec -->|implemented by| DB["orm.DB (pgxpool)"]
Exec -->|implemented by| Tx["orm.Tx (transaction)"]
DB -->|wraps| pgx["pgx/v5"]
Builder -->|scans rows via| Scan["scan.Rows[T]"]
Scan -->|uses| Cache Extension Points¶
| Extension | How |
|---|---|
| Custom query logic | Use SelectBuilder / InsertBuilder directly instead of Repository |
| Custom logger | Implement orm/log.Logger, pass to orm.Open |
| Custom base model | Implement the model.Entity constraint; BaseModel is not mandatory |
| Custom table name | Implement TableName() string on the struct |
| Hooks before/after writes | Wrap Repository in your own service type |