gorm

package
v0.11.15 Latest Latest
Warning

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

Go to latest
Published: Feb 20, 2024 License: Apache-2.0 Imports: 27 Imported by: 3

README

gorm

特性

  • 全功能 ORM
  • 关联 (Has One,Has Many,Belongs To,Many To Many,多态,单表继承)
  • Create,Save,Update,Delete,Find 中钩子方法
  • 支持 Preload、Joins 的预加载
  • 事务,嵌套事务,Save Point,Rollback To Saved Point
  • Context、预编译模式、DryRun 模式
  • 批量插入,FindInBatches,Find/Create with Map,使用 SQL 表达式、Context Valuer 进行 CRUD
  • SQL 构建器,Upsert,数据库锁,Optimizer/Index/Comment Hint,命名参数,子查询
  • 复合主键,索引,约束
  • Auto Migration
  • 自定义 Logger
  • 灵活的可扩展插件 API:Database Resolver(多数据库,读写分离)、Prometheus…
  • 每个特性都经过了测试的重重考验
  • 开发者友好

快速入门

package main

import (
  "github.com/douyu/jupiter/pkg/store/gorm"
)

type Product struct {
  gorm.Model
  Code  string
  Price uint
}

func main() {
  db := gorm.StdNew("test")

  // 迁移 schema
  db.AutoMigrate(&Product{})

  // Create
  db.Create(&Product{Code: "D42", Price: 100})

  // Read
  var product Product
  db.First(&product, 1) // 根据整形主键查找
  db.First(&product, "code = ?", "D42") // 查找 code 字段值为 D42 的记录

  // Update - 将 product 的 price 更新为 200
  db.Model(&product).Update("Price", 200)
  // Update - 更新多个字段
  db.Model(&product).Updates(Product{Price: 200, Code: "F42"}) // 仅更新非零值字段
  db.Model(&product).Updates(map[string]interface{}{"Price": 200, "Code": "F42"})

  // Delete - 删除 product
  db.Delete(&product, 1)
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrRecordNotFound record not found error.
	ErrRecordNotFound = gorm.ErrRecordNotFound
)

Functions

This section is empty.

Types

type Config

type Config struct {
	Name string
	// DSN地址: mysql://root:secret@tcp(127.0.0.1:3307)/mysql?timeout=20s&readTimeout=20s
	DSN string `json:"dsn" toml:"dsn"`
	// Debug开关
	Debug bool `json:"debug" toml:"debug"`
	// 最大空闲连接数
	MaxIdleConns int `json:"maxIdleConns" toml:"maxIdleConns"`
	// 最大活动连接数
	MaxOpenConns int `json:"maxOpenConns" toml:"maxOpenConns"`
	// 连接的最大存活时间
	ConnMaxLifetime time.Duration `json:"connMaxLifetime" toml:"connMaxLifetime"`
	// 创建连接的错误级别,=panic时,如果创建失败,立即panic
	OnDialError string `json:"level" toml:"level"`
	// 慢日志阈值
	SlowThreshold time.Duration `json:"slowThreshold" toml:"slowThreshold"`
	// 拨超时时间
	DialTimeout time.Duration `json:"dialTimeout" toml:"dialTimeout"`
	// 自动使用影子表
	AutoShadowTable bool `json:"autoShadowTable" toml:"autoShadowTable"`

	// 记录错误sql时,是否打印包含参数的完整sql语句
	// select * from aid = ?;
	// select * from aid = 288016;
	DetailSQL bool `json:"detailSql" toml:"detailSql"`
	// 重试次数
	Retry int `json:"retry" toml:"retry"`
	// 重试等待时间
	RetryWaitTime time.Duration `json:"retryWaitTime" toml:"retryWaitTime"`
	// contains filtered or unexported fields
}

Config options

func DefaultConfig

func DefaultConfig() *Config

DefaultConfig 返回默认配置

func RawConfig

func RawConfig(key string) *Config

RawConfig 传入mapstructure格式的配置

func StdConfig

func StdConfig(name string) *Config

StdConfig 标准配置,规范配置文件头

func (*Config) MustBuild added in v0.6.1

func (config *Config) MustBuild() *gorm.DB

func (*Config) MustSingleton added in v0.6.1

func (config *Config) MustSingleton() *gorm.DB

func (*Config) WithGormConfig added in v0.6.1

func (config *Config) WithGormConfig(gormConfig gorm.Config) *Config

type DB

type DB = gorm.DB

type DSN

type DSN struct {
	User     string            // Username
	Password string            // Password (requires User)
	Net      string            // Network type
	Addr     string            // Network address (requires Net)
	DBName   string            // Database name
	Params   map[string]string // Connection parameters
}

DSN ...

type Handler

type Handler func(*gorm.DB)

type Interceptor

type Interceptor func(dsn *DSN, op string, options *Config, next Handler) Handler

type Model

type Model = gorm.Model

Jump to

Keyboard shortcuts

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