zeta

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Sep 21, 2021 License: MIT Imports: 13 Imported by: 0

README

zeta

Build Status
integrate gorm and redis

Installation

go get https://github.com/daqiancode/zeta

Examples:

  • Define models
package tables

import (
	"time"
)

type Model struct {
	ID        uint64    `gorm:"primarykey"`
	CreatedAt time.Time `gorm:"type:datetime not null;"`
	UpdatedAt time.Time `gorm:"type:datetime not null;"`
	// DeletedAt sql.NullTime `gorm:"index"`
}

type ModelUser struct {
	Model
	UID uint64 `gorm:"type:bigint unsigned not null;fk:User"`
}

type User struct {
	Model
	Name     string `gorm:"type:varchar(30) not null;"`
	Disabled bool   `gorm:"not null;default:false"`
}
type UserPrivate struct {
	Model
	UID      uint64 `gorm:"type:bigint unsigned not null;fk:User;uniqueIndex"`
	Email    string `gorm:"type:varchar(100);uniqueIndex"`
	Mobile   string `gorm:"type:varchar(20);uniqueIndex"`
	Password string `gorm:"type:varchar(32) not null;"`
}
  • Define database connections
package dao

import (
	"iam/config"
	"log"
	"net/url"
	"os"
	"reflect"
	"sync"
	"time"

	"github.com/daqiancode/zeta"
	"github.com/go-redis/redis/v8"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"gorm.io/gorm/logger"
	"gorm.io/gorm/schema"
)

type Namer struct {
	schema.NamingStrategy
}

func (ns Namer) ColumnName(table, column string) string {
	return column
}

var _db *gorm.DB
func GetMysql() *gorm.DB {
	if _db != nil {
		return _db
	}
	mysqlUrl, err := url.PathUnescape(config.Config.MysqlUrl)
	if err != nil {
		panic(err)
	}
	dbConfig := &gorm.Config{}
	namer := &Namer{}
	dbConfig.NamingStrategy = namer
	db, err := gorm.Open(mysql.Open(mysqlUrl), dbConfig)
	if err != nil {
		panic(err)
	}
	_db = db
	return db

}

var _rdb *redis.Client

func GetRedis() *redis.Client {
	if _rdb != nil {
		return _rdb
	}
	rdb := redis.NewClient(&redis.Options{
		Addr:     config.Config.Redis.Addr,
		Password: config.Config.Redis.Password,
		DB:       config.Config.Redis.DB,
	})
	return rdb
}

var tableIndexes map[string][][]string = map[string][][]string{
	"UserPrivate": {{"UID"}},
	"UserToken":   {{"UID"}, {"AccessToken"}},
}
var ddl *zeta.DDL = nil
var cachedDaos sync.Map

func NewCachedDAO(entityRef interface{}) *zeta.CachedDAO {
	structName := reflect.Indirect(reflect.ValueOf(entityRef)).Type().Name()
	if r, ok := cachedDaos.Load(structName); ok {
		return r.(*zeta.CachedDAO)
	}
	db := GetMysql()
	if ddl == nil {
		ddl = zeta.NewDDL(db)
	}
	ddl.AddTables(entityRef)
	sch := ddl.GetSchema(entityRef)
	r := zeta.NewCachedDAO(db, entityRef, sch.Table, sch.PrimaryFields[0].Name, GetRedis(), &zeta.JSONValueSerializer{}, "user_center", config.Config.Redis.TTL)
	if indexes, ok := tableIndexes[structName]; ok {
		for _, index := range indexes {
			r.AddIndex(index...)
		}
	}
	cachedDaos.Store(structName, r)
	return r
}

func NewBaseDAO() *zeta.BaseDAO {
	return zeta.NewBaseDAO(GetMysql())
}

  • Migrate models into database
package dao
func Migrate(t *testing.T) {
	db := GetMysql()

	models := []interface{}{&tables.User{}, &tables.UserPrivate{}, &tables.UserToken{}}

	err := db.AutoMigrate(models...)
	if err != nil {
		panic(err)
	}

	ddl := zeta.NewDDL(db)
	ddl.AddTables(models...)
	ddl.MakeFKs() // handle foreign key like "UID uint64 "`gorm:"fk:User"`"
}
  • Use CachedDAO and BaseDAO in service
package service

type Users struct {
	userDao *zeta.CachedDAO
}

var _users *Users = nil

func NewUsers() *Users {
	if _users != nil {
		return _users
	}
	_users := &Users{
		userDao: dao.NewCachedDAO(&tables.User{}),
	}
	return _users
}

func (s *Users) Get(id uint64) tables.User {
	var r tables.User
	s.userDao.Get(&r, id)
	return r
}

// Create should be run in transaction
func (s *Users) Create(name string) tables.User {
	var r tables.User
	r.Name = name
	service.userDao.Insert(&r)
	return r
}
  • Transaction(Cache will be disabled in transaction)
func Signup() {
    err := s.userPrivateDao.Transaction(func(tx *zeta.BaseDAO) error {
		newUser := NewUsers().Create(tx, info.Name)
		up := tables.UserPrivate{UID: newUser.ID, Email: info.Email, Mobile: info.Mobile, Password: makepassword(info.Password)}
		tx.Insert(&up)
		
		token = s.userTokenDao.Create(tx, newUser.ID)
        // Clear cache manually
        s.userDao.ClearCache(newUser)
		s.userTokenDao.ClearCache(token)
		s.userPrivateDao.ClearCache(up)
		return nil
	})

	return token, err
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var TableUpdateListener tableUpdateListener = tableUpdateListener{/* contains filtered or unexported fields */}

Functions

func StructFields

func StructFields(root reflect.Type) []reflect.StructField

Types

type BaseDAO

type BaseDAO struct {
	DB            *gorm.DB
	TableMaxIdSql string
}

func NewBaseDAO

func NewBaseDAO(db *gorm.DB) *BaseDAO

func (*BaseDAO) All

func (s *BaseDAO) All(resultRef interface{})

func (*BaseDAO) Begin

func (s *BaseDAO) Begin(opts ...*sql.TxOptions) *BaseDAO

func (*BaseDAO) Count

func (s *BaseDAO) Count(sql string, values ...interface{}) uint64

func (*BaseDAO) Delete

func (s *BaseDAO) Delete(resultRef interface{}, id uint64)

func (*BaseDAO) DeleteMany

func (s *BaseDAO) DeleteMany(valueRef interface{}, ids []uint64)

func (*BaseDAO) Get

func (s *BaseDAO) Get(resultRef interface{}, id uint64)

func (*BaseDAO) GetBy

func (s *BaseDAO) GetBy(resultRef interface{}, kvs ...interface{})

func (*BaseDAO) GetByWithMap

func (s *BaseDAO) GetByWithMap(resultRef interface{}, kvs map[string]interface{})

func (*BaseDAO) GetDBFieldName

func (s *BaseDAO) GetDBFieldName(structFieldName string) string

func (*BaseDAO) GetDBTableName

func (s *BaseDAO) GetDBTableName(table string) string

func (*BaseDAO) GetMaxID

func (s *BaseDAO) GetMaxID(dbTableName string, dbIdField ...string) uint64

func (*BaseDAO) HandleError

func (s *BaseDAO) HandleError(err error)

func (*BaseDAO) Insert

func (s *BaseDAO) Insert(valueRef interface{})

func (*BaseDAO) InsertMany

func (s *BaseDAO) InsertMany(valuesRef interface{})

func (*BaseDAO) List

func (s *BaseDAO) List(resultRef interface{}, ids []uint64)

func (*BaseDAO) ListBy

func (s *BaseDAO) ListBy(resultRef interface{}, kvs ...interface{})

func (*BaseDAO) ListByWithMap

func (s *BaseDAO) ListByWithMap(resultRef interface{}, orderBy string, kvs map[string]interface{})

func (*BaseDAO) ListByWithOrder

func (s *BaseDAO) ListByWithOrder(resultRef interface{}, orderBy string, kvs ...interface{})

func (*BaseDAO) Put

func (s *BaseDAO) Put(resultRef interface{}, fields ...string)

func (*BaseDAO) Save

func (s *BaseDAO) Save(valueRef interface{})

func (*BaseDAO) Select

func (s *BaseDAO) Select(result interface{}, sql string, values ...interface{})

func (*BaseDAO) Transaction

func (s *BaseDAO) Transaction(fn func(db *BaseDAO) error, opts ...*sql.TxOptions) error

type CachedDAO

type CachedDAO struct {
	*TableDAO
	Prefix     string
	Redis      *redis.Client
	Serializer ValueSerializer
	RedisisCtx context.Context
	TTL        int        // in minutes
	Indexes    [][]string //eg. {{Uid} , {ProjectId, ModelId}}
}

func NewCachedDAO

func NewCachedDAO(db *gorm.DB, model interface{}, table string, idField string, red *redis.Client, serializer ValueSerializer, prefix string, ttlMinutes int) *CachedDAO

func (*CachedDAO) AddIndex

func (s *CachedDAO) AddIndex(index ...string) *CachedDAO

func (*CachedDAO) CacheGet

func (s *CachedDAO) CacheGet(key string, valueRef interface{}) bool

func (*CachedDAO) CacheGetUint64

func (s *CachedDAO) CacheGetUint64(key string) uint64

func (*CachedDAO) CacheSet

func (s *CachedDAO) CacheSet(key string, value interface{})

func (*CachedDAO) ClearCache

func (s *CachedDAO) ClearCache(objs ...interface{})

ClearCache clear cache after insert and delete .eg ClearCache(User{1},User{2})

func (*CachedDAO) ClearCacheForTable

func (s *CachedDAO) ClearCacheForTable()

func (*CachedDAO) ClearCacheWithMaps

func (s *CachedDAO) ClearCacheWithMaps(objs ...map[string]interface{})

func (*CachedDAO) ClearCacheWithPrefix

func (s *CachedDAO) ClearCacheWithPrefix(prefix string)

func (*CachedDAO) Delete

func (s *CachedDAO) Delete(id uint64)

func (*CachedDAO) DeleteMany

func (s *CachedDAO) DeleteMany(ids []uint64)

func (*CachedDAO) Get

func (s *CachedDAO) Get(valueRef interface{}, id uint64)

func (*CachedDAO) GetBy

func (s *CachedDAO) GetBy(valueRef interface{}, indexes ...interface{})

func (*CachedDAO) GetByWithMap

func (s *CachedDAO) GetByWithMap(valueRef interface{}, indexes map[string]interface{})

redis key eg. projectusers/pid/2/uid/1 -> id

func (*CachedDAO) GetCacheMany

func (s *CachedDAO) GetCacheMany(keys []string) []interface{}

return []string

func (*CachedDAO) GetIDValue

func (s *CachedDAO) GetIDValue(valueRef interface{}) uint64

func (*CachedDAO) GetKeyID

func (s *CachedDAO) GetKeyID(v interface{}) string

func (*CachedDAO) GetKeyMax

func (s *CachedDAO) GetKeyMax() string

func (*CachedDAO) GetMaxID

func (s *CachedDAO) GetMaxID() uint64

func (*CachedDAO) Insert

func (s *CachedDAO) Insert(valueRef interface{})

func (*CachedDAO) InsertMany

func (s *CachedDAO) InsertMany(valuesRef interface{})

func (*CachedDAO) List

func (s *CachedDAO) List(valueRef interface{}, ids []uint64)

func (*CachedDAO) ListBy

func (s *CachedDAO) ListBy(valueRef interface{}, indexes ...interface{})

func (*CachedDAO) ListByWithMap

func (s *CachedDAO) ListByWithMap(valueRef interface{}, indexes map[string]interface{})

redis key eg. projectusers/pid/2/uid/1 -> [id1,id2]

func (*CachedDAO) MakeKey

func (s *CachedDAO) MakeKey(indexValues ...interface{}) string

MakeKey MakeKey make cache key. eg. project_name/users/id/1. input like: k1,v1,k2,v2

func (*CachedDAO) MakeKeyByMap

func (s *CachedDAO) MakeKeyByMap(indexValues map[string]interface{}) string

MakeKey make cache key. eg. project_name/users/id/1

func (*CachedDAO) Put

func (s *CachedDAO) Put(resultRef interface{}, fields ...string)

func (*CachedDAO) Save

func (s *CachedDAO) Save(valueRef interface{})

type DDL

type DDL struct {
	DefaultOnDelete FKAction
	DefaultOnUpdate FKAction
	// contains filtered or unexported fields
}

func NewDDL

func NewDDL(db *gorm.DB) *DDL

func (*DDL) AddFK

func (s *DDL) AddFK(table, target interface{}, fk string)

func (*DDL) AddFKs

func (s *DDL) AddFKs(table interface{})

func (*DDL) AddForeignKey

func (s *DDL) AddForeignKey(table, fkey, target, targetCol string, onDelete, onUpdate FKAction)

func (*DDL) AddTables

func (s *DDL) AddTables(tables ...interface{})

func (*DDL) GetSchema

func (s *DDL) GetSchema(obj interface{}) *schema.Schema

func (*DDL) GetSchemaByStructName

func (s *DDL) GetSchemaByStructName(structName string) *schema.Schema

func (*DDL) MakeFKName

func (s *DDL) MakeFKName(table, fkey, target, targetCol string) string

func (*DDL) MakeFKs

func (s *DDL) MakeFKs()

func (*DDL) MatchTableName

func (s *DDL) MatchTableName(structType reflect.Type, tableName string) bool

func (*DDL) ParseFKInfo

func (s *DDL) ParseFKInfo(tag string) FKInfo

tag: Table , on delete , on update

func (*DDL) Range

func (s *DDL) Range(f func(structType reflect.Type, tableSchema *schema.Schema) bool)

type FKAction

type FKAction string
const (
	FKCascade  FKAction = "CASCADE"
	FKNoAction FKAction = "NO ACTION"
	FKSetNull  FKAction = "SET NULL"
	FKRestrict FKAction = "RESTRICT"
)

type FKInfo

type FKInfo struct {
	StructName string
	OnDelete   FKAction
	OnUpdate   FKAction
}

type JSONValueSerializer

type JSONValueSerializer struct {
}

func (*JSONValueSerializer) Marshal

func (s *JSONValueSerializer) Marshal(value interface{}) ([]byte, error)

func (*JSONValueSerializer) Unmarshal

func (s *JSONValueSerializer) Unmarshal(data []byte, value interface{}) error

type SliceRef

type SliceRef struct {
	// A slice pointer.
	Ref interface{}
	// contains filtered or unexported fields
}

func NewSliceRef

func NewSliceRef(ref interface{}) *SliceRef

func (*SliceRef) Append

func (s *SliceRef) Append(value interface{}) *SliceRef

func (*SliceRef) CreateInstance

func (s *SliceRef) CreateInstance(len, cap int) interface{}

func (*SliceRef) Get

func (s *SliceRef) Get(i int) interface{}

func (*SliceRef) GetInto

func (s *SliceRef) GetInto(i int, valueRef interface{})

func (*SliceRef) GetRef

func (s *SliceRef) GetRef(i int) interface{}

func (*SliceRef) Len

func (s *SliceRef) Len() int

func (*SliceRef) New

func (s *SliceRef) New(len, cap int) *SliceRef

func (*SliceRef) Set

func (s *SliceRef) Set(i int, value interface{})

type StructRef

type StructRef struct {
	// A struct pointer
	Ref interface{}
	// contains filtered or unexported fields
}

func NewStructRef

func NewStructRef(structRef interface{}) *StructRef

NewStructRef structRef should be a struct point

func (*StructRef) Create

func (s *StructRef) Create() *StructRef

func (*StructRef) Get

func (s *StructRef) Get(name string) interface{}

func (*StructRef) GetIgnoreCase

func (s *StructRef) GetIgnoreCase(name string) interface{}

func (*StructRef) GetInto

func (s *StructRef) GetInto(name string, valueRef interface{})

func (*StructRef) GetIntoIgnoreCase

func (s *StructRef) GetIntoIgnoreCase(name string, valueRef interface{})

func (*StructRef) GetRef

func (s *StructRef) GetRef(name string) interface{}

func (*StructRef) GetRefIgnoreCase

func (s *StructRef) GetRefIgnoreCase(name string) interface{}

func (*StructRef) Map

func (s *StructRef) Map() map[string]interface{}

func (*StructRef) Names

func (s *StructRef) Names() []string

func (*StructRef) NewSlice

func (s *StructRef) NewSlice(len, cap int) interface{}

func (*StructRef) Set

func (s *StructRef) Set(name string, value interface{}) *StructRef

func (*StructRef) SetIgnoreCase

func (s *StructRef) SetIgnoreCase(name string, value interface{}) *StructRef

type TableDAO

type TableDAO struct {
	*BaseDAO
	Model   interface{} // eg. &tables.User{}
	Table   string      // database table name of the model
	IDField string      // ID
}

func NewTableDAO

func NewTableDAO(db *gorm.DB, model interface{}, table string, idField string) *TableDAO

func (*TableDAO) Delete

func (s *TableDAO) Delete(id uint64)

func (*TableDAO) DeleteMany

func (s *TableDAO) DeleteMany(ids []uint64)

func (*TableDAO) GetMaxID

func (s *TableDAO) GetMaxID() uint64

type ValueSerializer

type ValueSerializer interface {
	Marshal(value interface{}) ([]byte, error)
	Unmarshal(data []byte, value interface{}) error
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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