gocrud

package module
v0.40.0 Latest Latest
Warning

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

Go to latest
Published: Jul 19, 2026 License: BSD-2-Clause Imports: 15 Imported by: 0

README

gocrud

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.

Check the documentation for more details.

Documentation

Index

Constants

View Source
const (
	PassOK = iota
	PassInvalid
)
View Source
const (
	DialectPostgres = "postgres"
	DialectSQLite   = "sqlite"
)
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{}) uint64

func ObjSetIDValue

func ObjSetIDValue(obj interface{}, id uint64)

func SetObjCreated

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

func SetObjModified

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

func SetObjStringField

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

func Validate

func Validate(obj interface{}, restrictFields map[string]bool, tagName string) (bool, map[string]uint64, 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{}, map[string]int, error)

Get returns the matching records and, for every password field that also appears in options.VerifyPasswordFields, either PassOK or PassInvalid — but only when exactly one record was found. Keys in VerifyPasswordFields that do not name an actual password field are ignored.

func (*CRUD) GetCount

func (c *CRUD) GetCount(ctx context.Context, obj interface{}, options GetCountOptions) (uint64, 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) *LoadOutput

func (*CRUD) PasswordFieldsFor

func (c *CRUD) PasswordFieldsFor(obj interface{}) []string

PasswordFieldsFor returns the struct field names tagged as passwords for the type of obj. Returns nil if obj has no password fields.

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 uint64)

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
}

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

	// VerifyPasswordFields maps a struct field name to a plaintext password
	// to verify against the bcrypt hash stored in that field. Verification
	// only happens when the query matches exactly one record; for any other
	// result count the returned map is empty.
	VerifyPasswordFields map[string]string
}

type LoadOptions

type LoadOptions struct {
	Unused bool

	// VerifyPasswordFields maps a struct field name to a plaintext password
	// to verify against the bcrypt hash stored in that field.
	VerifyPasswordFields map[string]string
}

type LoadOutput added in v0.36.0

type LoadOutput struct {
	Error          error
	PasswordFields map[string]int
}

LoadOutput is returned by Load. PasswordFields holds, for every password field that also appears in LoadOptions.VerifyPasswordFields, either PassOK or PassInvalid. Keys in VerifyPasswordFields that do not name an actual password field are ignored.

type Options

type Options struct {
	TableNamePrefix string
	TagName         string
	Dialect         string
	Flags           uint64
}

type SaveOptions

type SaveOptions struct {
	NoInsert   bool
	ModifiedBy uint64
	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]uint64
}

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