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 queriesconstauthor=awaitmodels.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 collectionagent.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 collectionagent.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 tableconstaddress=awaitgeoWebService.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 tableconstaddresses=awaitgeoWebService.getAddresses(users.map(user =>user.address_id), );// Return the full address for each userreturnusers.map(user => {constaddr=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.