pip_services3_mysql.persistence.MySqlPersistence module
-
class
pip_services3_mysql.persistence.MySqlPersistence.
MySqlPersistence
(table_name: str = None, schema_name: 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 MySQL 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 self._db or self._collection properties.
- ### Configuration parameters ###
table: (optional) MySQL table name
schema: (optional) MySQL schema 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:
connect_timeout: (optional) number of milliseconds to wait before timing out when connecting a new client (default: 0)
idle_timeout: (optional) number of milliseconds a client must sit idle in the pool and not be checked out (default: 10000)
max_pool_size: (optional) maximum number of clients the pool should contain (default: 10)
- ### 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 MyMySqlPersistence(MySqlPersistence): def __init__(self): super(MyMySqlPersistence, self).__init__('mydata') def get_by_name(self, correlation_id, name): criteria = {'name':name} return self._model.find_one(criteria) def set(self,correlation_id, item): criteria = {'name': item['name']} options = {'upsert': True, 'new': True} return self._model.find_one_and_update(criteria, item, options) persistence =MyMySqlPersistence() persistence.configure(ConfigParams.from_tuples( "host", "localhost", "port", 27017 )) persistence.open('123') persistence.set('123', {'name':'ABC'}) item = persistence.get_by_name('123', 'ABC') print(item) # Result: { name: "ABC" }
-
clear
(correlation_id: Optional[str]) Clears component state.
- Parameters
correlation_id – (optional) transaction id to trace execution through call chain.
- Returns
raise error or None no errors occured.
-
close
(correlation_id: Optional[str]) Closes component and frees used resources.
- Parameters
correlation_id – (optional) transaction id to trace execution through call chain.
- Returns
raise error or None no errors occured.
-
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: T) → Optional[T] Creates a data item.
- Parameters
correlation_id – (optional) transaction id to trace execution through call chain.
item – an item to be created.
- Returns
created item
-
delete_by_filter
(correlation_id: Optional[str], filter: Any) Deletes data items that match to a given filter.
- Parameters
correlation_id – (optional) transaction id to trace execution through call chain.
filter – (optional) a filter JSON object.
- Returns
null for success
-
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 getCountByFilter 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 data page or error
-
get_list_by_filter
(correlation_id: Optional[str], filter: Any, sort: Any, select: Any) → List[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 getListByFilter 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
sort – (optional) sorting JSON object
select – (optional) projection JSON object
- Returns
a data list
-
get_one_random
(correlation_id: Optional[str], filter: Any) → T Gets a random item from items that match to a given filter. This method shall be called by a public getOneRandom 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 random item
-
get_page_by_filter
(correlation_id: Optional[str], filter: Any, paging: pip_services3_commons.data.PagingParams.PagingParams, sort: Any, select: Any) → 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 getPageByFilter 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 or raise error
-
is_open
() 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.
- Returns
raise error or null no errors occured.
-
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.