When adding a new record, the search functionality to link to an existing collection does not work

Feature(s) impacted

When adding a new record, linking to an existing collection

Observed behavior

When adding a new record, the search functionality to link to an existing collection does not work.
When I type in the desired ID, the dropdown list of options does not contain the desired ID (despite scrolling to the end of the list) and it seems like the options in the dropdown list are seemingly unrelated to the ID I’m searching for. See the loom recording linked to above

Expected behavior

When I search for an ID (that I’ve confirmed exists) I expect to see that ID in the list that appears. I also expect the items in the drop down list to be relevant to what I’m typing in the search.

Context

Project: Bridgement.1
Package Version: “forest-express-sequelize”: “^8.3.2”,
Express Version: “express”: “^4.17.1”,
Sequelize Version: “sequelize”: “6.6.2”,
Database Dialect: postgresql

Hello @dig

When you type “44881” it will search for any record with any field (not only id) matching 44881 and give you 10 results in no particular order. The records you are getting should have an other field matching 44881.

Most users change their Collection settings > Reference field (documentation) to search and display related records using a human readable field instead of id.

Would that work for you or can you only search this collection by id ?

Would it be possible to prioritise the results based on the selected Reference field for that Collection? (that would solve my issue). So if I select the Id to be reference field, then I’d expect the matching Ids to appear first in the 10 restuls before other relevant matches on other fields.

In this example there isn’t really another human readable field (instead of id) that is unique enough for this search functionality to work

Hello @dig

Thank you for the feedback. I’ll create a feature request for prioritizing matching on id when there’s no reference field.

In the meantime, you can get this behavior by overriding the collections list route in your agent server (documentation). You may need to adapt the name of you “collections” model, the route path and the name of the id field :

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

...

router.get('/collections', permissionMiddlewareCreator.list(), (request, response, next) => {
  // Learn what this route does here: https://docs.forestadmin.com/documentation/v/v6/reference-guide/routes/default-routes#get-a-list-of-records
  const id = Number(request.query.search)
  // Only override when searching for a related data by id
  if (request.query.searchToEdit && !Number.isNaN(id)) {
    // Only get the record with this exact id
    request.query.filters = `{"field":"id","operator":"equal","value":${id}}`;
    const recordsGetter = new RecordsGetter(collections, request.user, request.query);
    recordsGetter.getAll(request.query)
      .then(records => recordsGetter.serialize(records))
      .then(recordsSerialized => response.send(recordsSerialized))
      .catch(next);
  } else {
    next();
  }
});

Let me know if this works for you. I’m happy to assist if you need any help.

1 Like