Documentation ¶
Index ¶
- func ConcatStr(strs ...string) string
- type Base
- type Field
- type GeneratePrimaryKeyFunc
- type Resource
- func (b *Resource) Delete(base *Base, id string) error
- func (b *Resource) Find(base *Base, id any) (map[string]any, error)
- func (b *Resource) FromJSON(filename string) error
- func (b *Resource) FromStruct(s any) error
- func (b *Resource) GeneratePrimaryKey() any
- func (b *Resource) GetFieldNames() []string
- func (b *Resource) HasField(field string) bool
- func (b *Resource) Insert(base *Base, data map[string]any) (int64, error)
- func (b *Resource) IsSearchable(field string) bool
- func (b *Resource) Search(base *Base, query map[string][]string) ([]map[string]any, error)
- func (b *Resource) Table() string
- func (b *Resource) Update(base *Base, data map[string]any) (int64, error)
- func (b *Resource) ValidateAllFields(v *validator.Validate, data map[string]interface{}) map[string]interface{}
- func (b *Resource) ValidateField(v *validator.Validate, field string, value any) error
- func (b *Resource) ValidateInputFields(v *validator.Validate, data map[string]interface{}) map[string]interface{}
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Field ¶
type Field struct { // Validator is the validation rules for the field, using the // package github.com/go-playground/validator/v10 Validator string `json:"validator"` // Unsearchable is a flag that indicates that a field can not be used // as query parameter in the search route Unsearchable bool `json:"unsearchable"` // Immutable is a flag that indicates that a field can not be updated Immutable bool `json:"immutable"` }
type GeneratePrimaryKeyFunc ¶ added in v1.2.0
type GeneratePrimaryKeyFunc func() any
type Resource ¶
type Resource struct { // Name of the resource Name string `json:"name"` // OverwriteTableName is the name of the table in case it is not the same as the resource name OverwriteTableName null.String `json:"overwrite_table_name"` // Fields is a list of fields in the table Fields map[string]Field `json:"fields"` // PrimaryKey is the name of field that is the primary key PrimaryKey string `json:"primary_key"` // AutoIncrementalPK is a flag that indicates if the primary key is autoincremental // and will not use it when inserting a new row AutoIncrementalPK bool `json:"incremental_pk"` // GeneratePrimaryKeyFunc is a function that generates a new primary key // if null, the defaultGeneratePrimaryKeyFunc is used GeneratePrimaryKeyFunc GeneratePrimaryKeyFunc `json:"-"` // SoftDeleteField is the name of the field that is used for soft deletes // if null, no soft deletes are used SoftDeleteField null.String `json:"soft_delete_field"` // CreatedAtField is the name of the field that is used as createion timestamp // if null, no creation timestamp is generated CreatedAtField null.String `json:"created_at_field"` // UpdatedAtField is the name of the field that is used as update timestamp // if null, no update timestamp is generated UpdatedAtField null.String `json:"updated_at_field"` // Ommmit<Route Type>Route are flags that omit the generation of the specific route from the router OmitCreateRoute bool `json:"omit_create_route"` OmitRetrieveRoute bool `json:"omit_retrieve_route"` OmitUpdateRoute bool `json:"omit_update_route"` OmitPartialUpdateRoute bool `json:"omit_partial_update_route"` OmitDeleteRoute bool `json:"omit_delete_route"` OmitSearchRoute bool `json:"omit_search_route"` OmitHeadRoutes bool `json:"omit_head_routes"` }
gosimplerest.Resource represents a data resource, such as a database table, a file in a storage system, etc.
func (*Resource) FromStruct ¶ added in v1.2.4
FromStruct reads a struct and populates the resource
The struct should have the following tags:
- db: used to get the field name
- json: used to get the field name (if db is not present)
- pk: used to get the primary key field name: set to autoincremental if the primary key is autoincremental set to true if the primary key is not autoincremental
- soft_delete: used to get the soft delete field
- created_at: used to get the created at field
- updated_at: used to get the updated at field
- validate: used to get the validation rules
- unsearchable: used to get the unsearchable fields
- pk: used to get the primary key
The omit route flags, OverwriteTableName and GeneratePrimaryKeyFunc are not populated by this function
func (*Resource) GeneratePrimaryKey ¶
GeneratePrimaryKey generates a new primary key
func (*Resource) GetFieldNames ¶
GetFieldNames returns a list of strings with the field names of the model
func (*Resource) IsSearchable ¶
HasField returns true if the model has the given field
func (*Resource) ValidateAllFields ¶ added in v1.2.4
func (b *Resource) ValidateAllFields(v *validator.Validate, data map[string]interface{}) map[string]interface{}
ValidateAllFields validates all fields of the model against the given data
func (*Resource) ValidateField ¶
ValidateField validates the given field of the model against the given data
func (*Resource) ValidateInputFields ¶ added in v1.2.4
func (b *Resource) ValidateInputFields(v *validator.Validate, data map[string]interface{}) map[string]interface{}
ValidateInputFields validates the given fields of the model against the given data