model

package
v1.0.14 Latest Latest
Warning

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

Go to latest
Published: Jul 16, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Aggregate added in v1.0.12

type Aggregate[T any] struct {
	// contains filtered or unexported fields
}

Aggregate is a type-safe builder for constructing aggregate queries on type T. T is the entity type used for table name resolution; result is scanned into a separate type.

func (*Aggregate[T]) Aggregate added in v1.0.12

func (a *Aggregate[T]) Aggregate(result any) error

Aggregate executes the aggregate query and scans the result into the provided pointer. For scalar results (e.g. *float64), a LIMIT 1 is applied automatically. For slice results (e.g. *[]T), all matching rows are returned.

Usage:

var total float64
err := model.Aggregate().Select("SUM(amount)").Where("status = ?", 1).Aggregate(&total)

var stats []CategoryStat
err := model.Aggregate().Select("category, SUM(amount) as total").Group("category").Aggregate(&stats)

func (*Aggregate[T]) Distinct added in v1.0.12

func (a *Aggregate[T]) Distinct(args ...any) *Aggregate[T]

Distinct adds a DISTINCT clause to the aggregate query. Usage:

a.Distinct().Select("category").Aggregate(&results)         // SELECT DISTINCT category ...
a.Distinct("category").Aggregate(&results)                  // SELECT DISTINCT category ...

func (*Aggregate[T]) Group added in v1.0.12

func (a *Aggregate[T]) Group(name string) *Aggregate[T]

Group adds a GROUP BY clause to the aggregate query. Usage: a.Group("category").Aggregate(&results)

func (*Aggregate[T]) Having added in v1.0.12

func (a *Aggregate[T]) Having(query any, args ...any) *Aggregate[T]

Having adds a HAVING condition to the aggregate query (used with Group). Usage: a.Group("category").Having("COUNT(*) > ?", 5).Aggregate(&results)

func (*Aggregate[T]) Joins added in v1.0.12

func (a *Aggregate[T]) Joins(query string) *Aggregate[T]

Joins adds a join clause to the aggregate query.

func (*Aggregate[T]) Order added in v1.0.12

func (a *Aggregate[T]) Order(query any) *Aggregate[T]

Order adds an ORDER BY clause to the aggregate query.

func (*Aggregate[T]) Select added in v1.0.12

func (a *Aggregate[T]) Select(query any, args ...any) *Aggregate[T]

Select specifies select columns or aggregate expressions for the query. Usage: a.Select("category, SUM(amount) as total").Aggregate(&results)

func (*Aggregate[T]) Where added in v1.0.12

func (a *Aggregate[T]) Where(query any, args ...any) *Aggregate[T]

Where adds a WHERE condition to the aggregate query.

func (*Aggregate[T]) WithContext added in v1.0.12

func (a *Aggregate[T]) WithContext(ctx context.Context) *Aggregate[T]

WithContext returns a shallow copy of the aggregate builder with the given context. The original instance is unchanged; safe for concurrent use.

type Delete

type Delete[T any] struct {
	// contains filtered or unexported fields
}

Delete is a type-safe delete builder for constructing delete operations on type T.

func (*Delete[T]) Delete added in v0.2.1

func (d *Delete[T]) Delete() error

Delete executes the delete operation for records matching the WHERE conditions.

func (*Delete[T]) Where

func (d *Delete[T]) Where(query interface{}, args ...interface{}) *Delete[T]

Where adds a WHERE condition to the delete operation.

func (*Delete[T]) WithContext added in v0.9.4

func (d *Delete[T]) WithContext(ctx context.Context) *Delete[T]

WithContext returns a shallow copy of the delete builder with the given context. The original instance is unchanged; safe for concurrent use.

type EntryModel

type EntryModel[T any, PK PKConstraint] struct {
	*Model[T]
}

EntryModel extends Model with primary-key-based lookup methods. PK is the type of the primary key column.

func NewEntryModel

func NewEntryModel[T any, PK PKConstraint](db *db.DB, tableName string) *EntryModel[T, PK]

NewEntryModel creates a new EntryModel for the given table.

func (*EntryModel[T, PK]) DeleteByPK added in v0.9.1

func (a *EntryModel[T, PK]) DeleteByPK(id PK) error

DeleteByPK deletes a record by its primary key.

func (*EntryModel[T, PK]) FindAll

func (a *EntryModel[T, PK]) FindAll() ([]T, error)

FindAll returns all records in the table.

func (*EntryModel[T, PK]) FindAllByPK added in v0.9.1

func (a *EntryModel[T, PK]) FindAllByPK(id ...PK) ([]T, error)

FindAllByPK finds all records matching the given primary keys.

func (*EntryModel[T, PK]) FindAllByPKWithPreload added in v0.9.1

func (a *EntryModel[T, PK]) FindAllByPKWithPreload(ids []PK, preloads ...string) ([]T, error)

FindAllByPKWithPreload finds all records by PKs with preloaded associations Usage: model.FindAllByPKWithPreload([]uint{1, 2, 3}, "Profile", "Role")

func (*EntryModel[T, PK]) FindAllWithPreload added in v0.5.1

func (a *EntryModel[T, PK]) FindAllWithPreload(preloads ...string) ([]T, error)

FindAllWithPreload finds all records with preloaded associations Usage: model.FindAllWithPreload("Profile", "Role")

func (*EntryModel[T, PK]) FindByPK added in v0.9.1

func (a *EntryModel[T, PK]) FindByPK(id PK) (T, error)

FindByPK finds a single record by its primary key.

func (*EntryModel[T, PK]) FindByPKWithPreload added in v0.9.1

func (a *EntryModel[T, PK]) FindByPKWithPreload(id PK, preloads ...string) (T, error)

FindByPKWithPreload finds a record by PK with preloaded associations Usage: model.FindByPKWithPreload(id, "Profile", "Role")

func (*EntryModel[T, PK]) FindOne

func (a *EntryModel[T, PK]) FindOne(query interface{}, args ...interface{}) (T, error)

FindOne finds a single record matching the given query condition.

func (*EntryModel[T, PK]) FindOneWithPreload added in v0.5.1

func (a *EntryModel[T, PK]) FindOneWithPreload(query interface{}, args []interface{}, preloads ...string) (T, error)

FindOneWithPreload finds one record with preloaded associations Usage: model.FindOneWithPreload("status = ?", 1, "Profile", "Role")

func (*EntryModel[T, PK]) GetTableName

func (a *EntryModel[T, PK]) GetTableName() string

GetTableName returns the database table name for this model.

func (*EntryModel[T, PK]) NewEntryModel

func (a *EntryModel[T, PK]) NewEntryModel(db *db.DB) *EntryModel[T, PK]

NewEntryModel creates a new EntryModel with the given database connection, reusing the same table name.

func (*EntryModel[T, PK]) Page

func (a *EntryModel[T, PK]) Page(page *util.Page) ([]T, int, error)

Page returns a paginated list of records ordered by primary key descending.

func (*EntryModel[T, PK]) PageForWeb added in v0.1.3

func (a *EntryModel[T, PK]) PageForWeb(page *util.Page) (*util.PageAble[T], error)

PageForWeb returns a paginated response suitable for web API responses.

func (*EntryModel[T, PK]) QueryPage

func (a *EntryModel[T, PK]) QueryPage(page *util.Page, query interface{}, args ...interface{}) ([]T, int, error)

QueryPage returns a paginated list matching the given query condition.

func (*EntryModel[T, PK]) UpdateByPK added in v0.9.1

func (a *EntryModel[T, PK]) UpdateByPK(t T) error

UpdateByPK updates a record using the primary key value embedded in the entity.

func (*EntryModel[T, PK]) UpdateColumn

func (a *EntryModel[T, PK]) UpdateColumn(id PK, column string, value interface{}) error

UpdateColumn updates a single column value for the record with the given primary key.

func (*EntryModel[T, PK]) UpdateForMap

func (a *EntryModel[T, PK]) UpdateForMap(id PK, data map[string]interface{}) error

UpdateForMap updates columns from a map for the record with the given primary key.

func (*EntryModel[T, PK]) WithContext added in v0.9.4

func (a *EntryModel[T, PK]) WithContext(ctx context.Context) *EntryModel[T, PK]

WithContext returns a shallow copy of the EntryModel with the given context, preserving the correct generic type. The original instance is unchanged.

type Model

type Model[T any] struct {
	// contains filtered or unexported fields
}

Model is a generic database model that provides CRUD operations for type T. It wraps GORM with type-safe query building and table management.

func NewModel

func NewModel[T any](db *db.DB, tableName string) *Model[T]

NewModel creates a new Model instance for the given table name.

func (*Model[T]) Aggregate added in v1.0.12

func (a *Model[T]) Aggregate() *Aggregate[T]

Aggregate returns a new Aggregate builder for constructing aggregate queries (SUM, AVG, COUNT, etc.). Usage: model.Aggregate().Select("SUM(amount)").Where("status = ?", 1).Aggregate(&total)

func (*Model[T]) CreateTable

func (a *Model[T]) CreateTable() error

CreateTable creates the database table for this model if it does not already exist.

func (*Model[T]) CreateWithPk added in v0.1.4

func (a *Model[T]) CreateWithPk(entry T, keyName string, kind reflect.Kind) (any, error)

CreateWithPk creates a record and returns the generated primary key

func (*Model[T]) Delete

func (a *Model[T]) Delete() *Delete[T]

Delete returns a new Delete builder for constructing type-safe delete operations.

func (*Model[T]) DeleteTable

func (a *Model[T]) DeleteTable() error

DeleteTable deletes all rows from the table (does not drop the table structure).

func (*Model[T]) DropTable added in v1.0.4

func (a *Model[T]) DropTable() error

DropTable drops the database table for this model, removing the table structure and all data.

func (*Model[T]) GetPkColumn added in v0.6.1

func (a *Model[T]) GetPkColumn() (string, error)

GetPkColumn returns the primary key column name for this model.

func (*Model[T]) GetTableName

func (a *Model[T]) GetTableName() string

GetTableName returns the database table name for this model.

func (*Model[T]) IsExist

func (a *Model[T]) IsExist() (bool, error)

IsExist reports whether the table for this model exists in the database.

func (*Model[T]) Query

func (a *Model[T]) Query() *Query[T]

Query returns a new Query builder for constructing type-safe read queries.

func (*Model[T]) Save

func (a *Model[T]) Save(entry T) error

Save creates or updates a single record in the database.

func (*Model[T]) SaveForMap added in v0.1.4

func (a *Model[T]) SaveForMap(mapValue map[string]any) error

SaveForMap creates a record from a map of column-value pairs.

func (*Model[T]) SaveForMapWithPk added in v0.1.4

func (a *Model[T]) SaveForMapWithPk(mapValue map[string]any, keyName string) (any, error)

SaveForMapWithPk saves a record from a map and returns the generated primary key

func (*Model[T]) SaveForMapWithUintPk added in v0.1.4

func (a *Model[T]) SaveForMapWithUintPk(mapValue map[string]any, keyName string) (uint, error)

SaveForMapWithUintPk saves a record from a map and returns the generated uint primary key.

func (*Model[T]) Saves

func (a *Model[T]) Saves(entry []T) error

Saves creates multiple records in a single batch operation.

func (*Model[T]) SavesForMap added in v0.1.4

func (a *Model[T]) SavesForMap(mapValues []map[string]any) error

SavesForMap creates multiple records from a slice of column-value maps.

func (*Model[T]) Update

func (a *Model[T]) Update() *Update[T]

Update returns a new Update builder for constructing type-safe update operations.

func (*Model[T]) WithContext added in v0.9.4

func (a *Model[T]) WithContext(ctx context.Context) *Model[T]

WithContext returns a shallow copy of the model with the given context. All database operations on the returned model will propagate the context. The original instance is unchanged; safe for concurrent use.

type PKConstraint added in v0.9.1

type PKConstraint interface {
	~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~int | ~int8 | ~int16 | ~int32 | ~int64 | ~string
}

PKConstraint defines the set of types that can be used as primary keys.

type Query

type Query[T any] struct {
	// contains filtered or unexported fields
}

Query is a type-safe query builder for constructing read operations on type T.

func (*Query[T]) All

func (q *Query[T]) All() ([]T, error)

All returns all records matching the query conditions.

func (*Query[T]) Count added in v0.1.3

func (q *Query[T]) Count() (int, error)

Count returns the number of records matching the query conditions.

func (*Query[T]) Distinct added in v1.0.12

func (q *Query[T]) Distinct(args ...any) *Query[T]

Distinct adds a DISTINCT clause to the query. Usage:

query.Distinct().All()                        // SELECT DISTINCT * ...
query.Distinct("name", "age").All()           // SELECT DISTINCT name, age ...
query.Select("name", "age").Distinct().All()  // SELECT DISTINCT name, age ... (via Select)

func (*Query[T]) Exec added in v0.5.1

func (q *Query[T]) Exec(sql string, args ...interface{}) ([]T, error)

Exec executes a raw SQL query with the accumulated WHERE conditions.

func (*Query[T]) ExecPage deprecated added in v0.5.1

func (q *Query[T]) ExecPage(page *util.Page, sql string, args ...interface{}) ([]T, int, error)

ExecPage executes a paginated raw SQL query with the accumulated WHERE conditions.

Deprecated: This method is not recommended for use. Use Page() or PageForWeb() instead.

func (*Query[T]) Group added in v1.0.12

func (q *Query[T]) Group(name string) *Query[T]

Group adds a GROUP BY clause to the query. Usage: query.Group("category").All()

func (*Query[T]) Having added in v1.0.12

func (q *Query[T]) Having(query any, args ...any) *Query[T]

Having adds a HAVING condition to the query (used with Group). Usage: query.Group("category").Having("COUNT(*) > ?", 5).All()

func (*Query[T]) Joins added in v0.5.1

func (q *Query[T]) Joins(query string) *Query[T]

Joins adds a join clause for association loading (GORM join support) Usage: query.Joins("Profile").All()

func (*Query[T]) List

func (q *Query[T]) List(size int) ([]T, error)

List returns up to size records matching the query conditions.

func (*Query[T]) ListPage

func (q *Query[T]) ListPage(page *util.Page) ([]T, error)

ListPage returns a page of records matching the query conditions.

func (*Query[T]) One

func (q *Query[T]) One() (T, error)

One returns a single record matching the query conditions.

func (*Query[T]) Order

func (q *Query[T]) Order(query interface{}) *Query[T]

Order adds an ORDER BY clause to the query.

func (*Query[T]) Page

func (q *Query[T]) Page(page *util.Page) ([]T, int, error)

Page returns a paginated list with total count. Both queries run in a single transaction for consistency.

func (*Query[T]) PageForWeb added in v0.1.3

func (q *Query[T]) PageForWeb(page *util.Page) (*util.PageAble[T], error)

PageForWeb returns a paginated web response suitable for API responses.

func (*Query[T]) Preload added in v0.5.1

func (q *Query[T]) Preload(query string) *Query[T]

Preload adds a preload clause for eager loading associations (GORM foreign key support) Usage: query.Preload("Profile").Preload("Role").All() Supports nested preloading: query.Preload("Profile.Addresses")

func (*Query[T]) Select added in v1.0.12

func (q *Query[T]) Select(query any, args ...any) *Query[T]

Select specifies select columns for the query. Usage: query.Select("name, age").All()

func (*Query[T]) Size

func (q *Query[T]) Size(size int) ([]T, int, error)

Size returns up to size records with total count.

func (*Query[T]) Where

func (q *Query[T]) Where(query any, args ...any) *Query[T]

Where adds a WHERE condition to the query.

func (*Query[T]) WithContext added in v0.9.4

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

WithContext returns a shallow copy of the query builder with the given context. The original instance is unchanged; safe for concurrent use.

type Transaction

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

Transaction wraps database transaction execution.

func NewTransaction

func NewTransaction(db *db.DB) *Transaction

NewTransaction creates a new Transaction with the given database connection.

func (*Transaction) Exec

func (t *Transaction) Exec(fc func(tx *db.DB) error) error

Exec executes the given function within a database transaction. The transaction is committed if fc returns nil, rolled back otherwise.

type Update

type Update[T any] struct {
	// contains filtered or unexported fields
}

Update is a type-safe update builder for constructing update operations on type T.

func (*Update[T]) Set added in v0.2.1

func (u *Update[T]) Set(s string, value any) *UpdateSet

Set begins a chain of column-value updates.

func (*Update[T]) Update added in v0.2.1

func (u *Update[T]) Update(t T) error

Update applies the entity values to records matching the WHERE conditions.

func (*Update[T]) UpdateColumn added in v0.2.1

func (u *Update[T]) UpdateColumn(column string, value any) error

UpdateColumn updates a single column for records matching the WHERE conditions.

func (*Update[T]) UpdateForMap added in v0.2.1

func (u *Update[T]) UpdateForMap(mapValue map[string]any) error

UpdateForMap updates records matching the WHERE conditions using a column-value map.

func (*Update[T]) Where

func (u *Update[T]) Where(query interface{}, args ...interface{}) *Update[T]

Where adds a WHERE condition to the update operation.

func (*Update[T]) WithContext added in v0.9.4

func (u *Update[T]) WithContext(ctx context.Context) *Update[T]

WithContext returns a shallow copy of the update builder with the given context. The original instance is unchanged; safe for concurrent use.

type UpdateSet added in v0.1.3

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

UpdateSet accumulates column-value pairs for a batch update.

func (*UpdateSet) Exec added in v0.1.3

func (w *UpdateSet) Exec() error

Exec executes the accumulated updates.

func (*UpdateSet) Set added in v0.1.3

func (w *UpdateSet) Set(s string, value any) *UpdateSet

Set adds a column-value pair to the update set.

Jump to

Keyboard shortcuts

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