sequelize include code example

Example 1: sequelize include only

Payment.findAll({
    where: {
        DairyId: req.query.dairyid
    },
    attributes: {
        exclude: ['createdAt', 'updatedAt']
    },
    include: {
        model: Customer,
        attributes:['customerName', 'phoneNumber']
    }
})

Example 2: nested include sequelize

models.products.findAll({
  include: [
    {model: models.comments, include: [models.comments.users] }
  ]
})

Example 3: include with where clause in sequelize

Document.findAll({
where: {'$employee.manager.id$': id},
      include: [{
        model: models.Employee,
        required: true,
        as: 'employee',
        include: [{
          model: models.Manager,
          required: true,
          as: 'manager',
          where: { id: managerId },
        }],
      }],

Example 4: sequelize documentation

const { Sequelize, Model, DataTypes } = require('sequelize');
const sequelize = new Sequelize('sqlite::memory:');

class User extends Model {}
User.init({
  username: DataTypes.STRING,
  birthday: DataTypes.DATE
}, { sequelize, modelName: 'user' });

sequelize.sync()
  .then(() => User.create({
    username: 'janedoe',
    birthday: new Date(1980, 6, 20)
  }))
  .then(jane => {
    console.log(jane.toJSON());
  });

Example 5: sequelize max

exports.getMinPrice = () => Item.findAll({    attributes: [[sequelize.fn('min', sequelize.col('price')), 'minPrice']],  });

Example 6: in in sequelize

await Tag.findAll({
  where: {
    id: {
      [Sequelize.Op.in]: [1, 2, 3, 4]
    }
  }
});