Node.js Developer Guide
Other documentationsDemoCommunityGitHub
  • Forest Admin
  • Getting started
    • How it works
    • Quick start
    • Install
      • Create your agent
      • Expose an HTTP endpoint
        • For standalone agents
        • On Express
        • On Koa
        • On Fastify
        • On NestJS
      • Autocompletion & Typings
      • Troubleshooting
    • Migrating legacy agents
      • What's new
      • Pre-requisites
      • Recommendations
      • Migration steps
        • Run new agent in parallel
        • Configure database connection
        • Code transformations
          • API Charts
          • Live Queries
          • Smart Charts
          • Route overrides
          • Smart Actions
          • Smart Fields
          • Smart Relationships
          • Smart Segments
        • Compare schemas
        • Swap agents
      • Post-migration
        • Dropping Sequelize
        • Optimize your agent
  • Data Sources
    • Getting Started
      • Collection selection
      • Naming conflicts
      • Cross-data source relationships
      • Query interface and Native Queries
        • Fields and projections
        • Filters
        • Aggregations
    • Provided data sources
      • SQL (without ORM)
      • Sequelize
      • Mongoose
      • MongoDB
    • Write your own
      • Replication strategy
        • Persistent cache
        • Updating the replica
          • Scheduled rebuilds
          • Change polling
          • Push & Webhooks
        • Schema & References
        • Write handlers
      • Translation strategy
        • Structure declaration
        • Capabilities declaration
        • Read implementation
        • Write implementation
        • Intra-data source Relationships
      • Contribute
  • Agent customization
    • Getting Started
    • Actions
      • Scope and context
      • Result builder
      • Static Forms
      • Widgets in Forms
      • Dynamic Forms
      • Form layout customization
      • Related data invalidation
    • Charts
      • Value
      • Objective
      • Percentage
      • Distribution
      • Leaderboard
      • Time-based
    • Fields
      • Add fields
      • Move, rename and remove fields
      • Override binary field mode
      • Override writing behavior
      • Override filtering behavior
      • Override sorting behavior
      • Validation
    • Hooks
      • Collection hook
      • Collection override
    • Pagination
    • Plugins
      • Provided plugins
        • AWS S3
        • Advanced Export
        • Flattener
      • Write your own
    • Relationships
      • To a single record
      • To multiple records
      • Computed foreign keys
      • Under the hood
    • Search
    • Segments
  • Frontend customization
    • Smart Charts
      • Create a table chart
      • Create a bar chart
      • Create a cohort chart
      • Create a density map
    • Smart Views
      • Create a Map view
      • Create a Calendar view
      • Create a Shipping view
      • Create a Gallery view
      • Create a custom tinder-like validation view
      • Create a custom moderation view
  • Deploying to production
    • Environments
      • Deploy on AWS
      • Deploy on Heroku
      • Deploy on GCP
      • Deploy on Ubuntu
      • Deploy on Azure
    • Development workflow
    • Using branches
    • Deploying your changes
    • Forest Admin CLI commands
      • init
      • login
      • branch
      • switch
      • set-origin
      • push
      • environments:create
      • environments:reset
      • deploy
  • Under the hood
    • .forestadmin-schema.json
    • Data Model
      • Typing
      • Relationships
    • Security & Privacy
Powered by GitBook
On this page
  • Computed fields
  • Replace queries with the dependencies option
  • Move async calls outside of the hot loop
  • Avoid duplicate queries
  • Search

Was this helpful?

  1. Getting started
  2. Migrating legacy agents
  3. Post-migration

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}`;
      }),
  });
});
agent.customizeCollection('post', postCollection => {
  postCollection.addField('authorFullName', {
    columnType: 'String',
    // The agent will automatically fetch the author collection and join it with the
    // post collection by performing a INNER JOIN on the authorId field.
    // This is _much_ faster than performing a query for each post

    dependencies: ['author:firstName', 'author:lastName'],
    getValues: posts =>
      posts.map(post => `${post.author.firstName} ${post.author.lastName}`),
  });
});

or (better)

// Define the field on the author collection
agent.customizeCollection('author', authorCollection => {
  authorCollection.addField('fullName', {
    columnType: 'String',
    dependencies: ['firstName', 'lastName'],
    getValues: authors =>
      authors.map(author => `${author.firstName} ${author.lastName}`),
  });
});

// And then import it on the post collection
agent.customizeCollection('post', postCollection => {
  postCollection.importField('authorFullName', { path: 'author:fullName' });
});

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',
        );
      }),
  });
});
agent.customizeCollection('users', users => {
  users.addField('full_address', {
    columnType: 'String',
    dependencies: ['address_id'],
    getValues: async users => {
      // Get all the addresses in a single request
      // This is much faster if you are displaying a lot of users in a table
      const addresses = await geoWebService.getAddresses(
        users.map(user => user.address_id),
      );

      // Return the full address for each user
      return users.map(user => {
        const addr = addresses.find(addr => addr.id === user.address_id);
        return [addr.line1, addr.line2, addr.city, addr.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,
      );
    },
  });
});
npm install @forestadmin/plugin-flattener

Fetch all the user info in a single request, and then flatten the result.

const { flattenColumn } = require('@forestadmin/plugin-flattener');

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

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

  users.use(flattenColumn, { columnName: 'userInfo' });
});

Search

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.

PreviousDropping SequelizeNextGetting Started

Last updated 1 year ago

Was this helpful?

Install the plugin.

Documentation about this feature is available .

@forestadmin/plugin-flattener
here