A
A
Alexander2021-10-08 18:47:04
PostgreSQL
Alexander, 2021-10-08 18:47:04

How to get the total number of elements of a nested sequelize model?

Hello, please tell me how to get the total number of elements of the sequelize nested model?
It is necessary that include returns, in addition to the array of all nested models, also count (the number of all nested models)
I will be grateful for the answer

const Product = sequelize.define('product', {
  id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true },
  name: { type: DataTypes.STRING, unique: true, allowNull: false },
})

const ProductVariant = sequelize.define('product_variant', {
  id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true },
  title: { type: DataTypes.STRING, allowNull: false },
  img: { type: DataTypes.STRING, allowNull: false },
  color: { type: DataTypes.STRING, allowNull: false },
  price: { type: DataTypes.INTEGER, allowNull: false },
  model: { type: DataTypes.STRING, allowNull: false },
})


Product.hasMany(ProductVariant, { as: 'variant' })
ProductVariant.belongsTo(Product)


products = await Product.findAndCountAll({
        include: [
          {
            model: ProductVariant,
            as: 'variant',
            limit: 4,
            order: ,
          },
        ],
      })

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question