store

package
v0.0.0-...-8b35fd5 Latest Latest
Warning

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

Go to latest
Published: Jul 24, 2024 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Adaptor

type Adaptor[T any] struct {
	Resource Resource[T]
}

Adaptor matches store.Resource to crud.Resource

func Adapt

func Adapt[T any](resource Resource[T]) Adaptor[T]

Adapt builds a resource for the given model

func (Adaptor[T]) Delete

func (vr Adaptor[T]) Delete(ctx context.Context, id string) error

Delete resource by id

func (Adaptor[T]) Get

func (vr Adaptor[T]) Get(ctx context.Context, filter []crud.Filter, outerOp crud.OuterOperation, innerOp crud.InnerOperation, sort []string, ascending bool, offset, limit int, count bool) (io.ReadCloser, error)

Get resource list

func (Adaptor[T]) GetById

func (vr Adaptor[T]) GetById(ctx context.Context, id string) (io.ReadCloser, error)

Get resource by id

func (Adaptor[T]) Post

func (vr Adaptor[T]) Post(ctx context.Context, r io.Reader) (io.ReadCloser, error)

Post (create) new resource

func (Adaptor[T]) Put

func (vr Adaptor[T]) Put(ctx context.Context, id string, r io.Reader) error

Put (update) resource

type DbType

type DbType interface {
	Where(field string, op crud.Operator, val string) (string, interface{}, error)
}

DBType represents a database column type that can be filtered

type EditableModel

type EditableModel interface {
	PrepareCreate() ([]string, error)
	PrepareUpdate(id string) ([]string, error)
}

EditableModel is a pointer to a Model, that allows modifications

type Executor

type Executor interface {
	Begin(ctx context.Context) (Transaction, error)
}

Executor is an interface used by Post, Put and Delete

type FilterSet

type FilterSet map[string]DbType

Set of all fields that can be used as filter

type IntDbType

type IntDbType struct{}

IntDbType represents an integer column

func (IntDbType) Where

func (s IntDbType) Where(field string, op crud.Operator, val string) (string, interface{}, error)

type JsonDbType

type JsonDbType struct{}

JsonDbType represents a string column with json format

func (JsonDbType) Where

func (s JsonDbType) Where(field string, op crud.Operator, val string) (string, interface{}, error)

type Limiter

type Limiter func(offset, limit int) string

Limiter builds a "LIMIT X, OFFSET Y" clause

type Model

type Model interface {
	GetID() string
}

Model represents any database table with an ID

type Querier

type Querier interface {
	GetContext(ctx context.Context, result interface{}, query string, args ...interface{}) error
	SelectContext(ctx context.Context, result interface{}, query string, args ...interface{}) error
}

Querier is the interface used by GetById and Get

type QueryError

type QueryError struct {
	Query   string
	Params  interface{}
	Message string
	Cause   error
}

QueryError encapsulates error in oracle Query

func (QueryError) Error

func (err QueryError) Error() string

func (QueryError) Unwrap

func (err QueryError) Unwrap() error

type Resource

type Resource[T any] interface {
	// Get resource by id
	GetById(context.Context, string) (T, error)
	// Get resource by filter
	Get(ctx context.Context, filter []crud.Filter, outerOp crud.OuterOperation, innerOp crud.InnerOperation, sort []string, ascending bool, offset, limit int) ([]T, error)
	// Count resource by filter
	Count(ctx context.Context, filter []crud.Filter, outerOp crud.OuterOperation, innerOp crud.InnerOperation) (uint64, error)
	// Post (create) new resource, return id
	Post(ctx context.Context, data T) (string, error)
	// Put (update) resource
	Put(ctx context.Context, id string, data T) error
	// Delete resource by id
	Delete(context.Context, string) error
}

Resource implements the CRUD operations

type SQLResource

type SQLResource[T Model, P interface {
	*T
	EditableModel
}] struct {
	// contains filtered or unexported fields
}

SQLResource manages database operations in a givem table

func New

func New[T Model, P interface {
	*T
	EditableModel
}](querier Querier, exec Executor, tableName string, columns map[string]DbType, limiter Limiter) SQLResource[T, P]

New creates a Resource for the given table

func (SQLResource[T, P]) Count

func (r SQLResource[T, P]) Count(ctx context.Context, filter []crud.Filter, outerOp crud.OuterOperation, innerOp crud.InnerOperation) (uint64, error)

Count filtered resources

func (SQLResource[T, P]) Delete

func (r SQLResource[T, P]) Delete(ctx context.Context, id string) error

Delete a resource from the database

func (SQLResource[T, P]) Get

func (r SQLResource[T, P]) Get(ctx context.Context, filter []crud.Filter, outerOp crud.OuterOperation, innerOp crud.InnerOperation, sort []string, ascending bool, offset, limit int) ([]T, error)

Get filtered (and possibly paginated) resources

func (SQLResource[T, P]) GetById

func (r SQLResource[T, P]) GetById(ctx context.Context, id string) (t T, err error)

GetById searches the table for the given id

func (SQLResource[T, P]) Post

func (r SQLResource[T, P]) Post(ctx context.Context, t T) (string, error)

Post creates a resource in the database

func (SQLResource[T, P]) Put

func (r SQLResource[T, P]) Put(ctx context.Context, id string, t T) error

Put updates a resource in the database

type Statement

type Statement interface {
	QueryString() string
	Execute(ctx context.Context, params ...any) (int, error)
	Close() error
}

type StringDbType

type StringDbType struct{}

StringDbType represents a string column

func (StringDbType) Where

func (s StringDbType) Where(field string, op crud.Operator, val string) (string, interface{}, error)

type TimeDbType

type TimeDbType struct{}

TimeDbType represents a time.Time column

func (TimeDbType) Where

func (s TimeDbType) Where(field string, op crud.Operator, val string) (string, interface{}, error)

type Transaction

type Transaction interface {
	PrepareNamed(ctx context.Context, sql string, mapping any) (Statement, []any, error)
	Commit() error
	Rollback()
}

Transaction is an interface used by Post, Put and Delete

Jump to

Keyboard shortcuts

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