Smart field with a join table

Hello. I am implementing a filter on a smart field. My code works but I don’t feel that will be able to scale.

This smart field comes from a joint table.

  • Table Event
    • id : integer
    • accountId : integer
  • Table Account
    • id : integer
    • countryFullname : integer

For the moment, my implementation looks like this (on Event collection) :

async function filterOnAccountCountryFullname(context: SmartFieldFiltererFilter, type: string) {
  if (context.condition.operator != "blank") {
    const parser = new FilterParser({ fields: [] }, "Europe/Paris");
    const where = await parser.formatCondition({
      field: "countryFullname",
      operator: context.condition.operator,
      value: context.condition.value,
    });

    const accounts = await AccountModel.findAll({ attributes: ["id"], where });
    return { [type]: { [Op.in]: accounts.map(a => a.id) } };
  }

  return { [type]: { [Op.or]: [{ [Op.eq]: null }, { [Op.eq]: "" }] } };
}

I encounter some performance issues as the Op.in may contain a lot of data.

Do you have any ideas for a better implementation of this pattern? I would like not to use Op.in and, if possible, load the Accounts when we load the Events.

Hello, Welcome to forest admin support,
Unfortunately our API does not allow to add a join during the customization of your filter. It means your code use the correct way… but I’m agree with you the performance is degraded.
An other solution is to use an sql view as collection and import your field from the relation in the view. After that you can apply some operator directly on your related field.
I hope it will help you :pray:

Hello. Thanks for the fast answer. Do you know if this kind of API improvement will be implemented one day?

We are developing a new agent from scratch: GitHub - ForestAdmin/agent-nodejs: 🌱 Node.js agent for Forest Admin. The API is very different but much simpler. We continue to maintain your version but it will not evolve.

1 Like