FilePersistorKeyBuilder<T> typedef

FilePersistorKeyBuilder<T> = String Function(DocumentSnapshot<T> snap)

By default, documents are persisted to a file grouped by their top-level collection. For example, a document with path users__1 and users__1__posts__1 are both persisted to a users.json file, while messages__1 would be persisted to messages.json.

To customize this behavior, a custom FilePersistorKey can be specified.

Ex. 1: Static keys.

Loon.collection(
  'users',
  settings: FilePersistorSettings(
    key: FilePersistor.key('custom'),
  ),
);

In this example, all documents of the 'users' collection will be stored using the custom persistence key and placed in the custom.json file instead of the default users.json file for their collection.

Ex. 2: Dynamic keys.

Loon.collection(
  'birds',
  settings: FilePersistorSettings(
    key: FilePersistor.keyBuilder((snap) {
      return "birds_${snap.data.species}";
    }),
  ),
);

Documents from the same collection can be given different persistence keys using FilePersistor.keyBuilder. This allows you to customize persistence on a per-document level, supporting scenarios like sharding of large collections.

Implementation

typedef FilePersistorKeyBuilder<T> = String Function(DocumentSnapshot<T> snap);