Documentation ¶
Overview ¶
Package database https://gorm.io/zh_CN/docs/connecting_to_the_database.html
Index ¶
Constants ¶
View Source
const ( // DefaultPageSize pagination default items limit size DefaultPageSize = 30 // MaxPageSize pagination default items max limit size MaxPageSize = 1000 )
Variables ¶
View Source
var NowFunc = func() time.Time { return time.Now().UTC() }
NowFunc returns current time, this function is exported in order to be able to give the flexibility to the developer to customize it according to their needs
Functions ¶
Types ¶
type Config ¶
type Config struct { Dialect string `mapstructure:"dialect"` // Database name Database string `mapstructure:"database"` Host string `mapstructure:"host"` Port int `mapstructure:"port"` Username string `mapstructure:"username"` Password string `mapstructure:"password"` Encoding string `mapstructure:"encoding"` gorm.Config *ConnPool }
Config mysql config
type ConnPool ¶
type ConnPool struct { // MaxIdleConns is the maximum number of connections in the idle connection pool MaxIdleConns int `mapstructure:"max_idle_conns"` // MaxOpenConns is the maximum number of open connections to the database. MaxOpenConns int `mapstructure:"max_open_conns"` // ConnMaxLifeTime is the maximum amount of time a connection may be reused. ConnMaxLifeTime int `mapstructure:"conn_max_life_time"` // SetConnMaxIdleTime sets the maximum amount of time a connection may be idle. ConnMaxIdleTime int `mapstructure:"conn_max_idle_time"` }
ConnPool config
type Database ¶
Database instance type
func NewWithDialector ¶
func NewWithDialector(dialector gorm.Dialector, config *gorm.Config) (database *Database, err error)
NewWithDialector database with dialector
func (*Database) SetConnPool ¶
SetConnPool set connection pool
type Model ¶
type Model struct { ID int64 `json:"id" gorm:"primaryKey"` CreatedAt int64 `json:"created_at"` UpdatedAt int64 `json:"updated_at"` DeletedAt soft_delete.DeletedAt `json:"deleted_at,omitempty" gorm:"index"` }
Model a basic GoLang struct which includes the following fields: ID, CreatedAt, UpdatedAt, DeletedAt It may be embedded into your model or you may build your own model without it
type User struct { database.Model }
type Pagination ¶
type Pagination[T any] struct { Page int `json:"page" form:"page" query:"page"` PageSize int `json:"page_size" form:"page_size" query:"page_size"` Total int64 `json:"total"` Items []T `json:"items"` }
Pagination model
func (*Pagination[T]) Paginate ¶
func (p *Pagination[T]) Paginate() func(db *gorm.DB) *gorm.DB
Paginate callback
func (*Pagination[T]) TotalPages ¶
func (p *Pagination[T]) TotalPages() int
Click to show internal directories.
Click to hide internal directories.