pip_services3_mysql.persistence.IdentifiableJsonMySqlPersistence module
-
class
pip_services3_mysql.persistence.IdentifiableJsonMySqlPersistence.
IdentifiableJsonMySqlPersistence
(table_name: str = None, schema_name: str = None) Bases:
pip_services3_mysql.persistence.IdentifiableMySqlPersistence.IdentifiableMySqlPersistence
Abstract persistence component that stores data in MySQL in JSON or JSONB fields and implements a number of CRUD operations over data items with unique ids. The data items must implement
IIdentifiable
interface.The JSON table has only two fields: id and data.
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 ###
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(IdentifiableJsonMySqlPersistence): def __init__(self): super(MyMySqlPersistence, self).__init__("mydata", MyDataMySqlSchema()) def __compose_filter(self, filter): filter = filter or FilterParams() criteria = [] name = filter.get_as_nullable_string('name') if name: criteria.append({'name': name}) return {'$and': criteria} if len(criteria) > 0 else None def get_page_by_filter(self, correlation_id, filter, paging): return super().get_page_by_filter(correlation_id, self.__compose_filter(filter), paging, None, None) persistence = MyMySqlPersistence() persistence.configure(ConfigParams.from_tuples( "host", "localhost", "port", 27017 )) persistence.open('123') persistence.create('123', {'id': "1", 'name': "ABC"}) page = persistence.get_page_by_filter('123', FilterParams.from_tuples('name', 'ABC'), None) print(page.data) # Result: { id: "1", name: "ABC" } persistence.delete_by_id("123", "1") # ...
-
update_partially
(correlation_id: Optional[str], id: Any, data: pip_services3_commons.data.AnyValueMap.AnyValueMap) → Optional[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
updated item