过滤Sequelize属于ToMany

时间:2019-12-14 00:03:57

标签: node.js sequelize.js has-and-belongs-to-many findall

我有以下问题:

我这样定义表(产品和集合):

module.exports = (sequelize, type) => {
    return sequelize.define('product', {
        id: {
            type: type.UUID,
            primaryKey: true,
            defaultValue: type.UUIDV4,
        },
        title: {
            type: type.STRING,
            allowNull: false,
        },
        description: {
            type: type.TEXT,
            allowNull: false,
        },
        width: {
            type: type.FLOAT,
            allowNull: false,
        },
        height: {
            type: type.FLOAT,
            allowNull: false,
        },
        weight: {
            type: type.FLOAT,
            allowNull: false,
        },
        length: {
            type: type.FLOAT,
            allowNull: false,
        },
        vendor: {
            type: type.STRING,
            allowNull: false,
        },
        status: {
            type: type.ENUM,
            values: ['inactive', 'active'],
            defaultValue: 'active',
            allowNull: false,
        },
    })
}
module.exports = (sequelize, type) => {
    return sequelize.define('collection', {
        id: {
            type: type.UUID,
            primaryKey: true,
            defaultValue: type.UUIDV4,
        },
        name: {
            type: type.STRING,
            allowNull: false,
        },
        image: {
            type: type.STRING,
            allowNull: true,
        },
        createdAt: {
            type: type.DATE,
            allowNull: false,
        },
        updatedAt: {
            type: type.DATE,
            allowNull: false,
        },
        status: {
            type: type.ENUM,
            values: ['inactive', 'active'],
            defaultValue: 'active',
            allowNull: false,
        },
    })
}

然后,我需要将表(产品和集合)与belongsToMany关联,而我做到了:

const ProductModel = require('../api/product/model')
const CategoryModel = require('../api/category/model')

const Product = ProductModel(sequelize, Sequelize)
const Collection = CollectionModel(sequelize, Sequelize)

Product.belongsToMany(Collection, {
    through: ProductCollection,
    foreignKey: 'productId',
    otherKey: 'collectionId',
    unique: false,        
})

Collection.belongsToMany(Product, {
    through: ProductCollection,
    foreignKey: 'collectionId',
    otherKey: 'productId',
    unique: false,        
})

现在,我想获取由请求主体发送的ID所给定的集合的所有产品,我几乎没有时间处理sequelize,而且我不知道如何进行这种查询。

您能帮我吗?

1 个答案:

答案 0 :(得分:0)

您可以使用类似的东西

  let products = Collection.findAll({
            where: {
                id: collection.id,
            },
            attributes: ['id', 'name'],
            include: [{
                model: Product,
                through: {
                    model: ProductCollection,
                },
                as: 'products',
                attributes: ['id', 'title', 'description']
            }],
        });

        return products

        hope it helps