Shehryar Tanoli
Shehryar Tanoli

Reputation: 429

Get specific attributes from database using sequelize model query

I have red the documentation of sequelize but could not get the idea/concept i wanted ,I don't want just one attribute to be shown. We have the following syntax to get the attributes we need

Model.findAll({
    attributes:['foo','bar]          
})

In my case I have many attributes in single table , I just want to hide one attribute.Is there any way in sequelize that we define the attribute which we don't want to see and get all other by default..

For Example....

    Model.findAll({
       attributes:hide foo , show all other attributes
})

AnyOne Can help..

Upvotes: 0

Views: 992

Answers (1)

Sujeet
Sujeet

Reputation: 3810

You can use below syntax.

Model.findAll({
  attributes: {
    exclude: ['foo'] // hide this
  }
});

You can also exclude fields on model level by adding in their default scope like below.

const Model = sequelize.define('model',{
  secretColumn: Sequelize.STRING,
  //... and other columns
}, {
  defaultScope: {
    attributes: { exclude: ['secretColumn'] }
  }
});

Upvotes: 2

Related Questions