mongodb

package
v0.0.0-...-7161ace Latest Latest
Warning

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

Go to latest
Published: Jul 2, 2023 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// SortOrderAscending defines the order of ascending for sorting
	SortOrderAscending = SortOrder(1)
	// SortOrderDescending defines the order of descending for sorting
	SortOrderDescending = SortOrder(-1)
)
View Source
const PrimaryKey = "_name"

PrimaryKey primary key

Variables

View Source
var (
	// ErrPrimaryEmpty Error that primary key is empty.
	ErrPrimaryEmpty = NewDBError(fmt.Errorf("entity primary is empty"))

	// ErrTableNameEmpty Error that table name is empty.
	ErrTableNameEmpty = NewDBError(fmt.Errorf("entity table name is empty"))

	// ErrNilEntity Error that entity is nil
	ErrNilEntity = NewDBError(fmt.Errorf("entity is nil"))

	// ErrRecordExist Error that entity primary key is exist
	ErrRecordExist = NewDBError(fmt.Errorf("data record is exist"))

	// ErrRecordNotExist Error that entity primary key is not exist
	ErrRecordNotExist = NewDBError(fmt.Errorf("data record is not exist"))

	// ErrIndexInvalid Error that entity index is invalid
	ErrIndexInvalid = NewDBError(fmt.Errorf("entity index is invalid"))

	// ErrEntityInvalid Error that entity is invalid
	ErrEntityInvalid = NewDBError(fmt.Errorf("entity is invalid"))
)
View Source
var PrimaryKeyMaxLength = 31

PrimaryKeyMaxLength The primary key length should be limited when the datastore is kube-api

Functions

func NewDBError

func NewDBError(err error) error

NewDBError new datastore error

Types

type Config

type Config struct {
	URL      string
	Database string
}

Config datastore config

type DBError

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

DBError datastore error

func (*DBError) Error

func (d *DBError) Error() string

type Entity

type Entity interface {
	SetCreateTime(time time.Time)
	SetUpdateTime(time time.Time)
	PrimaryKey() string
	TableName() string
	ShortTableName() string
	Index() map[string]interface{}
}

Entity database data model

func NewEntity

func NewEntity(in Entity) (Entity, error)

NewEntity Create a new object based on the input type

type FilterOptions

type FilterOptions struct {
	Queries    []FuzzyQueryOption
	In         []InQueryOption
	IsNotExist []IsNotExistQueryOption
	WithIn     []WithInTimeQueryOption
	Gte        []Gte
	Lte        []Lte
}

FilterOptions filter query returned items

type FuzzyQueryOption

type FuzzyQueryOption struct {
	Key   string
	Query string
}

FuzzyQueryOption defines the fuzzy query search filter option

type Gte

type Gte struct {
	Key  string
	Time time.Time
}

type InQueryOption

type InQueryOption struct {
	Key    string
	Values []string
}

InQueryOption defines the include search filter option

type IsNotExistQueryOption

type IsNotExistQueryOption struct {
	Key string
}

IsNotExistQueryOption means the value is empty

type Iterator

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

Iterator mongo iterator implementation

func (*Iterator) Close

func (i *Iterator) Close(ctx context.Context) error

Close iterator close

func (*Iterator) Decode

func (i *Iterator) Decode(entity interface{}) error

Decode decode data

func (*Iterator) Next

func (i *Iterator) Next(ctx context.Context) bool

Next read next data

type ListOptions

type ListOptions struct {
	FilterOptions
	Page     int
	PageSize int
	SortBy   []SortOption
}

ListOptions list api options

type Lte

type Lte struct {
	Key  string
	Time time.Time
}

type MongoDB

type MongoDB interface {
	// Add adds entity to database, Name() and TableName() can't return zero value.
	Add(ctx context.Context, entity Entity) error

	// BatchAdd will adds batched entities to database, Name() and TableName() can't return zero value.
	BatchAdd(ctx context.Context, entities []Entity) error

	// Put will update entity to database, Name() and TableName() can't return zero value.
	Put(ctx context.Context, entity Entity) error

	// Delete entity from database, Name() and TableName() can't return zero value.
	Delete(ctx context.Context, entity Entity) error

	// Get entity from database, Name() and TableName() can't return zero value.
	Get(ctx context.Context, entity Entity) error

	// List entities from database, TableName() can't return zero value, if no matches, it will return a zero list without error.
	List(ctx context.Context, query Entity, options *ListOptions) ([]Entity, error)

	// Count entities from database, TableName() can't return zero value.
	Count(ctx context.Context, entity Entity, options *FilterOptions) (int64, error)

	// IsExist Name() and TableName() can't return zero value.
	IsExist(ctx context.Context, entity Entity) (bool, error)
}

MongoDB datastore interface

func New

func New(ctx context.Context, cfg Config) (MongoDB, error)

New new mongodb datastore instance

type SortOption

type SortOption struct {
	Key   string
	Order SortOrder
}

SortOption describes the sorting parameters for list

type SortOrder

type SortOrder int

SortOrder is the order of sort

type WithInTimeQueryOption

type WithInTimeQueryOption struct {
	Key       string
	StartTime time.Time
	EndTime   time.Time
}

Jump to

Keyboard shortcuts

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