Segments

A Segment is a subset of a collection: it's a saved filter of your collection.

Segments are designed for those who want to systematically visualize data according to specific sets of filters. It allows you to save your filter configuration so you don’t have to compute the same actions every day.

From your admin panel

Segments can be configured from the interface, without the need to write any code.

This is documented in the User Guide ↗.

From your agent

Sometimes, segment filters are complicated and closely tied to your business. Forest Admin allows you to code how the segment is computed.

For instance, in our Live Demo example ↗, we’ve implemented a Segment on the collection products to allow admin users to see the bestsellers at a glance.

Example

In the following example, we are making queries using the Forest Admin Query Interface.

As Forest Admin does not impose any restriction on the handler, you are free to call external APIs or query your database directly instead.

The only requirement when implementing a segment from your agent is to return a valid ConditionTree (see Understanding Filters).

agent.customizeCollection('products', collection =>
  collection.addSegment('mySegment', async context => {
    // Query the ids of the 10 most populate products by looking at the `orders` collection.
    const rows = await context.dataSource
      .getCollection('orders')
      .aggregate({}, { operation: 'Count', groups: [{ field: 'product_id' }] }, 10);

    // Return a condition tree which matches those records
    return { field: 'id', operator: 'In', value: rows.map(r => r['product_id']) };
  });
);

Last updated

Revision created on 5/31/2023