Skip to main content

Hooks

Hooks are used to:

  • modify the data before it is saved to the database on create or update
  • execute something after data were saved or deleted
  • change the query before fetching items from the database
  • modify the fetched data before it is displayed in the list and show
  • prevent the request to db depending on some condition (Better use allowedActions for this)

Modify the data before it is saved to the database

Let's add reference to adminUser when user creates a new apartment:

./resources/apartments.ts
import type { AdminUser } from  'adminforth';

{
...
resourceId: 'aparts',
columns: [
...
{
name: 'realtor_id',
...
showIn: ['list', 'show', 'edit'], // don't even show this field in create
...
},
...
],
...
hooks: {
create: {
beforeSave: async ({ adminUser, record }: { adminUser: AdminUser, record: any }) => {
record.realtor_id = adminUser.dbUser.id;
return { ok: true, record };
}
}
}
}

All hooks

Check all hooks in the API reference.