GetIdsFromRequest
Last updated
Last updated
Please be sure of your agent type and version and pick the right documentation accordingly.
This is the documentation of the forest-express-sequelize
and forest-express-mongoose
Node.js agents that will soon reach end-of-support.
forest-express-sequelize
v9 and forest-express-mongoose
v9 are replaced by @forestadmin/agent
v1.
Please check your agent type and version and read on or switch to the right documentation.
In recent versions of our agents, you may have noticed a new helper, which is getIdsFromRequest
. This helper comes alongside the 'Select All' feature, allowing you to trigger a Smart Action on more records than those displayed in the UI.
Unfortunately, this helper is not compatible with Smart Actions triggered on Smart Relationships. This is due to the Smart Relationship concept. When you create a HasMany Smart Relationship, you become the owner of the way your data are linked together by overriding the routes. Forest can't retrieve the logic to link the data, this is why you also need to code your own getIdsFromRequest
helper. This documentation will guide you through the steps you need to create your own helper.
Let's take an example to illustrate what we want to achieve:
In this case, with have a HasMany Smart Relationship between owners
and articles
called Liked articles
. As you can see, we are about to trigger the Unlike
Smart Action on every article the owner liked that corresponds to the filter and the search we configured.
This helper simply takes a query as a parameter (containing your filters, your search, and some other configuration) and then returns the ids corresponding to this query. In other words, based on what the user selects ('select all', 'select current page', ...) this helper is able to return the exact ids the user wants to operate on. With these ids, your will then be able to perform operations related to your smart actions.
4 cases need to be handled there:
Select all: each of the related records should be impacted
Select all, minus some: each of the related records should be impacted, except specific ones
Select current page: each of the listed records should be impacted
Select some: only some specific records should be impacted
Only the two first cases need to be handled, because the last two cases consist of a simple list of the ids selected by the user directly in the request. So nothing special to do here.
In conjunction with the previous 4 cases, we also need to handle the filters and the search set up before executing the smart action.
Please find in the following snippet every of the requirement listed above fulfilled to make the Smart Action work with the Select All feature.
Explanation of the code:
Line 1: If the Select All feature has been used, we need to build a query to concatenate the filter, the search, and the Select All configuration. Otherwise, the ids are already present in the query (see line 58)
Line 25: Here is an example to show you how to quickly handle filters, if any
Line 36: Here is an example to show you how to handle the search, if any
Line 44: Finally, this snippet of code removes any ids that have been unselected by the user after using the Select All feature.