orm

package
v0.0.0-...-f56a457 Latest Latest
Warning

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

Go to latest
Published: Sep 27, 2022 License: MIT Imports: 12 Imported by: 0

README

orm:

  • 基于 gorm 扩展

gorm:

  • v2版本
callback hook 替换:

Documentation

Index

Constants

View Source
const (
	//
	// db type:
	//
	MySQL  = "mysql"
	SQLite = "sqlite3"
	TiDB   = "mysql"

	//
	// default options:
	//
	DefaultDialect     = MySQL
	DefaultActiveNum   = 20   // 连接数 max
	DefaultIdleNum     = 10   // 连接数 max
	DefaultIdleExpire  = "4h" // 超时 max
	DefaultQueryExpire = "5s" // 查询超时
	DefaultExecExpire  = "5s" // 写入超时
	DefaultTxExpire    = "5s" // tx 事务超时

)
View Source
const (
	//
	// timestamp类型取值范围:1970-01-01 00:00:00 到 2037-12-31 23:59:59,
	// 初始值调整为: [1970-01-02 00:00:00 , 1970-01-01 00:00:01], 自定义零值, > 1970-01-01 00:00:00, 即可
	// https://reading.developerlearning.cn/discuss/2019-06-19-gorm-mysql-timestamp/
	MySqlZeroTimestamp = "1970-01-02 00:00:00" // 时间戳零值, 特殊值(MySQL)

)

Variables

This section is empty.

Functions

func NewDefaultConn

func NewDefaultConn()

Types

type DeletedAt

type DeletedAt gorm.DeletedAt

软删除类型:

func (DeletedAt) DeleteClauses

func (n DeletedAt) DeleteClauses() []clause.Interface

软删除-删除子句:

func (DeletedAt) QueryClauses

func (DeletedAt) QueryClauses() []clause.Interface

软删除-查询子句:(修正 判空条件)

  • 自动补全
  • where (deleted_at <= "1970-01-02 00:00:00" )

func (*DeletedAt) Scan

func (n *DeletedAt) Scan(value interface{}) error

Scan implements the Scanner interface.

func (DeletedAt) Value

func (n DeletedAt) Value() (driver.Value, error)

Value implements the driver Valuer interface.

type Model

type Model struct {
	SqlBuilderMixin // gen sql:

	ID        uint64    `` /* 163-byte string literal not displayed */
	CreatedAt time.Time `` /* 163-byte string literal not displayed */
	UpdatedAt time.Time `` /* 163-byte string literal not displayed */
	DeletedAt DeletedAt `` // 删除默认时间戳
	/* 163-byte string literal not displayed */

}

type OptionFunc

type OptionFunc func(*Options)

fn:

func ConnParams

func ConnParams(activeNum int, idleNum int, idleExpire timeEx.Duration) OptionFunc

连接参数定制:

func DSN

func DSN(dsn string) OptionFunc

func DebugMode

func DebugMode(active bool) OptionFunc

log 模式:

func Dialect

func Dialect(dialect string) OptionFunc

func SingularTable

func SingularTable(active bool) OptionFunc

表名单数:

func TableFields

func TableFields(createdAt string, updatedAt string, deletedAt string, isDeleted string) OptionFunc

表默认字段:

type Options

type Options struct {
	Dialect     string
	DSN         string          // data source name.
	ActiveNum   int             // pool
	IdleNum     int             // pool
	IdleTimeout timeEx.Duration // connect max life time.

	// option for table:
	IsSingularTable bool // db 表名: 单数/复数
	IsDebugMode     bool // log switch: show raw sql

	// option item:
	CreatedTsName string
	UpdatedTsName string
	DeletedTsName string
	IsDeletedName string
}

db conn option:

func NewOptions

func NewOptions(opts ...OptionFunc) Options

func (*Options) DBConn

func (m *Options) DBConn() *gorm.DB

func (*Options) DBConnV2

func (m *Options) DBConnV2() *gormV2.DB

type SqlBuilderMixin

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

gen sql:

func (*SqlBuilderMixin) ToSelectFields

func (m *SqlBuilderMixin) ToSelectFields(model interface{}) string

model: 传数据实体的类型

type StatusModel

type StatusModel struct {
	SqlBuilderMixin // gen sql:

	ID        uint64    `` /* 163-byte string literal not displayed */
	Status    int64     `` /* 230-byte string literal not displayed */
	CreatedAt time.Time `` /* 163-byte string literal not displayed */
	UpdatedAt time.Time `` /* 163-byte string literal not displayed */
	DeletedAt DeletedAt `` // 删除默认时间戳
	/* 163-byte string literal not displayed */
}

has status:

type TemplateModel

type TemplateModel struct {
	SqlBuilderMixin // gen sql:

	ID  uint64 `` /* 163-byte string literal not displayed */
	Ver uint64 `` // 乐观锁
	/* 169-byte string literal not displayed */
	CreatedAt time.Time `` /* 163-byte string literal not displayed */
	UpdatedAt time.Time `` /* 163-byte string literal not displayed */
	DeletedAt DeletedAt `` /* 163-byte string literal not displayed */
	//
	Name       string `db:"name"         gorm:"type=varchar(128) CHARACTER SET utf8mb4;  NOT NULL; DEFAULT:''; COMMENT:'姓名'; "`
	BankCardNo int64  `db:"bank_card_no" gorm:"type=int(11);                             NOT NULL; DEFAULT:''; COMMENT:'银行卡号'; "`
}

集大成者:

type TxModel

type TxModel struct {
	SqlBuilderMixin // gen sql:

	ID  uint64 `` /* 163-byte string literal not displayed */
	Ver uint64 `` // 乐观锁
	/* 169-byte string literal not displayed */
	CreatedAt time.Time `` /* 163-byte string literal not displayed */
	UpdatedAt time.Time `` /* 163-byte string literal not displayed */
	DeletedAt DeletedAt `` /* 163-byte string literal not displayed */
}

db 事务: 乐观锁

type UserModel

type UserModel struct {
	SqlBuilderMixin // gen sql:

	ID     uint64 `` /* 163-byte string literal not displayed */
	UserID uint64 `` // 用户 ID
	/* 160-byte string literal not displayed */
	CreatedAt time.Time `` /* 163-byte string literal not displayed */
	UpdatedAt time.Time `` /* 163-byte string literal not displayed */
	DeletedAt DeletedAt `` /* 163-byte string literal not displayed */
}

user model:

type UserUniqueModel

type UserUniqueModel struct {
	SqlBuilderMixin // gen sql:

	ID     uint64 `` /* 163-byte string literal not displayed */
	UserID uint64 `` // 用户 ID
	/* 160-byte string literal not displayed */
	CreatedAt time.Time `` /* 163-byte string literal not displayed */
	UpdatedAt time.Time `` /* 163-byte string literal not displayed */
	DeletedAt DeletedAt `` /* 163-byte string literal not displayed */
}

唯一索引:

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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