db

package
v0.0.0-...-22a6a96 Latest Latest
Warning

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

Go to latest
Published: Apr 8, 2024 License: BSD-3-Clause Imports: 11 Imported by: 20

Documentation

Overview

Package db contains utility functions for dealing with database

Package db contains utility functions for dealing with database

Package db contains utility functions for dealing with database

Package db contains utility functions for dealing with database

Index

Constants

View Source
const (
	// DefaultMongoDBURL is a default mongodb URL
	DefaultMongoDBURL = "mongodb://localhost:27017"
)

Variables

This section is empty.

Functions

func CreateCollectionWithSchema

func CreateCollectionWithSchema(ctx context.Context, conn *MongoDBConnection, collectionName string, schema bson.M) error

CreateCollectionWithSchema creates a collection and sets validation schema on the collection. If the collection already exists, then we will attempt to set the validation schema on the existing collection. This function will check for any existing documents in the collection that does NOT match the new schema, if there is any such documents, this function will return an error without setting the validation schema.

See exampleCreateCollectionWithSchema for an example of using this function.

func GetBSONRegistry

func GetBSONRegistry() (*bsoncodec.Registry, error)

GetBSONRegistry returns bson registry with JSONFallbackStructTagParser

func IsDocumentValidationFailure

func IsDocumentValidationFailure(err error) bool

IsDocumentValidationFailure checks if an error is due to document validation (document does not match the validation schema).

func IsDuplicateError

func IsDuplicateError(err error) bool

IsDuplicateError returns true, if the mongodb error received is a duplicate entry on _id copied from https://stackoverflow.com/questions/56916969/with-mongodb-go-driver-how-do-i-get-the-inner-exceptions

func IsNoDocumentError

func IsNoDocumentError(err error) bool

IsNoDocumentError returns true, if the mongodb error received is for no document found

Types

type KeyValue

type KeyValue struct {
	Key   string
	Value interface{}
}

KeyValue is a struct for holding key-value data type

type MockObjectStore

type MockObjectStore struct {
	Mock *mock.Mock
}

MockObjectStore mocks object store implements ObjectStore interface

func CreateMockObjectStore

func CreateMockObjectStore() (*MockObjectStore, error)

CreateMockObjectStore creates MockObjectStore

func (*MockObjectStore) Delete

func (store *MockObjectStore) Delete(collection string, id string) (bool, error)

Delete deletes a document in the given collection, returns true if deleted successfully

func (*MockObjectStore) DeleteConditional

func (store *MockObjectStore) DeleteConditional(collection string, conditions map[string]interface{}) (int64, error)

DeleteConditional deletes multiple document in the given collection that satisfy the given conditions, returns the number of documents deleted as the first result

func (*MockObjectStore) Get

func (store *MockObjectStore) Get(collection string, id string, result interface{}) error

Get returns a document in the given collection with the given id results must be a pointer to a struct (e.g., *ExampleStruct)

func (*MockObjectStore) GetConditional

func (store *MockObjectStore) GetConditional(collection string, conditions map[string]interface{}, result interface{}) error

GetConditional returns a document in the given collection that satisfies the given conditions results must be a pointer to a struct (e.g., *ExampleStruct)

func (*MockObjectStore) GetConditionalWithSort

func (store *MockObjectStore) GetConditionalWithSort(collection string, conditions map[string]interface{}, sort []KeyValue, result interface{}) error

GetConditionalWithSort returns a document in the given collection that satisfies the given conditions then sorts results must be a pointer to a struct (e.g., *ExampleStruct)

func (*MockObjectStore) Insert

func (store *MockObjectStore) Insert(collection string, document interface{}) error

Insert inserts a document into the given collection

func (*MockObjectStore) InsertMany

func (store *MockObjectStore) InsertMany(collection string, documents []interface{}) error

InsertMany inserts documents into the given collection

func (*MockObjectStore) List

func (store *MockObjectStore) List(collection string, results interface{}) error

List lists all documents in the given collection results must be a pointer to a struct array (e.g., *[]ExampleStruct)

func (*MockObjectStore) ListConditional

func (store *MockObjectStore) ListConditional(collection string, conditions map[string]interface{}, results interface{}) error

ListConditional lists documents in the given collection that satisfy the given conditions results must be a pointer to a struct array (e.g., *[]ExampleStruct)

func (*MockObjectStore) ListConditionalWithSort

func (store *MockObjectStore) ListConditionalWithSort(collection string, conditions map[string]interface{}, sort []KeyValue, results interface{}) error

ListConditionalWithSort lists documents in the given collection that satisfy the given conditions then sorts results must be a pointer to a struct array (e.g., *[]ExampleStruct)

func (*MockObjectStore) ListForUser

func (store *MockObjectStore) ListForUser(collection string, owner string, results interface{}) error

ListForUser lists all documents in the given collection for a user results must be a pointer to a struct array (e.g., *[]ExampleStruct)

func (*MockObjectStore) Release

func (store *MockObjectStore) Release() error

Release realses resources

func (*MockObjectStore) Replace

func (store *MockObjectStore) Replace(collection string, id string, replaceDocument interface{}) (bool, error)

Replace replaces a document in the given collection, returns true if replaced successfully Replace is different from Update. It updates an entire document, while Update updates some fields in a document.

func (*MockObjectStore) Update

func (store *MockObjectStore) Update(collection string, id string, updateDocument interface{}, updateFieldNames []string) (bool, error)

Update updates a document in the given collection, returns true if updated successfully

func (*MockObjectStore) UpdateConditional

func (store *MockObjectStore) UpdateConditional(collection string, conditions map[string]interface{}, updateDocument interface{}, updateFieldNames []string) (int64, error)

UpdateConditional updates a document in the given collection that satisfies the given conditions, returns true if updated successfully

func (*MockObjectStore) UpdateConditionalWithMap

func (store *MockObjectStore) UpdateConditionalWithMap(collection string, conditions map[string]interface{}, updateDocument map[string]interface{}) (int64, error)

UpdateConditionalWithMap updates a document in the given collection that satisfies the given conditions, returns true if updated successfully

func (*MockObjectStore) UpdateWithMap

func (store *MockObjectStore) UpdateWithMap(collection string, id string, updateDocument map[string]interface{}) (bool, error)

UpdateWithMap updates a document in the given collection, returns true if updated successfully

type MongoDBConfig

type MongoDBConfig struct {
	URL    string `envconfig:"MONGODB_URL" default:"mongodb://localhost:27017"`
	DBName string `envconfig:"MONGODB_DB_NAME"` // no default, every microservice should ensure they use a different database name (and not cacao)
}

MongoDBConfig stores configurations used by mongodb

type MongoDBConnection

type MongoDBConnection struct {
	Config   *MongoDBConfig
	Client   *mongo.Client
	Database *mongo.Database
}

MongoDBConnection contains MongoDB connection info

func NewMongoDBConnection

func NewMongoDBConnection(config *MongoDBConfig) (*MongoDBConnection, error)

NewMongoDBConnection connects to MongoDB

func (*MongoDBConnection) Delete

func (conn *MongoDBConnection) Delete(collection string, filter map[string]interface{}, opts ...*options.DeleteOptions) (bool, error)

Delete deletes a document in the given collection, returns true if deleted successfully

func (*MongoDBConnection) DeleteMany

func (conn *MongoDBConnection) DeleteMany(collection string, filter map[string]interface{}, opts ...*options.DeleteOptions) (int64, error)

DeleteMany deletes multiple document in the given collection, returns the number of documents deleted as the first result

func (*MongoDBConnection) Disconnect

func (conn *MongoDBConnection) Disconnect() error

Disconnect disconnects MongoDB connection

func (*MongoDBConnection) Get

func (conn *MongoDBConnection) Get(collection string, filter map[string]interface{}, result interface{}, opts ...*options.FindOneOptions) error

Get returns a document in the given collection results must be a pointer to a struct (i.e., *ExampleStruct)

func (*MongoDBConnection) Insert

func (conn *MongoDBConnection) Insert(collection string, document interface{}, opts ...*options.InsertOneOptions) error

Insert inserts a document into the given collection

func (*MongoDBConnection) InsertMany

func (conn *MongoDBConnection) InsertMany(collection string, documents []interface{}, opts ...*options.InsertManyOptions) error

InsertMany inserts multiple documents into the given collection

func (*MongoDBConnection) List

func (conn *MongoDBConnection) List(collection string, filter map[string]interface{}, results interface{}, opts ...*options.FindOptions) error

List lists filtered documents in the given collection results must be a pointer to a struct array (i.e., *[]ExampleStruct)

func (*MongoDBConnection) ListAndGetCursor

func (conn *MongoDBConnection) ListAndGetCursor(collection string, filter map[string]interface{}, opts ...*options.FindOptions) (*mongo.Cursor, error)

ListAndGetCursor lists filtered documents in the given collection, and returns a cursor

func (*MongoDBConnection) Replace

func (conn *MongoDBConnection) Replace(collection string, filter map[string]interface{}, replaceDocument interface{}, opts ...*options.ReplaceOptions) (bool, error)

Replace replaces a document in the given collection, returns true if replaced successfully Replace is different from Update. It updates an entire document, while Update updates some fields in a document.

func (*MongoDBConnection) Update

func (conn *MongoDBConnection) Update(collection string, filter map[string]interface{}, updateValues map[string]interface{}, opts ...*options.UpdateOptions) (bool, error)

Update updates a document in the given collection, returns true if updated successfully

func (*MongoDBConnection) UpdateMany

func (conn *MongoDBConnection) UpdateMany(collection string, filter map[string]interface{}, updateValues map[string]interface{}, opts ...*options.UpdateOptions) (int64, error)

UpdateMany updates multiple document in the given collection, returns the number of documents updated as the first result

type MongoDBObjectStore

type MongoDBObjectStore struct {
	Connection *MongoDBConnection
}

MongoDBObjectStore that provides object level access to MongoDB implements ObjectStore interface

func CreateMongoDBObjectStore

func CreateMongoDBObjectStore(config *MongoDBConfig) (*MongoDBObjectStore, error)

CreateMongoDBObjectStore connects to MongoDB and returns MongoDBObjectStore

func (*MongoDBObjectStore) Delete

func (store *MongoDBObjectStore) Delete(collection string, id string) (bool, error)

Delete deletes a document in the given collection, returns true if deleted successfully

func (*MongoDBObjectStore) DeleteConditional

func (store *MongoDBObjectStore) DeleteConditional(collection string, conditions map[string]interface{}) (int64, error)

DeleteConditional deletes multiple document in the given collection that satisfy the given conditions, returns the number of documents deleted as the first result

func (*MongoDBObjectStore) Get

func (store *MongoDBObjectStore) Get(collection string, id string, result interface{}) error

Get returns a document in the given collection with the given id results must be a pointer to a struct (i.e., *ExampleStruct)

func (*MongoDBObjectStore) GetConditional

func (store *MongoDBObjectStore) GetConditional(collection string, conditions map[string]interface{}, result interface{}) error

GetConditional returns a document in the given collection that satisfies the given conditions results must be a pointer to a struct (i.e., *ExampleStruct)

func (*MongoDBObjectStore) GetConditionalWithSort

func (store *MongoDBObjectStore) GetConditionalWithSort(collection string, conditions map[string]interface{}, sort []KeyValue, result interface{}) error

GetConditionalWithSort returns a document in the given collection that satisfies the given conditions then sorts results must be a pointer to a struct (i.e., *ExampleStruct)

func (*MongoDBObjectStore) Insert

func (store *MongoDBObjectStore) Insert(collection string, document interface{}) error

Insert inserts a document into the given collection

func (*MongoDBObjectStore) InsertMany

func (store *MongoDBObjectStore) InsertMany(collection string, documents []interface{}) error

InsertMany inserts multiple documents into the given collection

func (*MongoDBObjectStore) List

func (store *MongoDBObjectStore) List(collection string, results interface{}) error

List lists all documents in the given collection results must be a pointer to a struct array (i.e., *[]ExampleStruct)

func (*MongoDBObjectStore) ListConditional

func (store *MongoDBObjectStore) ListConditional(collection string, conditions map[string]interface{}, results interface{}) error

ListConditional lists documents in the given collection that satisfie the given conditions results must be a pointer to a struct array (i.e., *[]ExampleStruct)

func (*MongoDBObjectStore) ListConditionalWithSort

func (store *MongoDBObjectStore) ListConditionalWithSort(collection string, conditions map[string]interface{}, sort []KeyValue, results interface{}) error

ListConditionalWithSort lists documents in the given collection that satisfie the given conditions then sorts results must be a pointer to a struct array (i.e., *[]ExampleStruct)

func (*MongoDBObjectStore) ListForUser

func (store *MongoDBObjectStore) ListForUser(collection string, owner string, results interface{}) error

ListForUser lists all documents in the given collection for a user results must be a pointer to a struct array (i.e., *[]ExampleStruct)

func (*MongoDBObjectStore) Release

func (store *MongoDBObjectStore) Release() error

Release disconnects MongoDB connection and realses resources

func (*MongoDBObjectStore) Replace

func (store *MongoDBObjectStore) Replace(collection string, id string, replaceDocument interface{}) (bool, error)

Replace replaces a document in the given collection, returns true if replaced successfully Replace is different from Update. It updates an entire document, while Update updates some fields in a document.

func (*MongoDBObjectStore) Update

func (store *MongoDBObjectStore) Update(collection string, id string, updateDocument interface{}, updateFieldNames []string) (bool, error)

Update updates a document in the given collection, returns true if updated successfully

func (*MongoDBObjectStore) UpdateConditional

func (store *MongoDBObjectStore) UpdateConditional(collection string, conditions map[string]interface{}, updateDocument interface{}, updateFieldNames []string) (int64, error)

UpdateConditional updates a document in the given collection that satisfies the given conditions, returns true if updated successfully

func (*MongoDBObjectStore) UpdateConditionalWithMap

func (store *MongoDBObjectStore) UpdateConditionalWithMap(collection string, conditions map[string]interface{}, updateDocument map[string]interface{}) (int64, error)

UpdateConditionalWithMap updates a document in the given collection that satisfies the given conditions, returns true if updated successfully

func (*MongoDBObjectStore) UpdateWithMap

func (store *MongoDBObjectStore) UpdateWithMap(collection string, id string, updateDocument map[string]interface{}) (bool, error)

UpdateWithMap updates a document in the given collection, returns true if updated successfully

type ObjectStore

type ObjectStore interface {
	Release() error
	List(collection string, results interface{}) error
	ListConditional(collection string, conditions map[string]interface{}, results interface{}) error
	ListConditionalWithSort(collection string, conditions map[string]interface{}, sort []KeyValue, results interface{}) error
	ListForUser(collection string, owner string, results interface{}) error
	Get(collection string, id string, result interface{}) error
	GetConditional(collection string, conditions map[string]interface{}, result interface{}) error
	GetConditionalWithSort(collection string, conditions map[string]interface{}, sort []KeyValue, result interface{}) error
	Insert(collection string, document interface{}) error
	InsertMany(collection string, documents []interface{}) error
	Update(collection string, id string, updateDocument interface{}, updateFieldNames []string) (bool, error)
	UpdateConditional(collection string, conditions map[string]interface{}, updateDocument interface{}, updateFieldNames []string) (int64, error)
	UpdateWithMap(collection string, id string, updateDocument map[string]interface{}) (bool, error)
	UpdateConditionalWithMap(collection string, conditions map[string]interface{}, updateDocument map[string]interface{}) (int64, error)
	Replace(collection string, id string, replaceDocument interface{}) (bool, error)
	Delete(collection string, id string) (bool, error)
	DeleteConditional(collection string, conditions map[string]interface{}) (int64, error)
}

ObjectStore is an interface for object-level database I/O

type SortDirection

type SortDirection int

SortDirection is the direction to sort the result

const (
	// AscendingSort sorts result in ascending order, this is a constants used by MongoDB for sorting order/index order
	AscendingSort SortDirection = 1 // default
	// DescendingSort sorts result in descending order, this is a constants used by MongoDB for sorting order/index order
	DescendingSort SortDirection = -1
)

https://www.mongodb.com/docs/manual/reference/method/cursor.sort/#ascending-descending-sort

Jump to

Keyboard shortcuts

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