domainx

package
v0.0.52 Latest Latest
Warning

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

Go to latest
Published: Jan 22, 2026 License: MIT Imports: 26 Imported by: 0

Documentation

Index

Constants

View Source
const ServiceCode = "DATABASE"

Variables

View Source
var GormDBServ = &gormDBService{}
View Source
var MigrationList []*Migration
View Source
var MongoDBServ = &mongoDBService{}

Functions

func AutoMigrate

func AutoMigrate(getDB func() (value ConTable), index ...Index)

func Check

func Check(s string) bool

func CountByCondition

func CountByCondition(c *Con, condition map[string]interface{}) (int64, *errors.Error)

CountByCondition queries the count by condition

func CountByField

func CountByField(c *Con, fieldName string, value interface{}) (int64, *errors.Error)

CountByField queries the count by field name and value

func CountByMatch

func CountByMatch(c *Con, matchList []Match) (int64, *errors.Error)

CountByMatch queries the count by condition

func Delete

func Delete(c *Con, data Identifiable) *errors.Error

func DeleteByMatch added in v0.0.20

func DeleteByMatch(c *Con, matchList []Match) *errors.Error

func ExistsByMatch added in v0.0.48

func ExistsByMatch(c *Con, matchList []Match) (bool, *errors.Error)

func FindByCondition

func FindByCondition[T any](c *Con, condition map[string]interface{}, result *[]T, prefixes ...string) (err *errors.Error)

FindByCondition queries multiple records by condition, returns up to 1000 records

func FindByField

func FindByField[T any](c *Con, fieldName string, value interface{}, result *[]T, prefixes ...string) (err *errors.Error)

FindByField queries multiple records by field name and value, returns up to 1000 records

func FindByIDs

func FindByIDs[T any](c *Con, ids []int64, result *[]T) *errors.Error

func FindByMatch

func FindByMatch[T any](c *Con, matchList []Match, result *[]T, prefixes ...string) (err *errors.Error)

FindByMatch queries multiple records by condition, returns up to 1000 records

func FindByPage

func FindByPage[T Identifiable](c *Con, condition map[string]interface{}, page *load.Page, pageResp *load.PageResp, result *[]T, prefixes ...string) *errors.Error

FindByPage queries paginated records by condition

func FindByPageField

func FindByPageField[T Identifiable](c *Con, fieldName string, value interface{}, page *load.Page, pageResp *load.PageResp, result *[]T, prefixes ...string) *errors.Error

FindByPageField queries paginated records by field name

func FindByPageMatch

func FindByPageMatch[T Identifiable](c *Con, matchList []Match, page *load.Page, pageResp *load.PageResp, result *[]T, prefixes ...string) *errors.Error

func FindByPageMatchT added in v0.0.45

func FindByPageMatchT[T Identifiable](c *Con, matchList []Match, page *load.Page, pageResp *load.PageRespT[T], result *[]T, prefixes ...string) *errors.Error

func GetByCondition

func GetByCondition[T any](c *Con, condition map[string]interface{}, result *T) (err *errors.Error)

GetByCondition queries a single record by condition

func GetByField

func GetByField[T any](c *Con, fieldName string, value interface{}, result *T) (err *errors.Error)

GetByField queries a single record by field name and value

func GetByID

func GetByID[T any](c *Con, id int64, result *T) (err *errors.Error)

func GetByMatch

func GetByMatch[T any](c *Con, matchList []Match, result *T) (err *errors.Error)

GetByMatch queries a single record by condition

func GetEntity

func GetEntity[T any](entity interface{}) (*T, *errors.Error)

func GetLastID

func GetLastID[T Identifiable](conList []T) int64

func GetListEntity

func GetListEntity[T any](docList any) (*[]T, *errors.Error)

func MustGetByID

func MustGetByID[T Identifiable](c *Con, id int64, result T) *errors.Error

func RegisterDBService

func RegisterDBService(conType ConType, s DBService)

func Save added in v0.0.17

func Save(c *Con, data Identifiable, newIDs ...int64) (id int64, err *errors.Error)

func SaveOrUpdate deprecated

func SaveOrUpdate(c *Con, data Identifiable, newIDs ...int64) (id int64, err *errors.Error)

Deprecated: This method is no longer recommended. Use Save instead.

func SumByMatch

func SumByMatch(c *Con, matchList []Match, field string) (float64, *errors.Error)

SumByMatch queries the sum by condition

func UpdateByMatch added in v0.0.30

func UpdateByMatch(c *Con, matchList []Match, data map[string]interface{}) *errors.Error

UpdateByMatch updates records by condition

func UpdatePart

func UpdatePart(c *Con, id int64, data map[string]interface{}) *errors.Error

UpdatePart updates partial fields by ID

func UseDbConn

func UseDbConn(dbname string) *gorm.DB

func UseMongoDbConn

func UseMongoDbConn(dbname string) *qmgo.Client

Types

type Complex

type Complex[T any] struct {
	*Con
	Data *T `bson:"data" gorm:"embedded" json:"data"`
	Options
}

func CreateComplex added in v0.0.45

func CreateComplex[T any](ctx context.Context, conType ConType, dbName string, table string, data *T, prefix ...string) *Complex[T]

func NewComplex

func NewComplex[T any](conType ConType, dbName string, table string) *Complex[T]

func UseComplex

func UseComplex[T any](conType ConType, dbName string, table string, prefix ...string) *Complex[T]

func UseComplexD

func UseComplexD[T any](conType ConType, dbName string, table string) Complex[T]

func (*Complex[T]) FindByPage

func (c *Complex[T]) FindByPage(condition map[string]interface{}, page *load.Page, pageResp *load.PageResp, prefixes ...string) *errors.Error

func (*Complex[T]) FindByPageField

func (c *Complex[T]) FindByPageField(fieldName string, value interface{}, page *load.Page, pageResp *load.PageResp, prefixes ...string) *errors.Error

func (*Complex[T]) FindByPageMatch

func (c *Complex[T]) FindByPageMatch(matchList []Match, page *load.Page, pageResp *load.PageResp, prefixes ...string) *errors.Error

func (*Complex[T]) GetID

func (c *Complex[T]) GetID() ID

func (*Complex[T]) IsNil

func (c *Complex[T]) IsNil() bool

func (*Complex[T]) TableName

func (c *Complex[T]) TableName() string

type ComplexList added in v0.0.45

type ComplexList[T any] []*Complex[T]

func (*ComplexList[T]) List added in v0.0.45

func (c *ComplexList[T]) List() []*T

type Con

type Con struct {
	Ctx            context.Context `gorm:"-" bson:"-" json:"-"`
	ID             int64           `gorm:"primaryKey" bson:"id" json:"id"`
	ConType        `gorm:"-" bson:"-" json:"-"`
	MysqlDB        *gorm.DB     `gorm:"-" bson:"-" json:"-"`
	MongoDB        *qmgo.Client `gorm:"-" bson:"-" json:"-"`
	DBName         string       `gorm:"-" bson:"-" json:"-"`
	GTable         string       `gorm:"-" bson:"-" json:"-"`
	Sort           Sorts        `gorm:"-" bson:"-" json:"-"`
	SelectFields   []string     `gorm:"-" bson:"-" json:"-"`
	OmitFields     []string     `gorm:"-" bson:"-" json:"-"`
	SaveCreateTime func()       `gorm:"-" bson:"-" json:"-"`
	SaveUpdateTime func()       `gorm:"-" bson:"-" json:"-"`
}

func UseCon

func UseCon(ctx context.Context, conType ConType, dbName string, table string) *Con

func (*Con) AfterCreate

func (c *Con) AfterCreate(gormDB *gorm.DB) error

func (*Con) AfterUpdate

func (c *Con) AfterUpdate(gormDB *gorm.DB) error

func (*Con) BeforeCreate

func (c *Con) BeforeCreate(gormDB *gorm.DB) error

func (*Con) BeforeUpdate

func (c *Con) BeforeUpdate(gormDB *gorm.DB) error

func (*Con) CountByCondition

func (c *Con) CountByCondition(condition map[string]interface{}) (int64, *errors.Error)

func (*Con) CountByField

func (c *Con) CountByField(fieldName string, value interface{}) (int64, *errors.Error)

func (*Con) CountByMatch

func (c *Con) CountByMatch(matchList []Match) (int64, *errors.Error)

func (*Con) Delete

func (c *Con) Delete(data Identifiable) *errors.Error

func (*Con) FindByCondition

func (c *Con) FindByCondition(condition map[string]interface{}, result *[]Identifiable, prefixes ...string) (err *errors.Error)

func (*Con) FindByField

func (c *Con) FindByField(fieldName string, value interface{}, result *[]Identifiable, prefixes ...string) (err *errors.Error)

func (*Con) FindByIDs

func (c *Con) FindByIDs(ids []int64, result *[]Identifiable) *errors.Error

func (*Con) FindByMatch

func (c *Con) FindByMatch(matchList []Match, result *[]Identifiable, prefixes ...string) (err *errors.Error)

func (*Con) GenerateID

func (c *Con) GenerateID() int64

func (*Con) GenerateId

func (c *Con) GenerateId() int64

func (*Con) GenerateSetID

func (c *Con) GenerateSetID()

func (*Con) GetByCondition

func (c *Con) GetByCondition(condition map[string]interface{}, result *Identifiable) (err *errors.Error)

func (*Con) GetByField

func (c *Con) GetByField(fieldName string, value interface{}, result Identifiable) (err *errors.Error)

func (*Con) GetByMatch

func (c *Con) GetByMatch(matchList []Match, result *Identifiable) (err *errors.Error)

func (*Con) GetCon

func (c *Con) GetCon() *Con

func (*Con) GetConStr added in v0.0.49

func (c *Con) GetConStr() string

func (*Con) GetConType

func (c *Con) GetConType() ConType

func (*Con) GetDB

func (c *Con) GetDB() any

func (*Con) GetID

func (c *Con) GetID() ID

func (*Con) HandleError

func (*Con) HandleError(tx *gorm.DB) (err *errors.Error)

func (*Con) HandleWithErr

func (c *Con) HandleWithErr(err error) (error *errors.Error)

func (*Con) MustGetDB

func (c *Con) MustGetDB() (any, *errors.Error)

func (*Con) Save added in v0.0.33

func (c *Con) Save(data Identifiable, newIDs ...int64) (id int64, err *errors.Error)

func (*Con) SaveOrUpdate

func (c *Con) SaveOrUpdate(data Identifiable, newIDs ...int64) (id int64, err *errors.Error)

func (*Con) SetID added in v0.0.10

func (c *Con) SetID(id int64)

func (*Con) SetOmitFields added in v0.0.49

func (c *Con) SetOmitFields(fields ...string)

func (*Con) SetSelectFields added in v0.0.49

func (c *Con) SetSelectFields(fields ...string)

func (*Con) TableName

func (c *Con) TableName() string

func (*Con) UpdatePart

func (c *Con) UpdatePart(id int64, data map[string]interface{}) *errors.Error

type ConTable

type ConTable interface {
	TableName() string
	GetCon() *Con
}

type ConType

type ConType string
const (
	Mysql ConType = "mysql"
	Mongo ConType = "mongo"
)

func (ConType) String

func (c ConType) String() string

type Converter

type Converter[T any] struct{}

func (*Converter[T]) FromPrimitiveD

func (c *Converter[T]) FromPrimitiveD(doc interface{}) (*T, *errors.Error)

type DBService

type DBService interface {
	Start() error
	End() error
	Migrate(con *Con, tableName string, value ConTable, indexList []Index) error
	GetByID(c *Con, id int64, result interface{}) error
	Save(c *Con, data Identifiable, newID int64, version ...int) (id int64, error error)
	UpdatePart(c *Con, id int64, data map[string]interface{}) error
	UpdateByMatch(c *Con, matchList []Match, data map[string]interface{}) error
	Delete(c *Con, data Identifiable) error
	DeleteByMatch(c *Con, matchList []Match) error
	FindByMatch(c *Con, matchList []Match, result interface{}, prefixes ...string) error
	GetByMatch(c *Con, matchList []Match, result interface{}) error
	CountByMatch(c *Con, matchList []Match) (int64, error)
	ExistsByMatch(c *Con, matchList []Match) (bool, error)
	SumByMatch(c *Con, matchList []Match, field string) (float64, error)
	FindByPageMatch(c *Con, matchList []Match, page *load.Page, total *load.Total, result interface{}, prefixes ...string) error
}

func GetDBService

func GetDBService(conType ConType) DBService

type ID

type ID int64

func (ID) Equal

func (i ID) Equal(id int64) bool

func (ID) GenerateID

func (i ID) GenerateID() int64

func (ID) GetID

func (i ID) GetID() ID

func (ID) Int64

func (i ID) Int64() int64

func (ID) IsNil

func (i ID) IsNil() bool

func (ID) IsZero

func (i ID) IsZero() bool

func (ID) New

func (i ID) New() *ID

func (ID) NotNil

func (i ID) NotNil() bool

func (ID) NotZero

func (i ID) NotZero() bool

func (ID) Reset

func (i ID) Reset()

func (ID) SetID

func (i ID) SetID(id int64)

type Identifiable

type Identifiable interface {
	GetID() ID
	SetID(id int64)
}

type IdxType

type IdxType string
const (
	Unique    IdxType = "unique"
	Idx       IdxType = "idx"
	Spatial2D IdxType = "2d"
)

type Index

type Index struct {
	IdxType IdxType
	Fields  []string
	IdxName string
}

func CtIdx

func CtIdx(idxType IdxType, fileds ...string) Index

type IndexFunc

type IndexFunc func() []Index

type Match

type Match struct {
	Field string
	Value interface{}
	Type  MatchType
}

func (*Match) ToNearMatch

func (h *Match) ToNearMatch() NearMatch

type MatchType

type MatchType string
const (
	MEq    MatchType = "="
	MLt    MatchType = "<"
	MLte   MatchType = "<="
	MGt    MatchType = ">"
	MGte   MatchType = ">="
	MNE    MatchType = "!="
	MLIKE  MatchType = "like"
	MIN    MatchType = "in"
	MNOTIN MatchType = "not in"
	// Array contains operators
	MHas    MatchType = "has"     // array contains a single value
	MHasAny MatchType = "has_any" // array contains any of values
	MHasAll MatchType = "has_all" // array contains all values
	Near    MatchType = "near"
	NearLoc MatchType = "nearloc"
	MNEmpty MatchType = "not empty"
)

type Matches

type Matches []Match

func NewMatches

func NewMatches() *Matches

func (*Matches) Add

func (m *Matches) Add(field string, value interface{}, t MatchType, ignore ...bool) *Matches

func (*Matches) AddMatch

func (m *Matches) AddMatch(match *Match) *Matches

func (*Matches) AddMatches

func (m *Matches) AddMatches(matches *Matches) *Matches

func (*Matches) Eq

func (m *Matches) Eq(field string, value interface{}, ignore ...bool) *Matches

func (*Matches) Gt

func (m *Matches) Gt(field string, value interface{}, ignore ...bool) *Matches

func (*Matches) Gte

func (m *Matches) Gte(field string, value interface{}, ignore ...bool) *Matches

func (*Matches) Has added in v0.0.49

func (m *Matches) Has(field string, value interface{}, ignore ...bool) *Matches

Has checks array field contains a single value

func (*Matches) HasAll added in v0.0.49

func (m *Matches) HasAll(field string, value interface{}, ignore ...bool) *Matches

HasAll checks array field contains all values

func (*Matches) HasAny added in v0.0.49

func (m *Matches) HasAny(field string, value interface{}, ignore ...bool) *Matches

HasAny checks array field contains any of values

func (*Matches) In

func (m *Matches) In(field string, value interface{}, ignore ...bool) *Matches

func (*Matches) Like

func (m *Matches) Like(field string, value string, ignore ...bool) *Matches

func (*Matches) Lt

func (m *Matches) Lt(field string, value interface{}, ignore ...bool) *Matches

func (*Matches) Lte

func (m *Matches) Lte(field string, value interface{}, ignore ...bool) *Matches

func (*Matches) NEmpty

func (m *Matches) NEmpty(field string) *Matches

func (*Matches) Ne

func (m *Matches) Ne(field string, value interface{}, ignore ...bool) *Matches

func (*Matches) Near

func (m *Matches) Near(latFiled, lngFiled string, lat, lng, distance float64) *Matches

func (*Matches) NearLoc

func (m *Matches) NearLoc(localField string, lat, lng, distance float64) *Matches

func (*Matches) NotIn

func (m *Matches) NotIn(field string, value interface{}, ignore ...bool) *Matches

type Migration

type Migration struct {
	DBFunc func() ConTable
	Index  []Index
}

type NearMatch

type NearMatch struct {
	LatField string
	LngField string
	Lat      float64
	Lng      float64
	Distance float64
}

type Options

type Options struct {
	CreatedAt time.Time      `gorm:"autoCreateTime:second;" bson:"createAt" json:"created_at"`
	UpdatedAt time.Time      `gorm:"autoUpdateTime:second" bson:"updateAt" json:"updated_at"`
	DeletedAt gorm.DeletedAt `json:"deleted_at" bson:"-"`
}

func (*Options) SaveCreate

func (o *Options) SaveCreate()

func (*Options) SaveUpdate

func (o *Options) SaveUpdate()

type Result

type Result[T any] struct {
	Data T
}

type Sort

type Sort struct {
	Field  string
	Asc    bool
	Prefix string
}

type Sorts

type Sorts []*Sort

func (*Sorts) AddSort

func (s *Sorts) AddSort(field string, asc bool, prefix ...string) *Sorts

func (*Sorts) GetSort

func (s *Sorts) GetSort() []*Sort

func (*Sorts) SetSort

func (s *Sorts) SetSort(sort ...*Sort) *Sorts

type ValueField

type ValueField string

func (ValueField) Check

func (v ValueField) Check(sqlKeywords ...string) bool

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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