orm

package module
v0.0.8 Latest Latest
Warning

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

Go to latest
Published: Feb 28, 2026 License: MIT Imports: 7 Imported by: 0

README

tinywasm/orm

Project Badges

Ultra-lightweight, strongly-typed ORM engineered for WebAssembly and backend environments.

Documentation

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrEmptyTable = fmt.Err("name", "table", "empty")

ErrEmptyTable is returned when TableName() returns an empty string.

View Source
var ErrNoTxSupport = fmt.Err("transaction", "not", "supported")

ErrNoTxSupport is returned by DB.Tx() when the executor does not implement TxExecutor.

View Source
var ErrNotFound = fmt.Err("record", "not", "found")

ErrNotFound is returned when ReadOne() finds no matching row.

View Source
var ErrValidation = fmt.Err("error", "validation")

ErrValidation is returned when validate() finds a mismatch.

Functions

func GenerateCodeForStruct added in v0.0.8

func GenerateCodeForStruct(structName string, goFile string)

GenerateCodeForStruct reads the Go File and generates the ORM implementations for a given struct name. This func is made public to allow easy programmatic testing.

func RunOrmcCLI added in v0.0.8

func RunOrmcCLI()

RunOrmcCLI is the entry point for the CLI tool.

Types

type Action

type Action int

Action represents the type of database operation.

const (
	ActionCreate Action = iota
	ActionReadOne
	ActionUpdate
	ActionDelete
	ActionReadAll
)

type Clause added in v0.0.8

type Clause struct {
	// contains filtered or unexported fields
}

Clause represents an intermediate state for building a query condition.

func (*Clause) Eq added in v0.0.8

func (c *Clause) Eq(value any) *QB

Eq creates an equality condition.

func (*Clause) Gt added in v0.0.8

func (c *Clause) Gt(value any) *QB

Gt creates a greater-than condition.

func (*Clause) Gte added in v0.0.8

func (c *Clause) Gte(value any) *QB

Gte creates a greater-than-or-equal condition.

func (*Clause) Like added in v0.0.8

func (c *Clause) Like(value any) *QB

Like creates a LIKE condition.

func (*Clause) Lt added in v0.0.8

func (c *Clause) Lt(value any) *QB

Lt creates a less-than condition.

func (*Clause) Lte added in v0.0.8

func (c *Clause) Lte(value any) *QB

Lte creates a less-than-or-equal condition.

func (*Clause) Neq added in v0.0.8

func (c *Clause) Neq(value any) *QB

Neq creates an inequality condition.

type Compiler added in v0.0.7

type Compiler interface {
	Compile(q Query, m Model) (Plan, error)
}

Compiler converts ORM queries into engine instructions.

type Condition

type Condition struct {
	// contains filtered or unexported fields
}

Condition represents a filter for a query. It is a sealed value type constructed via helper functions.

func Eq

func Eq(field string, value any) Condition

Eq creates a condition for checking equality.

func Gt

func Gt(field string, value any) Condition

Gt creates a condition for checking if a value is greater than another.

func Gte

func Gte(field string, value any) Condition

Gte creates a condition for checking if a value is greater than or equal to another.

func Like

func Like(field string, value any) Condition

Like creates a condition for checking if a value matches a pattern.

func Lt

func Lt(field string, value any) Condition

Lt creates a condition for checking if a value is less than another.

func Lte

func Lte(field string, value any) Condition

Lte creates a condition for checking if a value is less than or equal to another.

func Neq

func Neq(field string, value any) Condition

Neq creates a condition for checking inequality.

func Or

func Or(c Condition) Condition

Or creates a condition with OR logic.

func (Condition) Field

func (c Condition) Field() string

func (Condition) Logic

func (c Condition) Logic() string

func (Condition) Operator

func (c Condition) Operator() string

func (Condition) Value

func (c Condition) Value() any

type DB

type DB struct {
	// contains filtered or unexported fields
}

DB represents a database connection. Consumers instantiate it via New().

func New

func New(exec Executor, compiler Compiler) *DB

New creates a new DB instance.

func (*DB) Create

func (db *DB) Create(m Model) error

Create inserts a new model into the database.

func (*DB) Delete

func (db *DB) Delete(m Model, conds ...Condition) error

Delete deletes a model from the database.

func (*DB) Query

func (db *DB) Query(m Model) *QB

Query creates a new QB instance.

func (*DB) Tx

func (db *DB) Tx(fn func(tx *DB) error) error

Tx executes a function within a transaction.

func (*DB) Update

func (db *DB) Update(m Model, conds ...Condition) error

Update updates a model in the database.

type Executor added in v0.0.7

type Executor interface {
	Exec(query string, args ...any) error
	QueryRow(query string, args ...any) Scanner
	Query(query string, args ...any) (Rows, error)
}

Executor represents the database connection abstraction. It must remain compatible with sql.DB, sql.Tx, mocks, and WASM drivers.

type FieldInfo added in v0.0.8

type FieldInfo struct {
	Name       string
	ColumnName string
	IsPK       bool
}

type Model

type Model interface {
	TableName() string
	Columns() []string
	Values() []any
	Pointers() []any
}

Model represents a database model. Consumers implement this interface.

type Order

type Order struct {
	// contains filtered or unexported fields
}

Order represents a sort order for a query. It is a sealed value type constructed via QB.OrderBy().

func (Order) Column

func (o Order) Column() string

func (Order) Dir

func (o Order) Dir() string

type OrderClause added in v0.0.8

type OrderClause struct {
	// contains filtered or unexported fields
}

OrderClause represents an intermediate state for building an order by clause.

func (*OrderClause) Asc added in v0.0.8

func (o *OrderClause) Asc() *QB

Asc sets the order direction to ascending.

func (*OrderClause) Desc added in v0.0.8

func (o *OrderClause) Desc() *QB

Desc sets the order direction to descending.

type Plan added in v0.0.7

type Plan struct {
	Mode  Action
	Query string
	Args  []any
}

Plan describes how the Executor should run the operation.

type QB

type QB struct {
	// contains filtered or unexported fields
}

QB represents a query builder. Consumers hold a *QB reference in variables for incremental building.

func (*QB) GroupBy

func (qb *QB) GroupBy(columns ...string) *QB

GroupBy adds a group by clause to the query.

func (*QB) Limit

func (qb *QB) Limit(limit int) *QB

Limit sets the limit for the query.

func (*QB) Offset

func (qb *QB) Offset(offset int) *QB

Offset sets the offset for the query.

func (*QB) Or added in v0.0.8

func (qb *QB) Or() *QB

Or sets the next condition to use OR logic instead of AND.

func (*QB) OrderBy

func (qb *QB) OrderBy(column string) *OrderClause

OrderBy starts a new order clause for the given column.

func (*QB) ReadAll

func (qb *QB) ReadAll(new func() Model, onRow func(Model)) error

ReadAll executes the query and returns all results.

func (*QB) ReadOne

func (qb *QB) ReadOne() error

ReadOne executes the query and returns a single result.

func (*QB) Where

func (qb *QB) Where(column string) *Clause

Where starts a new condition clause for the given column.

type Query

type Query struct {
	Action     Action
	Table      string
	Columns    []string
	Values     []any
	Conditions []Condition
	OrderBy    []Order
	GroupBy    []string
	Limit      int
	Offset     int
}

Query represents a database query to be executed by an Executor. Planners read these fields to build Plans.

type Rows added in v0.0.7

type Rows interface {
	Next() bool
	Scan(dest ...any) error
	Close() error
	Err() error
}

Rows represents an iterator over query results.

type Scanner added in v0.0.7

type Scanner interface {
	Scan(dest ...any) error
}

Scanner represents a single row scanner.

type StructInfo added in v0.0.8

type StructInfo struct {
	Name        string
	TableName   string
	PackageName string
	Fields      []FieldInfo
}

type TxBoundExecutor added in v0.0.7

type TxBoundExecutor interface {
	Executor
	Commit() error
	Rollback() error
}

TxBoundExecutor represents an executor bound to a transaction.

type TxExecutor added in v0.0.7

type TxExecutor interface {
	Executor
	BeginTx() (TxBoundExecutor, error)
}

TxExecutor represents an executor that supports transactions.

Directories

Path Synopsis
cmd
ormc command

Jump to

Keyboard shortcuts

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