Documentation
¶
Overview ¶
Package orm is a small, driver-agnostic, Eloquent-style query builder over database/sql. The dialect (placeholder style, ILIKE handling) is chosen from the configured driver, so models are written once and the driver is swapped from .env (DB_DRIVER) without changing code.
models.Posts(app).Find(ctx, id)
models.Posts(app).Where("title", "ILIKE", "%go%").Order("created_at DESC").Get(ctx)
Index ¶
- Variables
- type Dialect
- type Query
- func (q *Query[T]) Create(ctx context.Context, data map[string]any) (*T, error)
- func (q *Query[T]) Delete(ctx context.Context) error
- func (q *Query[T]) Find(ctx context.Context, id any) (*T, error)
- func (q *Query[T]) First(ctx context.Context) (*T, error)
- func (q *Query[T]) Get(ctx context.Context) ([]T, error)
- func (q *Query[T]) Limit(n int) *Query[T]
- func (q *Query[T]) Offset(n int) *Query[T]
- func (q *Query[T]) Order(s string) *Query[T]
- func (q *Query[T]) Update(ctx context.Context, data map[string]any) error
- func (q *Query[T]) Where(col, op string, val any) *Query[T]
Constants ¶
This section is empty.
Variables ¶
var ErrInvalidInput = errors.New("orm: invalid input value")
ErrInvalidInput marks a database error caused by a value the caller sent: a malformed uuid, number or date the database couldn't convert (SQLSTATE class 22, "data exception"). Handlers map it to 4xx instead of 500:
if errors.Is(err, orm.ErrInvalidInput) { ... }
The original driver error is still reachable through errors.As.
Functions ¶
This section is empty.
Types ¶
type Dialect ¶
Dialect is the kernel's per-driver SQL dialect (re-exported for convenience).
func DialectFor ¶
DialectFor returns the dialect for a database/sql driver name.
type Query ¶
type Query[T any] struct { // contains filtered or unexported fields }
Query is a fluent, typed query builder for table rows scanned into T.
func (*Query[T]) Create ¶
Create inserts a row and returns it. Drivers with INSERT … RETURNING use it; on the others (MySQL) the id is generated here when data has none, the row is inserted, then re-selected by that id.
func (*Query[T]) Order ¶
Order sets the ORDER BY clause (e.g. "created_at DESC"). Validated against an identifier+direction allowlist.