mongodb

package
v0.0.0-...-34ec42c Latest Latest
Warning

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

Go to latest
Published: Jun 24, 2024 License: MIT Imports: 11 Imported by: 2

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 ClientOptions struct {
	Host        string
	Port        string
	User        string
	Password    string
	RetryWrites bool
}

type DatabaseConfig

type DatabaseConfig struct {
	DatabaseName   string
	CollectionName string
}

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 SortOrder

type SortOrder string
const (
	ASC  SortOrder = "ASC"
	DESC SortOrder = "DESC"
)

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

Directories

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

Jump to

Keyboard shortcuts

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