简体   繁体   中英

Sequelize - Return only relationships

I have a problem, I need to return an array just from my flows relationship. How can I get an array with a list of the flows of all companies?

My model and migrations are correct, I just don't know how to make the query return only the flows .

Company Model

module.exports = (sequelize) => {
    const company = sequelize.define('company', {
        id: {
            allowNull: false,
            autoIncrement: true,
            primaryKey: true,
            type: DataTypes.INTEGER
        },
        uuid: {
            type: DataTypes.UUID,
            defaultValue: DataTypes.UUIDV4
        },
        name: {
            type: DataTypes.STRING,
            allowNull: false
        }
    })

    company.hasMany(sequelize.models.flow, {foreignKey: 'company_id', as: 'flows'})
}

Flow model

module.exports = (sequelize) => {
    const flow = sequelize.define('flow', {
        id: {
            allowNull: false,
            autoIncrement: true,
            primaryKey: true,
            type: DataTypes.INTEGER
        },
        company_id: {
            allowNull: false,
            type: DataTypes.INTEGER
        },
        uuid: {
            type: DataTypes.UUID,
            defaultValue: DataTypes.UUIDV4
        },
        name: {
            type: DataTypes.STRING,
            allowNull: false
        },
        description: {
            type: DataTypes.TEXT
        }
    })

    flow.belongsTo(sequelize.models.company, {foreignKey: 'company_id', as: 'company'})
}

Query

const companies = await ORM.models.company
    .findAll({
        include: [{
            model: ORM.models.flow,
            as: 'flows'
        }]
    })

This query is returning like this:

[
    {
        "id": 1,
        "uuid": "f0c1a5e1-c54c-4083-8284-5a9b272e8ba1",
        "name": "Company 1",
        "created_at": "2021-02-11T05:47:55.830Z",
        "updated_at": "2021-02-11T05:47:55.830Z",
        "flows": [
            {
                "id": 1,
                "company_id": 1,
                "uuid": "768262d2-88b7-4e0f-81e8-30d7253aae65",
                "name": "Flow 1",
                "description": null,
                "created_at": "2021-02-11T05:48:10.211Z",
                "updated_at": "2021-02-11T05:48:10.211Z",
                "companyId": 1
            }
        ]
    },
    {
        "id": 2,
        "uuid": "3dea2541-a505-4f0c-a356-f1a2d449d050",
        "name": "Company 1",
        "created_at": "2021-02-11T05:48:11.872Z",
        "updated_at": "2021-02-11T05:48:11.872Z",
        "flows": [
            {
                "id": 2,
                "company_id": 2,
                "uuid": "3e66e8e6-3754-41e5-93ca-6e8ed49e2025",
                "name": "Flow 2",
                "description": null,
                "created_at": "2021-02-11T05:48:20.743Z",
                "updated_at": "2021-02-11T05:48:20.743Z",
                "companyId": 2
            }
        ]
    }
]

I need to return like this:

[
   {
      "id":1,
      "company_id":1,
      "uuid":"768262d2-88b7-4e0f-81e8-30d7253aae65",
      "name":"Flow 1",
      "description":null,
      "created_at":"2021-02-11T05:48:10.211Z",
      "updated_at":"2021-02-11T05:48:10.211Z",
      "companyId":1
   },
   {
      "id":2,
      "company_id":2,
      "uuid":"3e66e8e6-3754-41e5-93ca-6e8ed49e2025",
      "name":"Flow 2",
      "description":null,
      "created_at":"2021-02-11T05:48:20.743Z",
      "updated_at":"2021-02-11T05:48:20.743Z",
      "companyId":2
   }
]

If you just want the flows related data, why to fetch company related data with flows in it? Perhaps, you could only fetch flows data.

const flows = await ORM.models.flow
.findAll({
   where: ....,
   ..........
});

If anyhow, you still want to show the flows for particular companies without showing any attributes of the company model, just do something like this:

const companies = await ORM.models.company
.findAll({
    attributes: [], //empty 
    include: [{
        model: ORM.models.flow,
        as: 'flows'
    }]
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM