Documentation
¶
Index ¶
- func ConvertMongoError(err error, entity string, keyFmt string, args ...interface{}) error
- func ConvertSortOptionsToMongo(defaultField string, defaultDirection xpaging.SortDirection, ...) bson.D
- func CreateIndexes(logger xlogger.Logger, collection *mongo.Collection, ...)
- func WithErrorConverter[Entity any, Dto any](converter MongoErrorConverterFunc) func(*Repository[Entity, Dto])
- func WithRegistry[Entity any, Dto any](registry *bsoncodec.Registry) func(*Repository[Entity, Dto])
- type EncoderDecoder
- type MongoErrorConverterFunc
- type MongoInMemory
- type Option
- type Repository
- func (r *Repository[Entity, Dto]) Count(ctx context.Context) (int64, error)
- func (r *Repository[Entity, Dto]) Find(ctx context.Context, filter interface{}) ([]Entity, error)
- func (r *Repository[Entity, Dto]) FindAll(ctx context.Context) ([]Entity, error)
- func (r *Repository[Entity, Dto]) FindById(ctx context.Context, id interface{}) (Entity, error)
- func (r *Repository[Entity, Dto]) FindByIds(ctx context.Context, ids ...interface{}) ([]Entity, error)
- func (r *Repository[Entity, Dto]) FindOne(ctx context.Context, filter interface{}) (Entity, error)
- func (r *Repository[Entity, Dto]) Store(ctx context.Context, entity Entity) error
- func (r *Repository[Entity, Dto]) Update(ctx context.Context, id interface{}, updater Updater[Entity]) error
- type Updater
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ConvertMongoError ¶
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 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
func NewRepository ¶ added in v0.25.1
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