Woodshop
Search…
Override the count route
This example shows you how to override the count default route. Forest Admin comes packaged with a set of existing routes, which execute Forest Admin's default logic. At installation, they are generated in /routes.

Requirements

  • An admin backend running on forest-express-sequelize

How it works

Directory: /routes

This directory contains the users.js files where the routes are declared.
Forest Admin API is generating a default count query to manage the display and pagination of your data. To override the count route, simply remove the next() statement and add your own logic.
The solution is to override this default query using an approximative count. If you don't need an exact count, it will be much faster to retrieve the data for your big tables:
/routes/users.js
1
//...
2
3
// Define an optimized count using reltuples
4
const countOptimized = async (request, response, next) => {
5
try {
6
const noFilterOrSearch = !request.query.search && !request.query.filter;
7
if (noFilterOrSearch) {
8
const result = await models.sequelize
9
.query("SELECT reltuples AS count FROM pg_class WHERE relname = 'users';");
10
const { count } = result[0][0];
11
response.send({ count });
12
} else {
13
next();
14
}
15
} catch (error) {
16
next(error);
17
}
18
};
19
20
// Send back the optimized count in the default count route
21
router.get('/users/count', ensureAuthenticated, permissionMiddlewareCreator.list(), countOptimized);
22
23
//...
Copied!
Last modified 2mo ago