mongo

package
v0.0.59 Latest Latest
Warning

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

Go to latest
Published: Jul 20, 2025 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// InsertOperationType ...
	InsertOperationType = "insert"
	// UpdateOperationType ...
	UpdateOperationType = "update"
	// DeleteOperationType ...
	DeleteOperationType = "delete"
)

Variables

View Source
var (
	ErrNotFound        = errors.New("not found")
	ErrNothingToCreate = errors.New("nothing to create")
	ErrNothingToUpdate = errors.New("nothing to update")
)

Functions

This section is empty.

Types

type Creator

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

Creator ...

func NewCreator

func NewCreator(client *mongo.Client, db string, collection string, connectionTimeout time.Duration) *Creator

NewCreator ...

func (*Creator) C

func (s *Creator) C(ctx context.Context, doc interface{}) (id primitive.ObjectID, err error)

C ...

func (*Creator) Create

func (s *Creator) Create(ctx context.Context, doc bson.D) (id primitive.ObjectID, err error)

Create ...

type DocumentKey

type DocumentKey struct {
	ID primitive.ObjectID `json:"_id" bson:"_id"`
}

DocumentKey ...

type Listener

type Listener[T d] struct {
	// contains filtered or unexported fields
}

Listener ...

func NewListener

func NewListener[T d](
	collection string,
	handler handler[T],
) *Listener[T]

NewListener ...

func (*Listener[T]) Listen

func (s *Listener[T]) Listen(ctx context.Context, change []byte) (err error)

Listen ...

type Mongo

type Mongo[T d] struct {
	Searcher  searcher[T]
	Processor processor[T]
	Listener  StreamListener
	Creator   creator
	Updater   updater
	Upserter  upserter
	Remover   remover
}

Mongo ...

func NewMongo

func NewMongo[T d](
	client *mongo.Client,
	db string,
	collection string,
	connectionTimeout time.Duration,
	cache cache[T],
	handler handler[T],
) *Mongo[T]

NewMongo ...

type NS

type NS struct {
	Db   string `bson:"db"`
	Coll string `bson:"coll"`
}

NS ...

type Processor

type Processor[T d] struct {
	// contains filtered or unexported fields
}

Processor ...

func NewProcessor

func NewProcessor[T d](
	cache cache[T],
	creator creator,
	updater updater,
	remover remover,
) *Processor[T]

NewProcessor ...

func (*Processor[T]) Create

func (p *Processor[T]) Create(ctx context.Context, ps T) (id string, err error)

Create ...

func (*Processor[T]) Delete

func (p *Processor[T]) Delete(ctx context.Context, id string) (err error)

func (*Processor[T]) PrepareCreate

func (p *Processor[T]) PrepareCreate(ctx context.Context, ps T) (prepared T, doc bson.D, err error)

PrepareCreate ...

func (*Processor[T]) PrepareUpdate

func (p *Processor[T]) PrepareUpdate(ctx context.Context, ps T) (prepared T, set bson.D, unset bson.D, err error)

func (*Processor[T]) Update

func (p *Processor[T]) Update(ctx context.Context, ps T) (T, error)

Update ...

type Remover

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

Remover ...

func NewRemover

func NewRemover(client *mongo.Client, db string, collection string, connectionTimeout time.Duration) *Remover

NewRemover ...

func (*Remover) Remove

func (s *Remover) Remove(ctx context.Context, doc interface{}) (deletedCount int, err error)

Remove ...

func (*Remover) RemoveMany

func (s *Remover) RemoveMany(ctx context.Context, doc interface{}) (deletedCount int, err error)

RemoveMany ...

type Searcher

type Searcher[T d] struct {
	// contains filtered or unexported fields
}

Searcher ...

func NewSearcher

func NewSearcher[T d](
	client *mongo.Client,
	db string,
	collection string,
	connectionTimeout time.Duration,
) *Searcher[T]

NewSearcher ...

func (*Searcher[T]) All

func (s *Searcher[T]) All(ctx context.Context) (items []T, err error)

All ...

type Stream

type Stream struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

Stream ...

func NewStream

func NewStream(
	change *mongo.ChangeStream,
	listeners map[string]map[string]StreamListener,
) *Stream

NewStream ...

func (*Stream) AddListener

func (s *Stream) AddListener(ctx context.Context, db, col string, listener StreamListener)

AddListener ...

func (*Stream) Listen

func (s *Stream) Listen(ctx context.Context) (err error)

Listen ...

type StreamDelete

type StreamDelete struct {
	DocumentKey DocumentKey `json:"documentKey" bson:"documentKey"`
}

StreamDelete ...

type StreamInsert

type StreamInsert[T any] struct {
	FullDocument T `json:"fullDocument" bson:"fullDocument"`
}

StreamInsert ...

type StreamListener

type StreamListener = interface {
	Listen(ctx context.Context, change []byte) (err error)
}

StreamListener ...

type StreamType

type StreamType struct {
	OperationType string `json:"operationType" bson:"operationType"`
}

StreamType ...

type StreamUpdate

type StreamUpdate[T any] struct {
	DocumentKey       DocumentKey          `json:"documentKey" bson:"documentKey"`
	UpdateDescription UpdateDescription[T] `json:"updateDescription" bson:"updateDescription"`
}

StreamUpdate ...

type StreamingNS

type StreamingNS struct {
	NS NS `bson:"ns"`
}

StreamingNS ...

type UpdateDescription

type UpdateDescription[T any] struct {
	UpdatedFields T        `json:"updatedFields" bson:"updatedFields"`
	RemovedFields []string `json:"removedFields" bson:"removedFields"`
}

UpdateDescription ...

type Updater

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

Updater ...

func NewUpdater

func NewUpdater(client *mongo.Client, db string, collection string, connectionTimeout time.Duration) *Updater

NewUpdater ...

func (*Updater) UpdateOne

func (s *Updater) UpdateOne(ctx context.Context, id string, version *int64, set bson.D, unset bson.D) (found bool, err error)

UpdateOne ... Алгоритм записи данных в монго 1. Взяли данные с версией, например 1 2. Подготовили изменение в данных 3. Вытаемся обновить данные с версией 1 4. Если не получилось (версия ушла дальше), то берем снова данные с новой версией, например 3, и пытаемся их записать

type Upsert

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

Upsert ...

func NewUpsert

func NewUpsert(client *mongo.Client, db string, collection string, connectionTimeout time.Duration) *Upsert

NewUpsert ...

func (*Upsert) UpsertMany

func (s *Upsert) UpsertMany(ctx context.Context, ids []string, set []interface{}) (err error)

UpsertMany ...

func (*Upsert) UpsertOne

func (s *Upsert) UpsertOne(ctx context.Context, id string, doc interface{}) (err error)

UpsertOne ...

Jump to

Keyboard shortcuts

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