Hi. I set the QUERY_LIMIT_MAX option to 100 but it limits all the collections. Is there a way to restrict the limit value for each api request based on the role? So for example, if the request is from Public user, restrict max limit to 100 but if its Super Admin, they can pass limit=-1 in the query to get all the data? Thanks in advance.
Hello @Sadman_Yasar_Sayem
You can create a filter extension and use the items.query
and query
to modify the query
Refer here for details https://directus.io/docs/guides/extensions/api-extensions/hooks#filter-events
import { defineHook } from '@directus/extensions-sdk';
export default defineHook(({ filter }, { services, getSchema }) => {
const { ItemsService } = services;
// Listen for the 'items.query' filter for ALL collections
filter('items.query', (payload, { accountability }) => {
// If there's no user (public)
if (!accountability) {
payload.limit = 10;
}
if (accountability.role === 'xyz') { // use the uuid od the role
payload.limit = 100;
}
return payload;
});
});