crudui

package module
v0.8.1 Latest Latest
Warning

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

Go to latest
Published: Jan 10, 2025 License: BSD-2-Clause Imports: 15 Imported by: 2

README

crud-ui

Go Reference Go Report Card

Package CRUD UI allows you to run a simple administration panel for struct instances stored in the database. The idea is that you define structs, attach ORM and run a simple function.

Example usage

Please check a sample application in the cmd directory or type in make run-sample-app1 to start it, and navigate to http://localhost:9001/ui/.

screenshot

Documentation

Index

Constants

View Source
const MsgFailure = 2
View Source
const MsgSuccess = 1
View Source
const OpsCreate = 8
View Source
const OpsDelete = 64
View Source
const OpsList = 128
View Source
const OpsRead = 16
View Source
const OpsUpdate = 32
View Source
const VERSION = "0.8.1"

Variables

View Source
var ValuesMultipleBitChoice = 1
View Source
var ValuesSingleChoice = 2

Functions

func IsFieldKindSupported

func IsFieldKindSupported(k reflect.Kind) bool

IsFieldKindSupported checks if a field kind is supported by this module

Types

type ContextValue

type ContextValue string

type Controller

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

Controller is the main component that gets and saves objects in the database and generates HTTP handler that can be attached to an HTTP server.

func NewController

func NewController(dbConn *sql.DB, tblPrefix string, cfg *ControllerConfig) *Controller

NewController returns new Controller object

func (*Controller) GetORM

func (c *Controller) GetORM() ORM

func (*Controller) Handler

func (c *Controller) Handler(uri string, objFuncs ...func() interface{}) http.Handler

Handler returns an HTTP handler that can be attached to HTTP server. It runs a simple UI that allows managing the data. Each of the func() argument should be funcs that create objects that are meant to be managed in the UI.

type ControllerConfig

type ControllerConfig struct {
	TagName           string
	PasswordGenerator func(string) string
	IntFieldValues    map[string]IntFieldValues
	StringFieldValues map[string]StringFieldValues
	ORM               ORM
}

type ErrController

type ErrController struct {
	Op  string
	Err error
}

ErrController wraps original error that occurred in Err with name of the operation/step that failed, which is in Op field

func (ErrController) Error

func (e ErrController) Error() string

func (ErrController) Unwrap

func (e ErrController) Unwrap() error

type ErrValidation

type ErrValidation struct {
	Fields map[string]int
	Err    error
}

ErrValidation wraps error occurring during object validation

func (ErrValidation) Error

func (e ErrValidation) Error() string

func (ErrValidation) Unwrap

func (e ErrValidation) Unwrap() error

type HandlerConfig

type HandlerConfig struct {
	UserNameFunc func(int) string
}

type IntFieldValues

type IntFieldValues struct {
	Type   int
	Values map[int]string
}

type ORM

type ORM interface {
	// RegisterStruct initializes a specific object. ORMs often need to reflect the object to get the fields, build SQL queries etc.
	// When doing that, certain things such as tags can be inherited from another object. This is in the scenario where there is a root object (eg. Product) that contains all the validation tags and
	// another struct with less fields should be used as an input for API (eg. Product_WithoutCertainFields). In such case, there is no need to re-define tags such as validation.
	// Parameter `forceNameForDB` allows forcing another struct name (which later is used for generating table name).
	// This interface is based on the struct2db module and that module allows some cascade operations (such as delete or update). For this to work, and when certain fields are other structs, ORM must go
	// deeper and initializes that guys as well. When setting useOnlyRootFromInheritedObj to true, it's being avoided.
	RegisterStruct(obj interface{}, inheritFromObj interface{}, overwriteExisting bool, forceNameForDB string, useOnlyRootFromInheritedObj bool) error
	// CreateTables create database tables for struct instances
	CreateTables(objs ...interface{}) error
	// DeleteMultiple removes struct items by their ids
	DeleteMultiple(obj interface{}, filters map[string]interface{}) error
	// Get fetches data from the database and returns struct instances. Hence, it requires a constructor for the returned objects. Apart from the self-explanatory fields, filters in a format of (field name, any value)
	// can be added, and each returned object (based on a database row) can be transformed into anything else.
	Get(newObjFunc func() interface{}, order []string, limit int, offset int, filters map[string]interface{}, rowObjTransformFunc func(interface{}) interface{}) ([]interface{}, error)
	// GetCount returns number of struct items found in the database
	GetCount(newObjFunc func() interface{}, filters map[string]interface{}) (int64, error)
	// Load populates struct instance's field values with database values
	Load(obj interface{}, id string) error
	// Save stores (creates or updates) struct instance in the appropriate database table
	Save(obj interface{}) error
	// Delete removes struct instance from the database table
	Delete(obj interface{}) error
}

type ORMError

type ORMError interface {
	// IsInvalidFilters returns true when error is caused by invalid value of the filters when getting objects
	IsInvalidFilters() bool
	// Unwraps unwarps the original error
	Unwrap() error
	// Error returns error string
	Error() string
}

type StringFieldValues

type StringFieldValues struct {
	Type   int
	Values map[string]string
}

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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