scope

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Nov 24, 2021 License: MIT Imports: 15 Imported by: 0

README

Logo

Scope

The primary purpose of the scope package is to provide scopes that can be easily attached to Pop queries. Think tx.Scope([scopeFunc]).All(&books).

This repository also contains a limited "query language" for use with Go Buffalo. These scopes take query parameters and produce scope functions to generically manipulate pop.Models, see scope.For[Filter|Sort|Paginate]FromParams(...). The GetAggregationsFromParams function serves a similar purpose of taking a set of query params and providing generic output, however because the result structure is different the aggregations execute a query to return the results for you.

Motivation

See Blog post.

Integration

This is a go package. Integrate this repository by running go get github.com/alphaflow/scope within an executable go project.

Usage and Examples

A description of all available parameters can be found in USAGE.md.

An example buffalo resource file can be found in /examples/resource.go.

Local Development

You will need to have a postgres database running in order to run tests. Set TEST_DATABASE_URL in your environment and then run:

 buffalo pop create -e test;
 buffalo pop migrate -e test;

To create the scope test database with buffalo pop.

When you have completed your changes, please read DEPLOYMENT.md to merge your changes.

Documentation

Index

Constants

View Source
const FailQuery = "1=0"

Variables

Functions

func ForFiltersFromParams

func ForFiltersFromParams(ctx context.Context, model interface{}, params buffalo.ParamValues) (pop.ScopeFunc, error)

ForFiltersFromParams filters a model based on the provided filter params.

func ForFirst

func ForFirst() pop.ScopeFunc

ForFirst scopes a query for the first record in a table based on the `created_at` timestamp.

The first record is the oldest record.

func ForID

func ForID(id string) pop.ScopeFunc

func ForIDForModel

func ForIDForModel(id string, model interface{}) pop.ScopeFunc

func ForIDSet

func ForIDSet(idSet IDSet) pop.ScopeFunc

func ForIDWithTableName

func ForIDWithTableName(id string, tablename string) pop.ScopeFunc

func ForIDs

func ForIDs(ids []uuid.UUID) pop.ScopeFunc

func ForLast

func ForLast() pop.ScopeFunc

ForLast scopes a query for the last record in a table based on the `created_at` timestamp.

The last record is the newest record.

func ForLimit

func ForLimit(limit int) pop.ScopeFunc

func ForNotNull

func ForNotNull(field string) pop.ScopeFunc

ForNotNull scopes for a supplied field being not null.

func ForNotNullDeletedAt

func ForNotNullDeletedAt() pop.ScopeFunc

func ForNotNullDeletedAtForModel

func ForNotNullDeletedAtForModel(model interface{}) pop.ScopeFunc

func ForNotUuidID

func ForNotUuidID(uid uuid.UUID) pop.ScopeFunc

func ForNull

func ForNull(field string) pop.ScopeFunc

ForNull scopes for a supplied field being null.

func ForNullDeletedAt

func ForNullDeletedAt() pop.ScopeFunc

func ForNullDeletedAtForModel

func ForNullDeletedAtForModel(model interface{}) pop.ScopeFunc

func ForNullsUuidID

func ForNullsUuidID(uid nulls.UUID) pop.ScopeFunc

func ForOne

func ForOne() pop.ScopeFunc

func ForOrder

func ForOrder(orderClauses ...string) pop.ScopeFunc

ForOrder is a generic scope function for ordering.

func ForPaginateFromParams

func ForPaginateFromParams(params buffalo.ParamValues) pop.ScopeFunc

ForPaginateFromParams paginates a query based on a list of parameters, generally c.Params()

func ForSortFromParams

func ForSortFromParams(ctx context.Context, model interface{}, params buffalo.ParamValues) (pop.ScopeFunc, error)

ForSortFromParams orders a query based on the provided query params.

func ForUuidID

func ForUuidID(uid uuid.UUID) pop.ScopeFunc

func GetAggregations

func GetAggregations(ctx context.Context, tx *pop.Connection, modelsPtr interface{}, columnNames []string, scopes *Collection, aggregations Aggregations) (interface{}, error)

GetAggregations returns the aggregated value for column `columnName` of modelsPtr, restricting by the scope collection `scopes`.

`columnName` is either a CustomColumn returned by the CustomFilterable interface, or a field specified by the json tag. This is the same as the acceptable values for `filter_columns` in ForFiltersFromParams.

func GetAggregationsFromParams

func GetAggregationsFromParams(ctx context.Context, tx *pop.Connection, modelsPtr interface{}, params buffalo.ParamValues, scopes *Collection) (interface{}, error)

GetAggregationsFromParams aggregates a modelsPtr based on params, restricting by the scope collection scopes.

func GetAllFilterColumnNames

func GetAllFilterColumnNames(ctx context.Context, modelPtr interface{}) ([]string, error)

GetAllFilterColumnNames is a utility in order to automatically get a list of all tags that can be filtered on for the referenced model.

func GetAllSortColumnNames

func GetAllSortColumnNames(ctx context.Context, modelPtr interface{}) ([]string, error)

GetAllSortColumnNames is a utility in order to automatically get a list of all tags that can be sorted on for the referenced model.

func GetFilterOptions

func GetFilterOptions(ctx context.Context, tx *pop.Connection, modelsPtr interface{}, columnName string, scopes *Collection) ([]interface{}, error)

GetFilterOptions returns all of unique values for column 'columnName' of modelsPtr, restricting by the scope collection scopes.

'columnName' is either a CustomColumn returned by the CustomFilterable interface, or a field specified by the json tag. This is the same as the acceptable values for 'filter_columns' in ForFiltersFromParams.

func GetGroupedAggregations

func GetGroupedAggregations(ctx context.Context, tx *pop.Connection, modelsPtr interface{}, columnNames []string, grouperName string, scopes *Collection, aggregations Aggregations) ([]interface{}, error)

GetGroupedAggregations returns the aggregated value for column `columnName` of modelsPtr, grouped by `grouperName` of modelsPtr, restricting by the scope collection `scopes`.

`columnName` is either a CustomColumn returned by the CustomFilterable interface, or a field specified by the json tag. This is the same as the acceptable values for `filter_columns` in ForFiltersFromParams.

func GetGroupedAggregationsFromParams

func GetGroupedAggregationsFromParams(ctx context.Context, tx *pop.Connection, modelsPtr interface{}, params buffalo.ParamValues, scopes *Collection) ([]interface{}, error)

GetGroupedAggregationsFromParams groups and aggregates a modelsPtr based on params, restricting by the scope collection scopes.

Types

type Aggregation

type Aggregation struct {
	Name       string
	Statement  string
	ResultType reflect.Type
}

type Aggregations

type Aggregations []Aggregation

type Collection

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

func NewCollection

func NewCollection(tx ...*pop.Connection) *Collection

func (*Collection) Dedupe

func (sc *Collection) Dedupe() *Collection

func (*Collection) Flatten

func (sc *Collection) Flatten() pop.ScopeFunc

func (*Collection) Get

func (sc *Collection) Get() []pop.ScopeFunc

func (*Collection) Push

func (sc *Collection) Push(scopes ...pop.ScopeFunc) *Collection

type CustomColumn

type CustomColumn struct {
	Name       string
	Statement  string
	ResultType reflect.Type
}

CustomColumn represents a SQL statement that can be used like a column in a SQL query. CustomColumns are used in order to create filter and sort options that are more complex than simply sorting on a single field. For example:

Given an object with a db column 'test_int', in order to sort on the TEXT converted value of 'test_int' you would implement 'CustomSortable' and provide the CustomColumn:

 {
	  Name:       "test_int_text_value",
	  Statement:  "test_int::TEXT",
	  ResultType: reflect.TypeOf(""),
  }

Statement can be any valid sql Statement that returns a single value. ResultType is the type of the value returned by statement, and is used to scan the value from the DB.

func GetAllFilterColumns

func GetAllFilterColumns(ctx context.Context, modelPtr interface{}) ([]CustomColumn, error)

GetAllFilterColumns is a utility in order to automatically get a list of all columns that can be filtered on for the referenced model.

func GetAllSortColumns

func GetAllSortColumns(ctx context.Context, modelPtr interface{}) ([]CustomColumn, error)

GetAllSortColumns is a utility in order to automatically get a list of all columns that can be sorted on for the referenced model.

type CustomColumns

type CustomColumns []CustomColumn

func GenerateCustomColumnsForSubobject

func GenerateCustomColumnsForSubobject(subobjectPtr interface{}, subobjectJsonTag, joinClause string, optionalTablename ...string) (CustomColumns, error)

GenerateCustomColumnsForSubobject is a utility in order to automatically create custom filter columns for a model related to the model you are filtering.

For example, assume you have a table Houses, and each house has an address_id field pointing to an Addresses table. In order to make houses sortable by the columns of the Address table, you will need to implement 'CustomSortable' and return the following:

GenerateCustomColumnsForSubobject(&Address{}, "address", "addresses.id = houses.address_id")

Which will return a list of CustomColumns derived from the fields of the `&Address{}` model.

 CustomColumns{
   {
	    Name:       "address.id",
	    Statement:  "(select id from addresses where addresses.id = houses.address_id)",
	    ResultType: reflect.TypeOf(""),
   },
   {
	    Name:       "address.city",
	    Statement:  "(select city from addresses where addresses.id = houses.address_id)",
	    ResultType: reflect.TypeOf(""),
   },
   ...
 }

type CustomFilterable

type CustomFilterable interface {
	GetCustomFilters(ctx context.Context) CustomColumns
}

type CustomSortable

type CustomSortable interface {
	GetCustomSorts(ctx context.Context) CustomColumns
}

type IDSet

type IDSet map[uuid.UUID]bool

func (IDSet) Keys

func (idSet IDSet) Keys() []uuid.UUID

type StandardAggregationsType

type StandardAggregationsType string
const (
	StandardAggregationsTypeCount StandardAggregationsType = "COUNT"
	StandardAggregationsTypeSum   StandardAggregationsType = "SUM"
	StandardAggregationsTypeAvg   StandardAggregationsType = "AVG"
	StandardAggregationsTypeMax   StandardAggregationsType = "MAX"
	StandardAggregationsTypeMin   StandardAggregationsType = "MIN"
)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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