db

package
v0.0.0-...-a4f8eeb Latest Latest
Warning

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

Go to latest
Published: Aug 25, 2022 License: GPL-3.0 Imports: 18 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DB *gorm.DB
View Source
var DefaultID []uuid.UUID // skynet default id
View Source
var ErrSessionInvalid = tracerr.New("session invalid")
View Source
var Redis *redis.Client
View Source
var Session *redisstore.RedisStore

Functions

func DeleteSessions

func DeleteSessions(uid []uuid.UUID) error

DeleteSessions deletes all sessions by uid. This equals kick user operation.

If uid=nil, delete all sessions.

func FindSessions

func FindSessions(uid []uuid.UUID) (map[string]*SessionData, error)

FindSessionsByID find all sessions associate to user by uid. When uid = nil, return all sessions.

func GetCTXSession

func GetCTXSession(c *gin.Context) (*sessions.Session, error)

GetCTXSession gets session object from gin context.

func GetDefaultID

func GetDefaultID(index DefaultIDIndex) uuid.UUID

func NewDB

func NewDB()

NewDB connect database with config.

func NewRedis

func NewRedis()

NewRedis connect redis with config.

func NewSession

func NewSession()

NewSession connect session with config.

func ParseCondition

func ParseCondition(cond *Condition, tx *gorm.DB) *gorm.DB

ParseCondition parse Condition to gorm.DB object.

func SaveCTXSession

func SaveCTXSession(c *gin.Context) error

SaveCTXSession saves session object to gin context.

func SetDefaultID

func SetDefaultID(index DefaultIDIndex, value uuid.UUID)

Types

type Condition

type Condition struct {
	Order    []any
	Distinct []any
	Limit    any
	Offset   any
	Query    string
	Args     []any
}

Condition limit condition search, however, sqli is not protected.

Unprotected fields: Order, Distinct, Where(when not use ? as argument form)

Warning: Caller should check user input on their OWN!

func (*Condition) And

func (c *Condition) And(query string, args ...any)

func (*Condition) AndLike

func (c *Condition) AndLike(query string, arg string)

func (*Condition) MergeAnd

func (c *Condition) MergeAnd(in *Condition)

func (*Condition) MergeOr

func (c *Condition) MergeOr(in *Condition)

func (*Condition) Or

func (c *Condition) Or(query string, args ...any)

func (*Condition) OrLike

func (c *Condition) OrLike(query string, arg string)

type DBStruct

type DBStruct interface {
	ValidDBStruct()
}

DBStruct identify database struct.

type DefaultIDIndex

type DefaultIDIndex int32
const (
	GroupRootID DefaultIDIndex = iota // root user group
	PermAllID                         // full permission
	PermUserID                        // login user permission
	PermGuestID                       // guest permission

	PermManageUserID         // manage user
	PermManageUserPermID     // manage user permission
	PermManageGroupID        // manage user group
	PermManageGroupPermID    // manage user group
	PermManageNotificationID // manage notification
	PermManageSystemID       // manage system
	PermManagePluginID       // manage plugin

	DefaultIDMax // max id count
)

type GeneralFields

type GeneralFields struct {
	ID        uuid.UUID `gorm:"type:uuid;primaryKey;not null" json:"id"`
	CreatedAt int64     `gorm:"autoCreateTime:milli" json:"created_at"` // create time
	UpdatedAt int64     `gorm:"autoUpdateTime:milli" json:"updated_at"` // update time
}

func (*GeneralFields) BeforeCreate

func (u *GeneralFields) BeforeCreate(tx *gorm.DB) error

func (GeneralFields) ValidDBStruct

func (u GeneralFields) ValidDBStruct()

type Notification

type Notification struct {
	GeneralFields
	Level   NotifyLevel `gorm:"default:0;not null" json:"level"`
	Name    string      `gorm:"type:varchar(256)" json:"name"`
	Message string      `gorm:"type:varchar(256)" json:"message"`
	Detail  string      `gorm:"type:string" json:"detail"`
}

type NotifyLevel

type NotifyLevel = int32
const (
	NotifyInfo NotifyLevel = iota
	NotifySuccess
	NotifyWarning
	NotifyError
	NotifyFatal
)

type ORM

type ORM[T DBStruct] struct {
	// contains filtered or unexported fields
}

func NewORM

func NewORM[T DBStruct](tx *gorm.DB) *ORM[T]

func (*ORM[T]) Cond

func (o *ORM[T]) Cond(cond *Condition) *ORM[T]

func (*ORM[T]) Count

func (o *ORM[T]) Count(cond *Condition) (int64, error)

func (*ORM[T]) Create

func (o *ORM[T]) Create(value *T) error

func (*ORM[T]) Creates

func (o *ORM[T]) Creates(value []*T) error

func (*ORM[T]) Delete

func (o *ORM[T]) Delete(conds ...any) (int64, error)

func (*ORM[T]) DeleteAll

func (o *ORM[T]) DeleteAll() (int64, error)

func (*ORM[T]) DeleteID

func (o *ORM[T]) DeleteID(id uuid.UUID) (bool, error)

func (*ORM[T]) Find

func (o *ORM[T]) Find(conds ...any) (ret []*T, err error)

func (*ORM[T]) ID

func (o *ORM[T]) ID(id uuid.UUID) *ORM[T]

func (*ORM[T]) Joins

func (o *ORM[T]) Joins(query string, args ...any) *ORM[T]

func (*ORM[T]) Save

func (o *ORM[T]) Save(value *T) error

func (*ORM[T]) TX

func (o *ORM[T]) TX() *gorm.DB

func (*ORM[T]) Take

func (o *ORM[T]) Take(conds ...any) (ret *T, err error)

func (*ORM[T]) Update

func (o *ORM[T]) Update(column string, value any) error

func (*ORM[T]) Updates

func (o *ORM[T]) Updates(column []string, value *T) error

func (*ORM[T]) Where

func (o *ORM[T]) Where(query any, args ...any) *ORM[T]

type Permission

type Permission struct {
	GeneralFields
	UID            uuid.UUID       `gorm:"column:uid;type:uuid;uniqueIndex:perm_link" json:"uid"`
	GID            uuid.UUID       `gorm:"column:gid;type:uuid;uniqueIndex:perm_link" json:"gid"`
	PID            uuid.UUID       `gorm:"column:pid;type:uuid;uniqueIndex:perm_link;not null" json:"pid"`
	Perm           UserPerm        `gorm:"default:0;not null" json:"perm"`
	User           *User           `gorm:"foreignKey:UID" json:"-"`
	UserGroup      *UserGroup      `gorm:"foreignKey:GID" json:"-"`
	PermissionList *PermissionList `gorm:"foreignKey:PID" json:"-"`
}

func (*Permission) BeforeCreate

func (u *Permission) BeforeCreate(tx *gorm.DB) error

func (*Permission) BeforeUpdate

func (u *Permission) BeforeUpdate(tx *gorm.DB) error

type PermissionList

type PermissionList struct {
	GeneralFields
	Name string `gorm:"uniqueIndex;type:varchar(128);not null" json:"name"`
	Note string `gorm:"type:varchar(256)" json:"note"`
}

type RedisConfig

type RedisConfig struct {
	Address  string // redis address
	Password string // redis password
	DB       int    // redis db
}

RedisConfig is connection config for redis.

type SessionConfig

type SessionConfig struct {
	RedisClient *redis.Client // redis client for session
	Prefix      string        // session prefix in redis
}

SessionConfig is connection config for session.

type SessionData

type SessionData struct {
	ID uuid.UUID `gob:"id"`
}

func LoadSession

func LoadSession(session *sessions.Session) (*SessionData, error)

func LoadSessionString

func LoadSessionString(s string) (*SessionData, error)

func (*SessionData) SaveSession

func (s *SessionData) SaveSession(session *sessions.Session)

type Setting

type Setting struct {
	GeneralFields
	Name  string `gorm:"uniqueIndex;type:varchar(256);not null" json:"name"`
	Value string `gorm:"type:string" json:"value"`
}

type User

type User struct {
	GeneralFields
	Username  string `gorm:"uniqueIndex;type:varchar(32);not null" json:"username"`
	Password  string `gorm:"type:char(32);not null" json:"-"`
	Avatar    []byte `gorm:"type:bytes;not null" json:"avatar"`
	LastLogin int64  `json:"last_login"`
	LastIP    string `gorm:"type:varchar(64)" json:"last_ip"`
}

type UserGroup

type UserGroup struct {
	GeneralFields
	Name string `gorm:"type:varchar(32);uniqueIndex;not null" json:"name"`
	Note string `gorm:"type:varchar(256)" json:"note"`
}
type UserGroupLink struct {
	GeneralFields
	UID       uuid.UUID  `gorm:"column:uid;type:uuid;uniqueIndex:ugid_link;not null" json:"uid"`
	GID       uuid.UUID  `gorm:"column:gid;type:uuid;uniqueIndex:ugid_link;not null" json:"gid"`
	User      *User      `gorm:"foreignKey:UID" json:"-"`
	UserGroup *UserGroup `gorm:"foreignKey:GID" json:"-"`
}

type UserPerm

type UserPerm = int32
const (
	PermNone    UserPerm = 0        // PermNone is default, no permission
	PermExecute UserPerm = 1        // PermExecute can execute
	PermWrite   UserPerm = 1 << 1   // PermWrite can write database
	PermRead    UserPerm = 1 << 2   // PermRead can read
	PermAll     UserPerm = 1<<3 - 1 // PermAll give all permission

	PermWriteExecute UserPerm = PermWrite | PermExecute // PermWriteExecute can write and execute
)

Jump to

Keyboard shortcuts

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