Optimize your agent

This is the official documentation of the @forestadmin/agent Node.js agent.

Your new agent is up and running, congratulations!

Because of the internal changes, you might already have noticed a performance improvement when you migrated your agent. Let's see how you can optimize your agent even more.

Computed fields

Replace queries with the dependencies option

A common pattern in the old agent was to make queries in the get function of a computed field to fetch relations.

This is now natively supported with the dependencies option, and it is much faster.

If you ported your agent by using the simpler route, which is copying the old agent code to the new agent, you can probably replace a lot of your queries with the dependencies option.

agent.customizeCollection('post', postCollection => {
  postCollection.addField('authorFullName', {
    columnType: 'String',
    dependencies: ['authorId'],
    getValues: posts =>
      posts.map(async post => {
        // Those async queries take a long time and are performed in parallel with
        // the other queries
        const author = await models.authors.findOne({
          where: { id: post.authorId },
        });

        return `${author.firstName} ${author.lastName}`;
      }),
  });
});

Move async calls outside of the hot loop

The new computed field syntax works in batch mode.

If you have computed fields that take a long time to compute, you may want to use this new syntax to optimize them.

agent.customizeCollection('users', users => {
  users.addField('full_address', {
    columnType: 'String',
    dependencies: ['address_id'],
    getValues: users =>
      users.map(async user => {
        // Get the address for each user individually
        // This is very slow if you are displaying a lot of users in a table
        const address = await geoWebService.getAddress(customer.address_id);

        return [address.line_1, address.line_2, address.city, address.country].join(
          '\n',
        );
      }),
  });
});

Avoid duplicate queries

If you happen to have computed fields that are similar, or that depend on the same data, you can make fields that depend on other fields.

agent.customizeCollection('users', users => {
  users.addField('firstName', {
    columnType: 'String',
    dependencies: ['id'],
    getValues: async users => {
      const userIds = users.map(user => user.id);
      const userInfos = await authenticationWebService.getUserInfo(userIds);

      return users.map(
        user => userInfos.find(userInfo => userInfo.id === user.id).firstName,
      );
    },
  });

  users.addField('lastName', {
    columnType: 'String',
    dependencies: ['id'],
    getValues: async users => {
      const userIds = users.map(user => user.id);
      const userInfos = await authenticationWebService.getUserInfo(userIds);

      return users.map(
        user => userInfos.find(userInfo => userInfo.id === user.id).lastName,
      );
    },
  });
});

The new agent introduced the capability to customize the search behavior of your agent depending on the search query.

This is a very powerful feature that allows you to make sure that your agent is fast.

Documentation about this feature is available here.

Last updated