Hooks in sequelize doesn’t work when creating entries through ForestAdmin, however, they work perfectly fine with nodejs api calls. Why is it so?
Hello @girjesh1212,
Could you please provide us with more details about your project and the technology you are using, as demanded in the message template?
- Project name: …
- Team name: …
- Environment name: …
- Agent (forest package) name & version: …
- Database type: …
- Recent changes made on your end if any: …
Feature(s) impacted
Hooks in sequelize doesn’t work when creating entries through ForestAdmin, however, they work perfectly fine with nodejs api calls.
i.e. this code should print “Hello World” when a user gets created through ForestAdmin UI panel.
However, if a user is getting created through sequelize.create() method, the hook is working perfectly fine.
const User = sequelize.define('User', {}, {
tableName: 'users',
hooks : {
beforeCreate : (record, options) => {
console.log('Hello World');
}
}
});
Observed behavior
Nothing gets printed in the console.
Expected behavior
The console should print Hello World.
Context
- Project name: ForestTesting
- Team name: -
- Environment name: development (Nodejs)
- Agent (forest package) name & version: “@forestadmin/agent”: “^1.8.13”, “@forestadmin/datasource-sequelize”: “^1.3.1”,
- Database type: Postgres using sequelize
Hey @girjesh1212,
For creating records, we currently rely on the sequelize bulkCreate
method.
That explains why the beforeCreate
hook isn’t call, but beforeBulkCreate
would be.
Depending on your use case, you can either switch to the beforeBulkCreate
sequelize hooks, or use Forest Admin Hooks.
Let me know if that helps.
Can I get the list of sequelize hooks that works with ForestAdmin?
Also, I tried integrating Forest Admin Hooks but couldn’t do.
If my model is the one given below, where and how do I define Forest hooks using this model?
I’m not able to understand if I do User.addHook() method, how would Forest know User model exist? Should I wrap addHook method in customizeCollection?
const User = sequelize.define('User', {...}, {
tableName: 'users'
});
This is currently un-documented on our end - I’ll open a ticket right away.
In the meantime, you can find more informations on the Sequelize function we rely on by checking our datasource-sequelize
code available here