Optimize your agent
Computed fields
Replace queries with the dependencies option
dependencies optionagent.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}`),
});
});// 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
Avoid duplicate queries
Search
Last updated