#stackoverlog#graphql#sequelize

Query by condition of joined table with graphql-sequelize

I quickly note how to query data from a table base on condition of joined table in sequelize-graphql

There are 2 tables:

Users
idnameactive
1Bobtrue
2Alexfalse
3Kathyfalse
4Rosetrue
5Tonytrue
7Alicefalse
Books
idtitleuserIdavailable
1The Forest1true
2The City1true
3The Mount2false
4The Hill2false
5The Countryside5true
7The Sea7true

I want to query all available books that belongs to active users.

Then I will make the query like so:

models.Books.findAll({
  where: {
    available: true,
    '$User.active$': true,
  },
  include: 'User',  // Important to include the associate model. 
  // To learn more how to use association in Sequelize please refer to official doc.
})
Truong An

Truong An

Self-learner, passionate software engineer from Vietnam

Read More