Load smart fields using hook
Last updated
Last updated
This example shows you how to implement an afterFind hook with Sequelize to load several smart fields at once when a record is retrieved from the database. Learn more about Sequelize hooks.
The main advantage of using an afterFind hook is that it allows you to avoid multiplying queries to your database if those queries are redundant when retrieving smart fields values.
Here a Program hasMany
Activities.
We want to display two smart fields:
"hasActivity"
, that will return whether the program has any related activities in the database.
"duration"
, that will return the length of the program in weeks.
An admin backend running on forest-express-sequelize
This directory contains the programs.js
file where the model is declared.
As Sequelize hooks are declared upon the model definition, the afterFind hook is implemented in this file.
This directory contains the programs.js
file where the smart fields duration
and hasActivity
are declared.
As the logic to retrieve the field value is defined in the hook, you do not need to define the get method for each smart field.