basemodel

package
v0.0.0-...-791dfb5 Latest Latest
Warning

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

Go to latest
Published: Apr 16, 2020 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const (
	MysqlAdapter    string = "mysql_adapter"
	PostgresAdapter string = "postgres_adapter"
)

used constants

Variables

View Source
var DB *gorm.DB

DB is a connected db instance

Functions

func Close

func Close()

Close DB connection

func Create

func Create(i interface{}) error

Create creates row based on model

func DBConnect

func DBConnect(gormAdapter string, connectionString string, conf DBConfig) (err error)

DBConnect connects to db instance

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 FindbyID

func FindbyID(i interface{}, id uint64) (err error)

FindbyID finds row by id.

func First

func First(i interface{}, where ...interface{}) error

First get first matched record

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 Last

func Last(i interface{}, where ...interface{}) error

Last get last matched record

func Save

func Save(i interface{}) error

Save updates row based on model

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 Start

func Start(conf DBConfig)

Start set basemodel config

func WithinTransaction

func WithinTransaction(fn DBFunc) (err error)

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

func (*BaseModel) SetPaginationFilter

func (b *BaseModel) SetPaginationFilter(page int, rows int, orders []string, sorts []string)

SetPaginationFilter set default filter for pagination

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 DBFunc

type DBFunc func(tx *gorm.DB) error

DBFunc gorm trx function

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

Jump to

Keyboard shortcuts

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