Documentation
¶
Overview ¶
Package mongodb contains mongo specific configuration, client & settings to interact with a Mongo DB cluster/database
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ClientOptions ¶
type DatabaseConfig ¶
type FilterOptions ¶
type FilterOptions struct {
// Limit is the number of records to be returned from a query. If set to 0, all records are returned
// If set to more than what the records are in the database, all records are returned, A negative value
// will instead be set to the absolute value and that value is used as the limit, so -100 becomes 100
Limit int
// Offset is used to skip a number of records, so for a value of n, the first n documents are skipped
Offset int
// OrderBy represents the field to order collections by. This will be used as the key in the filter
OrderBy string
// Sort Order is the order to use in the sort, defaults to descending
SortOrder SortOrder
// FieldFilter is the map to apply to a filter to retrieve fields that match the given criteria
FieldFilter map[string]map[string]string
}
FilterOptions is a structure that contains filter options for a query to return many records
type FilterParams ¶
type FilterParams struct {
// Key is the name of the field of the document
Key string
// Value is the value of the field to query the document
Value any
}
FilterParams are the filter parameters used for querying specific fields in a document
type IndexParam ¶
type IndexParam struct {
// Keys to apply an index to
Keys []KeyParam
// Name is the name of the index
Name string
}
IndexParam is used to create unique index for a given key
type KeyParam ¶
type KeyParam struct {
// Key is the name of the field in the document
Key string
// Value indicates the order that is going to be applied to a key.
// If used for index creation, in the index for the given key, 1 sets in ascending order -1 sets it in descending order
Value any
}
KeParam
type MongoDBClient ¶
type MongoDBClient[T any] interface { // Insert adds a new data item Insert(ctx context.Context, model T) (primitive.ObjectID, error) // BulkInsert inserts a collection of data items BulkInsert(ctx context.Context, models []any) ([]primitive.ObjectID, error) // FindById retrieves the model using the given key name and the id value populating the model with the retrieved data if found FindById(ctx context.Context, keyName string, id string) (T, error) // FindAll retrieves all the items with a given filter FindAll(ctx context.Context, filterOptions FilterOptions) ([]T, error) // Update updates the model Update(ctx context.Context, model T, updateOptions UpdateOptions) error // Delete deletes a record given it's ID name and the id value Delete(ctx context.Context, keyName string, id string) error // Disconnect disconnects from the current connection Disconnect(context.Context) error // CreateIndex creates unique index for the given keys CreateIndex(ctx context.Context, indexParam IndexParam) (string, error) }
MongoDBClient defines the capabilities of this mongo database client
func New ¶
func New[T any](config MongoDBConfig, log logger.Logger) (MongoDBClient[T], error)
New creates a new mongo DB client
type MongoDBConfig ¶
type MongoDBConfig struct {
Client ClientOptions
DBConfig DatabaseConfig
}
type UpdateOptions ¶
type UpdateOptions struct {
// Upsert sets whether to create a record if it is not found while updating
Upsert bool
// FieldOptions are field options to use for updating a document
FieldOptions map[string]any
// SetOptions are set options to use for updating a nested document in a document
SetOptions map[string]map[string]any
// FilterParams is the filter parameters to use for querying a document to update
FilterParams FilterParams
}
UpdateOptions is a structure that contains update options
Click to show internal directories.
Click to hide internal directories.