Documentation ¶
Index ¶
- Constants
- Variables
- func Close()
- func Create(i interface{}) error
- func DBConnect(gormAdapter string, connectionString string, conf DBConfig) (err error)
- func Delete(i interface{}, where ...interface{}) error
- func FindFilter(i interface{}, order []string, sort []string, limit int, offset int, ...) (interface{}, error)
- func FindbyID(i interface{}, id uint64) (err error)
- func First(i interface{}, where ...interface{}) error
- func FirstOrCreate(i interface{}, where ...interface{}) error
- func FirstOrInit(i interface{}, where ...interface{}) error
- func Last(i interface{}, where ...interface{}) error
- func Save(i interface{}) error
- func SaveOrCreate(i interface{}, where ...interface{}) error
- func SingleFindFilter(i interface{}, filter interface{}) (err error)
- func Start(conf DBConfig)
- func WithinTransaction(fn DBFunc) (err error)
- type BaseInterfaceAfterCreate
- type BaseModel
- type BaseModules
- type CompareFilter
- type DBConfig
- type DBFunc
- type PagedFindResult
Constants ¶
const ( MysqlAdapter string = "mysql_adapter" PostgresAdapter string = "postgres_adapter" )
used constants
Variables ¶
var DB *gorm.DB
DB is a connected db instance
Functions ¶
func Delete ¶
func Delete(i interface{}, where ...interface{}) error
Delete removes row in db based on model.
func FindFilter ¶
func FindFilter(i interface{}, order []string, sort []string, limit int, offset int, filter interface{}) (interface{}, error)
FindFilter finds by filter. limit 0 to find all
func FirstOrCreate ¶
func FirstOrCreate(i interface{}, where ...interface{}) error
FirstOrCreate gets first matched record, or create a new one
func FirstOrInit ¶
func FirstOrInit(i interface{}, where ...interface{}) error
FirstOrInit gets first matched record, or create a new one
func SaveOrCreate ¶
func SaveOrCreate(i interface{}, where ...interface{}) error
SaveOrCreate saves if record found, else create new.
func SingleFindFilter ¶
func SingleFindFilter(i interface{}, filter interface{}) (err error)
SingleFindFilter finds by filter
func WithinTransaction ¶
WithinTransaction sql execute helper
Types ¶
type BaseInterfaceAfterCreate ¶
type BaseInterfaceAfterCreate interface { BeforeSave(data interface{}) AfterSave(data interface{}) }
BaseInterfaceAfterCreate not must exist or optionally implemented
type BaseModel ¶
type BaseModel struct { //main basemodel and stored in db too ID uint64 `json:"id" gorm:"primary_key"` CreatedAt time.Time `json:"created_at"` UpdatedAt time.Time `json:"updated_at"` DeletedAt *time.Time `json:"deleted_at" sql:"index"` //field helper for rows filter, i.e. pagination Rows int `json:"-" gorm:"-"` Page int `json:"-" gorm:"-"` Order []string `json:"-" gorm:"-"` Sort []string `json:"-" gorm:"-"` }
BaseModel will be used as foundation of all models
func (*BaseModel) PagedFindFilter ¶
func (b *BaseModel) PagedFindFilter(i interface{}, filter interface{}, allfieldcondition ...string) (result PagedFindResult, err error)
PagedFindFilter simplified
type BaseModules ¶
type BaseModules struct { //Pagination Filter AfterCreate []BaseInterfaceAfterCreate }
BaseModules TODO acts like middleware, execute sequenced
type CompareFilter ¶
type CompareFilter struct { Value1 interface{} `json:"value1"` Value2 interface{} `json:"value2"` }
CompareFilter filter used for comparing 2 values
type DBConfig ¶
type DBConfig struct { Adapter string Host string Port string Username string Password string Db string Timezone string Maxlifetime int IdleConnection int OpenConnection int SSL string Logmode bool }
DBConfig contains db configs
type PagedFindResult ¶
type PagedFindResult struct { TotalData int `json:"total_data"` // matched datas Rows int `json:"rows"` // shown datas per page CurrentPage int `json:"current_page"` // current page LastPage int `json:"last_page"` From int `json:"from"` // offset, starting index of data shown in current page To int `json:"to"` // last index of data shown in current page Data interface{} `json:"data"` }
PagedFindResult pagination format
func PagedFindFilter ¶
func PagedFindFilter(i interface{}, page int, rows int, order []string, sort []string, filter interface{}, allfieldcondition ...string) (result PagedFindResult, err error)
PagedFindFilter same with FindFilter but with pagination