Transform
The transform
method allows you to interact with the query and modify or read it.
Method Signature
transformer?: (params: Record<string, any>) => Record<string, any>;
Returns
An object with the query before being converted to url.
{
"with": "posts",
"where[id][whereBetween]": "10,30",
"paginate": 3,
"page": 4,
"getter": "get"
}
Examples
-
Track your queries.
const query = createQuery<Transaction>('/api/transactions', {
transformer: (params) => {
console.group('Query Parameters');
console.log('Original Params:', params);
console.time('Query Execution');
trackQueryParameters(params);
return params;
}
});
-
Dynamic transformations.
const query = createQuery<User>('/api/users', {
transformer: (params) => {
// Convert camelCase to snake_case for backend compatibility
return Object.fromEntries(
Object.entries(params).map(([key, value]) => [
key.replace(/([A-Z])/g, '_$1').toLowerCase(),
value
])
);
}
});
-
Conditional updates.
const query = createQuery<Product>('/api/products', {
transformer: (params) => {
// Add extra filter based on user's role
const userRole = getCurrentUserRole();
return userRole === 'admin'
? params
: {
...params,
'visibility': 'public' // Non-admins see only public items
};
}
});