db

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Jun 10, 2021 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const (
	TypePostgres = "postgres" // PostgresSQL 数据库
	TypeMysql    = "mysql"    // Mysql 数据库
	TypeSqlite   = "sqlite"   // sqlite3 数据库
)
View Source
const (
	MysqlCharsetUTF8MB4 = "utf8mb4"
)

Variables

This section is empty.

Functions

func Default

func Default() *gorm.DB

func New

func New(config Config) (db *gorm.DB, err error)

Types

type Config

type Config struct {
	Type           string         `json:"type" yaml:"type" mapstructure:"type"`
	MysqlConfig    MysqlConfig    `json:"mysql" yaml:"mysql" mapstructure:"mysql"`
	PostgresConfig PostgresConfig `json:"postgres" yaml:"postgres" mapstructure:"postgres"`
	Sqlite         SqliteConfig   `json:"sqlite" yaml:"sqlite" mapstructure:"sqlite"`
	Gorm           GormConfig     `json:"gorm" yaml:"gorm" mapstructure:"gorm"`
}

Config 用于承载数据库配置

type GormConfig

type GormConfig struct {
	TablePrefix   string `json:"tablePrefix" yaml:"tablePrefix" mapstructure:"tablePrefix"`
	SingularTable bool   `json:"singularTable" yaml:"SingularTable" mapstructure:"singularTable"`
}

type Model

type Model struct {
	UUID      string    `gorm:"column:uuid; primaryKey"`
	CreatedAt time.Time `gorm:"column:created_at; autoCreateTime"`
	UpdatedAt time.Time `gorm:"column:updated_at; autoUpdateTime"`
}

func (*Model) BeforeCreate

func (m *Model) BeforeCreate(tx *gorm.DB) error

type MysqlConfig

type MysqlConfig struct {
	Host      string `json:"host" yaml:"host" mapstructure:"host"`
	Port      int    `json:"port" yaml:"port" mapstructure:"port"`
	User      string `json:"user" yaml:"user" mapstructure:"user"`
	Password  string `json:"password" yaml:"password" mapstructure:"password"`
	DBName    string `json:"dbname" yaml:"dbname" mapstructure:"dbname"`
	Charset   string `json:"charset" yaml:"charset" mapstructure:"charset"`
	ParseTime bool   `json:"parsetime" yaml:"parsetime" mapstructure:"parsetime"`
	TimeZone  string `json:"timezone" yaml:"timezone" mapstructure:"timezone"`
}

MysqlConfig Mysql数据库配置

func (MysqlConfig) Datasource

func (c MysqlConfig) Datasource() (string, error)

Datasource 返回数据库连接地址

func (MysqlConfig) Validate

func (c MysqlConfig) Validate() error

Validate 验证数据库配置完整性

type PostgresConfig

type PostgresConfig struct {
	Host     string `json:"host" yaml:"host" mapstructure:"host"`
	Port     int    `json:"port" yaml:"port" mapstructure:"port"`
	User     string `json:"user" yaml:"user" mapstructure:"user"`
	Password string `json:"password" yaml:"password" mapstructure:"password"`
	DBName   string `json:"dbname" yaml:"dbname" mapstructure:"dbname"`
	SSLMode  string `json:"sslmode" yaml:"sslmode" mapstructure:"sslmode"`
	TimeZone string `json:"timezone" yaml:"timezone" mapstructure:"timezone"`
}

PostgresConfig PostgresSQL 数据库配置

func (PostgresConfig) Datasource

func (c PostgresConfig) Datasource() (string, error)

Datasource 返回数据库连接地址

func (PostgresConfig) Validate

func (c PostgresConfig) Validate() error

Validate 验证数据库配置完整性

type SqliteConfig

type SqliteConfig struct {
	Filepath string `json:"filepath" yaml:"filepath" mapstructure:"filepath"`
}

func (SqliteConfig) Datasource

func (c SqliteConfig) Datasource() (string, error)

Jump to

Keyboard shortcuts

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