Documentation ¶
Index ¶
- type IMongoDbPersistenceOverrides
- type IdentifiableMongoDbPersistence
- func (c *IdentifiableMongoDbPersistence) Configure(config *cconf.ConfigParams)
- func (c *IdentifiableMongoDbPersistence) Create(correlationId string, item interface{}) (result interface{}, err error)
- func (c *IdentifiableMongoDbPersistence) DeleteById(correlationId string, id interface{}) (item interface{}, err error)
- func (c *IdentifiableMongoDbPersistence) DeleteByIds(correlationId string, ids []interface{}) error
- func (c *IdentifiableMongoDbPersistence) GetListByIds(correlationId string, ids []interface{}) (items []interface{}, err error)
- func (c *IdentifiableMongoDbPersistence) GetOneById(correlationId string, id interface{}) (item interface{}, err error)
- func (c *IdentifiableMongoDbPersistence) Set(correlationId string, item interface{}) (result interface{}, err error)
- func (c *IdentifiableMongoDbPersistence) Update(correlationId string, item interface{}) (result interface{}, err error)
- func (c *IdentifiableMongoDbPersistence) UpdatePartially(correlationId string, id interface{}, data *cdata.AnyValueMap) (item interface{}, err error)
- type MongoDbPersistence
- func (c *MongoDbPersistence) Clear(correlationId string) error
- func (c *MongoDbPersistence) Close(correlationId string) error
- func (c *MongoDbPersistence) Configure(config *cconf.ConfigParams)
- func (c *MongoDbPersistence) ConvertFromPublic(item interface{}) interface{}
- func (c *MongoDbPersistence) ConvertFromPublicPartial(item interface{}) interface{}
- func (c *MongoDbPersistence) ConvertToPublic(value interface{}) interface{}
- func (c *MongoDbPersistence) Create(correlationId string, item interface{}) (result interface{}, err error)
- func (c *MongoDbPersistence) DefineSchema()
- func (c *MongoDbPersistence) DeleteByFilter(correlationId string, filter interface{}) error
- func (c *MongoDbPersistence) EnsureIndex(keys interface{}, options *mongoopt.IndexOptions)
- func (c *MongoDbPersistence) GetCountByFilter(correlationId string, filter interface{}) (count int64, err error)
- func (c *MongoDbPersistence) GetListByFilter(correlationId string, filter interface{}, sort interface{}, sel interface{}) (items []interface{}, err error)
- func (c *MongoDbPersistence) GetOneRandom(correlationId string, filter interface{}) (item interface{}, err error)
- func (c *MongoDbPersistence) GetPageByFilter(correlationId string, filter interface{}, paging *cdata.PagingParams, ...) (page *cdata.DataPage, err error)
- func (c *MongoDbPersistence) IsOpen() bool
- func (c *MongoDbPersistence) NewObjectByPrototype() reflect.Value
- func (c *MongoDbPersistence) Open(correlationId string) error
- func (c *MongoDbPersistence) SetReferences(references crefer.IReferences)
- func (c *MongoDbPersistence) UnsetReferences()
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type IMongoDbPersistenceOverrides ¶ added in v1.1.0
type IMongoDbPersistenceOverrides interface { DefineSchema() ConvertFromPublic(item interface{}) interface{} ConvertFromPublicPartial(item interface{}) interface{} ConvertToPublic(item interface{}) interface{} }
type IdentifiableMongoDbPersistence ¶
type IdentifiableMongoDbPersistence struct {
MongoDbPersistence
}
IdentifiableMongoDbPersistence is abstract persistence component that stores data in MongoDB and implements a number of CRUD operations over data items with unique ids. The data items must implement IIdentifiable interface.
In basic scenarios child classes shall only override GetPageByFilter, GetListByFilter or DeleteByFilter operations with specific filter function. All other operations can be used out of the box.
In complex scenarios child classes can implement additional operations by accessing c.Collection properties.
Configuration parameters:
- collection: (optional) MongoDB collection name
- connection(s):
- discovery_key: (optional) a key to retrieve the connection from IDiscovery
- host: host name or IP address
- port: port number (default: 27017)
- uri: resource URI or connection string with all parameters in it
- credential(s):
- store_key: (optional) a key to retrieve the credentials from ICredentialStore
- username: (optional) user name
- password: (optional) user password
- options:
- max_pool_size: (optional) maximum connection pool size (default: 2)
- keep_alive: (optional) enable connection keep alive (default: true)
- connect_timeout: (optional) connection timeout in milliseconds (default: 5000)
- socket_timeout: (optional) socket timeout in milliseconds (default: 360000)
- auto_reconnect: (optional) enable auto reconnection (default: true) (not used)
- reconnect_interval: (optional) reconnection interval in milliseconds (default: 1000) (not used)
- max_page_size: (optional) maximum page size (default: 100)
- replica_set: (optional) name of replica set
- ssl: (optional) enable SSL connection (default: false) (not implements in this release)
- auth_source: (optional) authentication source
- auth_user: (optional) authentication user name
- auth_password: (optional) authentication user password
- debug: (optional) enable debug output (default: false). (not used)
References:
- *:logger:*:*:1.0 (optional) ILogger components to pass log messages components to pass log messages - *:discovery:*:*:1.0 (optional) IDiscovery services - *:credential-store:*:*:1.0 (optional) Credential stores to resolve credentials
Example:
type MyMongoDbPersistence struct { IdentifiableMongoDbPersistence } func NewMyMongoDbPersistence() { proto := reflect.TypeOf(MyData{}) return &DummyMongoDbPersistence{*persist.NewIdentifiableMongoDbPersistence(proto, "mydata")} } composeFilter(filter cdata.FilterParams) interface{} { if filter == nil { filter = *cdata.NewEmptyFilterParams() } name := filter.GetAsNullableString("name") var filterObj bson.M if *name != "" { filterObj = bson.M{"name": *name} else { filterObj = bson.M{} } return filterObj } func (c *MyMongoDbPersistence) GetPageByFilter(correlationId string, filter cdata.FilterParams, paging cdata.PagingParams) (page MyDataPage, err error){ tempPage, err := c.IdentifiableMongoDbPersistence.GetPageByFilter(correlationId, composeFilter(filter), paging, nil, nil) // Convert to MyDataPage dataLen := int64(len(tempPage.Data)) // For full release tempPage and delete this by GC data := make([]MyData, dataLen) for i, v := range tempPage.Data { data[i] = v.(MyData) } page = *NewMyDataPage(&dataLen, data) return page, err } persistence = NewMyMongoDbPersistence() persistence.Configure(NewConfigParamsFromTuples( "host", "localhost", "port", "27017" "database", "test", )) opnErr := persitence.Open("123") if opnErr != nil { ... } crtRes, crtErr := persistence.Create("123", MyData{ id: "1", name: "ABC" }) if crtErr != nil { ... } getRes, getErr := persistence.GetPageByFilter("123", NewFilterParamsFromTuples("name", "ABC"), nil) if getErr != nil { ... } fmt.Println(getRes.Data); // Result: { id: "1", name: "ABC" } persistence.deleteById("123", "1") ...
func InheritIdentifiableMongoDbPersistence ¶ added in v1.1.0
func InheritIdentifiableMongoDbPersistence(overrides IMongoDbPersistenceOverrides, proto reflect.Type, collection string) *IdentifiableMongoDbPersistence
NewIdentifiableMongoDbPersistence is creates a new instance of the persistence component. Parameters:
- proto reflect.Type type of saved data, need for correct decode from DB
- collection string (optional) a collection name.
Return *IdentifiableMongoDbPersistence new created IdentifiableMongoDbPersistence component
func (*IdentifiableMongoDbPersistence) Configure ¶
func (c *IdentifiableMongoDbPersistence) Configure(config *cconf.ConfigParams)
Configure is configures component by passing configuration parameters. Parameters:
- config *cconf.ConfigParams configuration parameters to be set.
func (*IdentifiableMongoDbPersistence) Create ¶
func (c *IdentifiableMongoDbPersistence) Create(correlationId string, item interface{}) (result interface{}, err error)
Create was creates a data item. Parameters:
- correlation_id string (optional) transaction id to Trace execution through call chain.
- item interface{}
an item to be created. Returns result interface{}, err error created item and error, if they are occured
func (*IdentifiableMongoDbPersistence) DeleteById ¶
func (c *IdentifiableMongoDbPersistence) DeleteById(correlationId string, id interface{}) (item interface{}, err error)
DeleteById is deleted a data item by it"s unique id. Parameters:
- correlation_id string (optional) transaction id to Trace execution through call chain.
- id interface{} an id of the item to be deleted
Returns item interface{}, err error deleted item and error, if they are occured
func (*IdentifiableMongoDbPersistence) DeleteByIds ¶
func (c *IdentifiableMongoDbPersistence) DeleteByIds(correlationId string, ids []interface{}) error
DeleteByIds is deletes multiple data items by their unique ids.
- correlationId string (optional) transaction id to Trace execution through call chain.
- ids []interface{} ids of data items to be deleted.
Retrun error error or nil for success.
func (*IdentifiableMongoDbPersistence) GetListByIds ¶
func (c *IdentifiableMongoDbPersistence) GetListByIds(correlationId string, ids []interface{}) (items []interface{}, err error)
GetListByIds is gets a list of data items retrieved by given unique ids. Parameters:
- correlationId string (optional) transaction id to Trace execution through call chain.
- ids []interface{} ids of data items to be retrieved
Returns items []interface{}, err error a data list and error, if theq are occured.
func (*IdentifiableMongoDbPersistence) GetOneById ¶
func (c *IdentifiableMongoDbPersistence) GetOneById(correlationId string, id interface{}) (item interface{}, err error)
GetOneById is gets a data item by its unique id. Parameters:
- correlationId (optional) transaction id to Trace execution through call chain.
- id an id of data item to be retrieved.
- callback callback function that receives data item or error.
func (*IdentifiableMongoDbPersistence) Set ¶
func (c *IdentifiableMongoDbPersistence) Set(correlationId string, item interface{}) (result interface{}, err error)
Set is sets a data item. If the data item exists it updates it, otherwise it create a new data item. Parameters:
- correlation_id string (optional) transaction id to Trace execution through call chain.
- item interface{} a item to be set.
Returns result interface{}, err error updated item and error, if they occured
func (*IdentifiableMongoDbPersistence) Update ¶
func (c *IdentifiableMongoDbPersistence) Update(correlationId string, item interface{}) (result interface{}, err error)
Update is updates a data item. Parameters:
- correlation_id string (optional) transaction id to Trace execution through call chain.
- item interface{} an item to be updated.
Returns result interface{}, err error updated item and error, if theq are occured
func (*IdentifiableMongoDbPersistence) UpdatePartially ¶
func (c *IdentifiableMongoDbPersistence) UpdatePartially(correlationId string, id interface{}, data *cdata.AnyValueMap) (item interface{}, err error)
UpdatePartially is updates only few selected fields in a data item. Parameters:
- correlation_id string (optional) transaction id to Trace execution through call chain.
- id interface{} an id of data item to be updated.
- data cdata.AnyValueMap a map with fields to be updated.
Returns item interface{}, err error updated item and error, if they are occured
type MongoDbPersistence ¶
type MongoDbPersistence struct { Overrides IMongoDbPersistenceOverrides Prototype reflect.Type // The dependency resolver. DependencyResolver crefer.DependencyResolver // The logger. Logger clog.CompositeLogger // The MongoDB connection component. Connection *conn.MongoDbConnection // The MongoDB connection object. Client *mongodrv.Client // The MongoDB database name. DatabaseName string // The MongoDB colleciton object. CollectionName string // The MongoDb database object. Db *mongodrv.Database // The MongoDb collection object. Collection *mongodrv.Collection // contains filtered or unexported fields }
MongoDbPersistence abstract persistence component that stores data in MongoDB using plain driver. This is the most basic persistence component that is only able to store data items of any type. Specific CRUD operations over the data items must be implemented in child classes by accessing c.Db or c.Collection properties.
Configuration parameters:
collection: (optional) MongoDB collection name
connection(s):
discovery_key: (optional) a key to retrieve the connection from IDiscovery
host: host name or IP address
port: port number (default: 27017)
database: database name
uri: resource URI or connection string with all parameters in it
credential(s):
store_key: (optional) a key to retrieve the credentials from ICredentialStore
username: (optional) user name
password: (optional) user password
options:
max_pool_size: (optional) maximum connection pool size (default: 2)
keep_alive: (optional) enable connection keep alive (default: true)
connect_timeout: (optional) connection timeout in milliseconds (default: 5000)
socket_timeout: (optional) socket timeout in milliseconds (default: 360000)
auto_reconnect: (optional) enable auto reconnection (default: true) (not used)
reconnect_interval: (optional) reconnection interval in milliseconds (default: 1000) (not used)
max_page_size: (optional) maximum page size (default: 100)
replica_set: (optional) name of replica set
ssl: (optional) enable SSL connection (default: false) (not implements in this release)
auth_source: (optional) authentication source
debug: (optional) enable debug output (default: false). (not used)
References:
*:logger:*:*:1.0 (optional) ILogger components to pass log messages
*:discovery:*:*:1.0 (optional) IDiscovery services
*:credential-store:*:*:1.0 (optional) Credential stores to resolve credentials
Example:
type MyMongoDbPersistence struct { MongoDbPersistence } func NewMyMongoDbPersistence(proto reflect.Type, collection string) *MyMongoDbPersistence { mc:= MyMongoDbPersistence{} mc.MongoDbPersistence = NewMongoDbPersistence(proto, collection) return &mc } func (c * MyMongoDbPersistence) GetByName(correlationId string, name string) (item interface{}, err error) { filter := bson.M{"name": name} docPointer := NewObjectByPrototype(c.Prototype) foRes := c.Collection.FindOne(context.TODO(), filter) ferr := foRes.Decode(docPointer.Interface()) if ferr != nil { if ferr == mongo.ErrNoDocuments { return nil, nil } return nil, ferr } item = docPointer.Elem().Interface() c.Overrides.ConvertToPublic(&item) return item, nil } func (c * MyMongoDbPersistence) Set(correlatonId string, item MyData) (result interface{}, err error) { newItem = cmpersist.CloneObject(item, c.Prototype) // Assign unique id if not exist cmpersist.GenerateObjectId(&newItem) id := cmpersist.GetObjectId(newItem) c.Overrides.ConvertFromPublic(&newItem) filter := bson.M{"_id": id} var options mngoptions.FindOneAndReplaceOptions retDoc := mngoptions.After options.ReturnDocument = &retDoc upsert := true options.Upsert = &upsert frRes := c.Collection.FindOneAndReplace(context.TODO(), filter, newItem, &options) if frRes.Err() != nil { return nil, frRes.Err() } docPointer := NewObjectByPrototype(c.Prototype) err = frRes.Decode(docPointer.Interface()) if err != nil { if err == mongo.ErrNoDocuments { return nil, nil } return nil, err } item = docPointer.Elem().Interface() c.Overrides.ConvertToPublic(&item) return item, nil } persistence := NewMyMongoDbPersistence(reflect.TypeOf(MyData{}), "mycollection") persistence.Configure(NewConfigParamsFromTuples( "host", "localhost", "port", "27017", "database", "test", )) opnErr := persitence.Open("123") if opnErr != nil { ... } resItem, setErr := persistence.Set("123", MyData{ name: "ABC" }) if setErr != nil { ... } item, getErr := persistence.GetByName("123", "ABC") if getErr != nil { ... } fmt.Println(item) // Result: { name: "ABC" } ("123", "ABC") if getErr != nil { ... } fmt.Println(item) // Result: { name: "ABC" } ("123", "ABC") if getErr != nil { ... } fmt.Println(item) // Result: { name: "ABC" }
func InheritMongoDbPersistence ¶ added in v1.1.0
func InheritMongoDbPersistence(overrides IMongoDbPersistenceOverrides, proto reflect.Type, collection string) *MongoDbPersistence
InheritMongoDbPersistence are creates a new instance of the persistence component. Parameters:
- proto reflect.Type type of saved data, need for correct decode from DB
- collection string a collection name.
Return *MongoDbPersistence new created MongoDbPersistence component
func (*MongoDbPersistence) Clear ¶
func (c *MongoDbPersistence) Clear(correlationId string) error
Clear method are clears component state. Parameters:
- correlationId string (optional) transaction id to trace execution through call chain.
Returns error error or nil when no errors occured.
func (*MongoDbPersistence) Close ¶
func (c *MongoDbPersistence) Close(correlationId string) error
Close methos closes component and frees used resources. Parameters:
- correlationId string (optional) transaction id to trace execution through call chain.
Return error error or nil when no errors occured.
func (*MongoDbPersistence) Configure ¶
func (c *MongoDbPersistence) Configure(config *cconf.ConfigParams)
Configure method is configures component by passing configuration parameters. Parameters:
- config *cconf.ConfigParams configuration parameters to be set.
func (*MongoDbPersistence) ConvertFromPublic ¶
func (c *MongoDbPersistence) ConvertFromPublic(item interface{}) interface{}
ConvertFromPublic method help convert object (map) from public view by replaced "Id" to "_id" field Parameters:
- item *interface{} converted item
func (*MongoDbPersistence) ConvertFromPublicPartial ¶ added in v1.0.5
func (c *MongoDbPersistence) ConvertFromPublicPartial(item interface{}) interface{}
ConvertFromPublicPartial method help convert object (map) from public view by replaced "Id" to "_id" field Parameters:
- item *interface{} converted item
func (*MongoDbPersistence) ConvertToPublic ¶
func (c *MongoDbPersistence) ConvertToPublic(value interface{}) interface{}
ConvertToPublic method is convert object (map) to public view by replaced "_id" to "Id" field Parameters:
- item *interface{} converted item
func (*MongoDbPersistence) Create ¶ added in v1.0.2
func (c *MongoDbPersistence) Create(correlationId string, item interface{}) (result interface{}, err error)
Create was creates a data item. Parameters:
- correlation_id string (optional) transaction id to Trace execution through call chain.
- item interface{} an item to be created.
Returns result interface{}, err error created item and error, if they are occured
func (*MongoDbPersistence) DefineSchema ¶ added in v1.1.0
func (c *MongoDbPersistence) DefineSchema()
Defines schema for the collection. This method shall be overloaded in child classes
func (*MongoDbPersistence) DeleteByFilter ¶ added in v1.0.2
func (c *MongoDbPersistence) DeleteByFilter(correlationId string, filter interface{}) error
DeleteByFilter is deletes data items that match to a given filter. This method shall be called by a func (c *IdentifiableMongoDbPersistence) deleteByFilter method from child class that receives FilterParams and converts them into a filter function. Parameters:
- correlationId string (optional) transaction id to Trace execution through call chain.
- filter interface{} (optional) a filter BSON object.
Return error error or nil for success.
func (*MongoDbPersistence) EnsureIndex ¶
func (c *MongoDbPersistence) EnsureIndex(keys interface{}, options *mongoopt.IndexOptions)
EnsureIndex method are adds index definition to create it on opening Parameters:
- keys interface{} index keys (fields)
- options *mongoopt.IndexOptions index options
func (*MongoDbPersistence) GetCountByFilter ¶ added in v1.0.4
func (c *MongoDbPersistence) GetCountByFilter(correlationId string, filter interface{}) (count int64, err error)
GetCountByFilter is gets a count of data items retrieved by a given filter. This method shall be called by a func (c *IdentifiableMongoDbPersistence) GetCountByFilter method from child type that receives FilterParams and converts them into a filter function. Parameters:
- correlationId string (optional) transaction id to Trace execution through call chain.
- filter interface{}
Returns count int, err error a data count or error, if they are occured
func (*MongoDbPersistence) GetListByFilter ¶ added in v1.0.2
func (c *MongoDbPersistence) GetListByFilter(correlationId string, filter interface{}, sort interface{}, sel interface{}) (items []interface{}, err error)
GetListByFilter is gets a list of data items retrieved by a given filter and sorted according to sort parameters. This method shall be called by a func (c *IdentifiableMongoDbPersistence) GetListByFilter method from child type that receives FilterParams and converts them into a filter function. Parameters:
- correlationId string (optional) transaction id to Trace execution through call chain.
- filter interface{} (optional) a filter BSON object
- sort interface{} (optional) sorting BSON object
- select interface{} (optional) projection BSON object
Returns items []interface{}, err error data list and error, if they are ocurred
func (*MongoDbPersistence) GetOneRandom ¶ added in v1.0.2
func (c *MongoDbPersistence) GetOneRandom(correlationId string, filter interface{}) (item interface{}, err error)
GetOneRandom is gets a random item from items that match to a given filter. This method shall be called by a func (c *IdentifiableMongoDbPersistence) getOneRandom method from child class that receives FilterParams and converts them into a filter function. Parameters:
- correlationId string (optional) transaction id to Trace execution through call chain.
- filter interface{} (optional) a filter BSON object
Returns: item interface{}, err error random item and error, if theq are occured
func (*MongoDbPersistence) GetPageByFilter ¶ added in v1.0.2
func (c *MongoDbPersistence) GetPageByFilter(correlationId string, filter interface{}, paging *cdata.PagingParams, sort interface{}, sel interface{}) (page *cdata.DataPage, err error)
GetPageByFilter is gets a page of data items retrieved by a given filter and sorted according to sort parameters. This method shall be called by a func (c *IdentifiableMongoDbPersistence) GetPageByFilter method from child type that receives FilterParams and converts them into a filter function. Parameters:
- correlationId string (optional) transaction id to Trace execution through call chain.
- filter interface{} (optional) a filter JSON object
- paging *cdata.PagingParams (optional) paging parameters
- sort interface{} (optional) sorting BSON object
- select interface{} (optional) projection BSON object
Returns page cdata.DataPage, err error a data page or error, if they are occured
func (*MongoDbPersistence) IsOpen ¶
func (c *MongoDbPersistence) IsOpen() bool
IsOpen method is checks if the component is opened. Returns true if the component has been opened and false otherwise.
func (*MongoDbPersistence) NewObjectByPrototype ¶ added in v1.0.4
func (c *MongoDbPersistence) NewObjectByPrototype() reflect.Value
service function for return pointer on new prototype object for unmarshaling
func (*MongoDbPersistence) Open ¶
func (c *MongoDbPersistence) Open(correlationId string) error
Open method is opens the component. Parameters:
- correlationId string (optional) transaction id to trace execution through call chain.
Return error error or nil when no errors occured.
func (*MongoDbPersistence) SetReferences ¶
func (c *MongoDbPersistence) SetReferences(references crefer.IReferences)
SetReferences method are sets references to dependent components. Parameters:
- references crefer.IReferences references to locate the component dependencies.
func (*MongoDbPersistence) UnsetReferences ¶
func (c *MongoDbPersistence) UnsetReferences()
UnsetReferences method is unsets (clears) previously set references to dependent components.