orm

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 22, 2026 License: MIT Imports: 8 Imported by: 0

README

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)

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

This section is empty.

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 (RETURNING *).

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