简体   繁体   中英

Node JS Sequelize join query

i am new to sequelize, like i just had to learn it today. I dont even know how to ask a question on it so please let me know how can i improve this. I have the below query.

router.get('/:id/work', async (req, res) => {
    let results = await User.findAll(
        {
            where: {
                id: req.params.id
            },
            include: [Task, Project]
        }
    );
    res.status(200);
    res.send(results);
});

I want to get the user info, and to have the 'tasks' and 'projects' as arrays of the 'assigner' and 'project_id' properties. Both my Project and Task model have these columns as foreign keys. The result I get is the one below


{
  id: 5,
  email: "asd@asdads.com",
  name: "asdasdasd",
  surname: "asdasdasdasd1231",
Task: {
  id: 9,
  name: "req.body.name",
  description: "req.body.description",
  score: 1,
  status: "req.body.status",
  assigner: 5,
  project_id: null
},
Project: {
  id: 2,
  name: "req.body.name",
  body: "req.body.body",
  status: null,
  assigner: 5
 }
},
 {
  id: 5,
  email: "asd@asdads.com",
  name: "asdasdasd",
  surname: "asdasdasdasd1231",
 Task: {
  id: 9,
  name: "req.body.name",
  description: "req.body.description",
  score: 1,
  status: "req.body.status",
  assigner: 5,
  project_id: null
 },
Project: {
  id: 3,
  name: "req.body.name",
  body: "req.body.body",
  status: "req.body.status",
  assigner: 5
 }
},

Is there any quick way to achieve this?

{
  id: 5,
  email: "asd@asdads.com",
  name: "asdasdasd",
  surname: "asdasdasdasd1231",
  Task: [/*all task objects*/],
  Project: [/*all project objects*/]
}

Model definitions

const User = db.define('User', {
    id: {
        type: Sequelize.INTEGER,
        autoIncrement: true,
        primaryKey: true
    },
    email: Sequelize.STRING,
    name: Sequelize.STRING,
    surname: Sequelize.STRING,
}, {
    timestamps: false,
    tableName: 'users'
})

/**
 * Model Relations
 */

Project.belongsTo(User, { foreignKey: 'assigner' })
User.hasOne(Project, { foreignKey: 'assigner' })

Task.belongsTo(User, { foreignKey: 'assigner' })
User.hasOne(Task, { foreignKey: 'assigner' })

Task.belongsTo(Project, { foreignKey: 'project_id' })

export default User;

const Task = db.define('Task', {
    id: {
        type: Sequelize.INTEGER,
        autoIncrement: true,
        primaryKey: true
    },
    name: Sequelize.STRING,
    description: Sequelize.TEXT,
    score: Sequelize.INTEGER,
    status: Sequelize.STRING,
    assigner: {
        type: Sequelize.INTEGER,
        references: {
            model: User,
            key: 'id'
        }
    },
    project_id: {
        type: Sequelize.INTEGER,
        references: {
            model: Project,
            key: 'id'
        }
    }
}, {
    timestamps: false,
    tableName: 'tasks'
})


export default Task;
const Project = db.define('Project', {
    id: {
        type: Sequelize.INTEGER,
        autoIncrement: true,
        primaryKey: true
    },
    name: Sequelize.STRING,
    body: Sequelize.STRING,
    status: Sequelize.STRING,
    assigner: {
        type: Sequelize.INTEGER,
        references: {
            model: User,
            key: 'id'
        }
    }
}, {
    timestamps: false,
    tableName: 'projects'
})

export default Project;

Change association definitions from hasOne to hasMany because as I see many projects can link to a one user as well as tasks:

User.hasMany(Project, { foreignKey: 'assigner' })
User.hasMany(Task, { foreignKey: 'assigner' })

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