Documentation ¶
Overview ¶
Package gormutil implements gorm helpers
Index ¶
- Constants
- func Changeset(model interface{}, names []string) (map[string]interface{}, error)
- func FilterTables(tables []string, f *TableFilter) []string
- func Find[T any](tx *gorm.DB) []T
- func First[T any](tx *gorm.DB) *T
- type ConfigureFunc
- type DB
- func (db *DB) AfterCreateHook(model interface{})
- func (db *DB) AfterDeleteHook(model interface{})
- func (db *DB) AfterUpdateHook(model interface{})
- func (db *DB) Conn() *gorm.DB
- func (db *DB) Count(model interface{}) int64
- func (db *DB) CountBy(model interface{}, cond interface{}, args ...interface{}) int64
- func (db *DB) Create(model interface{}) error
- func (db *DB) Delete(model interface{}, conds ...interface{}) error
- func (db *DB) DeleteByID(model interface{}, id string) error
- func (db *DB) ExistsBy(model interface{}, cond interface{}, args ...interface{}) bool
- func (db *DB) ExistsByID(model interface{}, id string) bool
- func (db *DB) Export(filter *TableFilter) (map[string]interface{}, error)
- func (db *DB) ExportTable(table string) ([]map[string]interface{}, error)
- func (db *DB) Import(data map[string]interface{}, filter *TableFilter) error
- func (db *DB) ImportTable(table string, rows []interface{}) error
- func (db *DB) RegisterValidation(tag string, fn validator.Func) error
- func (db *DB) RegisterValidationTagNameFunc(fn validator.TagNameFunc)
- func (db *DB) SubscribeHook(model interface{}, fn HookHandlerFunc)
- func (db *DB) Tables(filter *TableFilter) ([]string, error)
- func (db *DB) Update(model interface{}, names ...string) error
- func (db *DB) Validate(model interface{}) error
- func (db *DB) WithHooks()
- type Hook
- type HookBus
- type HookEvent
- type HookHandlerFunc
- type HookSubscription
- type ModelBase
- type TableFilter
Constants ¶
const ( // HookAfterCreate is event name for AfterCreate hook HookAfterCreate = "AfterCreate" // HookAfterUpdate is event name for AfterUpdate hook HookAfterUpdate = "AfterUpdate" // HookAfterDelete is event name for AfterDelete hook HookAfterDelete = "AfterDelete" )
Variables ¶
This section is empty.
Functions ¶
func FilterTables ¶
func FilterTables(tables []string, f *TableFilter) []string
FilterTables returns tables with respect of include/exclude filters
Types ¶
type ConfigureFunc ¶ added in v1.4.0
ConfigureFunc defines configurator func
func WithLocks ¶ added in v1.4.0
func WithLocks() ConfigureFunc
WithLocks enables mutex locks during create/update/delete calls
func WithLogger ¶ added in v1.4.0
func WithLogger(l logger.Interface) ConfigureFunc
WithLogger sets given logger as gorm logger
func WithNowFunc ¶ added in v1.4.0
func WithNowFunc(fn func() time.Time) ConfigureFunc
WithNowFunc sets given func as gorm now func
type DB ¶
type DB struct {
// contains filtered or unexported fields
}
DB defines db container
func Open ¶
func Open(dialector gorm.Dialector, fns ...ConfigureFunc) (*DB, error)
Open initializes db session based on dialector
func (*DB) AfterCreateHook ¶ added in v1.1.0
func (db *DB) AfterCreateHook(model interface{})
AfterCreateHook publishes hook after create
func (*DB) AfterDeleteHook ¶ added in v1.1.0
func (db *DB) AfterDeleteHook(model interface{})
AfterDeleteHook publishes hook after delete
func (*DB) AfterUpdateHook ¶ added in v1.1.0
func (db *DB) AfterUpdateHook(model interface{})
AfterUpdateHook publishes hook after update
func (*DB) DeleteByID ¶ added in v1.1.0
DeleteByID deletes given record with given id from the db table
func (*DB) ExistsBy ¶
ExistsBy checks whether given model exists with given conditions
@TODO: try to optimize the query to something like SELECT EXISTS(SELECT 1 FROM vaults WHERE id="foobar" LIMIT 1);
func (*DB) ExistsByID ¶
ExistsByID checks whether given model exists with given id
func (*DB) Export ¶
func (db *DB) Export(filter *TableFilter) (map[string]interface{}, error)
Export returns map of all tables with their rows
func (*DB) ExportTable ¶
ExportTable returns all rows of given table as slice of maps
func (*DB) Import ¶
func (db *DB) Import(data map[string]interface{}, filter *TableFilter) error
Import inserts given data into db
func (*DB) ImportTable ¶
ImportTable inserts given rows into given table
func (*DB) RegisterValidation ¶ added in v1.1.4
RegisterValidation adds a custom validation for the given tag
func (*DB) RegisterValidationTagNameFunc ¶ added in v1.1.9
func (db *DB) RegisterValidationTagNameFunc(fn validator.TagNameFunc)
RegisterValidationTagNameFunc registers a function to get alternate names for StructFields
func (*DB) SubscribeHook ¶ added in v1.1.0
func (db *DB) SubscribeHook(model interface{}, fn HookHandlerFunc)
SubscribeHook creates subscription for create/update/delete changes of given model
func (*DB) Tables ¶
func (db *DB) Tables(filter *TableFilter) ([]string, error)
Tables returns list of existing db tables respecing the given filter
type HookBus ¶ added in v1.1.0
type HookBus struct {
// contains filtered or unexported fields
}
HookBus maintains the set of subscriptions and broadcast any incoming hooks
type HookEvent ¶ added in v1.1.0
type HookEvent string
HookEvent represents event that triggered hook
func (HookEvent) IsAfterCreate ¶ added in v1.1.0
IsAfterCreate checks whether event is "AfterCreate"
func (HookEvent) IsAfterDelete ¶ added in v1.1.0
IsAfterDelete checks whether event is "AfterDelete"
func (HookEvent) IsAfterUpdate ¶ added in v1.1.0
IsAfterUpdate checks whether event is "AfterUpdate"
type HookHandlerFunc ¶ added in v1.1.0
type HookHandlerFunc func(hook *Hook)
HookHandlerFunc is a subscription's callback
type HookSubscription ¶ added in v1.1.0
type HookSubscription struct {
// contains filtered or unexported fields
}
HookSubscription defines hook's subscription
type ModelBase ¶
type ModelBase struct { ID uuid.UUID `gorm:"type:uuid;primaryKey;uniqueIndex" json:"id" validate:"required"` CreatedAt time.Time `json:"createdAt"` UpdatedAt time.Time `json:"updatedAt"` }
ModelBase contains common columns for all tables
func (*ModelBase) GenerateID ¶
GenerateID generates and assigns new id value
type TableFilter ¶
type TableFilter struct { IncludeTables []string `json:"includeTables"` ExcludeTables []string `json:"excludeTables"` }
TableFilter defines table filtering options
Directories ¶
Path | Synopsis |
---|---|
Package zerologger implements gorm zerolog adapter
|
Package zerologger implements gorm zerolog adapter |