mongo

package
v1.22.0 Latest Latest
Warning

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

Go to latest
Published: Dec 19, 2023 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ObjectIDFromHexIDString

func ObjectIDFromHexIDString(str *string) (primitive.ObjectID, error)

ObjectIDFromHexIDString takes a pointer to a string as input and returns a primitive.ObjectID and an error. It converts the input string, which is expected to be a hexadecimal representation of an ObjectID, to a primitive.ObjectID. The function is a method of the MongoDatabase struct and can be used to convert a string representation of an ObjectID to a primitive.ObjectID that can be used in MongoDB queries.

Types

type Client

type Client struct {
	// Conn serves as the database co
	Conn *mongo.Client
	// Logger is the logging utility used by this object
	Logger *zap.Logger
	// MaxConnectionAttempts outlines the maximum connection attempts
	// to initiate against the database
	MaxConnectionAttempts int
	// MaxRetriesPerOperation defines the maximum retries to attempt per failed database
	// connection attempt
	MaxRetriesPerOperation int
	// RetryTimeOut defines the maximum time until a retry operation is observed as a
	// timed out operation
	RetryTimeOut time.Duration
	// OperationSleepInterval defines the amount of time between retry operations
	// that the system sleeps
	OperationSleepInterval time.Duration
	// QueryTimeout defines the maximal amount of time a query can execute before being cancelled
	QueryTimeout time.Duration
	// Telemetry defines the object by which we will emit metrics, trace requests, and database operations
	Telemetry *instrumentation.Client
	// DatabaseName database name
	DatabaseName *string

	// CollectionNames is a list of collection names
	CollectionNames []string
	// ClientOptions defines the options to use when connecting to the database
	ClientOptions *options.ClientOptions
	// ConnectionURI defines the connection string to use when connecting to the database
	ConnectionURI *string
	// contains filtered or unexported fields
}

func New

func New(options ...Option) (*Client, error)

New creates a new instance of the mongo client

func (*Client) Close

func (c *Client) Close() error

Close closes the database connection

func (*Client) ComplexTransaction

func (c *Client) ComplexTransaction(ctx context.Context, callback MongoTx) (any, error)

ComplexTransaction is a wrapper around the `WithTransaction` method of the `mongo.Session` object.

func (*Client) GetCollection

func (c *Client) GetCollection(name string) (*mongo.Collection, error)

GetCollection returns a collection object by name

func (*Client) GetConnection

func (c *Client) GetConnection() *mongo.Client

GetConnection returns the database connection

func (*Client) StandardTransaction

func (c *Client) StandardTransaction(ctx context.Context, callback MongoTx) error

StandardTransaction is a wrapper around the `WithTransaction` method of the `mongo.Session` object.

func (*Client) StartDbSegment

func (c *Client) StartDbSegment(ctx context.Context, name, collectionName string) *newrelic.DatastoreSegment

StartDbSegment starts a new `newrelic.DatastoreSegment` for database operations. It takes in a context, a name for the operation, and a collection name as parameters. If telemetry is enabled, it retrieves the transaction from the context and starts a new datastore segment for the operation using the `StartNosqlDatastoreSegment` method from the `Telemetry` object. It returns the newly created segment. If telemetry is not enabled, it returns `nil`.

func (*Client) Validate

func (c *Client) Validate() error

Validate validates the client

type IClient

type IClient interface{}

type InMemoryTestDbClient

type InMemoryTestDbClient struct {
	DatabaseName string
	Client       *Client
	Server       *mim.Server
}

func NewInMemoryTestDbClient

func NewInMemoryTestDbClient(collectionNames []string) (*InMemoryTestDbClient, error)

NewInMemoryTestDbClient creates a new in-memory MongoDB database and returns a client to it.

func (*InMemoryTestDbClient) Teardown

func (c *InMemoryTestDbClient) Teardown() error

Teardown cleans up resources initialized by Setup. This function must be called once after all tests have finished running.

type MongoTx

type MongoTx func(sessCtx mongo.SessionContext) (any, error)

MongoTx is a type alias for the `WithTransaction` method of the `mongo.Session` object.

type Option

type Option func(*Client)

func WithClientOptions

func WithClientOptions(clientOptions *options.ClientOptions) Option

WithClientOptions sets the client options

func WithCollectionNames

func WithCollectionNames(collectionNames []string) Option

WithCollectionNames sets the collection names

func WithConnectionURI

func WithConnectionURI(connectionURI string) Option

WithConnectionURI sets the connection URI

func WithDatabaseName

func WithDatabaseName(databaseName string) Option

WithDatabaseName sets the database name

func WithLogger

func WithLogger(logger *zap.Logger) Option

WithLogger sets the logging utility used by this object

func WithMaxConnectionAttempts

func WithMaxConnectionAttempts(maxConnectionAttempts int) Option

WithMaxConnectionAttempts sets the maximum connection attempts to initiate against the database

func WithMaxRetriesPerOperation

func WithMaxRetriesPerOperation(maxRetriesPerOperation int) Option

WithMaxRetriesPerOperation sets the maximum retries to attempt per failed database connection attempt

func WithOperationSleepInterval

func WithOperationSleepInterval(operationSleepInterval time.Duration) Option

WithOperationSleepInterval sets the amount of time between retry operations that the system sleeps

func WithQueryTimeout

func WithQueryTimeout(queryTimeout time.Duration) Option

WithQueryTimeout sets the maximal amount of time a query can execute before being cancelled

func WithRetryTimeOut

func WithRetryTimeOut(retryTimeOut time.Duration) Option

WithRetryTimeOut sets the maximum time until a retry operation is observed as a timed out operation

func WithTelemetry

func WithTelemetry(telemetry *instrumentation.Client) Option

WithTelemetry sets the object by which we will emit metrics, trace requests, and database operations

Jump to

Keyboard shortcuts

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