orm

package module
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 13, 2026 License: MIT Imports: 10 Imported by: 5 modules, 5 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

ToGO

togo-framework/orm

marketplace pkg.go.dev MIT

Part of the togo framework.

Install

togo install togo-framework/orm

ToGO

orm

Driver-agnostic, Eloquent-style query builder for togo. It uses the dialect from the kernel (togo.Dialect); column/operator/ORDER BY inputs are validated against an allowlist while values are always parameterized.

togo install togo-framework/orm
models.Posts(app).Where("title", "ILIKE", "%go%").Order("created_at DESC").Get(ctx)

💎 Premium sponsors

togo is proudly sponsored by ID8 Media and One Studio.

ID8 Media        One Studio


Premium sponsors

ID8 Media  ·  One Studio

Support togo — become a sponsor.

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

Constants

This section is empty.

Variables

View Source
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

type Dialect = togo.Dialect

Dialect is the kernel's per-driver SQL dialect (re-exported for convenience).

func DialectFor

func DialectFor(driver string) Dialect

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 For

func For[T any](db *sql.DB, d Dialect, table string) *Query[T]

For starts a query against table, scanning into T.

func (*Query[T]) Create

func (q *Query[T]) Create(ctx context.Context, data map[string]any) (*T, error)

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]) Delete

func (q *Query[T]) Delete(ctx context.Context) error

Delete deletes matching rows.

func (*Query[T]) Find

func (q *Query[T]) Find(ctx context.Context, id any) (*T, error)

Find returns the row with the given id.

func (*Query[T]) First

func (q *Query[T]) First(ctx context.Context) (*T, error)

First returns the first matching row, or (nil, nil) if none.

func (*Query[T]) Get

func (q *Query[T]) Get(ctx context.Context) ([]T, error)

Get returns all matching rows.

func (*Query[T]) Limit

func (q *Query[T]) Limit(n int) *Query[T]

Limit sets LIMIT.

func (*Query[T]) Offset

func (q *Query[T]) Offset(n int) *Query[T]

Offset sets OFFSET.

func (*Query[T]) Order

func (q *Query[T]) Order(s string) *Query[T]

Order sets the ORDER BY clause (e.g. "created_at DESC"). Validated against an identifier+direction allowlist.

func (*Query[T]) Update

func (q *Query[T]) Update(ctx context.Context, data map[string]any) error

Update sets columns on matching rows.

func (*Query[T]) Where

func (q *Query[T]) Where(col, op string, val any) *Query[T]

Where adds a condition. op may be =, !=, <, >, LIKE, ILIKE, etc. The column and operator are validated (the value is always parameterized).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址