SQL Smart segment example doesn't work

Expected behavior

Smart Segment example should work.

const { collection } = require('forest-express-sequelize');
const models = require('../models');

const { Op, QueryTypes } = models.objectMapping;

collection('products', {
  segments: [{
    name: 'Bestsellers',
    where: (product) => {
      return models.sequelize.query(`
        SELECT products.id, COUNT(orders.*)
        FROM products
        JOIN orders ON orders.product_id = products.id
        GROUP BY products.id
        ORDER BY count DESC
        LIMIT 5;
      `, { type: QueryTypes.SELECT })
      .then((products) => {
        let productIds = products.map((product) => product.id);
        return { id: { [Op.in]: productIds }};
      });
    }
  }]
});

Actual behavior

sequelize is not defined in the example so it doesn’t work.

Failure Logs

Error: [forest] 🌳🌳🌳 Unexpected error: Cannot read property 'query' of undefined

Context

Please provide any relevant information about your setup.

"meta": {
    "liana": "forest-express-sequelize",
    "liana_version": "7.11.5",
    "stack": {
      "database_type": "mysql",
      "engine": "nodejs",
      "engine_version": "14.17.0",
      "orm_version": "5.15.2"
    }
  }```

Hello @mathieuh,

Thanks for you message and welcome to our community :raised_hands:

To help you out with your case, could you tell me:

  • Did you generate your admin backend automatically (with lumber)?
  • Where did you placed this code (what is the file name and its path)?
  • Do you have an index file inside your /models directory? Did you change it?

Thanks!

Hi @anon34731316,

Did you generate your admin backend automatically (with lumber)?

Yes

Where did you placed this code (what is the file name and its path)?

In the /forest/products.js file like stated in the example Create and manage Smart Segments - Documentation

Do you have an index file inside your /models directory? Did you change it?

Yes, not that I’m aware of. Here it is.

const fs = require('fs');
const path = require('path');
const Sequelize = require('sequelize');

const databasesConfiguration = require('../config/databases');

const connections = {};
const db = {};

databasesConfiguration.forEach((databaseInfo) => {
  const connection = new Sequelize(databaseInfo.connection.url, databaseInfo.connection.options);
  connections[databaseInfo.name] = connection;

  const modelsDir = databaseInfo.modelsDir || path.join(__dirname, databaseInfo.name);
  fs
    .readdirSync(modelsDir)
    .filter((file) => file.indexOf('.') !== 0 && file !== 'index.js')
    .forEach((file) => {
      try {
        const model = connection.import(path.join(modelsDir, file));
        db[model.name] = model;
      } catch (error) {
        console.error(`Model creation error: ${error}`);
      }
    });
});

Object.keys(db).forEach((modelName) => {
  if ('associate' in db[modelName]) {
    db[modelName].associate(db);
  }
});

db.objectMapping = Sequelize;
db.connections = connections;

module.exports = db;

@mathieuh,

Thanks for your answers and for bringing up this subject.

You’re right, something is outdated in the documentation.
Could you please try to replace:

return models.sequelize.query(`

by:

return models.connections.default.query(`

?

1 Like

I works with models.connections.default.query.

Thank you

1 Like

I’ve updated the documentation.

Thanks!

1 Like