query_parser_to_db

package module
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2022 License: MIT Imports: 7 Imported by: 1

README

Go Catupiri Query Parser to DB

Query parser with support for build database operations / params from query params.

For now we only have supports for GORM queries.

Examples: https://github.com/go-catupiry/query_parser_to_db_examples

Usage:

  // GORM Model configuration
  // use filter param to allow or disable any query param parsing:
  type ContentModelStub struct {
    ID         uint64 `json:"id" filter:"param:id;type:string"`
    Title      string `json:"title" filter:"param:title;type:string"`
    Body       string `json:"body" filter:"type:string"`
    Published  bool   `json:"published" filter:"param:published;type:bool"`
    ClickCount int64  `json:"clickCount" filter:"param:clickCount;type:number"`
    Secret     string `json:"-"`
    Email      string `json:"email"`
    Email2     string `json:"email2" filter:""`
    PrivateBio string `json:"-" filter:"-"`
  }  
  
  // In your http handler:

  // get a new instance of the query parser:
  q := query_parser_to_db.NewQuery(50)
  // parse url query params and its operations:
  q.ParseFromURLValues(req.URL.Query())
  
  // db = *gorm.DB, that will parse 
  queryInterface, _ := q.SetDatabaseQueryForModel(db, &ContentModelStub{})
  
  // get the query, a *gorm.DB var
  query := queryInterface.(*gorm.DB)
  
  // execute the query as any gorm query:
  records := []ContentModelStub{}
  dbResultTX := query.Find(&records)

Operations:

Will accept this query params as filters:

  • 'get /post?id=[id]'
  • 'get /post?id_equal=[id]'
  • 'get /post?id_is-null=true'
  • 'get /post?id_is-null=true'
  • 'get /post?id_not-is-null=true'
  • 'get /post?id_between=10-20'
  • 'get /post?id_not-between=10-30'
  • 'get /post?id_gt=2'
  • 'get /post?id_gte=2'
  • 'get /post?id_lt=20'
  • 'get /post?id_lte=20'
  • 'get /post?title=Oi mundo'
  • 'get /post?title_equal=Oi mundo'
  • 'get /post?title_is-null=true'
  • 'get /post?title_not-is-null=true'
  • 'get /post?title_starts-with=Oi'
  • 'get /post?title_not-starts-with=Oi'
  • 'get /post?title_ends-with=Mundo'
  • 'get /post?title_not-ends-with=Mundo'
  • 'get /post?title_contains=Mundo'
  • 'get /post?title_not-contains=Mundo'
  • 'get /post?body=Something'
  • 'get /post?body_equal=Something'
  • 'get /post?body_equal=Something'

Roadmap

  • Improve to allows database adapter extension with interfaces
    • Create one mongoDB adapter
  • Add github CI tests and coverage
  • Add support for between operation
  • Add support for advanced JSON operations

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DBAdapter

type DBAdapter map[string]DBOperations

[fieldType][queryType]function

var GORMDBAdapter DBAdapter

func NewGORMDBAdapter

func NewGORMDBAdapter() DBAdapter

func (DBAdapter) Run

func (r DBAdapter) Run(fieldType, operator, column, value string, dbQuery interface{}, q *Query) (interface{}, error)

type DBOperations

type DBOperations map[string]func(column, value string, dbQuery interface{}, q QueryInterface) (interface{}, error)

type ModelFieldTagConfig

type ModelFieldTagConfig struct {
	Param       string
	Type        string
	DBFieldName string
}

type Query

type Query struct {
	Fields      []QueryAttr
	Limit       int64
	LimitMax    int64
	Page        int64
	QueryString string
}

func (*Query) AddQueryParamFromRaw

func (r *Query) AddQueryParamFromRaw(paramName string, values []string) error

func (*Query) AddQueryString

func (r *Query) AddQueryString(paramName string, values []string)

func (*Query) GetLimit

func (r *Query) GetLimit() int64

func (*Query) GetOffset

func (r *Query) GetOffset() int

func (*Query) GetPage

func (r *Query) GetPage() int64

func (*Query) GetParam

func (r *Query) GetParam(paramName string) *QueryAttr

func (*Query) GetParamValue

func (r *Query) GetParamValue(paramName string) string

func (*Query) GetQueryString

func (r *Query) GetQueryString(paramName string) string

func (*Query) ParseFromURLValues

func (r *Query) ParseFromURLValues(query url.Values) error

func (*Query) SetDatabaseQueryForModel

func (r *Query) SetDatabaseQueryForModel(query interface{}, model interface{}) (interface{}, error)

func (*Query) SetLimit

func (r *Query) SetLimit(v int64)

func (*Query) SetPage

func (r *Query) SetPage(v int64)

type QueryAttr

type QueryAttr struct {
	Operator   string
	Values     []string
	IsMultiple bool
	ParamName  string
}

type QueryInterface

type QueryInterface interface {
	ParseFromURLValues(query url.Values) error
	AddQueryParamFromRaw(paramName string, values []string) error
	AddQueryString(paramName string, values []string)
	GetQueryString(paramName string) string
	GetParamValue(paramName string) string
	GetParam(paramName string) *QueryAttr
	// Get limit query param
	GetLimit() int64
	SetLimit(v int64)
	// Get page query param
	GetPage() int64
	SetPage(v int64)
	GetOffset() int
	SetDatabaseQueryForModel(query interface{}, model interface{}) (interface{}, error)
}

func NewQuery

func NewQuery(limitMax int64) QueryInterface

Jump to

Keyboard shortcuts

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