growl

package module
v0.0.0-...-02ca4ed Latest Latest
Warning

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

Go to latest
Published: Sep 19, 2022 License: MIT Imports: 20 Imported by: 0

README

Growl

Build Status codecov

this package is deprecated

Overview

Growl is another layer for https://github.com/jinzhu/gorm, https://github.com/go-redis/redis and https://github.com/patrickmn/go-cache

  • Simple config file for db, redis
  • ORM like
  • Auto set/get cache on query
Installation
go get github.com/homina/growl
Import package in your project
import (
    "github.com/homina/growl"
)
Config file
growl:
  database:
    driver: mysql
    url: root:@/
    name: "growl_test?charset=utf8&parseTime=True&loc=Local&sql_mode='ALLOW_INVALID_DATES'"
    prefix:  
    singulartable: false # default : false
  redis:
    host: localhost
    port: "6379"
    password:
    channel: "channel"
    enable: true
  misc:
    localcache: true # enable go-cache
    log: true # enable gorm log
    flushatinit: true # flush cache at start
Example
package main

import (
  "fmt"

  "github.com/homina/growl"
  _ "github.com/jinzhu/gorm/dialects/mysql"
)

type TestTable struct {
  Name string `valid:"required" gorm:"unique_index"`
  Id   int    `gorm:"AUTO_INCREMENT"`
}

func (test *TestTable) Db() (db growl.Db) {
  return db.SetData(test)
}

func migrateTestTable() {
  conn, _ := growl.Conn()
  conn.AutoMigrate(TestTable{})
}

func deleteTestTable() {
  conn, _ := growl.Conn()
  conn.DropTable(TestTable{})
}

func main() {
  growl.Config.Path = "conf.yaml"
  err := growl.Config.Load()
  if err != nil {
    fmt.Println(err)
    return
  }

  migrateTestTable()

  test := new(TestTable)
  test.Name = "test01"
  err = test.Db().Save().Error()
  if err != nil {
    fmt.Println(err)
    return
  }

  fmt.Printf("%+v", test)
  // &{Name:test01 Id:1}

  test.Name = "test02"
  err = test.Db().Model(test).Where("id = ?",test.Id).Update().Error()
  if err != nil {
    fmt.Println(err)
    return
  }

  err = test.Db().First().Error()
  if err != nil {
    fmt.Println(err)
    return
  }
  fmt.Printf("%+v", test)
  // &{Name:test02 Id:1}

  err = test.Db().Where("id = ?",test.Id).Delete().Error()
  if err != nil {
    fmt.Println(err)
    return
  }

  deleteTestTable()
}
Validation

Reference : https://github.com/asaskevich/govalidator

todo
  • debug mode
  • optimize raw
Test
  • docker run -d -p 6379:6379 --name=redis redis:latest
  • docker run -d -p 3306:3306 --name=mysql -e MYSQL_ALLOW_EMPTY_PASSWORD=yes mysql:5.7
  • make test

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrFileNotExist     = errors.New("config file not exist(default : conf.yaml)")
	ErrDbDriverRequired = errors.New("database driver is required")
	ErrDbUrlRequired    = errors.New("database url is required")
	ErrDbNameRequired   = errors.New("database name is required")
	ErrCacheDisabled    = errors.New("cache is disabled")
	ErrCacheNotFound    = errors.New("cache not found")
)
View Source
var (
	Config     growlConfig
	YamlConfig growlYamlConfig
)
View Source
var LocalCache = gocache.New(YamlConfig.Growl.Redis.duration, 30*time.Minute)

Functions

func Cache

func Cache() *codecStruct

func Codec

func Codec() *codecStruct

func Conn

func Conn() (db *gorm.DB, err error)

func DeleteLookup

func DeleteLookup(id string)

func FlushCache

func FlushCache()

func GetCache

func GetCache(key string, data interface{}, options ...interface{}) error

func GetStructName

func GetStructName(data interface{}) string

func GetValue

func GetValue(v reflect.Value) interface{}

func IsFileExist

func IsFileExist(path string) bool

func MD5

func MD5(plain string) string

func OpenConnectionStats

func OpenConnectionStats() int

func PingCache

func PingCache() error

func Redis

func Redis() *redis.Client

func SetCache

func SetCache(key string, data interface{}, options ...interface{})

func ToSnake

func ToSnake(in string) string

Types

type Db

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

func (Db) AppendLookup

func (db Db) AppendLookup(lu *lookUp, key string) *lookUp

func (Db) Association

func (db Db) Association(qry string) Db

func (Db) Begin

func (db Db) Begin() Db

func (Db) Commit

func (db Db) Commit() Db

func (Db) Count

func (db Db) Count(data interface{}) Db

func (Db) Delete

func (db Db) Delete() Db

func (Db) Error

func (db Db) Error() error

func (Db) Find

func (db Db) Find(data interface{}) Db

func (Db) First

func (db Db) First() Db

func (Db) ForceUpdate

func (db Db) ForceUpdate() Db

func (Db) GenerateSelectRaw

func (db Db) GenerateSelectRaw() string

func (Db) GetTableName

func (db Db) GetTableName() string

func (Db) GetTx

func (db Db) GetTx() *gorm.DB

func (Db) Group

func (db Db) Group(qry string) Db

func (Db) Join

func (db Db) Join(qry string, params ...interface{}) Db

func (Db) Limit

func (db Db) Limit(qry int, filters ...func(int) int) Db

func (Db) LookupKey

func (db Db) LookupKey(id string) string

func (Db) Model

func (db Db) Model(data interface{}) Db

func (Db) NewLookup

func (db Db) NewLookup() *lookUp

func (Db) Offset

func (db Db) Offset(qry int, filters ...func(int) int) Db

func (Db) Or

func (db Db) Or(qry string, params ...interface{}) Db

func (Db) OrderBy

func (db Db) OrderBy(qry string, filters ...func(string) string) Db

func (Db) Preload

func (db Db) Preload(qry string) Db

func (Db) Replace

func (db Db) Replace(data interface{}) Db

func (Db) Rollback

func (db Db) Rollback() Db

func (Db) Save

func (db Db) Save() Db

func (Db) Select

func (db Db) Select(qry string, filters ...func(string) string) Db

func (Db) SetCacheDuration

func (db Db) SetCacheDuration(duration time.Duration) Db

func (Db) SetData

func (db Db) SetData(data interface{}) Db

func (Db) SetTx

func (db Db) SetTx(tx *gorm.DB) Db

func (Db) Update

func (db Db) Update() Db

func (Db) UpdateMap

func (db Db) UpdateMap(data map[string]interface{}) Db

func (Db) Where

func (db Db) Where(qry string, params ...interface{}) Db

Jump to

Keyboard shortcuts

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