pip_services3_mongodb.persistence package
Submodules
Module contents
pip_services3_mongodb.persistence.__init__
MongoDB module initialization
- copyright
Conceptual Vision Consulting LLC 2018-2019, see AUTHORS for more details.
- license
MIT, see LICENSE for more details.
- class pip_services3_mongodb.persistence.IdentifiableMongoDbPersistence(collection: Optional[str] = None)
Bases:
pip_services3_mongodb.persistence.MongoDbPersistence.MongoDbPersistence
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
get_page_by_filter
,get_list_by_filter()
ordelete_by_filter()
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 self.__collection and self.__model properties.
### Configuration parameters ###
- 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)
reconnect_interval: (optional) reconnection interval in milliseconds (default: 1000)
max_page_size: (optional) maximum page size (default: 100)
replica_set: (optional) name of replica set
ssl: (optional) enable SSL connection (default: false)
auth_source: (optional) authentication source
debug: (optional) enable debug output (default: false).
- ### 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)
ICredentialStore
stores to resolve credentials
Example:
class MyMongoDbPersistence(MongoDbPersistence): def __init__(self): super(MyMongoDbPersistence, self).__init__("mydata", MyData) def get_page_by_filter(self, correlation_id, filter, paging, sort = None, select = None): super().def get_page_by_filter(correlation_id, filter, paging, None, None): persistence = MyMongoDbPersistence() persistence.configure(ConfigParams.from_tuples("host", "localhost", "port", 27017)) persitence.open("123") persistence.create("123", { id: "1", name: "ABC" }) mydata = persistence.get_page_by_filter("123", FilterParams.from_tuples("name", "ABC"), None, None) print mydata persistence.delete_by_id("123", "1") # ...
- create(correlation_id: Optional[str], item: pip_services3_mongodb.persistence.IdentifiableMongoDbPersistence.T) pip_services3_mongodb.persistence.IdentifiableMongoDbPersistence.T
Creates a data item.
- Parameters
correlation_id – (optional) transaction id to trace execution through call chain.
item – an item to be created.
- Returns
a created item
- delete_by_id(correlation_id: Optional[str], id: Any) pip_services3_mongodb.persistence.IdentifiableMongoDbPersistence.T
Deleted a data item by it’s unique id.
- Parameters
correlation_id – (optional) transaction id to trace execution through call chain.
id – an id of the item to be deleted
- Returns
a deleted item.
- delete_by_ids(correlation_id: Optional[str], ids: List[Any])
Deletes multiple data items by their unique ids.
- Parameters
correlation_id – (optional) transaction id to trace execution through call chain.
ids – ids of data items to be deleted.
- get_list_by_ids(correlation_id: Optional[str], ids: List[Any]) List[pip_services3_mongodb.persistence.IdentifiableMongoDbPersistence.T]
Gets a list of data items retrieved by given unique ids.
- Parameters
correlation_id – (optional) transaction id to trace execution through call chain.
ids – ids of data items to be retrieved
- Returns
a data list of results by ids.
- get_one_by_id(correlation_id: Optional[str], id: Any) pip_services3_mongodb.persistence.IdentifiableMongoDbPersistence.T
Gets a data item by its unique id.
- Parameters
correlation_id – (optional) transaction id to trace execution through call chain.
id – an id of data item to be retrieved.
- Returns
data item by id.
- set(correlation_id: Optional[str], item: pip_services3_mongodb.persistence.IdentifiableMongoDbPersistence.T) pip_services3_mongodb.persistence.IdentifiableMongoDbPersistence.T
Sets a data item. If the data item exists it updates it, otherwise it create a new data item.
- Parameters
correlation_id – (optional) transaction id to trace execution through call chain.
item – an item to be set.
- Returns
an updated item
- update(correlation_id: Optional[str], item: pip_services3_mongodb.persistence.IdentifiableMongoDbPersistence.T) Optional[pip_services3_mongodb.persistence.IdentifiableMongoDbPersistence.T]
Updates a data item.
- Parameters
correlation_id – (optional) transaction id to trace execution through call chain.
item – an item to be updated.
- Returns
an updated item.
- update_partially(correlation_id: Optional[str], id: Any, data: pip_services3_commons.data.AnyValueMap.AnyValueMap) pip_services3_mongodb.persistence.IdentifiableMongoDbPersistence.T
Updates only few selected fields in a data item.
- Parameters
correlation_id – (optional) transaction id to trace execution through call chain.
id – an id of data item to be updated.
data – a map with fields to be updated.
- Returns
an updated item.
- class pip_services3_mongodb.persistence.MongoDbIndex(keys: Any, options: Optional[Any] = None)
Bases:
object
Index definition for mongodb
- class pip_services3_mongodb.persistence.MongoDbPersistence(collection: Optional[str] = None)
Bases:
pip_services3_commons.refer.IReferenceable.IReferenceable
,pip_services3_commons.refer.IUnreferenceable.IUnreferenceable
,pip_services3_commons.config.IConfigurable.IConfigurable
,pip_services3_commons.run.IOpenable.IOpenable
,pip_services3_commons.run.ICleanable.ICleanable
Abstract persistence component that stores data in MongoDB using the official MongoDB 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 self.__collection or self.__model 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)
reconnect_interval: (optional) reconnection interval in milliseconds (default: 1000)
max_page_size: (optional) maximum page size (default: 100)
replica_set: (optional) name of replica set
ssl: (optional) enable SSL connection (default: false)
auth_source: (optional) authentication source
debug: (optional) enable debug output (default: false).
- ### References ###
*:logger:*:*:1.0 (optional)
ILogger
components to pass log messages*:discovery:*:*:1.0 (optional)
IDiscovery
services*:credential-store:*:*:1.0 (optional)
ICredentialStore
stores to resolve credentials
Example:
class MyMongoDbPersistence(MongoDbPersistence): def __init__(self): super(MyMongoDbPersistence, self).__init__("mydata", MyData) def get_by_name(self, correlationId, name): item = self._collection.find_one({ 'name': name }) return item def set(self, correlationId, item): item = self._collection.find_one_and_update( { '_id': item.id }, { '$set': item }, return_document = pymongo.ReturnDocument.AFTER, upsert = True ) persistence = MyMongoDbPersistence() persistence.configure(ConfigParams.from_tuples("host", "localhost", "port", 27017)) persitence.open("123") persistence.set("123", { name: "ABC" }) item = persistence.get_by_name("123", "ABC") print (item)
- clear(correlation_id: Optional[str])
Clears component state.
- Parameters
correlation_id – (optional) transaction id to trace execution through call chain.
- close(correlation_id: Optional[str])
Closes component and frees used resources.
- Parameters
correlation_id – (optional) transaction id to trace execution through call chain.
- configure(config: pip_services3_commons.config.ConfigParams.ConfigParams)
Configures component by passing configuration parameters.
- Parameters
config – configuration parameters to be set.
- create(correlation_id: Optional[str], item: pip_services3_mongodb.persistence.MongoDbPersistence.T) pip_services3_mongodb.persistence.MongoDbPersistence.T
Creates a data item.
- Parameters
correlation_id – (optional) transaction id to trace execution through call chain.
item – an item to be created.
- Returns
a created item
- delete_by_filter(correlation_id: Optional[str], filter: Any)
Deletes data items that match to a given filter.
This method shall be called by a public
delete_by_filter()
method from child class that receivesFilterParams
and converts them into a filter function.- Parameters
correlation_id – (optional) transaction id to trace execution through call chain.
filter – (optional) a filter function to filter items.
- get_count_by_filter(correlation_id: Optional[str], filter: Any) int
Gets a number of data items retrieved by a given filter.
This method shall be called by a public get_count_by_filter method from child class that receives FilterParams and converts them into a filter function.
- Parameters
correlation_id – (optional) transaction id to trace execution through call chain.
filter – (optional) a filter JSON object
- Returns
a number of filtered items.
- get_list_by_filter(correlation_id: Optional[str], filter: Any, sort: Optional[Any] = None, select: Optional[Any] = None) List[pip_services3_mongodb.persistence.MongoDbPersistence.T]
Gets a list of data items retrieved by a given filter and sorted according to sort parameters.
This method shall be called by a public get_list_by_filter method from child class that receives FilterParams and converts them into a filter function.
- Parameters
correlation_id – (optional) transaction id to trace execution through call chain.
filter – (optional) a filter function to filter items
sort – (optional) sorting parameters
select – (optional) projection parameters (not used yet)
- Returns
a data list of results by filter.
- get_one_random(correlation_id: Optional[str], filter: Any) Optional[pip_services3_mongodb.persistence.MongoDbPersistence.T]
Gets a random item from items that match to a given filter.
This method shall be called by a public get_one_random method from child class that receives FilterParams and converts them into a filter function.
- Parameters
correlation_id – (optional) transaction id to trace execution through call chain.
- Returns
a random item.
- get_page_by_filter(correlation_id: Optional[str], filter: Any, paging: pip_services3_commons.data.PagingParams.PagingParams, sort: Optional[Any] = None, select: Optional[Any] = None) pip_services3_commons.data.DataPage.DataPage
Gets a page of data items retrieved by a given filter and sorted according to sort parameters.
This method shall be called by a public get_page_by_filter method from child class that receives FilterParams and converts them into a filter function.
- Parameters
correlation_id – (optional) transaction id to trace execution through call chain.
filter – (optional) a filter JSON object
paging – (optional) paging parameters
sort – (optional) sorting JSON object
select – (optional) projection JSON object
- Returns
a data page of result by filter
- is_open() bool
Checks if the component is opened.
- Returns
true if the component has been opened and false otherwise.
- open(correlation_id: Optional[str])
Opens the component.
- Parameters
correlation_id – (optional) transaction id to trace execution through call chain.
- set_references(references: pip_services3_commons.refer.IReferences.IReferences)
Sets references to dependent components.
- Parameters
references – references to locate the component dependencies.
- unset_references()
Unsets (clears) previously set references to dependent components.