gorm

package module
v0.0.0-...-0b86307 Latest Latest
Warning

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

Go to latest
Published: Mar 15, 2024 License: MIT Imports: 17 Imported by: 1

README

1. 项目简介

1.基于gorm库封装,增加tracing 2.接入统一日志输出

2. 详细文档地址

https://jasperxu.github.io/gorm-zh

3. 配置文件参数

gorm:
  alias: admin-server
  driver: mysql  #当前代码里面预制的是mysql
  server: 127.0.0.1
  port: 3306
  user: root
  password: my-secret-pw
  charset: utf8mb4
  database: ets_db
  maxIdleConns: 3
  maxOpenConns: 10
  maxLeftTime: 10s
  timezone: Local

4. 快速开始


// 如果配置文件中有gorm配置文件,则会默认初始化default

// 如果只希望获取自己定义的初始化DB,可通过一下接口获取
db := gorm.NewEngine(&config.Config{
    Driver:     "mysql",  
    Server:   "127.0.0.1",
    Port:     3306,
    Database: "db",
    User:     "db",
    Password: "123456",
})

// 如果想管理多个DB连接,则使用注册方式进行
gorm.RegisterDataBase("test-mysql",&config.Config{
    Driver:     "mysql",  
    Server:   "127.0.0.1",
    Port:     3306,
    Database: "db",
    User:     "db",
    Password: "123456",
})
// 获取注册的DB
gorm.GetEngine("test-mysql")

// 获取默认DB
gorm.GetEngine()
gorm.GetEngine("defalut")

//应用中使用,sess的具体方法查看 https://jasperxu.github.io/gorm-zh
sess = db.Context(context.Background())

5. 使用示例

参见gorm_test.go

6. 如果需要使用sqlite数据库,则需要环境中包括gcc编译环境

windows推荐直接下载https://github.com/niXman/mingw-builds-binaries/releases对应的版本, 如x86_64-13.2.0-release-posix-seh-msvcrt-rt_v11-rev0.7z,下载后,解压到某个目录,配置环境变量,指向该目录下的bin目录

Documentation

Overview

Package gorm is a wrapper for gorm.

Index

Constants

View Source
const (
	// LogLevelSilent is logger silent level
	LogLevelSilent = logger.Silent
	// LogLevelError is logger error level
	LogLevelError = logger.Error
	// LogLevelWarn is logger warn level
	LogLevelWarn = logger.Warn
	// LogLevelInfo is logger info level
	LogLevelInfo = logger.Info
)

Variables

View Source
var (
	// ErrRecordNotFound returns a "record not found error". Occurs only when attempting to query the database with a struct; querying with a slice won't return this error
	ErrRecordNotFound = gorm.ErrRecordNotFound
	// ErrInvalidTransaction occurs when you are trying to `Commit` or `Rollback`
	ErrInvalidTransaction = gorm.ErrInvalidTransaction
)

Functions

func Expr

func Expr(expr string, args ...interface{}) clause.Expr

Expr 是gorm.Expr的透传

func RegisterDataBase

func RegisterDataBase(aliasName string, conf *Config)

RegisterDataBase 注册一个别名的DB

Types

type Association

type Association = gorm.Association //nolint

type Config

type Config struct {
	Alias        string        `mapstructure:"alias" json:"alias"`
	Driver       string        `mapstructure:"driver" json:"driver"`
	Server       string        `mapstructure:"server" json:"server"`
	Port         int           `mapstructure:"port" json:"port"`
	Database     string        `mapstructure:"database" json:"database"`
	User         string        `mapstructure:"user" json:"user"`
	Password     string        `mapstructure:"password" json:"password"`
	MaxIdleConns int           `mapstructure:"maxIdleConns" json:"maxIdleConns"`
	MaxOpenConns int           `mapstructure:"maxOpenConns" json:"maxOpenConns"`
	Charset      string        `mapstructure:"charset" json:"charset"`
	TimeZone     string        `mapstructure:"timezone" json:"timezone"`
	MaxLeftTime  time.Duration `mapstructure:"maxLeftTime" json:"maxLeftTime"`
}

Config 是gorm的配置文件字段定义

type DB

type DB = gorm.DB //nolint

type DBConditions

type DBConditions struct {
	And       map[string]interface{}
	Or        map[string]interface{}
	Not       map[string]interface{}
	Limit     int
	Offset    int
	Order     interface{}
	Select    interface{}
	Group     string
	Having    interface{}
	NeedCount bool
	Count     int64
	Distinct  interface{} // grom v1 暂不支持
}

DBConditions DB常用的查询条件封装

func (*DBConditions) Fill

func (d *DBConditions) Fill(db *gorm.DB) *gorm.DB

Fill 填充查询条件

type DeletedAt

type DeletedAt = gorm.DeletedAt //nolint

type Engine

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

Engine 是gorm的一个封装类

func GetEngine

func GetEngine(aliasNames ...string) *Engine

GetEngine 通过别名,获取DB的实例

func NewEngine

func NewEngine(conf *Config) *Engine

NewEngine 实例化新的Gorm实例

func (*Engine) Context

func (db *Engine) Context(ctx context.Context) *gorm.DB

Context 设置db查询是的上下文

func (*Engine) GetDB

func (db *Engine) GetDB() *gorm.DB

GetDB 获取当前实例中的db对象

func (*Engine) SetLogLevel

func (db *Engine) SetLogLevel(level LogLevel)

SetLogLevel 设置输出日志的级别

func (*Engine) SetLogMode

func (db *Engine) SetLogMode(mode bool)

SetLogMode 设置日志模式开关

func (*Engine) WrapLog

func (db *Engine) WrapLog()

WrapLog 更新gorm的log实现

type LogLevel

type LogLevel = logger.LogLevel

LogLevel 日志等级

type Model

type Model = gorm.Model //nolint

type Writer

type Writer struct{}

Writer 重新定义gorm的writer类

func (Writer) Printf

func (w Writer) Printf(format string, args ...interface{})

Printf 是gorm日志输出的实现

Jump to

Keyboard shortcuts

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