Customising selection of existing related data

Feature(s) impacted

Smart relationships

Observed behavior

We have a (smart?) model defined in the /forest folder that pulls our list of user logins from a separate datastore (cognito).

A couple of the fields in this model use the reference prop to define links to models in the main datastore:

    {
      field: 'teamAccess',
      type: ['String'],
      reference: 'customer_admin_team.id',
    },
    {
      field: 'client',
      type: 'String',
      reference: 'customer_admin_client.id',
    },

These are surfaced in our admin panel under “related data” - the issue we have is when we go to associate an existing record:

image

at this point the api runs a query to populate the dropdown:

Executing (default): SELECT {{fields}} FROM customer_admin.team LIMIT 10 OFFSET 0;

The problem with this is that it makes it appear that there are only 10 options to choose from when there are many more.

However if the user types a search string into the box, then the filtered results are returned as expected:

Expected behaviour

I would like to either:

  • Remove the LIMIT 10 part of the query to populate the available options
  • Disable the initial results until the user has typed a search string

I have looked through the available documentation but cannot find anything relating to this, nor does there appear to be any available customisation options when going to “Edit Layout” in the admin panel.

What options do we have to customise the behaviour of this related field when adding a link to an existing record? Is there some documentation that I have missed describing this?

Context

  • Agent technology: node.js
  • Agent (forest package) name & version: forest-express-sequelize@8.5.12
  • Database type: Postgres

Hello @Shane,

Thank you for you very detailed feedback.

When you write that after typing a search term, the UI is showing results “as expected”, do you know if the limit is still applied in this case?

Thanks for coming back - yes, limit of 10 records is still applied when inputting a search term.

Thank you for your answer.

I don’t think we can change this behavior in the default code, because it could have performance implications for other users.

But the good news is that it’s possible to change this behavior in your case.

If you look at the documentation about customizing routes, you’ll find an example of the default code that is used to retrieve a list of elements:

...

const {
  PermissionMiddlewareCreator,
  RecordsGetter,
} = require('forest-express-sequelize');
const { companies } = require('../models');

const permissionMiddlewareCreator = new PermissionMiddlewareCreator('companies');

// Get a list of Companies - Check out our documentation for more details: https://docs.forestadmin.com/documentation/v/v6/reference-guide/routes/default-routes#get-a-list-of-records
router.get('/companies', permissionMiddlewareCreator.list(), (request, response, next) => {
  const { query, user } = request;
  const recordsGetter = new RecordsGetter(companies, user, query);

  recordsGetter.getAll()
    .then(records => recordsGetter.serialize(records))
    .then(recordsSerialized => response.send(recordsSerialized))
    .catch(next);
});

If I understand the code correctly, a default pagination of 10 is applied in the code when no pagination is specified at all.

So maybe you can apply your own default pagination on the route that is causing you trouble, in order to fix your issue?

In this case, you need to add the parameter page: { number: 1, size: X } to the query object.

1 Like

Oh god no, not suggesting that you should change this for everyone :sweat_smile: Just whether we can affect it for our case. Your example looks like it gives us the ability to do that, so will give it a go.

Thanks Guillaume, much appreciated :+1:

1 Like