orm

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Jul 14, 2023 License: MIT Imports: 14 Imported by: 0

README

orm使用

orm

package main

import (
	"os"

	"github.com/gowins/dionysus"
	"github.com/gowins/dionysus/cmd"
	"github.com/gowins/dionysus/orm"
	"gorm.io/gen"
)

func main() {
	log.Setup(log.SetProjectName("dbsetup"), log.WithWriter(io.Stdout))
	d := orm.DsnInfo{
        // 用户名
		User:   "user",
        // 密码
		Passwd: "pass",
        // ip地址
		Host:   "127.0.0.1",
        // 端口
		Port:   3306,
        // 数据库名
		DbName: "dbname",
	}
	d1 := orm.DsnInfo{
        // 用户名
		User:   "user",
        // 密码
		Passwd: "pass",
        // ip地址
		Host:   "127.0.0.1",
        // 端口
		Port:   3306,
        // 数据库名
		DbName: "dbname",
	}
	m := map[string]orm.DbMap{
		"default": {Dialector: d.Dialector(), Opts: []orm.ConfigOpt{testFnOpts(), testGormOpts()}},
		"d1": {Dialector: d1.Dialector(), Opts: nil},
	}
	orm.Setup(m)
    orm.GetDB("default")
}

    
// testGormOpts 设置gorm.Option
func testGormOpts() ConfigOpt {
	return orm.WithGormOpts(&gorm.Config{})
}

// testFnOpts 设置sql.DB连接参数
func testFnOpts() ConfigOpt {
	return orm.WithOptFns(
        // 最大打开连接数
        orm.WithMaxOpenConns(10),
        // 最大空闲连接数
		orm.WithMaxIdleConns(10),
        // 连接有效期
		orm.WithConnMaxLifetime(time.Second*5),
        // 参开: https://github.com/go-sql-driver/mysql#dsn-data-source-name
        // 连接字符集
		orm.WithCharset("utf8mb4"),
        // 是否解析time.Time
		orm.WithParseTime("True"),
        // time.Time市区
		orm.WithLoc("local"),
	)
}

gorm_gen

package main

import (
	"fmt"
	"os"
	"path/filepath"

	"github.com/gowins/dionysus"
	"github.com/gowins/dionysus/cmd"
	"github.com/gowins/dionysus/orm"
	"gorm.io/gen"
)

func main() {
	dsn := orm.DsnInfo{
		User:   "user",
		Passwd: "passwd",
		Host:   "127.0.0.1",
		Port:   3306,
		DbName: "rds5ewin",
		Params: map[string][]string{
			"charset":   {"utf8mb4"},
			"parseTime": {"True"},
			"loc":       {"Local"},
		},
	}
	tmp := filepath.Join(os.TempDir(), "gorm_gen_1234567890")
	fmt.Println(tmp)
	ormGen := orm.OrmGenCmd{
		Dialector: dsn.Dialector(),
		Cfg: gen.Config{
			OutPath:      filepath.Join(tmp, "repository"),
			ModelPkgPath: filepath.Join(tmp, "model"),
		},
		GenModels: []orm.GenModel{
			{TableName: "yw_user"},
		},
	}
	ormGen.RegShutdownFunc(cmd.StopStep{
		StopFn: func() {
			fmt.Println("shutdown func")
		},
		StepName: "remove_test_file",
	})
	dio := dionysus.NewDio()
	dio.DioStart("orm", &ormGen)
    // 移除生成代码
	os.RemoveAll(tmp)
}

运行

go run main.go gorm_gen

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DialectorByDB

func DialectorByDB(sqlDB *sql.DB) gorm.Dialector

DialectorByDB return gorm.Dialector by sql.DB

func GetDB

func GetDB(name string) *gorm.DB

func NewWriter

func NewWriter(opts ...log.Option) (logger.Writer, error)

NewWriter return loggerWiter

func Setup

func Setup(dbMaps map[string]DbMap)

Types

type Config

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

Config contain database connect options and gorm options

type ConfigOpt

type ConfigOpt func(*Config)

ConfigOpt set Config function

func WithGormOpts

func WithGormOpts(opts ...gorm.Option) ConfigOpt

WithGormOpts set Config gormOpts

func WithOptFns

func WithOptFns(optFn ...OptionFunc) ConfigOpt

WithOptFns set Config optFns

type DbMap

type DbMap struct {
	Dialector gorm.Dialector
	Opts      []ConfigOpt
}

DbMap multi-database maps

type DsnInfo

type DsnInfo struct {
	User   string
	Passwd string
	Host   string
	Port   uint32
	DbName string
	Params url.Values
}

DsnInfo user:pass@tcp(127.0.0.1:3306)/dbname?charset=utf8mb4&parseTime=True&loc=Local

func (*DsnInfo) Dialector

func (dsn *DsnInfo) Dialector() gorm.Dialector

Dialector return gorm.Dialector

func (*DsnInfo) String

func (dsn *DsnInfo) String() string

String convert DsnInfo to string user:pass@tcp(127.0.0.1:3306)/dbname?charset=utf8mb4&loc=Local&parseTime=True

type GenModel

type GenModel struct {
	TableName string
	ModelName string
	ModelOpts []gen.ModelOpt
}

GenModel generate data necessary for QueryStructMeta

type OptionFunc

type OptionFunc func(opts *Options)

OptionFunc set Options function

func WithCharset

func WithCharset(charset string) OptionFunc

WithCharset set charset

func WithConnMaxLifetime

func WithConnMaxLifetime(connmaxLifetime time.Duration) OptionFunc

WithConnMaxLifetime set connMaxLifetime

func WithLoc

func WithLoc(loc string) OptionFunc

WithLoc set loc

func WithMaxIdleConns

func WithMaxIdleConns(maxIdleConns int) OptionFunc

WithMaxIdleConns set maxIdleConns

func WithMaxOpenConns

func WithMaxOpenConns(maxOpenConns int) OptionFunc

WithMaxOpenConns set maxOpenConns

func WithParseTime

func WithParseTime(parseTime string) OptionFunc

WithParseTime set parseTime

type Options

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

Options connect database options

type OrmGenCmd

type OrmGenCmd struct {
	DataTyMap map[string]func(string) string
	ModelOpts []gen.ModelOpt
	GenModels []GenModel
	Dialector gorm.Dialector
	Cfg       gen.Config
}

OrmGenCmd generate orm model and repository command

func (*OrmGenCmd) GetCmd

func (og *OrmGenCmd) GetCmd() *cobra.Command

GetCmd implement GetCmd method of cmd.Commander

func (*OrmGenCmd) GetShutdownFunc

func (og *OrmGenCmd) GetShutdownFunc() cmd.StopFunc

GetShutdownFunc implement GetShutdownFunc method of cmd.Commander

func (*OrmGenCmd) RegShutdownFunc

func (og *OrmGenCmd) RegShutdownFunc(stopSteps ...cmd.StopStep)

RegShutdownFunc implement RegShutdownFunc method of cmd.Commander

Jump to

Keyboard shortcuts

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