xmongo

package
v0.25.1 Latest Latest
Warning

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

Go to latest
Published: Apr 16, 2023 License: MIT Imports: 20 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConvertMongoError

func ConvertMongoError(err error, entity string, keyFmt string, args ...interface{}) error

func ConvertSortOptionsToMongo

func ConvertSortOptionsToMongo(defaultField string, defaultDirection xpaging.SortDirection, options xpaging.SortOptions) bson.D

func CreateIndexes

func CreateIndexes(logger xlogger.Logger, collection *mongo.Collection, models ...mongo.IndexModel)

CreateIndexes creates one or more indexes in the provided collection reporting the result in the log

func WithErrorConverter added in v0.25.1

func WithErrorConverter[Entity any, Dto any](converter MongoErrorConverterFunc) func(*Repository[Entity, Dto])

WithErrorConverter provides a custom error converter

func WithRegistry added in v0.25.1

func WithRegistry[Entity any, Dto any](registry *bsoncodec.Registry) func(*Repository[Entity, Dto])

WithRegistry provides a registry to encode / decode entities

Types

type EncoderDecoder added in v0.25.1

type EncoderDecoder interface {
	bsoncodec.ValueDecoder
	bsoncodec.ValueEncoder
	Register(builder *bsoncodec.RegistryBuilder)
}

func NewDecoderEncoder added in v0.25.1

func NewDecoderEncoder[Entity, Dto, Base any](toDto func(Entity) Dto, fromDto func(Dto) Entity) EncoderDecoder

type MongoErrorConverterFunc added in v0.25.1

type MongoErrorConverterFunc func(err error, entity string, keyFmt string, args ...interface{}) error

type MongoInMemory

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

MongoInMemory allows to start a MongoDB instance in memory to run tests against To use it, create a new instance of MongoInMemory and call Connect() before running your tests and call Disconnect() after your tests are done. The client to connect to MongoDB can be retrieved by calling Client().

func (*MongoInMemory) Client

func (m *MongoInMemory) Client() *mongo.Client

func (*MongoInMemory) Connect

func (m *MongoInMemory) Connect()

func (*MongoInMemory) Disconnect

func (m *MongoInMemory) Disconnect()

type Option added in v0.25.1

type Option[Entity entity.Entity, Dto any] func(*Repository[Entity, Dto])

type Repository added in v0.25.1

type Repository[Entity entity.Entity, Dto any] struct {
	// contains filtered or unexported fields
}

func NewRepository added in v0.25.1

func NewRepository[Entity entity.Entity, Dto any](client *mongo.Client, databaseName string, collectionName string, options ...Option[Entity, Dto]) Repository[Entity, Dto]

func (*Repository[Entity, Dto]) Count added in v0.25.1

func (r *Repository[Entity, Dto]) Count(ctx context.Context) (int64, error)

func (*Repository[Entity, Dto]) Find added in v0.25.1

func (r *Repository[Entity, Dto]) Find(ctx context.Context, filter interface{}) ([]Entity, error)

func (*Repository[Entity, Dto]) FindAll added in v0.25.1

func (r *Repository[Entity, Dto]) FindAll(ctx context.Context) ([]Entity, error)

func (*Repository[Entity, Dto]) FindById added in v0.25.1

func (r *Repository[Entity, Dto]) FindById(ctx context.Context, id interface{}) (Entity, error)

func (*Repository[Entity, Dto]) FindByIds added in v0.25.1

func (r *Repository[Entity, Dto]) FindByIds(ctx context.Context, ids ...interface{}) ([]Entity, error)

func (*Repository[Entity, Dto]) FindOne added in v0.25.1

func (r *Repository[Entity, Dto]) FindOne(ctx context.Context, filter interface{}) (Entity, error)

func (*Repository[Entity, Dto]) Store added in v0.25.1

func (r *Repository[Entity, Dto]) Store(ctx context.Context, entity Entity) error

func (*Repository[Entity, Dto]) Update added in v0.25.1

func (r *Repository[Entity, Dto]) Update(ctx context.Context, id interface{}, updater Updater[Entity]) error

Update updates an entity with the provided updater function The updater function is passed the entity to update and should return the updated entity The update function will retry 5 times if an optimistic locking error occurs Sample use

func (e *SomeCommandHandler) SetName(ctx context.Context, cmd SetNameCommand) {
    e.repository.Update(ctx, e.Id(), func(ctx context.Context, e Entity) (Entity, error) {
        err := e.ChangeName(cmd) // this changes the name and creates NameChangedEvent
        return e, err
    })
}

Repository will update the entity and publish the NameChangedEvent

type Updater added in v0.25.1

type Updater[Entity any] func(ctx context.Context, entity Entity) (Entity, error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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