Documentation
¶
Index ¶
- type Aggregate
- func (a *Aggregate[T]) Aggregate(result any) error
- func (a *Aggregate[T]) Distinct(args ...any) *Aggregate[T]
- func (a *Aggregate[T]) Group(name string) *Aggregate[T]
- func (a *Aggregate[T]) Having(query any, args ...any) *Aggregate[T]
- func (a *Aggregate[T]) Joins(query string) *Aggregate[T]
- func (a *Aggregate[T]) Order(query any) *Aggregate[T]
- func (a *Aggregate[T]) Select(query any, args ...any) *Aggregate[T]
- func (a *Aggregate[T]) Where(query any, args ...any) *Aggregate[T]
- func (a *Aggregate[T]) WithContext(ctx context.Context) *Aggregate[T]
- type Delete
- type EntryModel
- func (a *EntryModel[T, PK]) DeleteByPK(id PK) error
- func (a *EntryModel[T, PK]) FindAll() ([]T, error)
- func (a *EntryModel[T, PK]) FindAllByPK(id ...PK) ([]T, error)
- func (a *EntryModel[T, PK]) FindAllByPKWithPreload(ids []PK, preloads ...string) ([]T, error)
- func (a *EntryModel[T, PK]) FindAllWithPreload(preloads ...string) ([]T, error)
- func (a *EntryModel[T, PK]) FindByPK(id PK) (T, error)
- func (a *EntryModel[T, PK]) FindByPKWithPreload(id PK, preloads ...string) (T, error)
- func (a *EntryModel[T, PK]) FindOne(query interface{}, args ...interface{}) (T, error)
- func (a *EntryModel[T, PK]) FindOneWithPreload(query interface{}, args []interface{}, preloads ...string) (T, error)
- func (a *EntryModel[T, PK]) GetTableName() string
- func (a *EntryModel[T, PK]) NewEntryModel(db *db.DB) *EntryModel[T, PK]
- func (a *EntryModel[T, PK]) Page(page *util.Page) ([]T, int, error)
- func (a *EntryModel[T, PK]) PageForWeb(page *util.Page) (*util.PageAble[T], error)
- func (a *EntryModel[T, PK]) QueryPage(page *util.Page, query interface{}, args ...interface{}) ([]T, int, error)
- func (a *EntryModel[T, PK]) UpdateByPK(t T) error
- func (a *EntryModel[T, PK]) UpdateColumn(id PK, column string, value interface{}) error
- func (a *EntryModel[T, PK]) UpdateForMap(id PK, data map[string]interface{}) error
- func (a *EntryModel[T, PK]) WithContext(ctx context.Context) *EntryModel[T, PK]
- type Model
- func (a *Model[T]) Aggregate() *Aggregate[T]
- func (a *Model[T]) CreateTable() error
- func (a *Model[T]) CreateWithPk(entry T, keyName string, kind reflect.Kind) (any, error)
- func (a *Model[T]) Delete() *Delete[T]
- func (a *Model[T]) DeleteTable() error
- func (a *Model[T]) DropTable() error
- func (a *Model[T]) GetPkColumn() (string, error)
- func (a *Model[T]) GetTableName() string
- func (a *Model[T]) IsExist() (bool, error)
- func (a *Model[T]) Query() *Query[T]
- func (a *Model[T]) Save(entry T) error
- func (a *Model[T]) SaveForMap(mapValue map[string]any) error
- func (a *Model[T]) SaveForMapWithPk(mapValue map[string]any, keyName string) (any, error)
- func (a *Model[T]) SaveForMapWithUintPk(mapValue map[string]any, keyName string) (uint, error)
- func (a *Model[T]) Saves(entry []T) error
- func (a *Model[T]) SavesForMap(mapValues []map[string]any) error
- func (a *Model[T]) Update() *Update[T]
- func (a *Model[T]) WithContext(ctx context.Context) *Model[T]
- type PKConstraint
- type Query
- func (q *Query[T]) All() ([]T, error)
- func (q *Query[T]) Count() (int, error)
- func (q *Query[T]) Distinct(args ...any) *Query[T]
- func (q *Query[T]) Exec(sql string, args ...interface{}) ([]T, error)
- func (q *Query[T]) ExecPage(page *util.Page, sql string, args ...interface{}) ([]T, int, error)deprecated
- func (q *Query[T]) Group(name string) *Query[T]
- func (q *Query[T]) Having(query any, args ...any) *Query[T]
- func (q *Query[T]) Joins(query string) *Query[T]
- func (q *Query[T]) List(size int) ([]T, error)
- func (q *Query[T]) ListPage(page *util.Page) ([]T, error)
- func (q *Query[T]) One() (T, error)
- func (q *Query[T]) Order(query interface{}) *Query[T]
- func (q *Query[T]) Page(page *util.Page) ([]T, int, error)
- func (q *Query[T]) PageForWeb(page *util.Page) (*util.PageAble[T], error)
- func (q *Query[T]) Preload(query string) *Query[T]
- func (q *Query[T]) Select(query any, args ...any) *Query[T]
- func (q *Query[T]) Size(size int) ([]T, int, error)
- func (q *Query[T]) Where(query any, args ...any) *Query[T]
- func (q *Query[T]) WithContext(ctx context.Context) *Query[T]
- type Transaction
- type Update
- func (u *Update[T]) Set(s string, value any) *UpdateSet
- func (u *Update[T]) Update(t T) error
- func (u *Update[T]) UpdateColumn(column string, value any) error
- func (u *Update[T]) UpdateForMap(mapValue map[string]any) error
- func (u *Update[T]) Where(query interface{}, args ...interface{}) *Update[T]
- func (u *Update[T]) WithContext(ctx context.Context) *Update[T]
- type UpdateSet
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
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
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
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
Having adds a HAVING condition to the aggregate query (used with Group). Usage: a.Group("category").Having("COUNT(*) > ?", 5).Aggregate(&results)
func (*Aggregate[T]) Select ¶ added in v1.0.12
Select specifies select columns or aggregate expressions for the query. Usage: a.Select("category, SUM(amount) as total").Aggregate(&results)
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
Delete executes the delete operation for records matching the WHERE conditions.
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
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 (*Model[T]) Aggregate ¶ added in v1.0.12
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 ¶
CreateTable creates the database table for this model if it does not already exist.
func (*Model[T]) CreateWithPk ¶ added in v0.1.4
CreateWithPk creates a record and returns the generated primary key
func (*Model[T]) Delete ¶
Delete returns a new Delete builder for constructing type-safe delete operations.
func (*Model[T]) DeleteTable ¶
DeleteTable deletes all rows from the table (does not drop the table structure).
func (*Model[T]) DropTable ¶ added in v1.0.4
DropTable drops the database table for this model, removing the table structure and all data.
func (*Model[T]) GetPkColumn ¶ added in v0.6.1
GetPkColumn returns the primary key column name for this model.
func (*Model[T]) GetTableName ¶
GetTableName returns the database table name for this model.
func (*Model[T]) SaveForMap ¶ added in v0.1.4
SaveForMap creates a record from a map of column-value pairs.
func (*Model[T]) SaveForMapWithPk ¶ added in v0.1.4
SaveForMapWithPk saves a record from a map and returns the generated primary key
func (*Model[T]) SaveForMapWithUintPk ¶ added in v0.1.4
SaveForMapWithUintPk saves a record from a map and returns the generated uint primary key.
func (*Model[T]) SavesForMap ¶ added in v0.1.4
SavesForMap creates multiple records from a slice of column-value maps.
func (*Model[T]) Update ¶
Update returns a new Update builder for constructing type-safe update operations.
func (*Model[T]) WithContext ¶ added in v0.9.4
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]) Count ¶ added in v0.1.3
Count returns the number of records matching the query conditions.
func (*Query[T]) Distinct ¶ added in v1.0.12
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
Exec executes a raw SQL query with the accumulated WHERE conditions.
func (*Query[T]) ExecPage
deprecated
added in
v0.5.1
func (*Query[T]) Group ¶ added in v1.0.12
Group adds a GROUP BY clause to the query. Usage: query.Group("category").All()
func (*Query[T]) Having ¶ added in v1.0.12
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
Joins adds a join clause for association loading (GORM join support) Usage: query.Joins("Profile").All()
func (*Query[T]) Page ¶
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
PageForWeb returns a paginated web response suitable for API responses.
func (*Query[T]) Preload ¶ added in v0.5.1
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
Select specifies select columns for the query. Usage: query.Select("name, age").All()
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.
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]) Update ¶ added in v0.2.1
Update applies the entity values to records matching the WHERE conditions.
func (*Update[T]) UpdateColumn ¶ added in v0.2.1
UpdateColumn updates a single column for records matching the WHERE conditions.
func (*Update[T]) UpdateForMap ¶ added in v0.2.1
UpdateForMap updates records matching the WHERE conditions using a column-value map.