manipmongo

package
v1.135.0 Latest Latest
Warning

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

Go to latest
Published: Mar 22, 2023 License: Apache-2.0 Imports: 18 Imported by: 4

Documentation

Overview

Package manipmongo provides a MongoDB backed TransactionalManipulator.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CompileFilter

func CompileFilter(f *elemental.Filter, opts ...CompilerOption) bson.D

CompileFilter compiles the given manipulate Filter into a mongo filter.

func ContextOptionUpsert added in v1.120.0

func ContextOptionUpsert(operations bson.M) manipulate.ContextOption

ContextOptionUpsert tells to use upsert for an Create operation. The given operation will be executed for the upsert command. You cannot use "$set" which is always set to be the identifier. If you do so, ContextOptionUpsert will panic. If you use $setOnInsert, you must not set _id. If you do so, it will panic.

func CreateCollection

func CreateCollection(manipulator manipulate.Manipulator, identity elemental.Identity, info *mgo.CollectionInfo) error

CreateCollection creates a collection using the given mgo.CollectionInfo.

func CreateIndex

func CreateIndex(manipulator manipulate.Manipulator, identity elemental.Identity, indexes ...mgo.Index) error

CreateIndex creates multiple mgo.Index for the collection storing info for the given identity using the given manipulator.

func DeleteIndex

func DeleteIndex(manipulator manipulate.Manipulator, identity elemental.Identity, indexes ...string) error

DeleteIndex deletes multiple mgo.Index for the collection.

func DoesDatabaseExist

func DoesDatabaseExist(manipulator manipulate.Manipulator) (bool, error)

DoesDatabaseExist checks if the database used by the given manipulator exists.

func DropDatabase

func DropDatabase(manipulator manipulate.Manipulator) error

DropDatabase drops the entire database used by the given manipulator.

func EnsureIndex added in v1.67.0

func EnsureIndex(manipulator manipulate.Manipulator, identity elemental.Identity, indexes ...mgo.Index) error

EnsureIndex works like create index, but it will delete existing index if they changed before creating a new one.

func GetAttributeEncrypter added in v1.98.0

func GetAttributeEncrypter(manipulator manipulate.Manipulator) elemental.AttributeEncrypter

GetAttributeEncrypter returns the attribute encrypter of the given mongo manipulator..

func GetDatabase

func GetDatabase(manipulator manipulate.Manipulator) (*mgo.Database, func(), error)

GetDatabase returns a ready to use mgo.Database. Use at your own risks. You are responsible for closing the session by calling the returner close function

func HandleQueryError added in v1.121.1

func HandleQueryError(err error) error

HandleQueryError handles the provided upstream error returned by Mongo by returning a corresponding manipulate error type.

func IsMongoManipulator added in v1.125.0

func IsMongoManipulator(manipulator manipulate.Manipulator) bool

IsMongoManipulator returns true if this is a mongo manipulator

func IsUpsert added in v1.123.0

func IsUpsert(mctx manipulate.Context) bool

IsUpsert returns True if the mongo request is an Upsert operation, false otherwise.

func New

func New(url string, db string, options ...Option) (manipulate.TransactionalManipulator, error)

New returns a new manipulator backed by MongoDB.

func RunQuery added in v1.61.0

func RunQuery(mctx manipulate.Context, operationFunc func() (any, error), baseRetryInfo RetryInfo) (any, error)

RunQuery runs a function that must run a mongodb operation. It will retry in case of failure. This is an advanced helper can be used when you get a session from using GetDatabase().

func SetAttributeEncrypter added in v1.98.0

func SetAttributeEncrypter(manipulator manipulate.Manipulator, enc elemental.AttributeEncrypter)

SetAttributeEncrypter switch the attribute encrypter of the given mongo manipulator. This is only useful in some rare cases like miugration, and it is not go routine safe.

func SetConsistencyMode

func SetConsistencyMode(manipulator manipulate.Manipulator, mode mgo.Mode, refresh bool)

SetConsistencyMode sets the mongo consistency mode of the mongo session.

Types

type CompilerOption added in v1.121.1

type CompilerOption func(*compilerConfig)

CompilerOption represents an option that can be passed to CompileFilter.

func CompilerOptionTranslateKeysFromSpec added in v1.121.1

func CompilerOptionTranslateKeysFromSpec(spec elemental.AttributeSpecifiable) CompilerOption

CompilerOptionTranslateKeysFromSpec is an option that will configure the compiler to use the provided elemental.AttributeSpecifiable to lookup the BSON field name corresponding to the filter keys.

This option is mostly useful in cases where the exposed attribute name is not the same as the field name that is stored in Mongo as sometimes you need to use a short field name to save space.

type Option

type Option func(*config)

An Option represents a maniphttp.Manipulator option.

func OptionAttributeEncrypter added in v1.98.0

func OptionAttributeEncrypter(enc elemental.AttributeEncrypter) Option

OptionAttributeEncrypter allows to set an elemental.AttributeEncrypter to use to encrypt/decrypt elemental.AttributeEncryptable.

func OptionConnectionPoolLimit

func OptionConnectionPoolLimit(poolLimit int) Option

OptionConnectionPoolLimit sets maximum size of the connection pool.

func OptionConnectionTimeout

func OptionConnectionTimeout(connectTimeout time.Duration) Option

OptionConnectionTimeout sets the connection timeout.

func OptionCredentials

func OptionCredentials(username, password, authsource string) Option

OptionCredentials sets the username and password to use for authentication.

func OptionDefaultReadConsistencyMode

func OptionDefaultReadConsistencyMode(consistency manipulate.ReadConsistency) Option

OptionDefaultReadConsistencyMode sets the default read consistency mode.

func OptionDefaultRetryFunc added in v1.61.0

func OptionDefaultRetryFunc(f manipulate.RetryFunc) Option

OptionDefaultRetryFunc sets the default retry func to use if manipulate.Context does not have one.

func OptionDefaultWriteConsistencyMode

func OptionDefaultWriteConsistencyMode(consistency manipulate.WriteConsistency) Option

OptionDefaultWriteConsistencyMode sets the default write consistency mode.

func OptionExplain added in v1.107.0

func OptionExplain(explain map[elemental.Identity]map[elemental.Operation]struct{}) Option

OptionExplain allows to tell manipmongo to explain the query before it runs it for the given identities on the given operations. For example, consider passing:

    map[elemental.Identity][]elemental.Operation{
        model.ThisIndentity: []elemental.Operation{elemental.OperationRetrieveMany, elemental.OperationCreate},
        model.ThatIndentity: []elemental.Operation{}, // or nil
    }

This would trigger explanation on retrieveMany and create for model.ThisIndentity
and every operation on model.ThatIndentity.

func OptionForceReadFilter added in v1.67.0

func OptionForceReadFilter(f bson.D) Option

OptionForceReadFilter allows to set a bson.D filter that will always reducing the scope of the reads to that filter.

func OptionSharder

func OptionSharder(sharder Sharder) Option

OptionSharder sets the sharder.

func OptionSocketTimeout

func OptionSocketTimeout(socketTimeout time.Duration) Option

OptionSocketTimeout sets the socket timeout.

func OptionTLS

func OptionTLS(tlsConfig *tls.Config) Option

OptionTLS sets the tls configuration for the connection.

func OptionTranslateKeysFromModelManager added in v1.121.1

func OptionTranslateKeysFromModelManager(manager elemental.ModelManager) Option

OptionTranslateKeysFromModelManager can be used to configure the manipulator to lookup the BSON field name for identities from the configured elemental.ModelManager. If a key or identity is not found in the manager, the value provided will be used.

This option is mostly useful in cases where the exposed attribute name is not the same as the field name that is stored in Mongo as sometimes you need to use a short field name to optimize storage.

type RetryInfo added in v1.61.0

type RetryInfo struct {
	Operation elemental.Operation
	Identity  elemental.Identity
	// contains filtered or unexported fields
}

A RetryInfo contains information about a retry,

func (RetryInfo) Context added in v1.61.0

func (i RetryInfo) Context() manipulate.Context

Context returns the manipulate.Context used.

func (RetryInfo) Err added in v1.61.0

func (i RetryInfo) Err() error

Err returns the error that caused the retry.

func (RetryInfo) Try added in v1.61.0

func (i RetryInfo) Try() int

Try returns the try number.

type Sharder

type Sharder interface {

	// Shard will be call when the shard key needs to be set to
	// the given elemental.Identifiable.
	Shard(manipulate.TransactionalManipulator, manipulate.Context, elemental.Identifiable) error

	// OnShardedWrite will be called after a successful sharded write
	// If it returns an error, this error will be returned to the caller
	// of the manipulate Operation, but the object that has been
	// created will still be created in database.
	OnShardedWrite(manipulate.TransactionalManipulator, manipulate.Context, elemental.Operation, elemental.Identifiable) error

	// FilterOne returns the filter bit as bson.M that must be
	// used to perform an efficient localized query for a single object.
	//
	// You can return nil which will trigger a broadcast.
	FilterOne(manipulate.TransactionalManipulator, manipulate.Context, elemental.Identifiable) (bson.D, error)

	// FilterMany returns the filter bit as bson.M that must be
	// used to perform an efficient localized query for multiple objects.
	//
	// You can return nil which will trigger a broadcast.
	FilterMany(manipulate.TransactionalManipulator, manipulate.Context, elemental.Identity) (bson.D, error)
}

A Sharder is the interface of an object that can be use to manage sharding of resources.

Directories

Path Synopsis
Package internal is a generated GoMock package.
Package internal is a generated GoMock package.

Jump to

Keyboard shortcuts

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