IdentifiableFilePersistence

Abstract persistence component that stores data in flat files and implements a number of CRUD operations over data items with unique ids.

Implements: IdentifiableMemoryPersistence, iidentifiable

Description

The IdentifiableFilePersistence class allows you to create persistence components that store data in flat files and implement a number of CRUD operations over data items with unique ids.

Important points

  • The data items must implement the IIdentifiable interface.
  • In basic scenarios child classes shall only override get_page_by_filter, get_list_by_filter or delete_by_filter operations with a specific filter function. All other operations can be used out of the box.
  • In complex scenarios child classes can implement additional operations by accessing cached items via the self._items property and calling the save method on updates.

Configuration parameters

  • path: path to the file where data is stored

  • options:

    • max_page_size: maximum number of items returned in a single page (default: 100)

References

  • *:logger:*:*:1.0 - (optional) (../../../components/log/ilogger) components to pass log messages

Constructors

Creates a new instance of the persistence.

IdentifiableFilePersistence(persister: Optional[JsonFilePersister] = None)

  • persister: JsonFilePersister - (optional) a persister component that loads and saves data from/to flat file.

Fields

_persister

JSON file persister.

_persister: JsonFilePersister

Instance methods

configure

Configures component by passing configuration parameters.

configure(config: ConfigParams)

  • config: ConfigParams - configuration parameters to be set.

Examples

class MyFilePersistence(IdentifiableFilePersistence):
    def __init__(self, path):
        super(MyFilePersistence, self).__init__(JsonPersister(path))

    def get_page_by_filter(self, correlationId, filter, paging):
        super().get_page_by_filter(correlationId, filter, paging, None)

    persistence = MyFilePersistence("./data/data.json")
    item = persistence.create("123", MyData("1", "ABC"))

    mydata = persistence.get_page_by_filter("123", FilterParams.from_tuples("name", "ABC"), None, None)

    print str(mydata.get_data())
    persistence.delete_by_id("123", "1")

See also