Documentation
¶
Overview ¶
Package gormutil implements gorm helpers
Index ¶
- Variables
- 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
- func NewLogger(lvl logger.LogLevel) logger.Interface
- type DB
- 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) 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) Tables(filter *TableFilter) ([]string, error)
- func (db *DB) Update(model interface{}, names ...string) error
- type ModelBase
- type TableFilter
Constants ¶
This section is empty.
Variables ¶
var Logger logger.Interface
Logger is a zerolog-backed gorm logger
Functions ¶
func FilterTables ¶
func FilterTables(tables []string, f *TableFilter) []string
FilterTables returns tables with respect of include/exclude filters
Types ¶
type DB ¶
type DB struct {
// contains filtered or unexported fields
}
DB defines db container
func (*DB) CountBy ¶ added in v1.0.2
CountBy returns number of record in given table with given conditions
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
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