gormx

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2024 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Close

func Close() error

Close 关闭数据库连接

func GetDB

func GetDB() *gorm.DB

GetDB 获取数据库实例

func Initialize

func Initialize(conf Config) error

Initialize 初始化数据库连接

func Paginate

func Paginate(p *Pagination) func(db *gorm.DB) *gorm.DB

Paginate 分页查询

Types

type Config

type Config struct {
	Debug        bool   // 是否开启调试模式
	Host         string // 服务地址
	Port         string // 服务端口
	User         string // 用户名
	Pass         string // 密码
	Name         string // 数据库名称
	Char         string // 字符集
	MaxIdleConns int    // 最大空闲连接数
	MaxOpenConns int    // 最大打开连接数
	MaxLifetime  int    // 连接最大生命周期
}

Config 数据库配置

type GormEngine

type GormEngine = gorm.DB

GormEngine GORM 数据库引擎

type Model

type Model struct {
	ID        uint           `gorm:"primarykey" json:"id"`              // 主键ID
	CreatedAt time.Time      `gorm:"not null" json:"created_at"`        // 创建时间
	UpdatedAt time.Time      `gorm:"not null" json:"updated_at"`        // 更新时间
	DeletedAt gorm.DeletedAt `gorm:"index" json:"deleted_at,omitempty"` // 删除时间(软删除)
}

Model 基础模型

type PageResult

type PageResult struct {
	List     interface{} `json:"list"`      // 数据列表
	Page     int         `json:"page"`      // 当前页码
	PageSize int         `json:"page_size"` // 每页数量
	Total    int64       `json:"total"`     // 总数据量
}

PageResult 分页结果

func GetPage

func GetPage(db *gorm.DB, p *Pagination, data interface{}) (*PageResult, error)

GetPage 获取分页结果

type Pagination

type Pagination struct {
	Page     int `json:"page" form:"page"`           // 当前页码
	PageSize int `json:"page_size" form:"page_size"` // 每页数量
}

Pagination 分页参数

type TimeModel

type TimeModel struct {
	ID        uint      `gorm:"primarykey" json:"id"`       // 主键ID
	CreatedAt time.Time `gorm:"not null" json:"created_at"` // 创建时间
	UpdatedAt time.Time `gorm:"not null" json:"updated_at"` // 更新时间
}

TimeModel 时间模型(不带软删除)

Jump to

Keyboard shortcuts

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