database

package
v0.1.9 Latest Latest
Warning

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

Go to latest
Published: Sep 18, 2022 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package database wraps gorm for convenience, we don't use goframe-gdb because of it's commitment of NEVER-SUPPORT-MIGRATE-SPECIALITY.

Example
package main

import (
	"github.com/lovelacelee/clsgo/v1/database"
	"github.com/lovelacelee/clsgo/v1/log"
)

func main() {
	ExampleNew()
}

type User struct {
	database.Model
	Name string `gorm:"name"`
}

func ExampleNew() {
	db := database.New()
	if db != nil {
		defer db.Close()
		if db.Valid() {

			db.Orm.AutoMigrate(&User{})
			user := User{
				Name: "Lee",
			}
			db.Orm.Create(&user)
			quser := User{}
			var count int64 = 0

			db.CacheFind("", &quser, "id = ?", 2)
			db.Orm.Model(&User{}).Count(&count)
			log.Info(quser)
			log.Info(count)
		}
	}
}
Output:

Index

Examples

Constants

This section is empty.

Variables

View Source
var Config map[string]DbConfig
View Source
var GormDriver map[string]func(string) gorm.Dialector = map[string]func(string) gorm.Dialector{
	"SQLITE":     Sqlite,
	"MYSQL":      MySQL,
	"SQLSERVER":  SQLServer,
	"POSTGRES":   Postgres,
	"CLICKHOUSE": ClickHouse,
}
View Source
var TracePlugin gorm.Plugin = &PluginTrace{}

Functions

func ClickHouse

func ClickHouse(dsn string) gorm.Dialector

dsn := "tcp://localhost:9000?database=gorm&username=gorm&password=gorm&read_timeout=10&write_timeout=20"

func ConnPoolSetting

func ConnPoolSetting(db *gorm.DB) error

func MySQL

func MySQL(dsn string) gorm.Dialector

dsn := "username:password@tcp(localhost:9910)/gorm?charset=utf8&parseTime=True&loc=Local"

func Postgres

func Postgres(dsn string) gorm.Dialector

dsn := "host=localhost user=gorm password=gorm dbname=gorm port=9920 sslmode=disable TimeZone=Asia/Shanghai"

func SQLServer

func SQLServer(dsn string) gorm.Dialector

dsn := "sqlserver://username:password@localhost:9930?database=gorm"

func Sqlite

func Sqlite(dsn string) gorm.Dialector

"test.db" "file::memory:?cache=shared"

Types

type Db

type Db struct {
	Orm    *gorm.DB
	Config DbConfig
	RDB    *redis.Client
}

func New

func New(group ...string) *Db

New return the instance of open gorm instance, group was config name under database section. Returns nil, if group not found.

Example
package main

import (
	"github.com/lovelacelee/clsgo/v1/database"
	"github.com/lovelacelee/clsgo/v1/log"
)

type User struct {
	database.Model
	Name string `gorm:"name"`
}

func main() {
	db := database.New()
	if db != nil {
		defer db.Close()
		if db.Valid() {
			// db.Orm.Use(database.TracePlugin)
			db.Orm.AutoMigrate(&User{})
			user := User{
				Name: "Lee",
			}
			db.Orm.Create(&user)
			quser := User{}
			var count int64 = 0
			// db.Orm.Find(&quser, "id = ?", 2)
			db.CacheFind("", &quser, "id = ?", 2)
			db.Orm.Model(&User{}).Count(&count)
			log.Info(quser)
			log.Info(count)
		}
	}
}
Output:

func (*Db) CacheFind

func (db *Db) CacheFind(cacheKeyPrefix string, dest interface{}, conds ...interface{})

User compose cache key which must be unique in the lifetime of application

func (*Db) Close

func (db *Db) Close()

func (*Db) Valid

func (db *Db) Valid() bool

type DbConfig

type DbConfig struct {
	Dsn   string
	Type  string
	Redis string
}

func GetConfig

func GetConfig(group string) DbConfig

type Model

type Model struct {
	gorm.Model
}

See more https://gorm.io/zh_CN/docs/models.html https://gorm.io/zh_CN/docs/models.html#embedded_struct Field inluded:

ID        uint           `gorm:"primaryKey"`
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt gorm.DeletedAt `gorm:"index"`

type PluginTrace

type PluginTrace struct{}

Plugin trace sql and time of it excuted

func (*PluginTrace) Initialize

func (op *PluginTrace) Initialize(db *gorm.DB) (err error)

func (*PluginTrace) Name

func (op *PluginTrace) Name() string

Jump to

Keyboard shortcuts

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