count in sequelize code example

Example 1: sequelize.fn

// Counting number of totalPrice from column itemPrice table 
Model.findAll({
  attributes: {
    include: [
      [sequelize.fn('COUNT', sequelize.col('itemPrice')), 'totalPrice']
    ]
  }
});

Example 2: sequelize count all

Project.count().then(c => {
  console.log("There are " + c + " projects!")
})

Project.count({ where: {'id': {[Op.gt]: 25}} }).then(c => {
  console.log("There are " + c + " projects with an id greater than 25.")
})

Example 3: count in sequelize example

exports.getItemSaleCount = () => SaleItem.findAll({    attributes: ['itemId', [sequelize.fn('count', sequelize.col('itemId')), 'count']],    group : ['SaleItem.itemId'],    raw: true,    order: sequelize.literal('count DESC')  });

Tags:

Misc Example