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
- func CreateCollectionWithSchema(ctx context.Context, conn *MongoDBConnection, collectionName string, ...) error
- func GetBSONRegistry() (*bsoncodec.Registry, error)
- func IsDocumentValidationFailure(err error) bool
- func IsDuplicateError(err error) bool
- func IsNoDocumentError(err error) bool
- type KeyValue
- type MockObjectStore
- func (store *MockObjectStore) Delete(collection string, id string) (bool, error)
- func (store *MockObjectStore) DeleteConditional(collection string, conditions map[string]interface{}) (int64, error)
- func (store *MockObjectStore) Get(collection string, id string, result interface{}) error
- func (store *MockObjectStore) GetConditional(collection string, conditions map[string]interface{}, result interface{}) error
- func (store *MockObjectStore) GetConditionalWithSort(collection string, conditions map[string]interface{}, sort []KeyValue, ...) error
- func (store *MockObjectStore) Insert(collection string, document interface{}) error
- func (store *MockObjectStore) InsertMany(collection string, documents []interface{}) error
- func (store *MockObjectStore) List(collection string, results interface{}) error
- func (store *MockObjectStore) ListConditional(collection string, conditions map[string]interface{}, results interface{}) error
- func (store *MockObjectStore) ListConditionalWithSort(collection string, conditions map[string]interface{}, sort []KeyValue, ...) error
- func (store *MockObjectStore) ListForUser(collection string, owner string, results interface{}) error
- func (store *MockObjectStore) Release() error
- func (store *MockObjectStore) Replace(collection string, id string, replaceDocument interface{}) (bool, error)
- func (store *MockObjectStore) Update(collection string, id string, updateDocument interface{}, ...) (bool, error)
- func (store *MockObjectStore) UpdateConditional(collection string, conditions map[string]interface{}, ...) (int64, error)
- func (store *MockObjectStore) UpdateConditionalWithMap(collection string, conditions map[string]interface{}, ...) (int64, error)
- func (store *MockObjectStore) UpdateWithMap(collection string, id string, updateDocument map[string]interface{}) (bool, error)
- type MongoDBConfig
- type MongoDBConnection
- func (conn *MongoDBConnection) Delete(collection string, filter map[string]interface{}, ...) (bool, error)
- func (conn *MongoDBConnection) DeleteMany(collection string, filter map[string]interface{}, ...) (int64, error)
- func (conn *MongoDBConnection) Disconnect() error
- func (conn *MongoDBConnection) Get(collection string, filter map[string]interface{}, result interface{}, ...) error
- func (conn *MongoDBConnection) Insert(collection string, document interface{}, opts ...*options.InsertOneOptions) error
- func (conn *MongoDBConnection) InsertMany(collection string, documents []interface{}, opts ...*options.InsertManyOptions) error
- func (conn *MongoDBConnection) List(collection string, filter map[string]interface{}, results interface{}, ...) error
- func (conn *MongoDBConnection) ListAndGetCursor(collection string, filter map[string]interface{}, opts ...*options.FindOptions) (*mongo.Cursor, error)
- func (conn *MongoDBConnection) Replace(collection string, filter map[string]interface{}, replaceDocument interface{}, ...) (bool, error)
- func (conn *MongoDBConnection) Update(collection string, filter map[string]interface{}, ...) (bool, error)
- func (conn *MongoDBConnection) UpdateMany(collection string, filter map[string]interface{}, ...) (int64, error)
- type MongoDBObjectStore
- func (store *MongoDBObjectStore) Delete(collection string, id string) (bool, error)
- func (store *MongoDBObjectStore) DeleteConditional(collection string, conditions map[string]interface{}) (int64, error)
- func (store *MongoDBObjectStore) Get(collection string, id string, result interface{}) error
- func (store *MongoDBObjectStore) GetConditional(collection string, conditions map[string]interface{}, result interface{}) error
- func (store *MongoDBObjectStore) GetConditionalWithSort(collection string, conditions map[string]interface{}, sort []KeyValue, ...) error
- func (store *MongoDBObjectStore) Insert(collection string, document interface{}) error
- func (store *MongoDBObjectStore) InsertMany(collection string, documents []interface{}) error
- func (store *MongoDBObjectStore) List(collection string, results interface{}) error
- func (store *MongoDBObjectStore) ListConditional(collection string, conditions map[string]interface{}, results interface{}) error
- func (store *MongoDBObjectStore) ListConditionalWithSort(collection string, conditions map[string]interface{}, sort []KeyValue, ...) error
- func (store *MongoDBObjectStore) ListForUser(collection string, owner string, results interface{}) error
- func (store *MongoDBObjectStore) Release() error
- func (store *MongoDBObjectStore) Replace(collection string, id string, replaceDocument interface{}) (bool, error)
- func (store *MongoDBObjectStore) Update(collection string, id string, updateDocument interface{}, ...) (bool, error)
- func (store *MongoDBObjectStore) UpdateConditional(collection string, conditions map[string]interface{}, ...) (int64, error)
- func (store *MongoDBObjectStore) UpdateConditionalWithMap(collection string, conditions map[string]interface{}, ...) (int64, error)
- func (store *MongoDBObjectStore) UpdateWithMap(collection string, id string, updateDocument map[string]interface{}) (bool, error)
- type ObjectStore
- type SortDirection
Constants ¶
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 ¶
GetBSONRegistry returns bson registry with JSONFallbackStructTagParser
func IsDocumentValidationFailure ¶
IsDocumentValidationFailure checks if an error is due to document validation (document does not match the validation schema).
func IsDuplicateError ¶
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 ¶
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 ¶
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) ConnectionTimeout uint `envconfig:"MONGODB_CONN_TIMEOUT" default:"10"` }
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 NewMongoDBConnectionWithCtx ¶
func NewMongoDBConnectionWithCtx(ctx context.Context, config *MongoDBConfig) (*MongoDBConnection, error)
NewMongoDBConnectionWithCtx 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