Documentation ¶
Overview ¶
Package CRUD is meant to make two things: map structs to PostgreSQL tables (like ORM) and create CRUD HTTP endpoint for simple data management.
HTTP endpoint can be set to allow creating, updating, removing new object, along with returning its details, or list of objects. All requests and responses are in the JSON format.
Please follow GitHub page for an example: https://github.com/MikolajGasior/go-mod-crud/
Index ¶
- Constants
- type Controller
- func (c Controller) CreateDBTable(obj interface{}) *ErrController
- func (c Controller) CreateDBTables(xobj ...interface{}) *ErrController
- func (c Controller) DeleteFromDB(obj interface{}) *ErrController
- func (c Controller) DropDBTable(obj interface{}) *ErrController
- func (c Controller) DropDBTables(xobj ...interface{}) *ErrController
- func (c Controller) GetFiltersInterfaces(mf map[string]interface{}) []interface{}
- func (c Controller) GetFromDB(newObjFunc func() interface{}, order []string, limit int, offset int, ...) ([]interface{}, *ErrController)
- func (c Controller) GetHTTPHandler(uri string, newObjFunc func() interface{}, newObjCreateFunc func() interface{}, ...) http.Handler
- func (c Controller) GetModelFieldInterfaces(obj interface{}) []interface{}
- func (c *Controller) GetModelIDInterface(obj interface{}) interface{}
- func (c *Controller) GetModelIDValue(obj interface{}) int64
- func (c Controller) ResetFields(obj interface{})
- func (c Controller) SaveToDB(obj interface{}) *ErrController
- func (c Controller) SetFromDB(obj interface{}, id string) *ErrController
- func (c Controller) Validate(obj interface{}, filters map[string]interface{}) (bool, map[string]int, error)
- type ErrController
- type ErrHelper
- type ErrValidation
- type HTTPResponse
- type Helper
- func (h *Helper) Err() *ErrHelper
- func (h *Helper) GetFlags() int
- func (h Helper) GetQueryCreateTable() string
- func (h *Helper) GetQueryDeleteById() string
- func (h Helper) GetQueryDropTable() string
- func (h *Helper) GetQueryInsert() string
- func (h *Helper) GetQuerySelect(order []string, limit int, offset int, filters map[string]interface{}, ...) string
- func (h *Helper) GetQuerySelectById() string
- func (h *Helper) GetQueryUpdateById() string
Constants ¶
const OpCreate = 8
const OpDelete = 16
const OpList = 32
const OpRead = 2
Values for CRUD operations
const OpUpdate = 4
const TypeInt = 128
const TypeInt64 = 64
const TypeString = 256
const VERSION = "0.3.0"
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
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 CRUD HTTP handler that can be attached to an HTTP server.
func NewController ¶
func NewController(dbConn *sql.DB, tblPrefix string) *Controller
NewController returns new Controller object
func (Controller) CreateDBTable ¶
func (c Controller) CreateDBTable(obj interface{}) *ErrController
CreateDBTable creates database table to store specified type of objects. It takes struct name and its fields, converts them into table and columns names (all lowercase with underscore), assigns column type based on the field type, and then executes "CREATE TABLE" query on attached DB connection
func (Controller) CreateDBTables ¶
func (c Controller) CreateDBTables(xobj ...interface{}) *ErrController
CreateDBTables creates tables in the database for specified objects (see CreateDBTable for a single struct)
func (Controller) DeleteFromDB ¶
func (c Controller) DeleteFromDB(obj interface{}) *ErrController
DeleteFromDB removes object from the database table and it does that only when ID field is set (greater than 0). Once deleted from the DB, all field values are zeroed
func (Controller) DropDBTable ¶
func (c Controller) DropDBTable(obj interface{}) *ErrController
DropDBTable drops database table used to store specified type of objects. It just takes struct name, converts it to lowercase-with-underscore table name and executes "DROP TABLE" query using attached DB connection
func (Controller) DropDBTables ¶
func (c Controller) DropDBTables(xobj ...interface{}) *ErrController
DropDBTables drop tables in the database for specified objects (see DropDBTable for a single struct)
func (Controller) GetFiltersInterfaces ¶
func (c Controller) GetFiltersInterfaces(mf map[string]interface{}) []interface{}
GetFiltersInterfaces returns list of interfaces from filters map (used in querying)
func (Controller) GetFromDB ¶
func (c Controller) GetFromDB(newObjFunc func() interface{}, order []string, limit int, offset int, filters map[string]interface{}) ([]interface{}, *ErrController)
GetFromDB runs a select query on the database with specified filters, order, limit and offset and returns a list of objects
func (Controller) GetHTTPHandler ¶
func (c Controller) GetHTTPHandler(uri string, newObjFunc func() interface{}, newObjCreateFunc func() interface{}, newObjReadFunc func() interface{}, newObjUpdateFunc func() interface{}, newObjDeleteFunc func() interface{}, newObjListFunc func() interface{}) http.Handler
GetHTTPHandler returns a CRUD HTTP handler that can be attached to HTTP server. It creates a CRUD endpoint for creating, reading, updating, deleting and listing objects. Each of the func() argument should be funcs that create new object (instance of a struct). For each of the operation (create, read etc.), a different struct with different fields can be used. It's important to pass "uri" argument same as the one that the handler is attached to.
func (Controller) GetModelFieldInterfaces ¶
func (c Controller) GetModelFieldInterfaces(obj interface{}) []interface{}
GetModelFieldInterfaces returns list of interfaces to object's fields without the ID field
func (*Controller) GetModelIDInterface ¶
func (c *Controller) GetModelIDInterface(obj interface{}) interface{}
GetModelIDInterface returns an interface{} to ID field of an object
func (*Controller) GetModelIDValue ¶
func (c *Controller) GetModelIDValue(obj interface{}) int64
GetModelIDValue returns value of ID field (int64) of an object
func (Controller) ResetFields ¶
func (c Controller) ResetFields(obj interface{})
ResetFields zeroes object's field values
func (Controller) SaveToDB ¶
func (c Controller) SaveToDB(obj interface{}) *ErrController
SaveToDB takes object, validates its field values and saves it in the database. If ID field is already set (it's greater than 0) then the function assumes that record with such ID already exists in the database and the function with execute an "UPDATE" query. Otherwise it will be "INSERT". After inserting, new record ID is set to struct's ID field
func (Controller) SetFromDB ¶
func (c Controller) SetFromDB(obj interface{}, id string) *ErrController
SetFromDB sets object's fields with values from the database table with a specific id. If record does not exist in the database, all field values in the struct are zeroed
type ErrController ¶
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 ErrHelper ¶
ErrHelper wraps original error with operation/step where the error occured and optionally with a tag when parsing "crud" failed
type ErrValidation ¶
ErrValidation wraps error occuring during object validation
func (ErrValidation) Error ¶
func (e ErrValidation) Error() string
func (ErrValidation) Unwrap ¶
func (e ErrValidation) Unwrap() error
type HTTPResponse ¶
type HTTPResponse struct { OK int8 `json:"ok"` ErrText string `json:"err_text"` Data map[string]interface{} `json:"data"` }
HTTPResponse is a base structure for all the HTTP responses from HTTP endpoints
func NewHTTPResponse ¶
func NewHTTPResponse(ok int8, errText string) HTTPResponse
NewHTTPResponse returns new HTTPResponse object
type Helper ¶
type Helper struct {
// contains filtered or unexported fields
}
Helper reflects the object to generate and cache PostgreSQL queries (CREATE TABLE, INSERT, UPDATE etc.). Database table and column names are lowercase with underscore and they are generated from field names. Helper is created within Controller and there is no need to instantiate it
func NewHelper ¶
NewHelper takes object and database table name prefix as arguments and returns Helper instance
func (Helper) GetQueryCreateTable ¶
GetQueryCreateTable return create table query
func (*Helper) GetQueryDeleteById ¶
GetQueryDeleteById returns delete query
func (Helper) GetQueryDropTable ¶
GetQueryDropTable returns drop table query
func (*Helper) GetQueryInsert ¶
GetQueryInsert returns insert query
func (*Helper) GetQuerySelect ¶
func (*Helper) GetQuerySelectById ¶
GetQuerySelectById returns select query
func (*Helper) GetQueryUpdateById ¶
GetQueryUpdateById returns update query