Migration to @forestadmin/agent - what about where queries?

In this part of the migration guide, it says that sequelize.where is changed to condition tree.

Throughout the project, there are a lot of queries like for example this random example:

        const myInstance = await models.myModel.findOne({
          where: {
            [Op.or]: [
              { modelName: providedName }, 
              { modelColor: providedColor }
            ]
          },
          transaction
        })

Do I have to change all these queries to a condition tree, or does this only refer to everything within forest/[model.js] segments definitions?

Thank you!

Hello @marksrr,

This documentation only refers to where in Forest Admin segments. Maybe it’s a misleading example. :slightly_frowning_face:

So basically, you can continue using the Sequelize queries.

In the other hand, we propose both usages (Native Querie Sequelize / Forest Admin Query Interface) in the agent code. You can find more relevant information about this in the following documentation.

PS: The main advantage of our Query Interface is that you can access customisations easily (computed field, relationships, …)

It only refers to Smart segments definition. And it’s up to you and your preferences, you can use Native query (sequelize) or the Query Interface. :smiley:

See those examples:

Native query (sequelize) example
collection.addSegment('mySegment', async context => {
  const rows = await context.collection.nativeDriver.rawQuery(`
    SELECT product_id, COUNT(*)
    FROM orders
    GROUP BY product_id
    ORDER BY count DESC
    LIMIT 10;
  `);

  return { field: 'id', operator: 'In', value: rows.map(r => r['product_id']) };
});
Query Interface example
collection.addSegment('mySegment', async context => {
  const rows = await context.dataSource
    .getCollection('orders')
    .aggregate({}, { operation: 'Count', groups: [{ field: 'product_id' }] }, 10);

  return { field: 'id', operator: 'In', value: rows.map(r => r['product_id']) };
});

Let me know if it’s more clear now.

Kind regards,
Morgan

2 Likes

Thank you very much, that cleared it up. Just had to be sure since the client needs the estimation for the migration.

Cheers!

1 Like

No problem. You’re welcome :bowing_man:

Let us know if you encounter any issue during the process.

Cheers,
Morgan