Sequelize. How to join a couple of tables and make a single object of data (without nested objects)

Viewed 41

I am using Sequelize ORM for Postgres db. My tables and associations look like:

enter image description here

My associations:

Users.hasMany(Options, {
  foreignKey: 'userId'
});
Options.belongsTo(User);

Users.hasMany(Finders, {
  foreignKey: 'userId'
});
Finders.belongsTo(User);

How can I make a query using Sequelize to get Users data and associated data from tables Options and Finders in the same object (without nested objects)? The query result should be:

[
  {
    id: 1,
    name: 'Mark',
    option: 'opt1',
    finder: 'films'
  },
  {
    id: 1,
    name: 'Mark',
    option: 'opt2',
    finder: 'films'
  },
  {
    id: 2,
    name: 'Lalo',
    option: 'opt3',
    finder: 'books'
  },
]
0 Answers
Related