felix-cao / Blog

A little progress a day makes you a big success!
31 stars 4 forks source link

Sequelize Best Practice #219

Closed felix-cao closed 2 months ago

felix-cao commented 2 months ago

数据脱敏data sensitive

使用 defaultScope

const User = sequelize.define(
      "User",
      {
        firstName: types.string,
        lastName: types.string,
        email: types.string,
        password: types.string
      },
      {
        defaultScope: {
          attributes: {
            exclude: ["password"]
          }
        }
      }
    );

User.findByPk(user_id)    // No password attribute
User.findByPk(user_id, {attributes: ['password']})  //