tinyorm

package
v0.68.0 Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2026 License: AGPL-3.0 Imports: 12 Imported by: 0

README

tinyorm

tinyorm maps Go structs to tinySQL tables and keeps the common CRUD path small and explicit.

type User struct {
	ID    int    `gorm:"column:id;primaryKey"`
	Email string `gorm:"column:email;unique;not null;type:VARCHAR(120)"`
	State string `gorm:"column:state;default:'active'"`
	Secret string `gorm:"-"`
}

func (User) TableName() string { return "users" }

orm := tinyorm.New(db, "default")
if err := orm.AutoMigrate(ctx, User{}); err != nil { /* handle */ }
if err := orm.Create(ctx, &User{ID: 1, Email: "ada@example.test"}); err != nil { /* handle */ }

var user User
if err := orm.First(ctx, &user, "email = :email", map[string]any{"email": "ada@example.test"}); err != nil { /* handle */ }
user.State = "disabled"
if err := orm.Save(ctx, &user); err != nil { /* handle */ }

Besides GORM, tinyorm understands the tag formats used by sqlx (db), Bun, go-pg, and XORM. The compact db and tinyorm forms are supported as well:

ID    int    `db:"id,pk"`
Email string `tinyorm:"email,unique,notnull,type=VARCHAR(120)"`
State string `db:"state,default=active"`
// Bun and go-pg use the same comma-separated convention.
ID    int    `bun:"id,pk"`
Email string `pg:"email,unique,notnull"`

// XORM uses space-separated options and a quoted column name.
Name string `xorm:"varchar(120) notnull 'display_name'"`

Supported options are pk/primaryKey, unique, notnull/not null, type, default, and -. The accepted GORM-style equivalents are primaryKey, unique, not null, type:…, default:…, and -. XORM also recognizes its common quoted column name, pk, unique, notnull, default, and SQL type modifiers.

AutoMigrate is additive: it creates missing tables but never drops or rewrites existing schema. Create/Insert, First, FindByPK, Select, Save/Update, and DeleteByPK cover the basic model lifecycle.

Documentation

Overview

Package tinyorm provides a deliberately small ORM layer for tinySQL.

It focuses on the pieces that are useful for embedded tools: struct mapping, additive table creation, named parameters, and simple primary-key CRUD.

Index

Constants

This section is empty.

Variables

View Source
var ErrNotFound = fmt.Errorf("tinyorm: not found")

ErrNotFound is returned when FindByPK finds no matching row.

Functions

func BindNamed

func BindNamed(sql string, params any) (string, error)

BindNamed replaces :name and @name placeholders outside SQL string literals.

func Columns

func Columns(model any) ([]string, error)

Columns returns mapped column names for a model. It is useful for demos and generated SQL in applications that want to stay explicit.

Types

type DB

type DB struct {
	Raw    *tinysql.DB
	Tenant string
}

DB wraps a tinySQL database with a tenant for ORM operations.

func New

func New(db *tinysql.DB, tenant string) *DB

New creates an ORM handle. Empty tenant defaults to "default".

func (*DB) AutoMigrate

func (db *DB) AutoMigrate(ctx context.Context, models ...any) error

AutoMigrate creates tables for the supplied models when they do not exist. It is intentionally additive-only; it never drops or rewrites tables.

func (*DB) Create added in v0.50.0

func (db *DB) Create(ctx context.Context, model any) error

Create is an ORM-style alias for Insert.

func (*DB) DeleteByPK

func (db *DB) DeleteByPK(ctx context.Context, model any, pk any) error

DeleteByPK deletes one row by primary key.

func (*DB) Exec

func (db *DB) Exec(ctx context.Context, sql string, params any) (*tinysql.ResultSet, error)

Exec executes SQL after replacing :name or @name placeholders from params.

func (*DB) FindByPK

func (db *DB) FindByPK(ctx context.Context, dest any, pk any) error

FindByPK loads one row by primary key into dest, which must be a pointer to a struct.

func (*DB) First added in v0.50.0

func (db *DB) First(ctx context.Context, dest any, where string, params any) error

First loads the first row matching where into dest. The where argument may be empty or a SQL fragment without the WHERE keyword.

func (*DB) Insert

func (db *DB) Insert(ctx context.Context, model any) error

Insert inserts one struct value.

func (*DB) Query

func (db *DB) Query(ctx context.Context, sql string, params any) (*tinysql.ResultSet, error)

Query executes SQL after replacing :name or @name placeholders from params.

func (*DB) Save added in v0.50.0

func (db *DB) Save(ctx context.Context, model any) error

Save updates every mapped, non-primary-key field using the model's primary key. A primary key is required so Save never accidentally updates a table.

func (*DB) Select

func (db *DB) Select(ctx context.Context, dest any, where string, params any) error

Select loads rows into dest, which must be a pointer to a slice of structs. The where argument may be empty or a SQL fragment without the WHERE keyword.

func (*DB) Update added in v0.50.0

func (db *DB) Update(ctx context.Context, model any) error

Update is an ORM-style alias for Save.

type TableNamer

type TableNamer interface {
	TableName() string
}

TableNamer lets a model override the default snake_case table name.

Jump to

Keyboard shortcuts

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