database

package
v0.0.0-...-5d22fbd Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2023 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrDbMigerationFailed = errors.New("database migeration failed")
	ErrDbNotFound         = errors.New("database not found")
)
View Source
var DiskAggregationOption = &options.AggregateOptions{AllowDiskUse: utils.ToValue(true)}

DiskAggregationOption tells mongo to store aggregation results on disk instead of in memory. use this option when the aggregation result is large. see: https://godoc.org/go.mongodb.org/mongo-driver/mongo/options#AggregateOptions

Functions

func FindBy

func FindBy[Q, T any](ctx context.Context, params *FindByParams[Q, T]) ([]*T, error)

func FindOneBy

func FindOneBy[Q, T any](ctx context.Context, params *FindByParams[Q, T]) (*T, error)

func Init

func Init()

func LogList

func LogList(ctx context.Context, m *mongo.Collection, spec specification.Set)

func LogObject

func LogObject(ctx context.Context, m *mongo.Collection, spec specification.Set)

Types

type Config

type Config struct {
	Host   string
	Port   int
	User   string
	Pass   string
	Name   string
	Driver string
	Schema string
}

func (Config) DSN

func (config Config) DSN() string

type DataContextGetter

type DataContextGetter interface {
	// GetDataContext is used when we want to access underlying database for crud
	GetDataContext() any
}

type DatabaseController

type DatabaseController interface {
	TransactionFactoryGetter
	DatabaseGenerator
}

type DatabaseGenerator

type DatabaseGenerator interface {
	Generate() error
	Init() error
}

TODO: Ambiguous definition for db first strategy, func name should be changed.

type Document

type Document interface {
	GetID() string
	SetID(id string)
	GenerateID()
	SetCreatedAt()
	SetUpdatedAt()
	CollectionName() string
}

type DocumentBase

type DocumentBase struct {
	ID        string    `json:"_id" bson:"_id"`
	CreatedAt time.Time `bson:"createdAt"`
	UpdatedAt time.Time `bson:"updatedAt"`
}

func (DocumentBase) CollectionName

func (d DocumentBase) CollectionName() string

func (DocumentBase) GenerateID

func (d DocumentBase) GenerateID()

func (DocumentBase) GetID

func (d DocumentBase) GetID() string

func (DocumentBase) SetCreatedAt

func (d DocumentBase) SetCreatedAt()

func (DocumentBase) SetID

func (d DocumentBase) SetID(id string)

func (DocumentBase) SetUpdatedAt

func (d DocumentBase) SetUpdatedAt()

type FindByParams

type FindByParams[Q, T any] struct {
	Spec       specification.Set
	Collection *mongo.Collection
	ToModel    func(Q) *T
}

type Mapper

type Mapper[T, J Document] interface {
	MapToEntity(model J) T
	MapToModel(T) J

	MapToEntities(model []J) []T
	MapToModels(identity []T) []J
}

func NewDefaultMapper

func NewDefaultMapper[T, J Document]() Mapper[T, J]

type MongoTransactionFactory

type MongoTransactionFactory interface {
	New() Transaction
}

type Transaction

type Transaction interface {
	DataContextGetter
	Begin(ctx context.Context) error
	Rollback(ctx context.Context) error
	RollbackUnlessCommitted(ctx context.Context)
	Commit(ctx context.Context) error
}

type TransactionFactoryGetter

type TransactionFactoryGetter interface {
	GetTransactionFactory() (MongoTransactionFactory, error)
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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