database

package
v0.0.0-...-71b8de0 Latest Latest
Warning

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

Go to latest
Published: Mar 23, 2023 License: MIT Imports: 14 Imported by: 0

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

func NewLogger

func NewLogger(slowThreshold int, requestID ...string) gormlogger.Interface

NewLogger return custom logger

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

func (*Config) Dialector

func (config *Config) Dialector() gorm.Dialector

Dialector return dialector with 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

type Database struct {
	*gorm.DB
}

Database instance type

func New

func New(config *Config) (database *Database, err error)

New database with configuration

func NewWithDialector

func NewWithDialector(dialector gorm.Dialector, config *gorm.Config) (database *Database, err error)

NewWithDialector database with dialector

func (*Database) SetConnPool

func (database *Database) SetConnPool(pool ConnPool) error

SetConnPool set connection pool

func (*Database) WithTrace

func (database *Database) WithTrace(traceID string) *gorm.DB

WithTrace gorm.DB instance with trace id

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

Jump to

Keyboard shortcuts

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