repos

package
v1.1.2 Latest Latest
Warning

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

Go to latest
Published: Oct 25, 2024 License: AGPL-3.0 Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const (
	IndexAsc  indexOrder = true
	IndexDesc indexOrder = false
)

Variables

View Source
var DefaultCursorPagination = CursorPagination{
	First:         functions.New(int64(10)),
	After:         nil,
	OrderBy:       "_id",
	SortDirection: SortDirectionAsc,
}
View Source
var ErrNoDocuments error = fmt.Errorf("no documents found")
View Source
var ErrRecordMismatch = fmt.Errorf("update with version check failed, last updated time mismatch")

Functions

func CursorToBase64

func CursorToBase64(c Cursor) string

func NewFxMongoRepo

func NewFxMongoRepo[T Entity](collectionName, shortName string, indexFields []IndexField) fx.Option

func NewMongoClientFx

func NewMongoClientFx[T MongoConfig]() fx.Option

func NewMongoDatabase

func NewMongoDatabase(ctx context.Context, uri string, dbName string) (db *mongo.Database, e error)

Types

type BaseEntity

type BaseEntity struct {
	PrimitiveId       ID        `json:"_id,omitempty" graphql:"ignore" struct-json-path:",ignore"`
	Id                ID        `json:"id" graphql:"scalar-type=ID"`
	CreationTime      time.Time `json:"creationTime"`
	UpdateTime        time.Time `json:"updateTime"`
	RecordVersion     int       `json:"recordVersion"`
	MarkedForDeletion *bool     `json:"markedForDeletion,omitempty"`
}

func (*BaseEntity) GetCreationTime

func (c *BaseEntity) GetCreationTime() time.Time

func (*BaseEntity) GetId

func (c *BaseEntity) GetId() ID

func (*BaseEntity) GetPrimitiveID

func (c *BaseEntity) GetPrimitiveID() ID

func (*BaseEntity) GetRecordVersion

func (c *BaseEntity) GetRecordVersion() int

func (*BaseEntity) GetUpdateTime

func (c *BaseEntity) GetUpdateTime() time.Time

func (*BaseEntity) IncrementRecordVersion

func (c *BaseEntity) IncrementRecordVersion()

func (*BaseEntity) IsEntity

func (c *BaseEntity) IsEntity()

added because gqlgen needs it when using @key directives read more [here](https://github.com/99designs/gqlgen/blob/ee526b05f28b0e7d5a8e7b1da28da3e03c826df9/plugin/federation/fedruntime/runtime.go#L12)

func (*BaseEntity) IsMarkedForDeletion

func (c *BaseEntity) IsMarkedForDeletion() bool

func (*BaseEntity) IsZero

func (c *BaseEntity) IsZero() bool

func (*BaseEntity) SetCreationTime

func (c *BaseEntity) SetCreationTime(ct time.Time)

func (*BaseEntity) SetId

func (c *BaseEntity) SetId(id ID)

func (*BaseEntity) SetUpdateTime

func (c *BaseEntity) SetUpdateTime(ut time.Time)

type Cursor

type Cursor string

func CursorFromBase64

func CursorFromBase64(b string) (Cursor, error)

type CursorPagination

type CursorPagination struct {
	First *int64  `json:"first"`
	After *string `json:"after,omitempty"`

	Last   *int64  `json:"last,omitempty"`
	Before *string `json:"before,omitempty"`

	OrderBy       string        `json:"orderBy,omitempty" graphql:"default=\"_id\""`
	SortDirection SortDirection `json:"sortDirection,omitempty" graphql:"enum=ASC;DESC,default=\"ASC\""`
}

type CursorSortBy

type CursorSortBy struct {
	Field     string        `json:"field"`
	Direction SortDirection `json:"sortDirection"`
}

type DbRepo

type DbRepo[T Entity] interface {
	NewId() ID
	Find(ctx context.Context, query Query) ([]T, error)
	FindOne(ctx context.Context, filter Filter) (T, error)
	FindPaginated(ctx context.Context, filter Filter, pagination CursorPagination) (*PaginatedRecord[T], error)
	FindById(ctx context.Context, id ID) (T, error)
	Create(ctx context.Context, data T) (T, error)
	CreateMany(ctx context.Context, data []T) error
	Exists(ctx context.Context, filter Filter) (bool, error)

	Count(ctx context.Context, filter Filter) (int64, error)

	// upsert
	Upsert(ctx context.Context, filter Filter, data T) (T, error)
	UpdateMany(ctx context.Context, filter Filter, updatedData map[string]any) error
	UpdateById(ctx context.Context, id ID, updatedData T, opts ...UpdateOpts) (T, error)
	PatchById(ctx context.Context, id ID, patch Document, opts ...UpdateOpts) (T, error)

	UpdateWithVersionCheck(ctx context.Context, id ID, updatedData T) (T, error)

	Patch(ctx context.Context, filter Filter, patch Document, opts ...UpdateOpts) (T, error)
	UpdateOne(ctx context.Context, filter Filter, updatedData T, opts ...UpdateOpts) (T, error)
	PatchOne(ctx context.Context, filter Filter, patch Document, opts ...UpdateOpts) (T, error)
	DeleteById(ctx context.Context, id ID) error
	DeleteMany(ctx context.Context, filter Filter) error
	IndexFields(ctx context.Context, indices []IndexField) error
	// Delete(ctx context.Context, query Query) ([]ID, error)
	DeleteOne(ctx context.Context, filter Filter) error

	GroupByAndCount(ctx context.Context, filter Filter, groupBy string, opts GroupByAndCountOptions) (map[string]int64, error)

	ErrAlreadyExists(err error) bool
	MergeMatchFilters(filter Filter, matchFilters ...map[string]MatchFilter) Filter
}

func NewMongoRepo

func NewMongoRepo[T Entity](
	db *mongo.Database,
	collectionName string,
	shortName string,
) DbRepo[T]

type Document

type Document bson.M

type Entity

type Entity interface {
	GetPrimitiveID() ID
	GetId() ID
	SetId(id ID)
	GetCreationTime() time.Time
	GetUpdateTime() time.Time
	SetCreationTime(time.Time)
	SetUpdateTime(time.Time)
	IsZero() bool

	IncrementRecordVersion()
	GetRecordVersion() int
	IsMarkedForDeletion() bool
}

type Filter

type Filter map[string]interface{}

func (Filter) Add

func (f Filter) Add(key string, value interface{}) Filter

type GroupByAndCountOptions

type GroupByAndCountOptions struct {
	Limit int64
	Sort  SortDirection
}

type ID

type ID string

type IndexField

type IndexField struct {
	Field  []IndexKey
	Unique bool
}

type IndexKey

type IndexKey struct {
	Key    string
	Value  indexOrder
	IsText bool
}

type MatchFilter

type MatchFilter struct {
	MatchType  MatchType `json:"matchType"`
	Exact      any       `json:"exact,omitempty"`
	Array      []any     `json:"array,omitempty"`
	NotInArray []any     `json:"notInArray,omitempty"`
	Regex      *string   `json:"regex,omitempty"`
}

type MatchType

type MatchType string
const (
	MatchTypeExact      MatchType = "exact"
	MatchTypeArray      MatchType = "array"
	MatchTypeNotInArray MatchType = "not_in_array"
	MatchTypeRegex      MatchType = "regex"
)

type MongoConfig

type MongoConfig interface {
	GetMongoConfig() (url string, dbName string)
}

type MongoRepoOptions

type MongoRepoOptions struct {
	IndexFields []string
}

type Opts

type Opts map[string]interface{}

type PageInfo

type PageInfo struct {
	StartCursor string
	EndCursor   string
	HasNextPage *bool
	HasPrevPage *bool
}

type PaginatedRecord

type PaginatedRecord[T Entity] struct {
	Edges      []RecordEdge[T]
	PageInfo   PageInfo
	TotalCount int64
}

type Query

type Query struct {
	Filter Filter
	Sort   map[string]interface{}
	Limit  *int64
}

type RecordEdge

type RecordEdge[T Entity] struct {
	Node   T
	Cursor string
}

type SortDirection

type SortDirection string
const (
	SortDirectionAsc  SortDirection = "ASC"
	SortDirectionDesc SortDirection = "DESC"
)

func (SortDirection) Int

func (s SortDirection) Int() int64

type SortOpts

type SortOpts map[string]int32

type UpdateOpts

type UpdateOpts struct {
	Upsert bool
}

Jump to

Keyboard shortcuts

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