Search on relationship fields by default

By default, the search is performed only on the classic columns of your table and does not take into account the belongsTo fields.

You usually have the possibility to click on Try an extended search to extend the search to belongsTo fields in a second step.

But you can as well extend Forest Admin's default routes to use the extended search by default.

Requirements

  • An admin backend running on forest-express-sequelize

How it works

Directory: /routes

This directory contains the customers.js file where the model is declared.

You will have to update the routes that get the list but also the count of customers.

/routes/customers.js
...
// Get a list of Customers - Check out our documentation for more details: https://docs.forestadmin.com/documentation/reference-guide/routes/default-routes#get-a-list-of-records
router.get('/customers', permissionMiddlewareCreator.list(), (request, response, next) => {
const recordsGetter = new RecordsGetter(customers);
const params = request.query;
params.searchExtended = '1';
recordsGetter.getAll(params)
.then(records => recordsGetter.serialize(records))
.then(recordsSerialized => response.send(recordsSerialized))
.catch(next);
});
// Get a number of Customers - Check out our documentation for more details: https://docs.forestadmin.com/documentation/reference-guide/routes/default-routes#get-a-list-of-records
router.get('/customers/count', permissionMiddlewareCreator.list(), (request, response, next) => {
const recordsCounter = new RecordsCounter(customers);
const params = request.query;
params.searchExtended = '1';
recordsCounter.count(params)
.then(count => response.send({ count }))
.catch(next);
});
...

With this snippet, only the customers'collection would use extended search by default.

Using extended search is less performant than default search. Use this wisely.