gormx

package module
v1.5.3 Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2024 License: MIT Imports: 14 Imported by: 0

README

GORMX - GORM Utils

Functions

package gormx // import "github.com/go-zoox/gormx"

var Version = "1.0.0"
func Create[T any](one *T) (*T, error)
func Delete[T any](where map[any]any) (err error)
func DeleteOneByID[T any](id uint) (err error)
func Exists[T any](where map[any]any) (bool, error)
func Find[T any](page, pageSize uint, where *Where, orderBy *OrderBy) (data []*T, total int64, err error)
func FindAll[T any](where *Where, orderBy *OrderBy) (data []*T, err error)
func FindByID[T any](id uint) (*T, error)
func FindOne[T any](where map[any]any) (*T, error)
func FindOneAndDelete[T any](where map[any]any) (*T, error)
func FindOneAndUpdate[T any](where map[any]any, callback func(*T)) (*T, error)
func FindOneByIDAndDelete[T any](id uint) (*T, error)
func FindOneByIDAndUpdate[T any](id uint, callback func(*T)) (*T, error)
func FindOneByIDOrCreate[T any](id uint, callback func(*T)) (*T, error)
func FindOneOrCreate[T any](where map[any]any, callback func(*T)) (*T, error)
func GetDB() *gorm.DB
func GetMany[T any](ids []uint) (data []*T, err error)
func GetOrCreate[T any](where map[any]any, callback func(*T)) (*T, error)
func Has[T any](where map[string]any) bool
func List[T any](page, pageSize uint, where *Where, orderBy *OrderBy) (data []*T, total int64, err error)
func ListALL[T any](where *Where, orderBy *OrderBy) (data []*T, err error)
func LoadDB(engine string, dsn string) (err error)
func Retrieve[T any](id uint) (*T, error)
func Save[T any](one *T) error
func Update[T any](id uint, uc func(*T)) (err error)
type OrderBy []OrderByOne
type OrderByOne struct{ ... }
type Page struct{ ... }
type SetWhereOptions struct{ ... }
type Where []WhereOne
type WhereOne struct{ ... }

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrRecordNotFound record not found error
	ErrRecordNotFound = gorm.ErrRecordNotFound
	// ErrInvalidTransaction invalid transaction when you are trying to `Commit` or `Rollback`
	ErrInvalidTransaction = gorm.ErrInvalidTransaction
	// ErrNotImplemented not implemented
	ErrNotImplemented = gorm.ErrNotImplemented
	// ErrMissingWhereClause missing where clause
	ErrMissingWhereClause = gorm.ErrMissingWhereClause
	// ErrUnsupportedRelation unsupported relations
	ErrUnsupportedRelation = gorm.ErrUnsupportedRelation
	// ErrPrimaryKeyRequired primary keys required
	ErrPrimaryKeyRequired = gorm.ErrPrimaryKeyRequired
	// ErrModelValueRequired model value required
	ErrModelValueRequired = gorm.ErrModelValueRequired
	// ErrModelAccessibleFieldsRequired model accessible fields required
	ErrModelAccessibleFieldsRequired = gorm.ErrModelAccessibleFieldsRequired
	// ErrSubQueryRequired sub query required
	ErrSubQueryRequired = gorm.ErrSubQueryRequired
	// ErrInvalidData unsupported data
	ErrInvalidData = gorm.ErrInvalidData
	// ErrUnsupportedDriver unsupported driver
	ErrUnsupportedDriver = gorm.ErrUnsupportedDriver
	// ErrRegistered registered
	ErrRegistered = gorm.ErrRegistered
	// ErrInvalidField invalid field
	ErrInvalidField = gorm.ErrInvalidField
	// ErrEmptySlice empty slice found
	ErrEmptySlice = gorm.ErrEmptySlice
	// ErrDryRunModeUnsupported dry run mode unsupported
	ErrDryRunModeUnsupported = gorm.ErrDryRunModeUnsupported
	// ErrInvalidDB invalid db
	ErrInvalidDB = gorm.ErrInvalidDB
	// ErrInvalidValue invalid value
	ErrInvalidValue = gorm.ErrInvalidValue
	// ErrInvalidValueOfLength invalid values do not match length
	ErrInvalidValueOfLength = gorm.ErrInvalidValueOfLength
	// ErrPreloadNotAllowed preload is not allowed when count is used
	ErrPreloadNotAllowed = gorm.ErrPreloadNotAllowed
	// ErrDuplicatedKey occurs when there is a unique key constraint violation
	ErrDuplicatedKey = gorm.ErrDuplicatedKey
	// ErrForeignKeyViolated occurs when there is a foreign key constraint violation
	ErrForeignKeyViolated = gorm.ErrForeignKeyViolated
)
View Source
var Version = "1.5.3"

Version is the version of this package.

Functions

func Connect added in v1.5.0

func Connect(engine string, dsn string, opts ...func(*LoadDBOptions)) (db *gorm.DB, err error)

Connect connects the database

func Count added in v1.2.0

func Count[T any](where *Where) (count int64, err error)

Count counts records.

func CountALL added in v1.2.0

func CountALL[T any]() (total int64, err error)

CountALL counts all records.

func Create

func Create[T any](one *T) (*T, error)

Create creates a record.

func Delete

func Delete[T any](where map[any]any) (err error)

Delete deletes the record from database by the given conditions.

func DeleteOneByID

func DeleteOneByID[T any](id uint) (err error)

DeleteOneByID deletes one record by id.

func Exists

func Exists[T any](where map[any]any) (bool, error)

Exists returns true if the record exists.

func Find

func Find[T any](page, pageSize uint, where *Where, orderBy *OrderBy) (data []*T, total int64, err error)

Find finds records.

func FindAll

func FindAll[T any](where *Where, orderBy *OrderBy) (data []*T, err error)

FindAll finds all records.

func FindByID

func FindByID[T any](id uint) (*T, error)

FindByID finds a record by id.

func FindOne

func FindOne[T any](where map[any]any) (*T, error)

FindOne finds one record.

func FindOneAndDelete

func FindOneAndDelete[T any](where map[any]any) (*T, error)

FindOneAndDelete finds one record and delete it.

func FindOneAndUpdate

func FindOneAndUpdate[T any](where map[any]any, callback func(*T)) (*T, error)

FindOneAndUpdate finds one and update it.

func FindOneByIDAndDelete

func FindOneByIDAndDelete[T any](id uint) (*T, error)

FindOneByIDAndDelete finds one record by id and delete it.

func FindOneByIDAndUpdate

func FindOneByIDAndUpdate[T any](id uint, callback func(*T)) (*T, error)

FindOneByIDAndUpdate finds one by id and update.

func FindOneByIDOrCreate

func FindOneByIDOrCreate[T any](id uint, callback func(*T)) (*T, error)

FindOneByIDOrCreate finds one record by id or create a new one.

func FindOneOrCreate

func FindOneOrCreate[T any](where map[any]any, callback func(*T)) (*T, error)

FindOneOrCreate find one or create one.

func FindOneWithComplexConditions added in v1.4.2

func FindOneWithComplexConditions[T any](where *Where, orderBy *OrderBy) (*T, error)

FindOneWithComplexConditions finds one record.

func Get added in v1.4.0

func Get[T any](id string) T

Get returns the model by the given id.

func GetDB

func GetDB() *gorm.DB

GetDB returns the gorm.DB instance

func GetDSN added in v1.3.6

func GetDSN() string

GetDSN returns the database DSN

func GetEngine added in v1.3.6

func GetEngine() string

GetEngine returns the database engine

func GetMany

func GetMany[T any](ids []uint) (data []*T, err error)

GetMany gets many records by ids.

func GetOrCreate

func GetOrCreate[T any](where map[any]any, callback func(*T)) (*T, error)

GetOrCreate gets or creates a record.

func Has

func Has[T any](where map[string]any) bool

Has returns true if the record exists.

func IsDryRunModeUnsupportedError added in v1.0.9

func IsDryRunModeUnsupportedError(err error) bool

IsDryRunModeUnsupportedError returns true if err is related to dry run mode unsupported error

func IsDuplicatedKeyError added in v1.0.9

func IsDuplicatedKeyError(err error) bool

IsDuplicatedKeyError returns true if err is related to duplicated key error

func IsEmptySliceError added in v1.0.9

func IsEmptySliceError(err error) bool

IsEmptySliceError returns true if err is related to empty slice error

func IsForeignKeyViolatedError added in v1.0.9

func IsForeignKeyViolatedError(err error) bool

IsForeignKeyViolatedError returns true if err is related to foreign key violated error

func IsInvalidDBError added in v1.0.9

func IsInvalidDBError(err error) bool

IsInvalidDBError returns true if err is related to invalid db error

func IsInvalidDataError added in v1.0.9

func IsInvalidDataError(err error) bool

IsInvalidDataError returns true if err is related to invalid data error

func IsInvalidFieldError added in v1.0.9

func IsInvalidFieldError(err error) bool

IsInvalidFieldError returns true if err is related to invalid field error

func IsInvalidTransactionError added in v1.0.9

func IsInvalidTransactionError(err error) bool

IsInvalidTransactionError returns true if err is related to invalid transaction error

func IsInvalidValueError added in v1.0.9

func IsInvalidValueError(err error) bool

IsInvalidValueError returns true if err is related to invalid value error

func IsInvalidValueOfLengthError added in v1.0.9

func IsInvalidValueOfLengthError(err error) bool

IsInvalidValueOfLengthError returns true if err is related to invalid value of length error

func IsMissingWhereClauseError added in v1.0.9

func IsMissingWhereClauseError(err error) bool

IsMissingWhereClauseError returns true if err is related to missing where clause error

func IsModelAccessibleFieldsRequiredError added in v1.0.9

func IsModelAccessibleFieldsRequiredError(err error) bool

IsModelAccessibleFieldsRequiredError returns true if err is related to model accessible fields required error

func IsModelValueRequiredError added in v1.0.9

func IsModelValueRequiredError(err error) bool

IsModelValueRequiredError returns true if err is related to model value required error

func IsNotImplementedError added in v1.0.9

func IsNotImplementedError(err error) bool

IsNotImplementedError returns true if err is related to not implemented error

func IsPreloadNotAllowedError added in v1.0.9

func IsPreloadNotAllowedError(err error) bool

IsPreloadNotAllowedError returns true if err is related to preload not allowed error

func IsPrimaryKeyRequiredError added in v1.0.9

func IsPrimaryKeyRequiredError(err error) bool

IsPrimaryKeyRequiredError returns true if err is related to primary key required error

func IsRecordNotFoundError added in v1.0.9

func IsRecordNotFoundError(err error) bool

IsRecordNotFoundError returns true if err is related to record not found error

func IsRegisteredError added in v1.0.9

func IsRegisteredError(err error) bool

IsRegisteredError returns true if err is related to registered error

func IsSubQueryRequiredError added in v1.0.9

func IsSubQueryRequiredError(err error) bool

IsSubQueryRequiredError returns true if err is related to sub query required error

func IsUnsupportedDriverError added in v1.0.9

func IsUnsupportedDriverError(err error) bool

IsUnsupportedDriverError returns true if err is related to unsupported driver error

func IsUnsupportedRelationError added in v1.0.9

func IsUnsupportedRelationError(err error) bool

IsUnsupportedRelationError returns true if err is related to unsupported relation error

func List

func List[T any](page, pageSize uint, where *Where, orderBy *OrderBy) (data []*T, total int64, err error)

List lists records.

func ListALL added in v1.2.1

func ListALL[T any](where *Where, orderBy *OrderBy) (data []*T, err error)

ListALL lists all records.

func LoadDB

func LoadDB(engine string, dsn string, opts ...func(*LoadDBOptions)) (err error)

LoadDB loads the database

func Migrate added in v1.4.0

func Migrate()

Migrate migrates the models to the database.

func Register added in v1.0.6

func Register(name string, m Model)

Register registers the model.

func Retrieve

func Retrieve[T any](id uint) (*T, error)

Retrieve retrieves a record.

func SQL added in v1.3.5

func SQL[T any](sql string, values ...any) (*T, error)

SQL finds one record by id or create a new one.

func Save

func Save[T any](one *T) error

Save saves a record.

func SetDB added in v1.5.3

func SetDB(d *gorm.DB)

SetDB sets the global gorm.DB instance. This is useful for old projects that already use gorm.

func Update

func Update[T any](id uint, uc func(*T)) (err error)

Update updates a record.

Types

type Controller added in v1.0.5

type Controller interface {
	Name() string
	//
	Params(ctx *zoox.Context) *Params
}

Controller is the interface that wraps the basic methods.

type ControllerImpl added in v1.0.5

type ControllerImpl struct {
}

ControllerImpl is the implementation of the Controller interface.

func (*ControllerImpl) Params added in v1.0.7

func (c *ControllerImpl) Params(ctx *zoox.Context) *Params

Params returns the params.

type ListParams added in v1.0.7

type ListParams struct {
	Page     uint
	PageSize uint
	Where    *Where
	OrderBy  *OrderBy
}

ListParams is the struct that wraps the basic fields.

type ListParamsDefault added in v1.0.7

type ListParamsDefault struct {
	Page     uint
	PageSize uint
}

ListParamsDefault is the struct that wraps the basic fields.

type LoadDBOptions added in v1.0.3

type LoadDBOptions struct {
	IsProd bool
	//
	TablePrefix string
	//
	DryRun bool
}

LoadDBOptions is the options for LoadDB

type Model added in v1.0.5

type Model interface {
	ModelName() string
	Model() ioc.Container
}

Model is the interface that wraps the basic methods.

type ModelGeneric added in v1.0.10

type ModelGeneric[T any] struct {
}

ModelGeneric ...

func (*ModelGeneric[T]) Create added in v1.0.10

func (m *ModelGeneric[T]) Create(one *T) (*T, error)

Create ...

func (*ModelGeneric[T]) Delete added in v1.0.10

func (m *ModelGeneric[T]) Delete(id uint) (err error)

Delete ...

func (*ModelGeneric[T]) Exists added in v1.0.10

func (m *ModelGeneric[T]) Exists(where map[any]any) (bool, error)

Exists ...

func (*ModelGeneric[T]) FindAll added in v1.0.10

func (m *ModelGeneric[T]) FindAll(where *Where, orderBy *OrderBy) ([]*T, error)

FindAll ...

func (*ModelGeneric[T]) FindByID added in v1.0.10

func (m *ModelGeneric[T]) FindByID(id uint) (*T, error)

FindByID ...

func (*ModelGeneric[T]) FindOne added in v1.0.10

func (m *ModelGeneric[T]) FindOne(where map[any]any) (*T, error)

FindOne ...

func (*ModelGeneric[T]) FindOneAndDelete added in v1.0.10

func (m *ModelGeneric[T]) FindOneAndDelete(where map[any]any) (*T, error)

FindOneAndDelete ...

func (*ModelGeneric[T]) FindOneAndUpdate added in v1.0.10

func (m *ModelGeneric[T]) FindOneAndUpdate(where map[any]any, callback func(*T)) (*T, error)

FindOneAndUpdate ...

func (*ModelGeneric[T]) FindOneOrCreate added in v1.0.10

func (m *ModelGeneric[T]) FindOneOrCreate(where map[any]any, callback func(*T)) (*T, error)

FindOneOrCreate ...

func (*ModelGeneric[T]) GetMany added in v1.0.10

func (m *ModelGeneric[T]) GetMany(ids []uint) (data []*T, err error)

GetMany ...

func (*ModelGeneric[T]) List added in v1.0.10

func (m *ModelGeneric[T]) List(page, pageSize uint, where *Where, orderBy *OrderBy) (data []*T, total int64, err error)

List ...

func (*ModelGeneric[T]) Retrieve added in v1.0.10

func (m *ModelGeneric[T]) Retrieve(id uint) (*T, error)

Retrieve ...

func (*ModelGeneric[T]) Save added in v1.0.10

func (m *ModelGeneric[T]) Save() error

Save ...

func (*ModelGeneric[T]) Update added in v1.0.10

func (m *ModelGeneric[T]) Update(id uint, uc func(*T)) (err error)

Update ...

type ModelImpl added in v1.0.5

type ModelImpl struct {
	ID        uint           `gorm:"primarykey" json:"id"`
	CreatedAt time.Time      `json:"created_at"`
	UpdatedAt time.Time      `json:"updated_at"`
	DeletedAt gorm.DeletedAt `gorm:"index" json:"deleted_at"`
	//
	Creator  uint `json:"creator"`
	Modifier uint `json:"modifier"`
}

ModelImpl is the implementation of the Model interface.

func (*ModelImpl) Model added in v1.0.5

func (m *ModelImpl) Model() ioc.Container

Model returns the model container.

func (*ModelImpl) ModelName added in v1.0.5

func (m *ModelImpl) ModelName() string

ModelName returns the name of the model.

type OrderBy

type OrderBy []OrderByOne

OrderBy is a list of order bys.

func (*OrderBy) Build

func (w *OrderBy) Build() string

Build builds the order bys.

func (*OrderBy) Debug

func (w *OrderBy) Debug()

Debug prints the order bys.

func (*OrderBy) Del added in v1.0.8

func (w *OrderBy) Del(key string)

Del deletes a order by.

func (*OrderBy) Get

func (w *OrderBy) Get(key string) (bool, bool)

Get gets a order by.

func (*OrderBy) Length

func (w *OrderBy) Length() int

Length returns the length of the order bys.

func (*OrderBy) Reset added in v1.3.4

func (w *OrderBy) Reset()

Reset resets the order bys.

func (*OrderBy) Set

func (w *OrderBy) Set(key string, IsDESC bool)

Set sets a order by.

type OrderByOne

type OrderByOne struct {
	Key    string
	IsDESC bool
}

OrderByOne is a single order by.

type Page

type Page struct {
	Page     int64 `query:"page,default=1"`
	PageSize int64 `query:"pageSize,default=10"`
}

Page is the page query.

type Params added in v1.0.7

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

Params is the interface that wraps the basic methods.

func NewParams added in v1.0.7

func NewParams(ctx *zoox.Context) *Params

NewParams returns the params.

func (*Params) GetList added in v1.0.7

func (c *Params) GetList(defaults ...*ListParamsDefault) (*ListParams, error)

GetList is the struct that wraps the basic fields.

func (*Params) ID added in v1.0.7

func (c *Params) ID() (uint, error)

ID is the struct that wraps the basic fields.

func (*Params) OrderBy added in v1.0.7

func (c *Params) OrderBy() *OrderBy

OrderBy is the struct that wraps the basic fields.

func (*Params) Page added in v1.0.7

func (c *Params) Page() (uint, error)

Page is the struct that wraps the basic fields.

func (*Params) PageSize added in v1.0.7

func (c *Params) PageSize() (uint, error)

PageSize is the struct that wraps the basic fields.

func (*Params) Where added in v1.0.7

func (c *Params) Where() *Where

Where is the struct that wraps the basic fields.

type Service added in v1.0.5

type Service interface {
	Name() string
}

Service is the interface that wraps the basic methods.

type ServiceImpl added in v1.0.5

type ServiceImpl struct {
}

ServiceImpl is the implementation of the Service interface.

func (*ServiceImpl) Name added in v1.0.5

func (s *ServiceImpl) Name() string

Name returns the name of the service.

type SetWhereOptions

type SetWhereOptions struct {
	IsEqual              bool
	IsNotEqual           bool
	IsFuzzy              bool
	IsIn                 bool
	IsNotIn              bool
	IsPlain              bool
	IsFullTextSearch     bool
	FullTextSearchFields []string
}

SetWhereOptions is the options for SetWhere.

type Where

type Where struct {
	Items []WhereOne
	//
	FullTextSearchFields []string
}

Where is the where.

func (*Where) Add added in v1.5.1

func (w *Where) Add(key string, value interface{}, opts ...*SetWhereOptions)

Add adds a where, if exists, append.

func (*Where) Build

func (w *Where) Build() (query string, args []interface{}, err error)

Build builds the wheres.

func (*Where) Debug

func (w *Where) Debug()

Debug prints the wheres.

func (*Where) Del added in v1.0.8

func (w *Where) Del(key string)

Del deletes a where.

func (*Where) Get

func (w *Where) Get(key string) (interface{}, bool)

Get gets a where.

func (*Where) Length

func (w *Where) Length() int

Length returns the length of the wheres.

func (*Where) Reset added in v1.3.4

func (w *Where) Reset()

Reset resets the wheres.

func (*Where) Set

func (w *Where) Set(key string, value interface{}, opts ...*SetWhereOptions)

Set sets a where, if exists, update.

type WhereOne

type WhereOne struct {
	Key   string
	Value interface{}

	// IsEqual => =
	IsEqual bool
	// IsNotEqual => !=
	IsNotEqual bool

	// IsFuzzy => ILike
	IsFuzzy bool

	// IsIn => in (?)
	IsIn bool
	// IsNotIn => not in (?)
	IsNotIn bool

	// IsPlain => plain
	IsPlain bool

	// IsFullTextSearch => ILike (field1) OR ILike (field2) OR ...
	IsFullTextSearch     bool
	FullTextSearchFields []string
}

WhereOne is the where one.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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