Smart Actions
This is the official documentation of the @forestadmin/agent
Node.js agent.
In legacy agents declaring a Smart Action was a two-step process:
First, you had to declare by changing the parameters of the
collection
function in the appropriatecollections/*.js
file.Then, you had to implement the action by creating a route handler in the appropriate
routes/*.js
file.In the new agent, the process is simplified to a single step.
Code cheatsheet
Steps
Step 1: Calling addAction
for the appropriate collection
addAction
for the appropriate collectionStart by calling the addAction
function on the appropriate collection and passing the appropriate parameters.
Most notably, you will need to pass:
type
should becomescope
Note that the values are now capitalized (e.g.
single
becomesSingle
)Legacy agents defaulted to
'bulk'
if no type was specified. The new agent requires you to specify the scope.
download
should becomegenerateFile
. This is still a boolean and the same value can be passed.endpoint
andhttpMethod
should be removed. The agent will now automatically handle the routing.
Step 2: Porting the form definition
Forms are now defined in the form
property of the action.
You can simply copy the field's definition from the legacy agent to the new agent with the following differences:
fields
should becomeform
.widget
choice is no longer supported. A default widget will be used depending on the field type.hook
can be removed, those will be handled by the new agent automatically.reference
no longer exists. Use{ type: 'Collection', collectionName: '...' }
instead.enums
no longer exist. Use{ type: 'Enum', enumValues: ['...'] }
instead.
Step 3: Porting the route to the new agent execute
function
execute
functionIn the legacy agent, users had to implement the action by creating a route handler in the appropriate routes/*.js
file.
This is no longer needed as the new agent provides a context
object that contains all the information that is needed to implement the action.
When porting the route handler to the new agent, you will need to:
Move the body of the route handler to the
execute
function of the action.Replace
RecordsGetter.getIdsFromRequest()
call withcontext.getRecordIds()
.
Step 4: Porting Smart Action hooks
Load hooks and change hooks have been replaced on the new agent by the possibility to use callbacks in the form definition.
Here is an example of a load hook where the default value of a field is set to 50 euros converted into dollars:
And another for a change hook which makes a field required if the value of another field is greater than 100:
Last updated