How to request many relationships in many ways?

Tables have many, many relationships, the connection between the order table between them.

Exit → Order <- Product

I want to get an Outlet list for today.

So, here is the function to get all outputs:

db.Outlet.findAll({include: [ {model:db.Product, attributes: ['id', 'name', 'nameKh']} ]}).then(function(outlets){ return res.jsonp(outlets); }) 

I got this result:

enter image description here

I can only choose where the product id is using this:

 db.Outlet.findAll({include: [ {model:db.Product, attributes: ['id', 'name', 'nameKh'], where: {id: 2} ]}).then(function(outlets){ return res.jsonp(outlets); }) 

How can I request a specific order amount or today's order date?

Here are my models:

Output:

 var Outlet = sequelize.define('Outlet', { outletCode: DataTypes.STRING, outletName: DataTypes.STRING, outletNameKh: DataTypes.STRING, outletSubtype: DataTypes.STRING, perfectStoreType: DataTypes.STRING, address: DataTypes.STRING }, { associate: function(models){ Outlet.belongsToMany(models.Product, {through: models.Order}); Outlet.belongsTo(models.Distributor); // Outlet.hasMany(models.Order); } } ); 

Product:

 var Product = sequelize.define('Product', { inventoryCode: DataTypes.STRING, name: DataTypes.STRING, nameKh: DataTypes.STRING, monthlyCaseTarget: DataTypes.INTEGER, pieces: DataTypes.INTEGER, star: DataTypes.BOOLEAN, price: DataTypes.FLOAT, active: DataTypes.BOOLEAN }, { associate: function(models){ Product.belongsToMany(models.Outlet, {through: models.Order}); Product.belongsTo(models.Category); // Product.hasMany(models.Order); } } ); 

Order:

 var Order = sequelize.define('Order', { id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true }, amount: DataTypes.INTEGER }, { associate: function(models){ Order.belongsTo(models.Outlet); Order.belongsTo(models.Product); Order.belongsTo(models.User); } } ); 
+5
source share
1 answer

Try:

 db.Outlet.findAll({ include: [{ model:db.Product, attributes: ['id', 'name', 'nameKh'], through: { where: { amount: 10 } } }] }) 
0
source

All Articles