Hello everyone ![]()
I’m working on a Directus hook extension that intercepts item creation and displays a custom message when a duplicate record is detected.
The goal is to replace the generic SQL error message:
Duplicate entry for key 'unique_xxx'
with something clearer like:
This course already exists for the selected teacher, subject, and date.
Technical Details
I created a custom hook in the following folder:
/extensions/hooks/hooks_log/
Here’s my index.js file:
export default ({ action }, { services, exceptions }) => {
const { ItemsService } = services;
const { ServiceUnavailableException } = exceptions;
action('items.create', async ({ collection, payload }, { database, accountability }) => {
if (collection !== 'cours') return;
const { id_enseignant, matiere, date } = payload;
const coursService = new ItemsService('cours', { database, accountability });
const existing = await coursService.readByQuery({
filter: {
id_enseignant: { _eq: id_enseignant },
matiere: { _eq: matiere },
date: { _eq: date },
},
limit: 1,
});
if (existing.length > 0) {
throw new ServiceUnavailableException(
'⚠️ This course already exists for the selected teacher, subject, and date.'
);
}
});
};
And the package.json file:
{
"name": "hooks_log",
"version": "1.0.0",
"type": "module",
"directus:extension": {
"type": "hook"
}
}
Problem Encountered
The hook sometimes doesn’t appear in the “Extensions loaded” list in Directus.
I’d like to understand:
-
Do I need a
srcanddistfolder for a simple JavaScript-based hook? -
Is there a better way to show user-friendly messages in the Directus UI instead of backend error messages?
Goal
I’m trying to:
-
Create a reliable hook that prevents duplicates before inserting records
-
Display a clear, user-friendly error message
-
Better understand how Directus loads hook extensions
Can you correct me this example?:




