Class IdentifiableFilePersistence<T extends org.pipservices3.commons.data.IIdentifiable<K>,K>

All Implemented Interfaces:
org.pipservices3.commons.config.IConfigurable, org.pipservices3.commons.refer.IReferenceable, org.pipservices3.commons.run.ICleanable, org.pipservices3.commons.run.IClosable, org.pipservices3.commons.run.IOpenable, IGetter<T,K>, IPartialUpdater<T,K>, ISetter<T>, IWriter<T,K>

public class IdentifiableFilePersistence<T extends org.pipservices3.commons.data.IIdentifiable<K>,K> extends IdentifiableMemoryPersistence<T,K>
Abstract persistence component that stores data in flat files 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 cached items via this._items property and calling 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) ILogger components to pass log messages

### Examples ###

 
 class MyFilePersistence extends IdentifiableFilePersistence<MyData, String> {
   public MyFilePersistence(String path) {
     super(MyData.class, new JsonPersister(path));
   }
 
   private Predicate<MyData> composeFilter(FilterParams filter) {
       filter = filter != null ? filter : new FilterParams();
       String name = filter.getAsNullableString("name");
       return (item) -> {
           if (name != null && item.name != name)
               return false;
           return true;
       };
   }
 
   public DataPage<MyData> getPageByFilter(String correlationId, FilterParams filter, PagingParams paging) {
       super.getPageByFilter(correlationId, this.composeFilter(filter), paging, null, null);
   }
 
 }
 
 MyFilePersistence persistence = new MyFilePersistence("./data/data.json");
 
 MyData item = persistence.create("123", new MyData("1", "ABC"));
 DataPage<MyData> mydata = persistence.getPageByFilter(
         "123",
         FilterParams.fromTuples("name", "ABC"),
         null, null, null);
 System.out.println(page.getData().toString());          // Result: { id: "1", name: "ABC" }
 persistence.deleteById("123", "1");
 ...
 
 
See Also:
  • Field Details

    • _persister

      protected JsonFilePersister<T extends org.pipservices3.commons.data.IIdentifiable<K>> _persister
  • Constructor Details

    • IdentifiableFilePersistence

      protected IdentifiableFilePersistence(Class<T> type)
      Creates a new instance of the persistence.
      Parameters:
      type - the class type
    • IdentifiableFilePersistence

      protected IdentifiableFilePersistence(Class<T> type, JsonFilePersister<T> persister)
      Creates a new instance of the persistence.
      Parameters:
      type - the class type
      persister - (optional) a persister component that loads and saves data from/to flat file.
  • Method Details

    • configure

      public void configure(org.pipservices3.commons.config.ConfigParams config) throws org.pipservices3.commons.errors.ConfigException
      Configures component by passing configuration parameters.
      Specified by:
      configure in interface org.pipservices3.commons.config.IConfigurable
      Overrides:
      configure in class MemoryPersistence<T extends org.pipservices3.commons.data.IIdentifiable<K>>
      Parameters:
      config - configuration parameters to be set.
      Throws:
      org.pipservices3.commons.errors.ConfigException