Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sequelize Default Exclude

I have a table named person, I want a column to be excluded as default,

const Person = sequelize.define('person',{
  secretColumn: Sequelize.STRING,
  //... and other columns
});

I see that there is a feature called Scope in Sequelize: http://docs.sequelizejs.com/manual/tutorial/scopes.html

I tried to exclude like this;

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

But that does't work. Is there any other way to exclude a column by default?

like image 360
Yasin Okumuş Avatar asked Oct 30 '17 17:10

Yasin Okumuş


People also ask

How do I set default value in Sequelize?

When you create a Sequelize model, you can add the default value for your model by adding the defaultValue option to the column(s) definition. The defaultValue option will be used by Sequelize to define default value(s) for your SQL column(s) when you create a table using Sequelize.

What is findByPk?

findByPk ​ The findByPk method obtains only a single entry from the table, using the provided primary key. const project = await Project. findByPk(123); if (project === null) {

What is Upsert Sequelize?

The upsert() method accepts an object of data with the property keys serving as the column names and the property values as the column values. The method would then return an array of two elements: The instance the Model where you call the method, returning the new/updated row.


1 Answers

I firgured it out. exclude needs to be in attributes part:

const Person = sequelize.define('person',{
  secretColumn: Sequelize.STRING,
  //... and other columns
}, {
  defaultScope: {
    attributes: { exclude: ['secretColumn'] }
  }
});
like image 159
Yasin Okumuş Avatar answered Oct 27 '22 21:10

Yasin Okumuş