resource

package
v1.3.1 Latest Latest
Warning

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

Go to latest
Published: Feb 19, 2023 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConcatStr added in v1.3.1

func ConcatStr(strs ...string) string

Types

type Base

type Base struct {
	Logger   *logrus.Logger
	DB       *sql.DB
	Resource *Resource
	Validate *validator.Validate
}

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

func (b *Resource) Delete(base *Base, id string) error

Delete deletes a row with the given primary key from the database

func (*Resource) Find

func (b *Resource) Find(base *Base, id any) (map[string]any, error)

Find returns a single row from the database, search by the primary key

func (*Resource) FromJSON

func (b *Resource) FromJSON(filename string) error

FromJSON reads a JSON file and populates the model

func (*Resource) FromStruct added in v1.2.4

func (b *Resource) FromStruct(s any) error

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

func (b *Resource) GeneratePrimaryKey() any

GeneratePrimaryKey generates a new primary key

func (*Resource) GetFieldNames

func (b *Resource) GetFieldNames() []string

GetFieldNames returns a list of strings with the field names of the model

func (*Resource) HasField

func (b *Resource) HasField(field string) bool

HasField returns true if the model has the given field

func (*Resource) Insert

func (b *Resource) Insert(base *Base, data map[string]any) (int64, error)

Insert inserts a new row into the database

func (*Resource) IsSearchable

func (b *Resource) IsSearchable(field string) bool

HasField returns true if the model has the given field

func (*Resource) Search

func (b *Resource) Search(base *Base, query map[string][]string) ([]map[string]any, error)

Search searches for rows in the database with where clauses

func (*Resource) Table

func (b *Resource) Table() string

Table returns the table name

func (*Resource) Update

func (b *Resource) Update(base *Base, data map[string]any) (int64, error)

Update updates a row in the database data must contain the primary key

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

func (b *Resource) ValidateField(v *validator.Validate, field string, value any) error

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

Jump to

Keyboard shortcuts

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