orm

package
v0.11.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: 6 Imported by: 0

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 struct {
	Placeholder func(n int) string // 1-based positional placeholder
	ILike       string             // case-insensitive LIKE operator
}

Dialect captures the per-driver SQL differences the builder needs.

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 (raw, e.g. "created_at DESC").

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.

Jump to

Keyboard shortcuts

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