Is it possible to customize the relationship search query?

Hello there,

I have a belongsTo relationship in a model and I’d like to know if it’s possible to customize the search query that is used when the user searches for the related data to create a relationship.
The reason is that I have a column in the table that is indexed and would make the search much faster, without this customisation the search is done across all string fields which makes it much slower for the size of the table we’re working with.

Thanks!

1 Like

Hi @nathanqueija,

This can be done using a “route overidde”:
Figure your model matches user.belongsTo(project).
On user creation form there is a project field, that search for projects. (the ugly request on all strings)

Opening dev-tools show the details of the request:
GET https://<server>:<port>/forest/project?<query params>

This route is on your server and has to be overriden. Note that the searched term is the query param named search.

In your server, it’s found in routes/project.js (line router.get('/project', ...))

This page show help about how to do this.

So you have to:

  • remove the next() call already here
  • write the request that match perfectly your needs
  • serialize the result before returning it.

Important: You will also need to repeat the operation for the /project/count route.

Note: The GET route is used on every project search, including the search from project collection. If you want a separated behavior, you can use the query parameter context to distinguish the cases.

Let me know if that’s ok for you.
Regards.

1 Like

It works well @Sliman_Medini . Thanks!

1 Like