crud

package module
v0.22.0 Latest Latest
Warning

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

Go to latest
Published: Mar 24, 2026 License: BSD-2-Clause Imports: 14 Imported by: 0

README

struct-crud

Package crud uses reflection to create CRUD operations for structs.

⚠️ The project might be good for small prototyping and showcase of reflection package. It is not recommended to use it in production. Please use a more mature library instead.

Example usage

TL;DR

Go through *_test.go files, starting with main_test.go to get working examples. The tests should be covering all the use cases.

Documentation

Index

Constants

View Source
const (
	GetCountOnUniq = 1
)
View Source
const (
	IDField = "ID"
)

Variables

This section is empty.

Functions

func ObjFieldInterfaces

func ObjFieldInterfaces(obj interface{}, includeID bool) []interface{}

ObjFieldInterfaces returns a list of interfaces to object's fields Argument includeID tells it to include or omit the ID field

func ObjFieldValue

func ObjFieldValue(obj interface{}, fieldName string) interface{}

func ObjIDInterface

func ObjIDInterface(obj interface{}) interface{}

func ObjIDValue

func ObjIDValue(obj interface{}) int64

func SetObjCreated

func SetObjCreated(obj interface{}, at int64, by int64)

func SetObjModified

func SetObjModified(obj interface{}, at int64, by int64)

func SetObjStringField

func SetObjStringField(obj interface{}, passField, hashField string)

func Validate

func Validate(obj interface{}, restrictFields map[string]bool, tagName string) (bool, map[string]int, error)

func ValidateFilters

func ValidateFilters(obj interface{}, filters *sqlfilters.Filters, tagName string) error

func ZeroObjFields

func ZeroObjFields(obj interface{})

Types

type CRUD

type CRUD struct {
	// contains filtered or unexported fields
}

func New

func New(db *sql.DB, options Options) *CRUD

func (*CRUD) CreateTable

func (c *CRUD) CreateTable(ctx context.Context, obj interface{}) error

CreateTable creates a database table for the specified object.

func (*CRUD) Delete

func (c *CRUD) Delete(ctx context.Context, obj interface{}, options DeleteOptions) error

Delete removes an object from the database table only when ID field is set (greater than 0). Once deleted from the DB, all field values are zeroed

func (*CRUD) DeleteMultiple

func (c *CRUD) DeleteMultiple(ctx context.Context, obj interface{}, options DeleteMultipleOptions) error

DeleteMultiple removes objects from the database based on specified filters

func (*CRUD) DropTable

func (c *CRUD) DropTable(ctx context.Context, obj interface{}) error

DropTable drops the database table for the specified object.

func (*CRUD) Get

func (c *CRUD) Get(ctx context.Context, newObjFunc func() interface{}, options GetOptions) ([]interface{}, error)

func (*CRUD) GetCount

func (c *CRUD) GetCount(ctx context.Context, obj interface{}, options GetCountOptions) (int64, error)

GetCount runs a 'SELECT COUNT(*)' query on the database with specified filters, order, limit and offset and returns count of rows

func (*CRUD) Load

func (c *CRUD) Load(ctx context.Context, obj interface{}, id string, options LoadOptions) error

func (*CRUD) Save

func (c *CRUD) Save(ctx context.Context, obj interface{}, options SaveOptions) error

Save takes an object, validates its field values, and saves it in the database. If ID is not present, then an INSERT will be performed. If ID is set, then an "upsert" is performed.

func (*CRUD) SetFlag

func (c *CRUD) SetFlag(flag int64)

func (*CRUD) UpdateMultiple

func (c *CRUD) UpdateMultiple(ctx context.Context, obj interface{}, fieldsToUpdate map[string]interface{}, options UpdateMultipleOptions) error

type CRUDError

type CRUDError struct {
	Op  string
	Tag string
	Err error
}

func (*CRUDError) Error

func (e *CRUDError) Error() string

type DeleteMultipleOptions

type DeleteMultipleOptions struct {
	Filters            *sqlfilters.Filters
	CascadeDeleteDepth int8
}

type DeleteOptions

type DeleteOptions struct{}

type GetCountOptions

type GetCountOptions struct {
	Filters                  *sqlfilters.Filters
	ConvertFiltersFromString bool
}

type GetOptions

type GetOptions struct {
	Order                    []string
	Limit                    int
	Offset                   int
	Filters                  *sqlfilters.Filters
	RowObjTransformFunc      func(interface{}) interface{}
	ConvertFiltersFromString bool
}

type LoadOptions

type LoadOptions struct {
	Unused bool
}

type Options

type Options struct {
	TableNamePrefix string
	TagName         string
	Flags           int64
}

type SaveOptions

type SaveOptions struct {
	NoInsert   bool
	ModifiedBy int64
	ModifiedAt int64
}

type UniqError

type UniqError struct {
	Field string
}

func (*UniqError) Error

func (e *UniqError) Error() string

type UpdateMultipleOptions

type UpdateMultipleOptions struct {
	Filters                 *sqlfilters.Filters
	ConvertValuesFromString bool
}

type ValidationError

type ValidationError struct {
	Violations map[string]int
}

func (*ValidationError) Error

func (e *ValidationError) Error() string

Jump to

Keyboard shortcuts

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