I have a smart field role
on the user
model. This field is populated with a query to userRole
. To improve performance I decided to move the query to user.afterFind
and fetch all roles at once with a WHERE IN
query.
It works as expected when browsing the user
collection in forestadmin but a user can be in a organization and if I open the organization
summary view where a the users
table is shown the afterFind hook is called with null
. When inspecting the query of the afterFind:
- the
attributes
are for user
not for organization
- the
id
in the where
{id: '...'}
is the id
of the organization
, not a user.id
When opening the related data view of user
directly the afterFind
is not even called:
data/organization/index/record/organization/5815867569443950735/has-many/organization-users
Expected behavior
The afterFind
hook should be executed whenever the model is fetched.
Actual behavior
The afterFind
hool is not executed consistently with the correct values
Context
Please provide any relevant information about your setup.
- Package Version: forest-express-sequelize 7.0.3
- Express Version: 4.17.1
- Sequelize Version: ~5.15.1
- Database Dialect: pg
- Database Version: 9.4
- Forest-Environment-Id: 68385
- Forest-Project-Id: 54928
- Forest-Rendering-Id: 82745
- Forest-Team-Id: 54359
Hello @JonasKman,
This seems to be the issue from Sequelize directly, so we won’t be able to help you. You can see the GitHub issue here
Sorry 
Ok 
Are both of theses calls include calls in forest-admin?
And what about the part where it looks like a user
find is called with the organization
find, might this be a bug on your side?
Would it be possible to have both implemented? a get
in the smart field when used as related data and in afterFind
when used for listing?
Hello @JonasKman,
The package forest-express-sequelize
is actually creating a sequelize query when a route is requested.
If you display a user or list users, it creates a query on the user
collection of sequelize, and then adds includes on related data.
As hooks are defined on collections, you’ll probably have to create hooks on every collection that reference user, if you want to use afterFind
hooks.
Sequelize hooks are managed by sequelize, and forest-express-sequelize
don’t have any interaction with them, so if their behavior is not what you expected, it’s likely that it’s due to how sequelize works.
Thanks for the information.
Is there an alternate way to increase the performance of such operations instead of sending X single requests when implementing it as a smart field?
I don’t see how to proceed directly from the code, but one solution would be to create a view in the DB, in order to compute the values you want to associate to users directly in the DB.
As these values seem coming from the same DB, I guess that this kind of solution would work for you.
I am now implementing both the get
and the afterFind
with a check in the get
if the property is already set.