sql

package
v0.0.0-...-993f9dd Latest Latest
Warning

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

Go to latest
Published: Feb 23, 2024 License: AGPL-3.0 Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	SEARCH   = 1  // Filter response with LIKE query "search={search_phrase}"
	FILTER   = 2  // Filter response by column name values "filter={column_name}:{value}"
	PAGINATE = 4  // Paginate response with page and page_size
	ORDER_BY = 8  // Order response by column name
	ALL      = 15 // Equivalent to SEARCH|FILTER|PAGINATE|ORDER_BY

)

Variables

View Source
var (
	ErrTransactionOpen   = errors.New("There is already a transaction in this context.")
	ErrNoTransactionOpen = errors.New("There is no transaction in this context.")
)
View Source
var Migrations embed.FS

Functions

func Build

func Build(structFields []*structs.Field) string

func FilterByQuery

func FilterByQuery(context fiber.Ctx, config int) func(db *gorm.DB) *gorm.DB

Filter DB request with query parameters. Note: Don't forget to initialize DB Model first, otherwise filter and search won't work Example:

db.Model(&UserModel).Scope(filter.FilterByQuery(ctx, filter.ALL)).Find(&users)

Or if only pagination and order is needed:

db.Model(&UserModel).Scope(filter.FilterByQuery(ctx, filter.PAGINATION|filter.ORDER_BY)).Find(&users)

And models should have appropriate`fitler` tags:

type User struct {
	gorm.Model
	Username string `gorm:"uniqueIndex" filter:"param:login;searchable;filterable"`
	// `param` defines custom column name for the query param
	FullName string `filter:"searchable"`
}

func ILike

func ILike(value string) string

Types

type Database

type Database interface {
	DB(ctx context.Context) *gorm.DB
}

type Manager

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

func NewManager

func NewManager(db *gorm.DB, l *zap.SugaredLogger) (*Manager, error)

func (*Manager) DB

func (p *Manager) DB(ctx context.Context) *gorm.DB

type Options

type Options struct {
	Selects       []string `json:"fields,omitempty"`
	Skip          int      `json:"offset,omitempty"`
	Take          int      `json:"limit,omitempty"`
	Relations     []string `json:"expand,omitempty"`
	Order         string   `json:"order,omitempty"`
	Specification []Specification
	Not           []string
	Null          []string
	Q             string
}

func FromQuery

func FromQuery(context fiber.Ctx) (*Options, *utils.ApplictaionError)

func NewOptions

func NewOptions(selects []string, skip int, take int, relations []string, order string, secification []Specification) Options

type Provider

type Provider interface {
	Database() Database
}

type Query

type Query struct {
	Selects     []string
	Skip        int
	Take        int
	Relations   []string
	Where       string
	WithDeleted bool
	Order       string
}

func BuildQuery

func BuildQuery[T any](selector T, config *Options) Query

func NewQuery

func NewQuery(where string, selects []string, skip int, take int, relations []string, order string, withDeleted bool) Query

type Repository

type Repository[M any] struct {
	// contains filtered or unexported fields
}

func NewRepository

func NewRepository[M any](db *gorm.DB) *Repository[M]

func (*Repository[M]) Clear

func (r *Repository[M]) Clear(target interface{}) *utils.ApplictaionError

func (*Repository[M]) Count

func (r *Repository[M]) Count(ctx context.Context, query Query) (*int64, *utils.ApplictaionError)

Count counts all records matching given conditions conds

func (*Repository[M]) CountBy

func (r *Repository[M]) CountBy(ctx context.Context, field []string, query Query) (*int64, *utils.ApplictaionError)

CountBy counts by all records matching given conditions conds

func (*Repository[M]) Create

func (r *Repository[M]) Create(ctx context.Context, model *M) *utils.ApplictaionError

Create inserts value, returning the inserted data's primary key in value's id

func (*Repository[M]) CreateBatch

func (r *Repository[M]) CreateBatch(ctx context.Context, model *M, batchSize int) *utils.ApplictaionError

CreateInBatches inserts value, returning the inserted data's primary key in value's id

func (*Repository[M]) CreateSlice

func (r *Repository[M]) CreateSlice(ctx context.Context, model *[]M) *utils.ApplictaionError

CreateSlice inserts value, returning the inserted data's primary key in value's id

func (*Repository[M]) CreateTx

func (r *Repository[M]) CreateTx(ctx context.Context, model *M, tx *gorm.DB) *utils.ApplictaionError

CreateTx inserts value, returning the inserted data's primary key in value's id

func (*Repository[M]) Db

func (r *Repository[M]) Db() *gorm.DB

func (*Repository[M]) Delete

func (r *Repository[M]) Delete(ctx context.Context, model *M) *utils.ApplictaionError

Delete deletes value matching given conditions. If value contains primary key it is included in the conditions. If value includes a deleted_at field, then Delete performs a soft delete instead by setting deleted_at with the current time if null.

func (*Repository[M]) DeletePermanently

func (r *Repository[M]) DeletePermanently(ctx context.Context, model *M) *utils.ApplictaionError

DeletePermanently deletes value matching given conditions. If value contains primary key it is included in the conditions.

func (*Repository[M]) DeleteSlice

func (r *Repository[M]) DeleteSlice(ctx context.Context, model []M) *utils.ApplictaionError

Remove deletes value matching given conditions. If value contains primary key it is included in the conditions. If value includes a deleted_at field, then Delete performs a soft delete instead by setting deleted_at with the current time if null.

func (*Repository[M]) DeleteTx

func (r *Repository[M]) DeleteTx(ctx context.Context, model *M, tx *gorm.DB) *utils.ApplictaionError

DeleteTx deletes value matching given conditions. If value contains primary key it is included in the conditions. If value includes a deleted_at field, then Delete performs a soft delete instead by setting deleted_at with the current time if null.

func (*Repository[M]) Find

func (r *Repository[M]) Find(ctx context.Context, models *[]M, query Query) *utils.ApplictaionError

Find finds all records matching given conditions conds

func (*Repository[M]) FindAndCount

func (r *Repository[M]) FindAndCount(ctx context.Context, models *[]M, query Query) (*int64, *utils.ApplictaionError)

FindAndCount finds all records matching given conditions conds and count them

func (*Repository[M]) FindOne

func (r *Repository[M]) FindOne(ctx context.Context, model *M, query Query) *utils.ApplictaionError

FindOne finds the first record ordered by primary key, matching given conditions conds

func (*Repository[M]) HandleDBError

func (r *Repository[M]) HandleDBError(err error) *utils.ApplictaionError

func (*Repository[M]) HandleError

func (r *Repository[M]) HandleError(res *gorm.DB) *utils.ApplictaionError

func (*Repository[M]) HandleOneError

func (r *Repository[M]) HandleOneError(res *gorm.DB) *utils.ApplictaionError

func (*Repository[M]) Insert

func (r *Repository[M]) Insert(ctx context.Context, model *M) *utils.ApplictaionError

Insert inserts value, returning the inserted data's primary key in value's id

func (*Repository[M]) InsertSlice

func (r *Repository[M]) InsertSlice(ctx context.Context, model *[]M) *utils.ApplictaionError

InsertSlice inserts values, returning the inserted data's primary key in value's id

func (*Repository[M]) InsertTx

func (r *Repository[M]) InsertTx(ctx context.Context, model *M, tx *gorm.DB) *utils.ApplictaionError

InsertTx inserts value, returning the inserted data's primary key in value's id

func (*Repository[M]) ParseQuery

func (r *Repository[M]) ParseQuery(ctx context.Context, query Query) *gorm.DB

func (*Repository[M]) Recover

func (r *Repository[M]) Recover(ctx context.Context, model *M) *utils.ApplictaionError

Recover recovers given entitie in the database.

func (*Repository[M]) Relations

func (r *Repository[M]) Relations(relations []string) *Repository[M]

func (*Repository[M]) Remove

func (r *Repository[M]) Remove(ctx context.Context, model *M) *utils.ApplictaionError

Remove deletes value matching given conditions. If value contains primary key it is included in the conditions. If value includes a deleted_at field, then Delete performs a soft delete instead by setting deleted_at with the current time if null.

func (*Repository[M]) RemoveSlice

func (r *Repository[M]) RemoveSlice(ctx context.Context, model []M) *utils.ApplictaionError

Remove deletes value matching given conditions. If value contains primary key it is included in the conditions. If value includes a deleted_at field, then Delete performs a soft delete instead by setting deleted_at with the current time if null.

func (*Repository[M]) RemoveTX

func (r *Repository[M]) RemoveTX(ctx context.Context, model *M, tx *gorm.DB) *utils.ApplictaionError

RemoveTX deletes value matching given conditions. If value contains primary key it is included in the conditions. If value includes a deleted_at field, then Delete performs a soft delete instead by setting deleted_at with the current time if null.

func (*Repository[M]) Save

func (r *Repository[M]) Save(ctx context.Context, model *M) *utils.ApplictaionError

Save updates value in database. If value doesn't contain a matching primary key, value is inserted.

func (*Repository[M]) SaveSlice

func (r *Repository[M]) SaveSlice(ctx context.Context, model *[]M) *utils.ApplictaionError

Save updates value in database. If value doesn't contain a matching primary key, value is inserted.

func (*Repository[M]) SaveTx

func (r *Repository[M]) SaveTx(ctx context.Context, model *M, tx *gorm.DB) *utils.ApplictaionError

SaveTx updates value in database. If value doesn't contain a matching primary key, value is inserted.

func (*Repository[M]) SoftRemove

func (r *Repository[M]) SoftRemove(ctx context.Context, model *M) *utils.ApplictaionError

SoftRemove deletes value matching given conditions. If value contains primary key it is included in the conditions. If value includes a deleted_at field, then Delete performs a soft delete instead by setting deleted_at with the current time if null.

func (*Repository[M]) Specification

func (r *Repository[M]) Specification(specification ...Specification) *Repository[M]

func (*Repository[M]) Update

func (r *Repository[M]) Update(ctx context.Context, model *M) *utils.ApplictaionError

Update updates attributes using callbacks. values must be a struct or map. Reference: https://gorm.io/docs/update.html#Update-Changed-Fields

func (*Repository[M]) UpdateSlice

func (r *Repository[M]) UpdateSlice(ctx context.Context, model *[]M) *utils.ApplictaionError

UpdateSlice updates attributes using callbacks. values must be a struct or map. Reference: https://gorm.io/docs/update.html#Update-Changed-Fields

func (*Repository[M]) Upsert

func (r *Repository[M]) Upsert(ctx context.Context, model *M) *utils.ApplictaionError

Upsert inserts a given entity into the database, unless a unique constraint conflicts then updates the entity Unlike save method executes a primitive operation without cascades, relations and other operations included. Executes fast and efficient INSERT ... ON CONFLICT DO UPDATE/ON DUPLICATE KEY UPDATE query.

type Specification

type Specification interface {
	GetQuery() string
	GetValues() []any
}

func And

func And(specifications ...Specification) Specification

func Equal

func Equal[T any](field string, value T) Specification

func GreaterOrEqual

func GreaterOrEqual[T comparable](field string, value T) Specification

func GreaterThan

func GreaterThan[T comparable](field string, value T) Specification

func In

func In[T any](field string, value []T) Specification

func IsNull

func IsNull(field string) Specification

func LessOrEqual

func LessOrEqual[T comparable](field string, value T) Specification

func LessThan

func LessThan[T comparable](field string, value T) Specification

func Like

func Like[T any](field string, value T) Specification

func Not

func Not(specification Specification) Specification

func Or

func Or(specifications ...Specification) Specification

Jump to

Keyboard shortcuts

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