Skip to content

Segments

A segment is a predefined subset of the records of a model, such as “Signed up this week”, “Unpaid invoices” or “VIP customers”. Segments appear in the menu of the model, so your team can open them in one click, and filters can be combined with them.

Segments are declared in the segments array of a model. Each segment has a label, a unique code and a query, written with the syntax of your ORM.

const { Op } = require('sequelize');
{
slug: 'invoices',
model: models.invoices,
segments: [
{
label: 'Unpaid',
code: 'unpaid',
query: { status: { [Op.in]: ['sent', 'overdue'] } }
}
]
}

The query object is created when the Data API starts. A date computed directly in it would be frozen at that moment. Use a getter, so that the query is computed again on each request:

{
label: 'Signed up this week',
code: 'signed_up_this_week',
get query() {
const oneWeekAgo = new Date(Date.now() - 7 * 24 * 3600 * 1000);
return { createdAt: { [Op.gte]: oneWeekAgo } };
}
}