如何获得嵌套序列化模型的元素总数?



请告诉我如何获得嵌套序列化模型的元素总数?include除了返回所有嵌套模型的数组外,还必须返回count(所有嵌套模型的数量)。如果你能回答我,我会很感激的。

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: [['createdAt', 'DESC']],
},
],
})
products = await Product.findAndCountAll({
include: [
{
model: ProductVariant,
as: 'variant',
attributes:[[sequelize.fn('count', sequelize.col('model')), 'TotalNumberOfModels']
},
],
})

最新更新